Linux High Performance Service programming (I/O multiplexing)

Source: Internet
Author: User
Tags epoll int size readable set time

I/O multiplexing (itself blocked)
Network applications require the use of I/O multiplexing technology:
1. The client program needs to process multiple sockets simultaneously.
2. Clients need to handle both user input and network connection
The 3.TCP server handles both the listening socket and the connection socket.
4. Server to process both TCP and UDP requests
5. The server listens to multiple ports at the same time, or handles multiple services.


System calls to implement I/O multiplexing under Linux are mainly Select,poll and Epoll

Purpose of the select system call: For a period of time, listen for events such as readable, writable, and abnormal on file descriptors that are of interest to the user
Prototypes for select system calls:
#include <sys/select.h>
int select (int nfds,fd_set readfds,fd_set writefds,fd_set exceptfds,struct timeval timeout);
The Nfds parameter specifies the total number of file descriptors that are being listened to, and it is usually set to the maximum value in all file descriptors of the Select Listener plus 1, because the file descriptor is counted starting from 0
The Readfds,writefds,exceptfds parameter points to a set of file descriptors that are readable, writable, and corresponding to an event, respectively
The timeout parameter is used to set the time-out for the Select function, which is a pointer to the Timeval struct type (microsecond level) and takes a pointer parameter because the kernel modifies it to tell the application how long the select has waited

Select returns the total number of ready (readable, writable, exception) file descriptors when successful. If no file descriptor is ready within the timeout period, select returns a 0.select failure with 1 and set errno.
During the wait, the program accepts the signal, then select returns-1 immediately, and sets errno to Eintr.

Poll system call: Polling for a certain number of file descriptors within a set time, tested for readiness
Poll Prototypes:
#include <poll.h>
int poll (struct pollfd* fds,nfds_t nfds,int timeout);
The FDS parameter is an array of POLLFD struct types that specifies events such as readable, writable, and exceptions that occur on all of the file descriptors that we are interested in.
The Nfds parameter specifies the size of the FDS for the Listener event collection, whose type is nfds_t defined: typedef unsigned long int nfds_t;
The timeout parameter specifies the timeout value for poll, in milliseconds. When timeout is-1, the poll call will block forever, knowing that an event occurs; When timeout is 0 o'clock, the poll call returns immediately

epoll Series system calls:
Epoll is a Linux-specific I/O multiplexing function
The creation of the file descriptor (used to identify the event table in the kernel) is epoll_creat to complete:
#include <sys/epoll.h>
int epoll_creat (int size)
Operation Epoll Kernel Schedule:
#include <sys/epoll.h>
int epoll_ctl (int epfd,int op,int fd,struct epoll_event *event)
The FD parameter is the file descriptor to be manipulated, and the OP parameter formulates the operation type (Epoll_ctl_add (to register a large event on the FD in the event table), Epoll_ctl_mod (modifies the registration event on the FD), Epoll_ctl_del (delete the registration event on the FD))
The event parameter specifies the events, which are epoll_event struct pointer types

epoll_wait Function:
The primary interface of the Epoll series system call is the Epoll_wait function, which waits for an event on a set of file descriptors during a time-out period
#include <sys/epoll.h>
int epoll_wait (int epfd,struct epoll_event* events,int maxevents,int Timeout)
The function returns the number of ready file descriptors when successful, returns 1 on failure and sets errno
The maxevent parameter specifies the maximum number of events to listen on, which must be greater than 0


Epoll There are two modes of operation for file descriptors
: LT (Level Trigger) mode (default operating mode) and ET (Edge trigger) mode (efficient operating mode)
LT: When Epoll_wait detects that an event has occurred and notifies the application that the application can not handle the event immediately, Epoll_wait will also advertise the event to the application again, until the event is processed, when the application next calls Epoll_wait.
ET: When Epoll_wait detects that an event has occurred on it and notifies the application of the event, the application must handle the event immediately because subsequent epoll_wait calls will no longer notify the application of this event
Epolloneshot Events


The difference between Select,poll and Epoll:

Advanced application of I/O multiplexing one: non-blocking connect
A errno value when connect fails: einprogress This error occurs when connect is called to a non-blocking socket, and the connection is not immediately established.
Advanced application of I/O Multiplexing II: chat Room Program
Client function: One is to read the user data from the standard input terminal and send the user data to the server, and the second is to print the data sent to it by the server program to the standard output terminal.
Server functions: Accepts customer data and sends customer data to every client that logs on to the service area (except for data senders)
Client: Use poll to simultaneously listen to user input and network connection, and use the splice function to direct the user input to the network connection to send, so as to achieve 0 copies of data, improve program execution efficiency.
Server: Use poll to simultaneously listen for sockets and connect sockets, and use a policy of sacrificing space for time to improve server performance.
Advanced applications for I/O multiplexing Three: * * * * processing both TCP and UDP services
Super Service XINETD: Manage multiple sub-services simultaneously, that is, listening to multiple ports
XINETD manages all services using the/etc/xinetd.conf master configuration file and the self-configuration file under the. Etc.xinetd.d Directory

Linux High Performance Service programming (I/O multiplexing)

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.