Both select and Epoll are used to listen for the occurrence of an event on the socket-interface descriptor and to implement I/O multiplexing
Select (poll)
#include <sys/select.h>
#include <sys/time.h>
int Select (int maxfdpl, fd_set* readset, fd_set* Writeset, fd_set* exceptset, const struct timeval* timeout)
Poll all descriptive words once at the time of the call, and then poll again when the timeout occurs. If there is no descriptive word ready, then return 0; Midway error return-1; the description is ready, the corresponding position is 1, the other description word is 0, return the number of the prepared description.
Fd_set: An array of integers, each digit of each number corresponds to a descriptive word whose specific size is determined by the kernel's fd_setsize (1024).
void Fd_zero (fd_set* fdset);//clear all bits in fdset
void Fd_set (int. fd, fd_set* Fdset);//turn on "bit for FD" F Dset
void fd_clr (int fd, fd_set* fdset);//turn off the bit for FD in fdset
int fd_isset (int fd, fd_set* fdset);//i s the bit for FD on in Fdset
Epoll (Trigger)
Epoll has a callback function for each FD that is listening, and the corresponding callback function is invoked when an event occurs on the FD.
Series functions:
To create a function:
Creates a epoll handle that size-the number of listening sockets. When a epoll handle is created, it occupies an FD value, so after using Epoll, close () must be invoked, or it may cause the FD to run out.
#include <sys/epoll.h>
int epoll_create (int size);
Event Registration function:
int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);
The first parameter is the return value of the epoll_create ();
The second parameter represents the action, represented by three macros:
Epoll_ctl_add: Register the new FD into EPFD,
Epoll_ctl_mod: Modify the Listening event for the registered FD,
Epoll_ctl_del: Deletes an FD from the EPFD;
The third parameter is the FD that needs to be monitored;
The fourth parameter tells the kernel to listen for the event, 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
Use the following:
struct epoll_event ev;
EV.DATA.FD=LISTENFD;
ev.events=epollin| Epollet;
Epoll_ctl (Epfd,epoll_ctl_add,listenfd,&ev);