linux網路編程之socket(十三) epoll系列函數簡介與select、poll的區別

來源:互聯網
上載者:User

一、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表示永不逾時

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.