Libevent 和文件 I/O
libevent
是否处理缓冲文件 I/O?我知道它可以很好地处理套接字,但它是否也涉及普通文件,或者它“只是”一个 epoll/...
包装器?
Does the libevent
deal with buffered file I/O? I know it handles sockets pretty good, but does it concern also normal files or it's "only" an epoll/...
wrapper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 libevent(或任何底层就绪通知机制,例如 epoll 或 kqueue)与普通文件描述符一起使用通常没有意义。例外情况是 NFS 上的文件或使用带有
eventfd
的内核 AIO。本地磁盘上的文件描述符始终准备就绪,始终有足够的缓冲区空间,并且操作始终“立即”完成。写入操作只是将数据复制到缓冲区高速缓存,并且实际写入磁盘发生... 无论何时发生。 (请注意,此链接是特定于 Linux 的,但除了一些小的实现细节之外,它在其他系统上的工作方式相同)
Using libevent (or any of the underlying readiness notification mechanisms such as e.g.
epoll
orkqueue
) with normal file descriptors does not normally make sense. Exceptions are files on NFS or using kernel AIO with aneventfd
.File descriptors on local disks are always ready, there is always sufficient buffer space, and operations always complete "immediately". The write operation merely copies data to the buffer cache, and the actual write to the disk happens ... whenever it happens. (note that this link is Linux-specific, but apart from maybe some little implementation details it works the same on other systems)
libevent 不是 epoll 包装器。它选择每个平台上可用的最高性能方法。
套接字也是文件描述符,因此您应该能够使用 libevent 进行文件 io。
不过,您需要禁用 libevent 的 epoll 使用。如果我没记错的话,Epoll 不支持 unix 文件。
libevent is not an epoll wrapper. It selects the highest performance method available on each platform.
Sockets are also file descriptors so you can should be able to use libevent for file io.
You will need to disable epoll usage of libevent though. If I remember right Epoll does not support unix files.
libevent 位于比缓冲文件 I/O(通过
stdio.h
获得的内容)更低的级别,直接使用文件描述符。您认为它“只是”一个 epoll/select/kevent/etc 包装器是正确的。它的目的是监听描述符上的事件,这是文件 I/O 的最低级别。但是,您可以将其与stdio.h
文件 I/O 库工具结合使用,因为它最终也会使用文件描述符。您可以使用fileno(3)
从您想要等待的FILE *
中检索文件描述符。libevent sits at a lower level than buffered file I/O (what you get with
stdio.h
), using file descriptors directly. You are correct thinking that it is 'just' an epoll/select/kevent/etc wrapper. Its purpose is to listen for events on descriptors, which is the lowest level of file I/O. However, you can use it in conjunction with thestdio.h
file I/O library facilities, as that also eventually uses the file descriptors. You can usefileno(3)
to retrieve the file descriptor from theFILE *
you want to wait on.