From what I have been reading on The Open Group website on fcntl, open, read, and write, I get the impression that whether O_NONBLOCK
is set on a file descriptor, a
O_NONBLOCK
is a property of the open file descriptor, not of the file descriptor, nor of the underlying file.
Yes, you could have separate file descriptors open for the same file, one of which is blocking and the other of which is non-blocking.
You need to distinguish between a FIFO (created using mkfifo()) and a pipe (created using pipe()).
Note that the blocking status is a property of the 'open file description', but in the simplest cases, there is a one-to-one mapping between file descriptors and open file descriptions. The open() function call creates a new open file description and a new file descriptor that refers to the open file description.
When you use dup(), you have two file descriptors sharing one open file description, and the properties belong to the open file description. The description of fcntl() says that F_SETFL affects the open file description associated with the file descriptor. Note that lseek() adjusts the file position of the open file description associated with the file descriptor - so it affects other file descriptors duplicated from the original one.
Removing the error handling from your code to reduce it, you have:
int fds[2];
pipe(fds);
int fd0_dup = dup(fds[0]);
fcntl(fd0_dup, F_SETFL, fcntl(fd0_dup, F_GETFL) | O_NONBLOCK);
Now both fd0_dup and fds[0] refer to the same open file description (because of the dup()
), so the fcntl()
operation affected both file descriptors.
if ((fcntl(fds[0], F_GETFL) & O_NONBLOCK)) { ... }
Hence the observed behavior here is required by POSIX.