code_junkie

Time remaining on a select() call

Nov 13th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. int wait_fd(int fd, int msec)
  2. {
  3. struct timeval tv;
  4. fd_set rws;
  5.  
  6. tv.tv_sec = msec / 1000ul;
  7. tv.tv_usec = (msec % 1000ul) * 1000ul;
  8.  
  9. FD_ZERO( & rws);
  10. FD_SET(fd, & rws);
  11.  
  12. (void)select(fd + 1, & rws, NULL, NULL, & tv);
  13.  
  14. if (FD_ISSET(fd, &rws)) { /* There is data */
  15. msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
  16. return(msec?msec:1);
  17. } else { /* There is no data */
  18. return(0);
  19. }
  20. }
  21.  
  22. Timeout is not changed by select(), and may be reused on subsequent calls, however it
  23. is good style to re-ini-tialize it before each invocation of select().
  24.  
  25. (ii) The select function may update the timeout parameter to indicate
  26. how much time was left. The pselect function does not change
  27. this parameter.
  28.  
  29. On Linux, the function select modifies timeout to reflect the amount of
  30. time not slept; most other implementations do not do this. This causes
  31. problems both when Linux code which reads timeout is ported to other
  32. operating systems, and when code is ported to Linux that reuses a
  33. struct timeval for multiple selects in a loop without reinitializing
  34. it. Consider timeout to be undefined after select returns.
Add Comment
Please, Sign In to add comment