views:

314

answers:

4

I'm using select() on a Linux/ARM platform to see if a udp socket has received a packet. I'd like to know how much time was remaining in the select call if it returns before the timeout (having detected a 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);
    }
}
+1  A: 

If I recall correctly, the select() function treats the timeout and an I/O parameter and when select returns the time remaining is returned in the timeout variable.

Otherwise, you will have to record the current time before calling, and again after and obtain the difference between the two.

Software Monkey
The documentation for select() says that the timeout value 'may' be changed if the functions is returns succesfully (doesn't timeout). I was wondering if there was a simple idiom or alternate library call to do this.
Jamie
Since you are writing your app for a specific platform, why not just try it and see if the timeout value is changed after the call on that platform.
Adam Pierce
I just had a re-read of the C doc, and I agree, the use of timeout is ambiguous at best. As Adam said you can try it and see for your platform, otherwise it's my suggest plan B for you.
Software Monkey
Looking at the timeout after a select call is implementation defined. Relying on what your implementation does is at best non-portable.
Dave C
+1  A: 

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

You'll need to call gettimeofday before calling select, and then gettimeofday on exit.

[Edit] It seems that linux is slightly 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.
A: 

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

Note that this is not portable across other systems (hence the warning in the OS X manual quoted above) but does work with Linux.

Gilad

gby
+2  A: 

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

Just get the time before and after the select and subtract that from the interval you wanted.

Jason Cohen
I ended up doing just this with a gettimeofday() function call before and after.
Jamie