我使用在Linux / ARM平台select(),看看UDP套接字已接收到数据包。我想知道多少时间在选择呼叫剩余如果它的超时时间(已检测的分组)。

之前返回

沿东西线:

int wait_fd(int fd, int msec)
{
    struct timeval tv;
    fd_set rws;

    tv.tv_sec = msec / 1000ul;
    tv.tv_usec = (msec % 1000ul) * 1000ul;

    FD_ZERO( & rws);
    FD_SET(fd, & rws);

    (void)select(fd + 1, & rws, NULL, NULL, & tv);

    if (FD_ISSET(fd, &rws)) { /* There is data */
        msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
        return(msec?msec:1);
    } else { /* There is no data */
        return(0);
    }
}
有帮助吗?

解决方案

在最安全的事情是忽略select()和时间它自己的暧昧定义

刚刚得到的时间之前和之后的选择和减去从你想要的时间间隔。

其他提示

如果正确地记得的select()函数将超时和一个I / O参数,并且当选择返回剩余时间在超时变量返回。

否则,你将有后打电话之前记录当前的时间,并一次获得两者之间的区别。

从 “人选择” 上OSX:

 Timeout is not changed by select(), and may be reused on subsequent calls, however it 
 is good style to re-ini-tialize it before each invocation of select().

您将需要调用select之前调用gettimeofday的,然后退出gettimeofday的。

[编辑]看来linux是稍有不同:

   (ii)   The select function may update the timeout parameter to indicate
          how much time was left. The pselect  function  does  not  change
          this parameter.

   On Linux, the function select modifies timeout to reflect the amount of
   time not slept; most other implementations do not do this.  This causes
   problems  both  when  Linux code which reads timeout is ported to other
   operating systems, and when code is  ported  to  Linux  that  reuses  a
   struct  timeval  for  multiple selects in a loop without reinitializing
   it.  Consider timeout to be undefined after select returns.

Linux的选择()更新超时参数,以反映已经过去的时间。

请注意,这不是在其他系统(因此在上面引述的OS X手册的警告)便携式但确实工作与Linux操作系统。

吉拉德

不要使用选择,尝试用超过1024 FD大与您的代码,看看你会得到什么。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top