使用 libevent2 处理文件 I/O 时出现问题
我使用 libevent2 一段时间,但通常我用它来处理网络 I/O(使用套接字)。现在我需要读取许多不同的文件,所以我也想使用它。我创建了这段代码:
int file = open(filename, O_RDONLY);
struct event *ev_file_read = event_new(ev_base, file, EV_READ | EV_PERSIST, read_file, NULL);
if(event_add(ev_file_read, NULL))
error("adding file event");
不幸的是它不起作用。尝试添加事件时收到此消息:
[警告] fd 7 上的 Epoll ADD(1) 失败。旧事件为 0;读取更改为 1(添加);写入更改为 0(无):不允许操作 添加文件事件:不允许操作
文件存在并且具有读/写权限。
有人知道如何使用 libevent 处理文件 IO 吗?我还考虑了 bufferred 事件,但在 API 中只有函数 bufferevent_socket_new() 不适用于此处。
提前致谢。
I worked with libevent2 for some time, but usually I used it to handle network I/O (using sockets). Now I need to read many different files so I also wanted to use it. I created this code:
int file = open(filename, O_RDONLY);
struct event *ev_file_read = event_new(ev_base, file, EV_READ | EV_PERSIST, read_file, NULL);
if(event_add(ev_file_read, NULL))
error("adding file event");
Unfortunately it doesn't work. I get this message when trying to add event:
[warn] Epoll ADD(1) on fd 7 failed. Old events were 0; read change was 1 (add); write change was 0 (none): Operation not permitted
adding file event: Operation not permitted
The file exists and has rights to read/write.
Anyone has any idea how to handle file IO using libevent? I thought also about bufferred events, but in API there's only function bufferevent_socket_new() which doesn't apply here.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我需要 libevent 来读取许多有关优先级的文件。问题出在 epoll 上,而不是 libevent 上。 Epoll 不支持常规 Unix 文件。
为了解决这个问题,我强制 libevent 不要使用 epoll:
首选项列表中的下一个方法是 poll,它完全支持文件,就像我想要的那样。
谢谢大家的解答。
I needed libevent to read many files regarding priorities. The problem was in epoll not in libevent. Epoll doesn't support regular Unix files.
To solve it I forced libevent not to use epoll:
Next method on the preference list was poll, which fully support files just as I wanted to.
Thank you all for answers.
使用 libevent 注册常规文件描述符是没有意义的。 与常规文件关联的文件描述符应始终选择 true 表示准备读取、准备写入,和错误条件。
Makes no sense to register regular file descriptors with
libevent
. File descriptors associated with regular files shall always select true for ready to read, ready to write, and error conditions.如果你想做异步磁盘 I/O,你可能需要检查 aio_* 系列(参见 man (3) aio_read)。它是 POSIX.1-2001 并且可以在 linux 和 bsd 上使用(至少)。
有关将 aio 操作与 libevent 集成的信息,请参阅 libevent aio 补丁 和相关的 stackoverflow 帖子 提到使用 signalfd(2) 将 aio 信号事件路由到可与各种 fd 事件轮询实现一起使用的文件描述符(因此隐式地与 libevent 循环一起使用)。
编辑:libevent 还具有信号处理支持(完全忘记了),因此您可以尝试直接使用 libevent 循环注册/处理 aio 信号。如果您的开发规则允许的话,我个人会先尝试 libevent 补丁。
if you want to do async disk i/o you may want to check the aio_* family (see man (3) aio_read). it's POSIX.1-2001 and available on linux and bsd (at least).
for integrating aio operations with libevent, see libevent aio patch and a related stackoverflow post that mention using signalfd(2) to route the aio signal events to a file descriptor that can be used with various fd event polling implementations (so implicitly with libevent loop).
EDIT: libevent also has signal handling support (totally forgot about that) so you can try and register/handle the aio signals directlry with/from libevent loop. I'd personally go and try the libevent patch first if your development rules allows you to.