linux中投票系统调用的错误行为

Incorrect behaviour of poll system call in linux

本文关键字:错误 系统调用 linux      更新时间:2023-10-16

在使用poll系统调用时,我发现了一个奇怪的行为。我有下面的代码片段:

struct pollfd myPollfds[nCount];
ACE_Time_Value selectTime;
selectTime.set(60);
 myPollfds[0].fd = rtrrmEvent[0];
 myPollfds[0].events = POLLIN | POLLRDHUP | POLLHUP | POLLERR;
        myPollfds[0].revents = 0;
        myPollfds[1].fd = rtrfeEvent[0];
        myPollfds[1].events = POLLIN | POLLRDHUP | POLLHUP | POLLERR;
        myPollfds[1].revents = 0;
        myPollfds[2].fd = _h[msclient_pos];
        myPollfds[2].events = POLLIN | POLLRDHUP | POLLHUP | POLLERR;
        myPollfds[2].revents = 0;
        myPollfds[3].fd = holdTimeEvent[0];
        myPollfds[3].events = POLLIN | POLLRDHUP | POLLHUP | POLLERR;
        myPollfds[3].revents = 0;
        ACE_Time_Value sleepTime(0,20000);
        while(isRunning() && !_stopRequested)
        {
                ACE_OS::sleep(sleepTime);
                for(int i = 0; i < 4; i++)
                        myPollfds[i].revents = 0;
                waitResult = ACE_OS::poll (myPollfds, nCount, &selectTime);
                if(waitResult == -1) // poll failed
                {
                       DEBUG("%s", "poll failed");
                         continue;
                }
                else if(waitResult == 0) // Time out
                {
                        //Do something .
                }
                char nodata[256];
                for(short i = 0; i < nCount; i++)
  if(myPollfds[i].revents == POLLIN)
                        {
                                if(i == rtrrm_pos)
                                {
                                         // Stop channel
                                }
                                else if(i == rtrfe_pos) // 'rtrfe' command
                                {
                                        DEBUG("%s", "fe issued");
                                }
                                else if(i == msclient_pos || waitResult == 0)
                                {
                                      //Do something
                                }
                                else if(i == holdTime_pos)
                                {
                                        DEBUG("%s", "Hold issued");
                                }
                        }
                        else
                        {
                                DEBUG("polling failed with with myPollfds[i].revents == %d",myPollfds[i].revents);
                        }

问题是,我得到一些时间:"使用myPollfds进行的民意调查失败了。事件== 0":"使用myPollfds进行的民意调查失败了。

轮询调用没有等待fd的设置。有人能帮忙吗?

这个测试是错误的:

  if(myPollfds[i].revents == POLLIN)

它可能应该是(不能确定,因为你删除了所有的逻辑):

  if((myPollfds[i].revents & POLLIN) != 0)

同样,看到"轮询失败"消息是完全正常的。它只是意味着在那个特定的文件描述符上没有活动。你在轮询不止一个描述符,对吧?所以有些人没有活动是正常的

正如David所说,

"轮询失败"消息是完全正常的。

在任何情况下,当poll返回的不仅仅是POLLIN时例如:

poll ... = 1

{fd=8193, revents=POLLIN|**POLLHUP**}
if (POLLIN|POLLHUP == POLLIN)

将进入"else"子句。

我的C语言不太好,所以我不知道David的if语句是否正确