select() 调用的剩余时间

问题描述 投票:0回答:5

我在 Linux/ARM 平台上使用

select()
来查看 udp 套接字是否已收到数据包。我想知道如果 select 调用在超时之前返回(已检测到数据包),那么 select 调用还剩下多少时间。

大致如下:

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);
    }
}
c linux polling posix-select
5个回答
3
投票

最安全的事情是忽略

select()
的模糊定义并自己计时。

只需获取选择之前和之后的时间,然后从您想要的间隔中减去该时间即可。


1
投票

如果我没记错的话,select()函数会处理超时和I/O参数,当select返回时,剩余时间会在超时变量中返回。

否则,您必须在拨打电话前记录当前时间,并在拨打电话后再次记录当前时间并获取两者之间的差值。


1
投票

来自 OSX 上的“man select”:

 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.

0
投票

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

请注意,这不能跨其他系统移植(因此上面引用的 OS X 手册中出现警告),但可以在 Linux 上使用。

吉拉德


-2
投票

不要使用 select,尝试在代码中使用大于 1024 的 fd,看看会得到什么。

© www.soinside.com 2019 - 2024. All rights reserved.