Epol Study Notes

Source: Internet
Author: User

Epol Study Notes

Epoll-related system calls

Epoll_create ()

Epoll_ctl ()

Epoll_wait ()

Int epoll_create (int size );

Create an epoll handle.

  1. The size parameter has been ignored since linux2.6.8.
  2. After an epoll handle is created, it occupies an fd value. After epoll is used, close () must be called to close it.

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

Epoll event registration function.

1. The first parameter is the returned value of epoll_create.

2. The second parameter represents an action, represented by three macros:

EPOLL_CTL_ADD: register a new fd to epfd;

EPOLL_CTL_MOD: modifies the listener events of the registered fd;

EPOLL_CTL_DEL: deletes an fd from epfd.

3. The third parameter is the fd to be monitored.

4. The fourth parameter is to tell the kernel what to listen.

The structure of struct epoll_event is as follows:

Typedef union epoll_data

{// Save the data related to a file descriptor of the trigger event

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 the following macros:

  1. EPOLLIN: indicates that the corresponding file descriptor can be read (including the normal shutdown of the Peer SOCKET );
  2. EPOLLOUT: indicates that the corresponding file descriptor can be written;
  3. EPOLLPRI: indicates that the corresponding file descriptor has an urgent readable data (Here it should indicate that out-of-band data has arrived );
  4. EPOLLERR: indicates that the corresponding file descriptor is incorrect;
  5. EPOLLHUP: indicates that the corresponding file descriptor is hung up;
  6. EPOLLET: Set EPOLL to Edge Triggered mode, which is relative to Level Triggered.
  7. EPOLLONESHOT: only listens for an event once. After listening for this event, if you want to continue listening for this socket, you need to add this socket to the EPOLL queue again.

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

Collect events that have occurred in epoll monitoring events.

  1. The events parameter is the allocated epoll_event struct array. epoll will assign events to the events array (the events cannot be a null pointer. the kernel is only responsible for copying data to this events array, will not help us allocate memory in user mode ).
  2. Maxevents tells us the kernel size of this events. The value of this maxevents cannot be greater than the size when epoll_create () is created. (Since linux2.6.8, the size parameter is ignored .) The maxevents value is queried using the cat/proc/sys/fs/file-max command, which is related to the memory size.
  3. The last timeout is the epoll_wait timeout. If it is 0, it indicates immediate return. If it is-1, it indicates waiting until an event occurs, if it is an arbitrary positive integer, it indicates waiting for such a long time. If there is no event, it is returned. Generally, if the main loop of the network is a separate thread, you can use-1 to ensure some efficiency. If it is in the same thread as the main logic, 0 can be used to ensure the efficiency of the main loop.
  4. If the function is successfully called, the number of file descriptors prepared on the corresponding I/O is returned. If the return value is 0, the system times out.

Edge Triggered Working Mode

  1. When epoll works in the ET mode, it is necessary to use a non-blocking set interface to avoid starving the task of processing multiple file descriptors due to the blocking read/blocking write operation of a file handle.
  2. It is best to call the epoll interface in ET mode in the following ways to avoid possible defects.

@ Based on non-blocking file handle

@ It must be suspended only when read () or write () returns EAGAIN.

However, this does not mean that every read () operation needs to be performed cyclically until an EAGAIN is generated. When read () when the returned data length is less than the requested data length, you can determine that there is no data in the buffer at this time, so that the read event has been processed.

This article permanently updates the link address:

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.