1. Usage scenarios

  1. Handling multiple sockets
  2. Handles both data entry and network connections
  3. Handles both listening sockets and connection sockets
  4. Process both TCP and UDP requests
  5. Listen on multiple ports and handle multiple services

2. Select system call

Purpose: Listen for readable, writable, exception events on file descriptors of user interest

#include <sys/select.h>
int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);
Copy the code
  1. NFDS specifies the total number of file descriptors to listen on, and since file descriptors start at 0, it is usually set to the maximum file descriptor value plus 1
  2. Readfds, Writefds, ExceptfDS are respectively readable and writable file descriptors corresponding to exception events, which are passed in when calling SELECT and modified by the kernel when returning
  3. The number of file descriptors that the fd_set structure can hold is specified by FD_SETSIZE, which limits the number of file descriptors that select can process at the same time
#include <sys/select.h> FD_ZERO(fd_set* fdset); FD_SET(int fd, FD_SET * fdset); /* set the fdset bit */ FD_CLR(int fd, fd_set* fdset); */ int FD_ISSET(int fd, fd_set* fdset); /* Whether the fd bit of fdset is set */Copy the code
  1. The timeout parameter sets the timeout period. If all members are 0, select returns immediately. If timeout passes NULL, block until a file descriptor is ready
struct timeval { long tv_set; /* seconds */ long tv_usec; /* subtle */};Copy the code
  1. Returns the total number of ready file descriptors

File descriptor readable ready conditions:

  1. The socket kernel can receive buffer bytes without blocking reads, and the number of bytes returned by the read operation is greater than 0
  2. When the peer party closes the connection, the socket read operation returns 0
  3. There is a new connection on the listening socket
  4. The socket has an unhandled error. Use getsockopt to read and clear the error

File descriptor writable ready conditions:

  1. The socket kernel send buffer can write to the socket without blocking, and the number of bytes returned by the write operation is greater than 0
  2. The write operation is disabled. The SIGPIPE signal is triggered when the write operation is performed on the closed socket
  3. The socket succeeded or failed to use non-blocking connet
  4. An unhandled error occurred on the socket. Repeat 4

3. poll system invocation

Purpose: Similar to select, polls a certain number of file descriptors to test for readiness

#include <poll.h>
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
Copy the code
  1. FDS is an array of structures that specify the file descriptor to listen for and the events of interest
struct pollfd { int fd; /* File descriptor */ short events; /* Registered events */ short revents; /* The event that actually happened, kernel fill */};Copy the code
  1. Events, Revents consists of a series of events in bitwise or, of the following types:
POLLIN: data (common and priority) readable POLLPRI: data (common and priority) readable POLLOUT: data (common and priority) writable POLLRDHUP: the peer party closes the TCP connection or closes the write end POLLERR: error POLLHUP: suspends POLLNVAL: The file descriptor is not openCopy the code
  1. The return value of the RECV call should distinguish whether the socket received valid data from the request to close the connection
  2. NFDS specifies the size of the FDS array

If the timeout is -1, the call will block until an event occurs. If the timeout is 0, return 6 immediately. Returns the total number of ready file descriptors

4. Epoll system invocation

The task is accomplished by a set of functions that place events on file descriptors of interest to the user in an event table in the kernel without retransmission on each call. Epoll requires an additional file descriptor to identify the event table in the kernel

#include <sys/epoll.h>
int epoll_create(int size);
Copy the code

Size doesn’t work, it just tells the kernel how big the event table needs to be, and returns the file descriptor for the kernel event table

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

Fd is the file descriptor to operate on, op is the operation type:

EPOLL_CTL_MOD: modifies the events registered on the fd. EPOLL_CTL_DEL: deletes the events registered on the FDCopy the code

Event Specifies an event.

struct epoll_event { __uint32_t events; /* epoll event */ epoll_data_t data; /* User data */}; typedef union epoll_data { void* ptr; int fd; uint32_t u32; uint64_t u64; } epoll_data_t;Copy the code

Epoll_ctl Returns 0 on success and -1 on failure, and sets errno. Epoll_ctl returns 0 on success and -1 on failure

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

As in poll, maxEvents specifies the maximum number of events to listen on, which must be greater than 0. Ready events are copied from the kernel event table into the array pointed to by Events

5. LT and ET modes

LT: Level Trigger The read buffer is not empty for read operations and the file descriptor is returned for each call. For write operations, each call returns the file descriptor write-ready as long as the write buffer is not sufficient. The application may not process the event immediately.

ET: Edge Trigger. When a ready event is present, the application must process it immediately and subsequent calls will not Trigger the ready event of the file descriptor. This mode reduces the number of times events are fired.

The registered file descriptor for ET mode should be non-blocking; otherwise, read and write operations will block.

Select poll epoll difference

Event set:

Select: A file descriptor that corresponds to a readable, writable, or exception event is passed in with 3 parameters. Poll: Pollfd. events is passed in file descriptors and events of interest, and pollfd.revents is changed by the kernel. Epoll: The kernel manages file descriptors and events through the event table

Select: O(n) poll: O(n) epoll: O(1)

Maximum number of supported file descriptors:

Select: indicates the maximum value. Poll: 65535 epoll: 65535

Working Mode:

Select: LT poll: LT epoll: LT and ET

Kernel implementation and productivity:

Poll: O(n) poll: O(1) poll: O(1)