【发布时间】:2009-01-27 22:22:03
【问题描述】:
我在 Linux/ARM 平台上使用select() 来查看 udp 套接字是否收到了数据包。如果在超时之前返回(检测到数据包),我想知道 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);
}
}
【问题讨论】:
-
我不确定你的问题是什么。您的代码解决了问题;剩余时间写入超时参数。 Quoth select(2):“在 Linux 上,select() 修改超时以反映未睡眠的时间量;大多数其他实现不这样做。(POSIX.1-2001 允许任何一种行为。)”
-
@phihag: "允许任何一种行为"... 在 select 调用后精确查看超时值是不可移植的