我有兩個文件描述符,一個讀取管道上的文件描述符和另一個套接字連接描述符。它們都不是非阻塞的。它們都被添加到單個EPOLLIN事件的epoll上下文中。最後,我用timeout = -1調用epoll_wait。以下是示例代碼。我有 兩個問題: -epoll返回0事件
不要管和連接描述符需要非阻塞。這不是邊緣觸發的。如果是,是好的做法還是強制性的,如果是強制性的,爲什麼?
我將超時設置爲-1,但epoll_wait立即返回值爲0.爲什麼會發生這種情況?當超時時間爲-1時,只有在發生事件時才返回epoll_wait。
int total_fd_ready = -1; struct epoll_event pipe_event, connection_event, total_events[64]; pipe_event.data.fd = pipe_fd[0]; piple_event.events = EPOLLIN; connection_event.data.fd = conn_fd; connection_event.events = EPOLLIN; total_fd_ready = Epoll_wait(epoll_fd, total_events, 64, -1); printf("%d\n", total_fd_ready);
Epoll_wait被定義爲
int Epoll_wait(int e_fd, struct epoll_event *events, int max_events, int timeout) { #ifdef DBG printf("Epoll_wait called on epoll_fd: %d with max_events: %d and timeout: %d\n", e_fd, max_events, timeout); #endif int result = -1; if(result = (epoll_wait(e_fd, events, max_events, timeout)) < 0) if(errno != EINTR) err_sys("epoll_wait error with epoll fd: %d and timeout : %d\n", e_fd, timeout); #ifdef DBG else printf("epoll_wait was interrupted\n"); #endif return result; }
UPDATE: 發現的問題,雖然我無法解釋爲什麼結果被設置爲0。我需要護腕下面的if語句
if((result = (epoll_wait(e_fd, events, max_events, timeout))) < 0)
如何聲明和初始化total_events? –
更新了它的代碼 – Jimm
如果編譯時啓用了警告,例如用'gcc -Wall',編譯器會適當的提醒你 –