【Linux】I/O 轮询技术 - read

Posted by 西维蜀黍 on 2021-09-28, Last Modified on 2022-12-10

read 方法 - 同步非阻塞I/O模型

#include <unistd.h>

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

On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read. If the file offset is at or past the end of file, no bytes are read, and read() returns zero.

If count is zero, read() may detect the errors described below. In the absence of any errors, or if read() does not check for errors, a read() with a count of 0 returns zero and has no other effects.

read轮询方法对应read()系统调用。即用户线程需要在一定时间间隔内非阻塞地持续发起轮询请求来获知此I/O操作的当前状态。因此,轮询操作是由用户线程来负责的。

虽热用户线程不会被阻塞,但它需要不停地发起系统调用,直到数据可用为止。

它是较原始、性能较低的一种,不仅整体数据吞吐量较低,而且(相较于阻塞式系统调用)消耗了额外的CPU资源。

Reference