一、epoll 系列函數簡介
#include <sys/epoll.h>
int epoll_create(int size);
int epoll_create1(int flags);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
* epoll_create(2) creates an epoll instance and returns a file descriptor referring to that instance. (The more recent
epoll_create1(2) extends the functionality of epoll_create(2).)
* Interest in particular file descriptors is then registered via epoll_ctl(2). The set of file descriptors currently
registered on an epoll instance is sometimes called an epoll set.
* epoll_wait(2) waits for I/O events, blocking the calling thread if no events are currently available.
1、epoll_create1 產生一個epoll 執行個體,返回的是執行個體 的控制代碼。flag 可以設定為0 或者EPOLL_CLOEXEC,為0時函數表現與epoll_create一致,EPOLL_CLOEXEC標誌與open 時的 O_CLOEXEC 標誌類似,即進程被替換時會關閉檔案描述符。
2、epoll_ctl :
(1)epfd:epoll 執行個體控制代碼;
(2)op:對檔案描述符fd 的操作,主要有EPOLL_CTL_ADD、 EPOLL_CTL_DEL等;
(3)fd:需要操作的目標 檔案描述符;
(4)event:結構體指標
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 參數主要有EPOLLIN、EPOLLOUT、EPOLLET 、EPOLLLT等;一般data 共同體我們設定其成員fd即可,也就是epoll_ctl 函數的第三個參數。
3、epoll_wait:
(1)epfd:epoll 執行個體控制代碼;
(2)events:結構體指標
(3)maxevents:事件的最大個數
(4)timeout:逾時時間,設為-1表示永不逾時