使用 libevent2 处理文件 I/O 时出现问题

发布于 2024-10-26 21:45:20 字数 597 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2024-11-02 21:45:20

我需要 libevent 来读取许多有关优先级的文件。问题出在 epoll 上,而不是 libevent 上。 Epoll 不支持常规 Unix 文件。

为了解决这个问题,我强制 libevent 不要使用 epoll:

    struct event_config *cfg = event_config_new();

event_config_avoid_method(cfg, "epoll");

ev_base = event_base_new_with_config(cfg);  
    event_config_free(cfg);

首选项列表中的下一个方法是 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:

    struct event_config *cfg = event_config_new();

event_config_avoid_method(cfg, "epoll");

ev_base = event_base_new_with_config(cfg);  
    event_config_free(cfg);

Next method on the preference list was poll, which fully support files just as I wanted to.

Thank you all for answers.

书间行客 2024-11-02 21:45:20

如果你想做异步磁盘 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文