简体   繁体   中英

FIFOs in C (Named-pipes)

I'm getting permission errors when trying to mkfifo() in the current directory. I definitely have permission to create files here. Any idea what the problem could be?

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);
    }
}

You're creating it with an oflag not a mode_t .

mkfifo takes a second parameter of type mode_t

In other words something like: 0666 . You're trying to feed it an oflag as defined in fcntl.h , this is normally like:

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

Hence, Invalid argument . Here's a way to open the 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

If you are relying on the output of perror to tell you that you are getting permission errors, you are likely mistaken. The call to printf is very likely changing errno, so that information is bogus. Do not call printf. Just write:

perror( path );

and see if the error messages change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM