Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Jun 2007 03:23:31 +0100
From:      "mal content" <artifact.one@googlemail.com>
To:        freebsd-hackers@freebsd.org
Subject:   FIFO asymmetry
Message-ID:  <8e96a0b90706021923i2ef1d95ey909e269f3061ca82@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
Hello.

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
(ENXIO).

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.

thanks,
MC



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8e96a0b90706021923i2ef1d95ey909e269f3061ca82>