Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about epoll_ctl()

Tags:

c

sockets

epoll

when using epoll_ctl(), I found that the third parameter "fd" is another file descriptor besides the epoll file descriptor "epfd". And I saw an example like this:

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, file descriptor in event.data.fd is the same as the third parameter in epoll_ctl, why need to pass this descriptor twice? is there any difference?

like image 912
realjin Avatar asked Feb 17 '12 09:02

realjin


People also ask

What is Epoll_ctl?

Description. The interface epoll_ctl() shall control an epoll file descriptor. The parameter epfd shall specify the epoll file descriptor to control. The parameter op shall specify the operation to perform on the specified target file descriptor.

What is the use of epoll?

epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5. 44 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them.

What is Epollpri?

EPOLLPRI There is an exceptional condition on the file descriptor. See the discussion of POLLPRI in poll(2). EPOLLERR Error condition happened on the associated file descriptor. This event is also reported for the write end of a pipe when the read end has been closed.

What is epoll_wait?

The epoll_wait() system call waits for events on the epoll(7) instance referred to by the file descriptor epfd. The buffer pointed to by events is used to return information from the ready list about file descriptors in the interest list that have some events available. Up to maxevents are returned by epoll_wait().


1 Answers

Actually you don't have to set event.data.fd. It's a union, you can set other members. When epoll_wait returns you get the event.data associated with the descriptor that became interesting:

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

Which means you're completely free not to put anything in fd and put something in ptr instead (for example).

In conclusion, epoll_ctl can't rely on the fact you'll fill fd in, that's why it has a separate explicit parameter.

like image 147
cnicutar Avatar answered Sep 23 '22 05:09

cnicutar