Epo2.6 kernel epoll usage example

Source: Internet
Author: User
Article Title: epo2.6 kernel epoll usage example. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

All functions used by epoll are declared in the sys/epoll. h header file. The following briefly describes the data structures and functions used:

Data Structure Used

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 */

};

The epoll_event struct is used to register events of interest and return events to be processed. The epoll_data consortium is used to save data related to a file descriptor that triggers the event. For example, a client is connected to the server, the server can obtain the socket file descriptor corresponding to the client by calling the accept function. This file descriptor can be assigned to the fd field of epoll_data so that subsequent read/write operations can be performed on this file descriptor. The events field of the epoll_event struct indicates the event of interest and the event to be triggered. The possible value is EPOLLIN, which indicates that the corresponding file descriptor can be read;

EPOLLOUT: indicates that the corresponding file descriptor can be written;

EPOLLPRI: indicates that the corresponding file descriptor has an urgent amount of data readable (I don't quite understand what it means, it may be an event like a client closing a socket connection );

EPOLLERR: indicates that the corresponding file descriptor is incorrect;

EPOLLHUP: indicates that the corresponding file descriptor is hung up;

EPOLLET: indicates that an event occurs in the corresponding file descriptor;

Functions used:

1. epoll_create Function

Function declaration: int epoll_create (int size)

This function generates a file descriptor dedicated to epoll, the parameter specifies the maximum range of the generated Descriptor (I think this parameter is similar to the first parameter of the select function, but I am not sure how to set it ).

2. epoll_ctl Function

Function declaration: int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event)

This function is used to control events on a file descriptor. You can register events, modify events, and delete events.

Parameter: epfd: The epoll-specific file descriptor generated by epoll_create;

Op: the operation to be performed, such as registration event. The possible values include EPOLL_CTL_ADD registration and EPOLL_CTL_MOD repair.

Modify and delete EPOLL_CTL_DEL

Fd: the associated file descriptor;

Event: pointer to epoll_event;

If the call is successful, 0 is returned. If the call is unsuccessful,-1 is returned.

3. epoll_wait Function

Function declaration: int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout)

This function is used to poll the occurrence of I/O events;

Parameters:

Epfd: The epoll-specific file descriptor generated by epoll_create;

Epoll_event: the array used to return the events to be processed;

Maxevents: Number of events that can be processed each time;

Timeout: the timeout value for waiting for an I/O event;

Number of events returned.

Example:

# Include

# Include

# Include

# Include

# Include

# Include

# Include

# Include

# Define MAXLINE 10

# Define OPEN_MAX 100

# Define LISTENQ 20

# Define SERV_PORT 5555

# Define inftim1000

[1] [2] [3] Next page

Related Article

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.