常规文件上的 Kqueue

问题描述 投票:0回答:3

kqueue(在 OS X 上)对于读取/写入常规文件有用吗?我知道 epoll 对于 Linux 上的常规文件没有有用,所以我想知道 kqueue 是否也是如此。

编辑:我的意思不是读/写文件,显然 read() 和 write() 就是用于此目的。我的意思是,“kqueue 对于检测文件何时可读/可写实际上有用吗?”

c macos sockets operating-system kqueue
3个回答

1
投票
是的,kqueue 可用于监视文件的可读性。从手册页:

EVFILT_READ Takes a file descriptor as the identifier, and returns whenever there is data available to read. The behavior of the filter is slightly different depending on the descriptor type. [...] Vnodes Returns when the file pointer is not at the end of file. data contains the offset from current posi- tion to end of file, and may be negative.

(在这种情况下,“vnodes”是常规文件。)

由于常规文件始终是可写的,因此对它们应用

EVFILT_WRITE

 是没有意义的。


0
投票
从技术上讲,您可以使用它来轮询读取可用性。

但读取可用性仅意味着磁盘上有可用数据,而不是数据已经缓冲并准备好提供服务。因此后续读取可能仍会阻塞。

只要有足够的内存,文件写入通常是非阻塞的。写入本质上是对内核内另一个缓冲区的 memcpy,该缓冲区最终将提交到磁盘。

这也是为什么 poll 总是返回准备好常规文件的原因。

在 Linux 和 BSD 上,O_DIRECT 和 O_SYNC 改变了这种行为,在 OS X 上,我相信有一个 fcntl 具有类似的行为。不过,使用这些几乎总是一个坏主意。

© www.soinside.com 2019 - 2024. All rights reserved.