【问题标题】:Incorrect behaviour of poll system call in linuxlinux中poll系统调用的不正确行为
【发布时间】:2013-06-12 09:56:51
【问题描述】:

在使用 poll 系统调用时,我发现了一种奇怪的行为。 我有以下代码sn-p。

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[i].revents == 0 轮询失败” : “轮询失败,myPollfds[i].revents == 8193”

轮询调用未等待 fd 设置。 有人可以帮忙吗?

【问题讨论】:

  • -1 指责 linux。
  • 我不是在责怪 Linux,我很想知道我做错了什么......就是这样

标签: c++ linux system


【解决方案1】:

这个测试不正确:

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

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

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

此外,看到“轮询失败”消息是完全正常的。这只是意味着该特定文件描述符上没有任何活动。您正在对多个描述符进行轮询,对吗?所以有些人没有活动是正常的。

【讨论】:

  • 好的,我明白你在说什么,但是,即使没有正在进行的活动,它也应该达到代码的等待超时部分。但我从来没有观察到。else if(waitResult == 0) // Time out { //Do something . }
  • @samairtimer 没有活动时会发生什么?
  • 当没有活动时..我得到 myPollfds[i].revents = 8193 一些值,它不等同于 POLLIN,因此逻辑未定义。
  • 所以这个错误正是我所说的。 8193 &amp; POLLIN 不为零。
【解决方案2】:

正如大卫所说,

“轮询失败”消息完全正常。

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

poll ... = 1



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

正在进入“else”子句。

我的 C 语言不太好,所以我无法判断 David 的“if 语句”是否正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 2010-12-15
    • 2012-06-30
    • 1970-01-01
    • 2015-12-22
    • 2012-05-04
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多