Time remaining on select () call

I use select()on the Linux / ARM platform to find out if the udp socket received a packet. I would like to know how much time is left in the select call if it returns before the timeout (after detecting the packet).

Something along the lines of:

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);
    }
}
+5
source share
5 answers

The safest thing is to ignore the ambiguous definition select()and the time of himself.

Just find the time before and after the selection and subtract from the required interval.

+3
source

If I remember correctly, the select () function refers to the timeout and I / O parameter, and when it returns, the remaining time is returned in the timeout variable.

, .

+1

"man select" 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().

gettimeofday select, gettimeofday .

[Edit] It seems linux is a little different:

   (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.
+1
source

Linux select () updates the timeout argument to reflect the past.

Note that this is not portable for other systems (hence the warning in the OS X manual above), but works with Linux.

Gilad

0
source

Do not use select, try with fd greater than 1024 with your code and see what you get.

-3
source

All Articles