Epoll II: Epoll Combat

Source: Internet
Author: User
Tags epoll
epoll_create system Call
int epoll_create (int size);

Epoll_create returns a handle, and the use of Epoll will be identified by this handle, the argument size is the number of approximate events to tell Epoll to handle, and the close handle must be called when the epoll is no longer used.

Size This parameter simply tells the kernel the approximate number of events that the Epoll object will handle, not the maximum number of events that can be handled EPOLL_CTL system calls

int epoll_ctl (int epfd,int op,int fd,struct epoll_event*event);

Epoll_ctl to add, modify, or delete an event of interest to the Epoll object, return 0 for success, or return 1, at which point the error type is to be judged according to the errno error code, and the event returned by the Epoll_wait method is necessarily added to the epoll_ctl through Epoll. The parameter EPFD is the handle returned by the epoll_create.
Parameter op's meaning table:

OP's value meaning
Epoll_ctl_add Add a new event to the Epoll
Epoll_ctl_mod To modify events in Epoll
Epoll_ctl_del To delete an event in Epoll

The third parameter FD is the connection socket to be monitored, and the fourth parameter is to tell Epoll what kind of event is interested in, using the epoll_event structure, which is described earlier in the Epoll implementation mechanism to create EPITEM structure for each event, while There is a Epoll_event type event member in the Epitem

struct epoll_event{
  _uint32_t events;
  epoll_data_t data;
                          Significance of events in table Epoll_event
the value of events meaning
Epollin Indicates that there is data on the corresponding connection that can be read
Epollout Indicates that the corresponding connection can be written to send data
Epollrdhup Indicates a remote shutdown or Half-open connection for a TCP connection
Epollpri Indicates that there is urgent data to be read on the corresponding connection
Epollerr Indicates an error occurred on the corresponding connection
Epollhup Indicates that the corresponding connection has been suspended
Epollet Indicates that the trigger mode is set to the Edge trigger (ET) system by default is a horizontal trigger (LT)
Epolloneshot This event is only handled once, and the next time you need to process it, rejoin the Epoll

And the data member is a EPOLL_DATA union, defined as follows:

typedef Union  epoll_data{
    void  *ptr;
    int  FD;
    uint32_t u32;
    uint64_t  u64;

} epoll_data_t;

This member of data is also related to how it is used, and the Ngx_epoll_module module uses only federated PTR members as pointers to ngx_connection_t connections. epoll_wait system Call

int epoll_wait (int epfd,struct epoll_event *  events,int maxevents,int timeout);

Collects events that have occurred in epoll-monitored events, and if no event occurs in Epoll, waits timeout milliseconds to return, epoll_wait return value indicates the number of events currently occurring, returns 0 in fruit, indicates no event occurred, if-1, Indicates an error, you need to check the errno error type to determine the type of error. The first parameter EPFD is the descriptor of the Epoll, and the second parameter events is an array of epoll_event structures that are allocated, and Epoll will copy the occurrences into the events array, and the third parameter maxevents represents the maximum number of times that can be returned. In general, the maxevents and pre-configured events array sizes are equal. The fourth parameter timeout indicates that the maximum waiting time (in milliseconds) is not detected. Epoll has two ways of working LT (horizontal trigger) ET (Edge Trigger)

See Blog IO multiplexing Select poll Epoll function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.