epoll.data
is a union (see http://linux.die.net/man/2/epoll_wait). This means you can add either a fd or a data pointer, but not both.
Epoll: SD added to epoll-fd differ with actual SD value on an event
-
30-06-2023 - |
문제
I have two threads that operate on epoll. One thread tries the TCP connection to server, adds the socket to epoll-fd with EPOLL_CTL_ADD option.
Another thread is responsible for waiting for events on the SDs that were added to epoll-fd.
Thread 1:
establish_tcp_conn()
{
struct epoll_event event;
sd = socket(AF_INET,SOCK_STREAM,0);
fcntl(sd, F_SETFL, O_NONBLOCK);
connect(sd,(struct sockaddr *)&servaddr, sizeof(servaddr));
event.events = EPOLLOUT | EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET ;
event.data.fd = sd;
event.data.ptr = data; //user data
epoll_ctl((int)g_epfd, EPOLL_CTL_ADD, sd, &event) ; // successful
......
}
Thread 2:
void * sock_thread()
{
n = epoll_wait(g_epfd, events, MAX_SOCKETS, -1);
...
}
When a event happens on the sd
added to g_epfd
, I see that sd != events[i].data.fd
. The value of events[i].data.fd
contain unreasonable value. But the event is detected correctly.Also, the user data that was attached to event.data is obtained fine from events[i].data.ptr
why is events[i].data.fd
is not the same as sd
that was added with EPOLL_CTL
?
해결책
제휴하지 않습니다 StackOverflow