0

I am working with pipes and one pipe won't open, even though mkfifo() was successful.

I have this:

/* create the FIFO (named pipe) */
int ret_mk = mkfifo(out_myfifo, 0666);
if(ret_mk < 0) {
  perror(out_myfifo);
  unlink(out_myfifo);
  return -1;
}

printf("ret_mk = %d\n", ret_mk);
/* write to the FIFO */
out_fd = open(out_myfifo, O_WRONLY);
printf("out_fd = %d\n", out_fd);

but nothing gets printed after open(), even a print of random text won't show up.

From here we have:

The open() function returns an integer value, which is used to refer to the file. If unsuccessful, it returns -1, and sets the global variable errno to indicate the error type.

What can I do to see why it won't open?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Your code does not show how you know that `mkfifo()` was successful. You'd have to capture or test the value returned by `mkfifo()` and demonstrate that it was 0 to know that `mkfifo()` was successful. And, as you now know, if you don't see the file descriptor number printed, it is because the `open()` has not completed, because no process has the FIFO open for reading. – Jonathan Leffler Nov 16 '14 at 20:51

1 Answers1

2

Read fifo(7). For FIFOs, an open call may block. To make open(2) non-blocking, use O_NONBLOCK in the flag argument:

out_fd = open(out_myfifo, O_WRONLY|O_NONBLOCK);
if (out_fd<0) perror(out_myfifo);
printf("%d\n", out_fd);

But usually you want a blocking open for write on a FIFO, because some other process should open the same FIFO for reading (and you want your writing process to wait that to happen).

Notice that there is no way to poll(2) the event that someone else has opened the other end of your FIFO (because poll wants an opened file descriptor). See also inotify(7); you could also want to use unix(7) sockets.

BTW, you could also use strace(1) for debugging purposes.

See also intro(2) and Advanced Linux Programming.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Ok I see what you mean by blocking. Do you know why my question was voted down? – gsamaras Nov 16 '14 at 20:45
  • I guess because of RTFM. I did not downvote your question, but a question whose answer is immediately available in man pages is not "showing research effort" – Basile Starynkevitch Nov 16 '14 at 20:46
  • The reference I had didn't cover that up. Anyway, so that means that the question is not helpful for the site, but I can't delete it because of your answers, what I should do now? :/ By the way, do you think it's a -1 question? – gsamaras Nov 16 '14 at 20:47
  • But you should always (on Linux & POSIX systems) try first to RTFM. See and *use* [man(1)](http://man7.org/linux/man-pages/man1/man.1.html) & [Linux manpages online](http://man7.org/linux/man-pages/). On Debian or Ubuntu, install the `manpages` & `manpages-dev` & `manpages-posix` & `manpages-posix-dev` packages! – Basile Starynkevitch Nov 16 '14 at 20:50
  • I'll keep that in mind Basile, thanks. Now the scarf of -1 will be forever with me! haha – gsamaras Nov 16 '14 at 20:53
  • BTW, in the late 1980s I learned all on Unix (SunOS3.2 at that time on Sun3) by reading man pages, from section 1 to section 8. It was harsh but very instructive! (At that time the Web did not even exist) – Basile Starynkevitch Nov 16 '14 at 20:54
  • Well that's something for a student like me to remember! – gsamaras Nov 16 '14 at 20:55