简体   繁体   English

epoll_wait 直到按下 enter 才会唤醒

[英]epoll_wait doesn't wake up until pressing on enter

I'm new with epoll.我是 epoll 的新手。 My code is working fine.我的代码工作正常。 The epoll is storing my file-descriptor and wait until file-descriptor is "ready". epoll 正在存储我的文件描述符并等待文件描述符“准备好”。 But, for some reason it will not wake up until I will press on Enter (even though data has already received to fd, and after enter I will immediately see all data that has been sent before).但是,出于某种原因,直到我按下回车键它才会醒来(即使数据已经接收到 fd,并且在回车后我会立即看到之前发送的所有数据)。 After one enter it will work as expected (no enters needed and when fd is ready again it will wake up again).一次输入后它将按预期工作(不需要输入,当 fd 再次准备好时它将再次唤醒)。

Here is am essence of my code:这是我的代码的本质:

    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;
        }
    }

This这个

if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &event))

should probably be大概应该是

if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, myfd, &event))

In the first line you tell epoll to monitor fd 0 which is typically the standard input.在第一行中,您告诉 epoll 监视通常是标准输入的 fd 0。 That's why it waits for it, eg for your Enter.这就是它等待它的原因,例如等待您的输入。

Note that your original code works only by coincidence.请注意,您的原始代码只是巧合。 It just happens that when you Enter there is data in your myfd (and even if there's none msgrcv blocks).碰巧当您输入时,您的myfd中就有数据(即使没有msgrcv块)。 And once you pressed Enter it will wake up all the time since epoll knows that STDIN is ready but you didn't read from it.一旦你按下回车,它就会一直醒来,因为 epoll 知道 STDIN 已经准备好了,但你没有从中读取。

Thanks to kamilCuk, I noticed that msgget doesn't return a file descriptor as I thought.感谢 kamilCuk,我注意到 msgget 没有像我想的那样返回文件描述符。 It returns a "System V message queue identifier".它返回一个“System V 消息队列标识符”。

And as freakish said before, System V message queues don't work with selectors like epoll.正如之前所说的那样,System V 消息队列不适用于像 epoll 这样的选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM