RE: PROBLEM: select() on TCP socket sleeps for 1 tick even if data available

David Schwartz (davids@webmaster.com)
Fri, 19 Jan 2001 15:20:29 -0800


> If select() is waiting for data to become available on a
> TCP socket FD, and
> data becomes available, it doesn't return until the next clock tick.

If your application has scheduling requirements, you need to communicate
them to the scheduler.

> #include <sys/time.h>
> #include <sys/types.h>
> #include <unistd.h>
>
> /* this program should take 1 second to complete, but takes 10 */
> /* yes i know, it doesn't do any I/O, but the behavior is the
> same as if it did */
> main()
> {
> for (int i = 0; i < 1000; i++) {
> struct timeval to;
> to.tv_sec = 0;
> to.tv_usec = 1000;
> select(0, 0, 0, 0, &to);
> }
> return 0;
> }

This program doesn't demonstrate anything except that Linux's sleep time is
granular. This shouldn't be news to anyone. If you don't force a reschedule,
everything works the way it's supposed to:

main()
{
int i, j, pipes[2];
struct timeval tv;
fd_set rd;
pipe(pipes);
write(pipes[1], "foo", 4);
for(i=0; i<1000; i++)
{
FD_ZERO(&rd);
FD_SET(pipes[0], &rd);
tv.tv_sec=0;
tv.tv_usec=1000;
j=select(10, &rd, NULL, NULL, &tv);
if(j!=1) printf("oops\n");
}
}

DS

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/