select()调用剩余时间

Jam*_*mie 6 c linux select polling

我正在select()Linux/ARM平台上使用,看看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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jas*_*hen 3

最安全的做法是忽略模棱两可的定义select()并自己计时。

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