The epoll_ctl() system call can be used to add file descriptors to and remove file descriptors from a given epoll context: #include <sys/epoll.h> int epoll_ctl (int epfd, The header <sys/epoll.h> defines theepoll_eventstructure as:
struct epoll_event { A successful call toepoll_ctl()controls the epoll instance associated with the file descriptorepfd. The parameteropspecifies the operation to be taken against the file associated withfd. Theevent parameter further describes the behavior of the operation. Here are valid values for theopparameter: EPOLL_CTL_ADD
EPOLL_CTL_DEL
EPOLL_CTL_MOD
Theevents field in theepoll_eventstructure lists which events to monitor on the given file descriptor. Multiple events can be bitwise-ORed together. Here are valid values: EPOLLERR
EPOLLET
EPOLLHUP
EPOLLIN
EPOLLONESHOT
EPOLLOUT
EPOLLPRI
Thedatafield inside theevent_pollstructure is for the user’s private use. The contents are returned to the user upon receipt of the requested event. The common practice is to setevent.data.fdtofd, which makes it easy to look up which file descriptor caused the event. Upon success,epoll_ctl()returns0. On failure, the call returns-1, and setserrnoto one of the following values: EBADF
EEXIST
EINVAL
ENOENT
ENOMEM
EPERM
As an example, to add a new watch on the file associated withfdto the epoll instanceepfd, you would write: struct epoll_event event; event.data.fd = fd; /* return the fd to us later */ ret = epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &event); To modify an existing event on the file associated withfdon the epoll instanceepfd, you would write: struct epoll_event event; event.data.fd = fd; /* return the fd to us later */ ret = epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &event); Conversely, to remove an existing event on the file associated withfdfrom the epoll instanceepfd, you would write: struct epoll_event event; ret = epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &event); Note that theeventparameter can beNULLwhenopisEPOLL_CTL_DEL, as there is no event mask to provide. Kernel versions before 2.6.9, however, erroneously check for this parameter to be non-NULL. For portability to these older kernels, you should pass in a valid non-NULLpointer; it will not be touched. Kernel 2.6.9 fixed this bug.
blog comments powered by Disqus |
|
|
|
|
|
|
|