UNIX Environment Advanced Programming: The Epoll function uses the detailed explanation

Source: Internet
Author: User
Tags epoll int size socket linux

EPOLL-I/O Event Notification Facility

In Linux network programming, it is a long time to use Select to do event triggering. In the new Linux kernel, there is a mechanism to replace it, which is epoll.

The biggest advantage over Select,epoll is that it does not reduce efficiency as the number of listening FD increases. Because in the select implementation in the kernel, it is handled by polling, the more the number of polled FD, the more natural time consuming.

Also, the LINUX/POSIX_TYPES.H header file has such a statement:

#define __FD_SETSIZE 1024

Indicates that the select has up to 1024 FD at the same time, of course, you can enlarge the number by modifying the header file and then recompiling the kernel, but this does not seem to be a permanent fix.

The Epoll interface is very simple, with a total of three functions:

int epoll_create (int size);

Creates a epoll handle, which is used to tell the kernel how large the number of listeners is. This parameter differs from the first parameter in select () and gives the value of the maximum listener fd+1. Note that when you create a good epoll handle, it will occupy an FD value, under Linux if you look at the/proc/process id/fd/, you can see this fd, so after using Epoll, you must call Close () closed, otherwise it may cause FD to be depleted.

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

The Epoll event registration function, which differs from select () is to tell the kernel what type of event to listen to when listening to an event, but to register the type of event to listen for first. The first parameter is the return value of Epoll_create (), and the second parameter represents the action, which is represented by three macros:

Epoll_ctl_add: Register the new FD to EPFD;

Epoll_ctl_mod: Modify the Listening event of the registered FD;

Epoll_ctl_del: Deletes an FD from the EPFD;

The third parameter is the FD that needs to be monitored, and the fourth parameter tells the kernel what to listen to, and the struct epoll_event structure is as follows:

typedef Union EPOLL_DATA {  
    void *ptr;  
    int FD;  
    __uint32_t u32;  
    __uint64_t u64;  
} epoll_data_t;  
      
struct Epoll_event {  
    __uint32_t events;/* Epoll Events * *
    epoll_data_t Data/* USER data variable * *
};

Events can be a collection of several macros:

Epollin: Indicates that the corresponding file descriptor can be read (including closing the socket properly);

Epollout: Indicates that the corresponding file descriptor can be written;

Epollpri: Indicates that the corresponding file descriptor has an urgent data readable (here should indicate the arrival of Out-of-band data);

Epollerr: Indicates that the corresponding file descriptor has an error;

Epollhup: Indicates that the corresponding file descriptor is hung;

Epollet: Sets the Epoll as the Edge trigger (edge triggered) mode, which is relative to the level triggered.

Epolloneshot: Listen to only one event, when listening to the event, if you still need to continue to listen to this socket, you need to add this socket to the Epoll queue.

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

Wait for the event to occur, similar to the Select () call. Parameter events are used to get the collection of events from the kernel, maxevents the kernel of this event, the Maxevents value cannot be greater than the size when the Epoll_create () is created, and the parameter timeout is the timeout (milliseconds, 0 will return immediately ,-1 will be uncertain, and there are claims that it is permanently blocked. The function returns the number of events that need to be handled, such as returning 0 to indicate that the timeout has expired.

About ET, lt two kinds of working mode:

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.