为什么 Posix 文件 I/O 总是阻塞?
Why Posix File I/O is always blocking?
来自手册页
O_NONBLOCK or O_NDELAY
This flag has no effect for regular files and block
devices; that is, I/O operations will (briefly) block when
device activity is required, regardless of whether O_NONBLOCK
is set. Since O_NONBLOCK semantics might eventually be
implemented, applications should not depend upon blocking
behavior when specifying this flag for regular files and block
devices.
来自 我对io系统的理解如下
Device <-----> Kernel Buffers <-----> Process
所以每当缓冲区满(写)或空(读)时,来自进程的相应命令可以阻塞或不阻塞,这取决于上面的标志。与设备交互的内核不会阻塞进程。内核可能使用也可能不使用 DMA 与设备通信。
但看起来我的理解是错误的,因为我不明白为什么常规文件描述符不能是非阻塞的。有人可以帮我吗?
"Blocking" 定义为等待文件变得可读或可写。
常规文件总是可读and/or可写;换句话说,总是可以尝试 start read/write 操作,而不必等待某些外部事件:
- 读取时,内核已经知道文件中是否还有更多字节(如果已到达文件末尾,则不可能阻塞以等待其他进程追加更多字节);
- 写入时,内核已经知道磁盘上是否有足够的 space 可以写入一些东西(如果磁盘已满,则不可能阻塞等待其他进程删除一些数据释放 space).
来自手册页
O_NONBLOCK or O_NDELAY This flag has no effect for regular files and block devices; that is, I/O operations will (briefly) block when device activity is required, regardless of whether O_NONBLOCK is set. Since O_NONBLOCK semantics might eventually be implemented, applications should not depend upon blocking behavior when specifying this flag for regular files and block devices.
来自
Device <-----> Kernel Buffers <-----> Process
所以每当缓冲区满(写)或空(读)时,来自进程的相应命令可以阻塞或不阻塞,这取决于上面的标志。与设备交互的内核不会阻塞进程。内核可能使用也可能不使用 DMA 与设备通信。
但看起来我的理解是错误的,因为我不明白为什么常规文件描述符不能是非阻塞的。有人可以帮我吗?
"Blocking" 定义为等待文件变得可读或可写。
常规文件总是可读and/or可写;换句话说,总是可以尝试 start read/write 操作,而不必等待某些外部事件:
- 读取时,内核已经知道文件中是否还有更多字节(如果已到达文件末尾,则不可能阻塞以等待其他进程追加更多字节);
- 写入时,内核已经知道磁盘上是否有足够的 space 可以写入一些东西(如果磁盘已满,则不可能阻塞等待其他进程删除一些数据释放 space).