[英]epoll_wait doesn't wake up until pressing on enter
我是 epoll 的新手。 我的代碼工作正常。 epoll 正在存儲我的文件描述符並等待文件描述符“准備好”。 但是,出於某種原因,直到我按下回車鍵它才會醒來(即使數據已經接收到 fd,並且在回車后我會立即看到之前發送的所有數據)。 一次輸入后它將按預期工作(不需要輸入,當 fd 再次准備好時它將再次喚醒)。
這是我的代碼的本質:
int nEventCountReady = 0;
epoll_event event, events[EPOLL_MAX_EVENTS];
int epoll_fd = epoll_create1(0);
if(epoll_fd == -1)
{
std::cout << "Error: Failed to create EPoll" << std::endl;
return ;
}
event.events = EPOLLIN;
event.data.fd = myfd;
if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &event))
{
fprintf(stderr, "Failed to add file descriptor to epoll\n");
close(epoll_fd);
return ;
}
while(true)
{
std::cout << "Waiting for messages" << std::endl;
nEventCountReady = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, 30000); << Stuck until Enter will be pressed (at first while loop)
for(int i=0; i<nEventCountReady; i++)
{
msgrcv(events[i].data.fd, oIpCMessageContent, sizeof(SIPCMessageContent), 1, 0);
std::cout << oIpCMessageContent.buff << std::endl;
}
}
這個
if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &event))
大概應該是
if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, myfd, &event))
在第一行中,您告訴 epoll 監視通常是標准輸入的 fd 0。 這就是它等待它的原因,例如等待您的輸入。
請注意,您的原始代碼只是巧合。 碰巧當您輸入時,您的myfd
中就有數據(即使沒有msgrcv
塊)。 一旦你按下回車,它就會一直醒來,因為 epoll 知道 STDIN 已經准備好了,但你沒有從中讀取。
感謝 kamilCuk,我注意到 msgget 沒有像我想的那樣返回文件描述符。 它返回一個“System V 消息隊列標識符”。
正如之前所說的那樣,System V 消息隊列不適用於像 epoll 這樣的選擇器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.