9

根据 read(2) 的手册页,它仅在到达 EOF 时返回零。

但是,这似乎是不正确的,它有时可能会返回零,可能是因为文件还没有准备好读取?在从磁盘读取文件之前,我应该调用 select() 来查看它是否准备就绪?

请注意,nBytes 为:1,445,888

一些示例代码:

fd_set readFdSet;
timeval timeOutTv;

timeOutTv.tv_sec = 0;
timeOutTv.tv_usec = 0;

// Let's see if we'll block on the read.
FD_ZERO(&readFdSet);
FD_SET(fd, &readFdSet);

int selectReturn = ::select(fd + 1, &readFdSet, NULL, NULL, &timeOutTv);

if (selectReturn == 0) {
  // There is still more to read.
  return false; // But return early.
} else if (selectReturn < 0) {
  clog << "Error: select failure: " << strerror(errno) << endl;
  abort();
} else {
  assert(FD_ISSET(fd, &readFdSet));

  try {
    const int bufferSizeAvailable = _bufferSize - _availableIn;

    if (_availableIn) {
      assert(_availableIn <= _bufferSize);

      memmove(_buffer, _buffer + bufferSizeAvailable, _availableIn);
    }

    ssize_t got = ::read(fd, _buffer + _availableIn, bufferSizeAvailable);

    clog << " available: " << bufferSizeAvailable << " availableIn: "
         << _availableIn << " bufferSize: " << _bufferSize << " got "
         << got << endl;

    return got == 0;
  } catch (Err &err) {
    err.append("During load from file.");
    throw;
  }
}

输出读取(当它失败且没有读取数据时):

available: 1445888 availableIn: 0 bufferSize: 1445888 got: 0

这是在 centos4 32 位上作为使用 VMware Server 1.0.10 的虚拟机运行的。正在读取的文件系统是虚拟机本地的。主机是windows server 2008 32位。

uname -a 说:

Linux q-centos4x32 2.6.9-89.0.25.ELsmp #1 SMP Thu May 6 12:28:03 EDT 2010 i686 i686 i386 GNU/Linux

我注意到下面给出的链接http://opengroup.org/onlinepubs/007908775/xsh/read.html指出:

The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal...

If a read() is interrupted by a signal before it reads any data, it will return -1 with errno set to [EINTR].

If a read() is interrupted by a signal after it has successfully read some data, it will return the number of bytes read. 

所以,也许我收到一个中断读取的信号,因此返回的值是零,因为错误或者它认为读取了零字节?

4

6 回答 6

6

经过一番研究,实际上在某些情况下它会返回 0,您可能不会认为它是“EOF”。

有关详细信息,请参阅 read() 的 POSIX 定义:http: //opengroup.org/onlinepubs/007908775/xsh/read.html

一些值得注意的是,如果你要求它读取 0 个字节——仔细检查你是否不小心将 0 传递给它——并读取文件“写入”部分的末尾(实际上你可以查找末尾文件,如果你写在那里,它会用零“扩展”文件,但在你这样做之前,“EOF”仍然在已经写入部分的末尾)。

我最好的猜测是你在某个地方遇到了时间问题。您需要问的一些问题是“这些文件是如何编写的?” 和“当我尝试阅读它们时,我确定它们不是零长度吗?”。对于第二个,您可以在读取文件之前尝试在文件上运行 stat() 以查看其当前大小。

于 2010-06-19T03:36:54.627 回答
3

我能想到 read() 返回 0 的唯一另一种情况是,如果您将 nbytes 作为 0 传递;有时,如果您将某物或其他东西的大小作为参数传递,则可能会发生这种情况。这可能是现在正在发生的事情吗?

如果文件还没有准备好被读取,应该发生的是 read 返回 -1 并且 errno 设置为 EAGAIN。

于 2010-06-19T03:32:49.537 回答
1

弄清楚了!我有一个未初始化的内存读取 (UMR) 并且错误地寻找到文​​件的末尾。

于 2010-06-20T04:21:16.000 回答
0

我已经处理过很多次了。由于应用程序不知道 fd 是否附加到平面文件、网络套接字、管道等,因此某些进程可能会发送某个优先级或其他优先级的零长度消息并触发此事件。我采取观望态度,看看EOF是否坚持:

#include <errno.h>
#include <unistd.h>
#include <poll.h>

 .
 .
 .
int eofct = 0 ;
 .
 .
 .  
do {
    switch ( readcount = read( fd, buff+currentsize, bufSize )){
        case -1:
             if ( errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR ){
               continue ;
             }

            perror( "read()" );
            return -1 ;
        case 0:
            if ( eofct++ < 100 ){
              poll( 0, 0, 1 );
              continue ;
            }

            break ;
      default:
        eofct = 0 ;
        currentsize += readcount ;

        if ( NULL == ( buff = realloc( buff, currentsize + buffsz ))){
          perror( "realloc()" );
          return -1 ;
        }

        continue ;
    }    
  } while ( readcount ); // readcount 0 break is EOF
于 2020-02-25T19:43:33.203 回答
0

如果 open 或 fcntl 设置 O_NONBLOCK,则读取应返回 0,直到数据准备好。

于 2020-02-25T20:37:07.717 回答
0

我刚刚在 Go 中遇到了这个问题。使行为类似于 read 的东西(即:Go 中的 io.Reader)返回零长度而没有 io.EOF 似乎是非常危险的。如果是你没有写的调用者,它可能会中断;假设它将阻塞至少 1 个字节。但是如果你知道调用者处理它,那么你就可以做到。

于 2021-05-11T21:53:20.900 回答