0

When using select system call on linux to check if a socket is ready to be read and contains data, is there any difference when setting the timeout value to 0 (not nullptr but real zeros) and setting it to low amount of microseconds?

I am specially interested in the precision of the timeout. Let's say it can wait only 10ms on minimum, no matter what timeout I am giving, this would affect the polling frequency of this implementation a lot.

int socket= 5 ; //just for example, usually somewhere retreived
timeval timevalStruct;
timevalStruct.tv_sec=0;
timevalStruct.tv_usec=0; //Solution A
timevalStruct.tv_usec=1; //Solution B

fd_set in_set;

FD_ZERO(&in_set);
FD_SET(socket, &in_set);

select(socket + 1, &in_set, nullptr, nullptr, &timevalStruct); //how long does it wait here for minimum
user17732522
  • 53,019
  • 2
  • 56
  • 105
  • The [man page](https://man7.org/linux/man-pages/man2/select.2.html) is pretty clear on what happens, except that it doesn't specify what the "system clock granularity" is. – user17732522 Mar 03 '22 at 17:19
  • Why not test it ? There is lot of code where they prefer `select()` & `poll()` to put a monitor-thread to sleep. https://stackoverflow.com/questions/3125645/ – जलजनक Mar 03 '22 at 17:34
  • There is `pselect()` which supports `nano seconds`; evidently they're accurate. – जलजनक Mar 03 '22 at 17:39
  • My 11 year old i3 2310M `select()` takes at most `2-5 μs` to return with `0 μs` wait; but takes `60-80 μs` even for `1μs` wait time. Same figures for `pselect()`. Current servers will be way faster. – जलजनक Mar 03 '22 at 18:28
  • Thank you all for the comments, there is a lot of input there. I was aware of the "system clock granilarity" from the manpages and was looking for more details on that one. Was experiencing some weird things there, that it at least waited for 10ms instead of the given 1us. Not sure what to do with it so I am trying to ask you guys. – MauriceRandomNumber Mar 04 '22 at 09:52
  • @SparKot how did you measure that? I can take time before and after the select, then print but it does not seem accurate to me, or at least the random spread is very high. – MauriceRandomNumber Mar 07 '22 at 09:12

0 Answers0