FIFO asymmetry

mal content at
Sun Jun 3 02:23:33 UTC 2007


This is related to my earlier email, which I now believe
to be unanswerable (so it's probably worth ignoring).

Why do FIFOs work asymmetrically with regards to opening
for reading or writing?

  int rfd;
  int wfd;

  if (mkfifo("fifo_r", 0600) == -1) die();
  rfd = open("fifo_r", O_RDONLY | O_NONBLOCK);
  if (rfd == -1) die();

  if (mkfifo("fifo_w", 0600) == -1) die();
  wfd = open("fifo_w", O_WRONLY | O_NONBLOCK);
  if (wfd == -1) die();

The first open() call will (unless there's a catastrophic
error), be successful and will not block, even if there's
no writer on the other end of the fifo.

The second open() call will fail if there's no reader

Why this irritating difference in functionality? Surely
the second open() call should fail and any write() on
the fd should return -1 (EWOULDBLOCK) or 0 like the first
would in the case of a read()?

In a hypothetical program, I would create a FIFO in the
filesystem and select for readability (a writer has turned
up and has data). I can't do the same in reverse (create
a FIFO and select for writability - a reader has turned
up and expects data).

Is there some sort of rationale for this surprising
behaviour (in POSIX or some ancient UNIX docs)?

Before anybody tries to shoot me down in flames, I realise
this isn't FreeBSD-specific.


More information about the freebsd-hackers mailing list