read() from files - blocking vs. non-blocking behavior

前端 未结 2 1383
谎友^
谎友^ 2021-01-25 00:34

Let\'s assume we opened a file using fopen() and from the file-pointer received, fetch the file-descriptor using fileno(). Then we do lots (>10^8) of r

2条回答
  •  失恋的感觉
    2021-01-25 01:09

    We solved the problem described as having read() return less bytes then request when reading from a file located on a NFS mount, pointing to an OCFS2 file system (case 4 in my question).

    It is a fact that using the setup mentioned above, such read()s on file descriptors sometimes return less bytes then requested, without having errno set.

    To have all data read it is as simple as just read()ing again and again up until the amount of data requested had been read.

    Moreover such setup sometimes makes read() fail with EIO, and even then a simple re-read() leads to success and data arrives.

    My conclusion: Reading via OCFS2 via NFS makes read()ing from files behave like read()ing from sockets which is inconsistent with the specifications of read() http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html :

    When attempting to read a file (other than a pipe or FIFO) that supports non-blocking reads and has no data currently available:

    If O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].

    If O_NONBLOCK is clear, read() shall block the calling thread until some data becomes available.

    No need to say we never ever tried, nor even thought about to set O_NONBLOCK for the file descriptors in question.

提交回复
热议问题