About epoll_ctl ()

when using epoll_ctl (), I found that the third parameter "fd" is a different file descriptor than the epoll file descriptor "epfd". And I saw an example:

event.data.fd = sfd; //sfd is a fd for listening event.events = EPOLLIN | EPOLLET; s = epoll_ctl (efd, EPOLL_CTL_ADD, sfd, &event); 

As I saw, the file descriptor in event.data.fd matches the third parameter in epoll_ctl, why do I need to pass this descriptor twice? is there any difference

+8
source share
1 answer

In fact, you do not need to install event.data.fd . This is a union; you can establish other members. When epoll_wait returns, you get the event.data associated with the handle, which has become interesting:

 typedef union epoll_data { void *ptr; int fd; uint32_t u32; uint64_t u64; } epoll_data_t; 

This means that you are completely free to not put anything in fd and instead put something in ptr (for example).

In conclusion, epoll_ctl cannot rely on the fact that you fill in fd , so it has a separate explicit parameter.

+14
source

Source: https://habr.com/ru/post/1397001/


All Articles