弱弱地问下epoll_ctl参数的问题

youqika 2012-03-28 10:11:11
NAME
epoll_ctl - control interface for an epoll descriptor

SYNOPSIS
#include <sys/epoll.h>

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

DESCRIPTION
This system call performs control operations on the epoll instance
referred to by the file descriptor epfd. It requests that the opera‐
tion op be performed for the target file descriptor, fd.
看到有些开源代码里EPOLL_CTL_ADD的epoll_ctl调用的时候其实fd和event->data.fd是同一个,比如像这样:
ev.events = EPOLLIN;
ev.data.fd = listen_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) {
perror("epoll_ctl: listen_sock");
exit(EXIT_FAILURE);
}
去掉第二行可以吗?EPOLL_CTL_MOD的时候就没有这行。
...全文
192 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
youqika 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

ev.data.fd = listen_sock;
没有也一样能运行,但是你处理的时候只有data,不知道是属于哪个fd的数据

你自己写一个epoll的简单代码就知道为什么了
[/Quote]
按道理说,add的时候就应该把传入的fd关联上才合理啊
youqika 2012-03-28
  • 打赏
  • 举报
回复
多谢ls两位,找到答案了
static ngx_int_t
ngx_epoll_add_connection(ngx_connection_t *c)
{
struct epoll_event ee;

ee.events = EPOLLIN|EPOLLOUT|EPOLLET;
ee.data.ptr = (void *) ((uintptr_t) c | c->read->instance);

ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
"epoll add connection: fd:%d ev:%08XD", c->fd, ee.events);

if (epoll_ctl(ep, EPOLL_CTL_ADD, c->fd, &ee) == -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"epoll_ctl(EPOLL_CTL_ADD, %d) failed", c->fd);
return NGX_ERROR;
}

c->read->active = 1;
c->write->active = 1;

return NGX_OK;
}
nice_cxf 2012-03-28
  • 打赏
  • 举报
回复
ev.data.fd = listen_sock;
没有也一样能运行,但是你处理的时候只有data,不知道是属于哪个fd的数据

你自己写一个epoll的简单代码就知道为什么了
jiuchang 2012-03-28
  • 打赏
  • 举报
回复
这个取决于接口函数的实现,不是说有内容一样了,就可以不给了
接口有可能支持两者不一致的情况,所以需要带上

你可以搜一个源码看看具体的实现

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧