弱弱地问下epoll_ctl参数的问题
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的时候就没有这行。
[解决办法]
这个取决于接口函数的实现,不是说有内容一样了,就可以不给了
接口有可能支持两者不一致的情况,所以需要带上
你可以搜一个源码看看具体的实现
[解决办法]
ev.data.fd = listen_sock;
没有也一样能运行,但是你处理的时候只有data,不知道是属于哪个fd的数据
你自己写一个epoll的简单代码就知道为什么了