简体   繁体   English

在什么情况下 read() 系统调用返回 0?

[英]Under what circumstances does the read() syscall return 0?

I'm looking at the read syscall in Unix, which (at least in Linux) has this signature: [1]我正在查看 Unix 中的read系统调用,它(至少在 Linux 中)具有以下签名:[1]

ssize_t read(int fd, void* buf, size_t count);

Let's assume that the call succeeds (ie no negative return values) and that count > 0 (ie the buffer actually can store a nonzero amount of bytes).让我们假设调用成功(即没有负返回值)并且count > 0 (即缓冲区实际上可以存储非零字节数)。 Under which circumstances would read() return 0?在什么情况下read()返回 0? I can think of the following:我可以想到以下几点:

  • When fd refers to a regular file and the end of the file has been reached.fd引用常规文件并且已到达文件末尾时。
  • When fd refers to the receiving end of a pipe, socket or FIFO, the sending end has been closed and the pipe's/socket's/FIFO's own buffer has been exhausted.fd指的是管道、套接字或 FIFO 的接收端时,发送端已经关闭并且管道/套接字/FIFO 自己的缓冲区已经耗尽。
  • When fd refers to the slave side of a terminal device that is in ICANON and Ctrl-D has been sent into the master side while the line buffer was empty.fd指的是ICANON中的终端设备的从端并且Ctrl-D已发送到主端时,而行缓冲区为空。

I'm curious if there are any other situations that I'm not aware of, where read() would return with a result of 0. I'm especially interested (because of reasons) in situations like the last one in the list above, where read() returns 0 once, but subsequent calls to read() on the same FD could return a nonzero result.我很好奇是否还有其他我不知道的情况,其中read()将返回 0 结果。我对上面列表中的最后一种情况特别感兴趣(由于原因) ,其中read()返回 0 一次,但随后在同一 FD 上调用read()可能返回非零结果。 If an answer only applies to a certain flavor of Unix, I'm still interested in hearing it.如果答案仅适用于某种风格的 Unix,我仍然有兴趣听到它。

[1] I know this signature is for the libc wrapper, not the actual syscall, but that's not important right now. [1] 我知道这个签名是用于 libc 包装器的,而不是实际的系统调用,但这现在并不重要。

  • If the Physical File System does not support simple reads from directories, read() will return 0 if it is used for a directory.如果物理文件系统不支持从目录中简单读取,则 read() 将返回 0(如果它用于目录)。
  • If no process has the pipe open for writing, read() returns 0 to indicate the end of the file.如果没有进程打开管道进行写入,则 read() 返回 0 以指示文件结束。
  • If the connection is broken on a stream socket, but no data is available, then the read() function returns 0 bytes as EOF.如果流套接字上的连接中断,但没有数据可用,则 read() 函数返回 0 字节作为 EOF。

Normally a return value of 0 always means end-of-file.通常返回值0总是意味着文件结束。 However, if you specify 0 as the number of bytes to read, it will always return 0 unless there's an error detected.但是,如果您指定0作为要读取的字节数,除非检测到错误,否则它将始终返回0

Terminal devices are a special case.终端设备是一个特例。 If the terminal is in cooked mode, typing Control-d tells the device driver to return from any pending read() immediately with whatever is in the input editing buffer, rather than waiting for the user to enter a newline.如果终端处于熟模式,输入Control-d 会告诉设备驱动程序立即从任何挂起的read()返回输入编辑缓冲区中的任何内容,而不是等待用户输入换行符。 If the buffer is empty, this results in a zero-length read.如果缓冲区为空,则会导致读取长度为零。 This is how typing the EOF character at the beginning of a line is automatically treated as EOF by applications.这就是在行首输入 EOF 字符被应用程序自动视为 EOF 的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM