C 中的 FIFO(命名管道)

Zac*_*man 1 c named-pipes fifo mkfifo

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*ike 5

您正在使用而oflag不是创建它mode_t

mkfifo采用mode_t类型的第二个参数

换句话说,类似于:0666。您尝试oflag按照 中的定义为其提供数据fcntl.h,通常如下所示:

#define O_RDONLY             00
#define O_WRONLY             01
#define O_RDWR               02
Run Code Online (Sandbox Code Playgroud)

因此,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
Run Code Online (Sandbox Code Playgroud)