Is O_NONBLOCK being set a property of the file descriptor or underlying file?

后端 未结 1 2023
孤城傲影
孤城傲影 2021-02-01 19:09

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

相关标签:
1条回答
  • 2021-02-01 20:06

    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.

    0 讨论(0)
提交回复
热议问题