Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top