尝试在当前目录中mkfifo()时出现权限错误。我绝对有权在这里创建文件。知道问题出在哪里吗?

char dir[FILENAME_MAX];
getcwd(dir, sizeof(dir));


for(i = 0; i<num_nodes; i++)
{
    char path[FILENAME_MAX];
    sprintf(path, "%s/%d",dir, i);
    printf("%s\n", path);
    fifoArray[i] = mkfifo(path, O_WRONLY);
    if(fifoArray[i] < 0)
    {
         printf("Couldn't create fifo\n");
         perror(NULL);
    }
}

最佳答案

你用的是oflag而不是mode_t
mkfifo接受第二个mode_t类型的参数
换句话说,类似于:0666。您试图给它输入oflag中定义的fcntl.h,通常如下所示:

#define O_RDONLY             00
#define O_WRONLY             01
#define O_RDWR               02

因此,Invalid argument。以下是打开fifo的方法:
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);

if((fd = open(myfifo, O_RDONLY | O_NONBLOCK)) < 0){
  printf("Couldn't open the FIFO for reading!\n");
  return 0;
}
else {
   //do stuff with the fifo

10-04 13:44