how do I listen on a socket without sucking up all the CPU time?

jepler epler jepler.lnk at lnk.ispi.net
Fri Oct 13 18:23:44 EDT 2000


On 12 Oct 2000 14:59:25 -0700, Samuel A. Falvo II
 <kc5tja at garnet.armored.net> wrote:
>I've read the select() source in Linux's source code.  From what I can see,
>it apparently busy-waits on the selectors you give it -- I don't see any
>calls to functions that put the process to sleep.

/usr/src/linux/fs/select.c:do_select() does not busy-wait.  The statements
	current->state = TASK_INTERRUPTABLE; // line 176 in my copy
	__timeout = schedule_timeout(__timeout); // line 216 in my copy
together cause the program to yield the CPU after the FD array is scanned
through once until the timeout expires or one of the files in the assembled
'poll_table'  becomes ready. (2.2.12-20 from redhat 6.1)

>If I'm wrong on this, and I sure hope I am, could someone please point out
>where Linux does put the calling process to sleep?  Maybe I just skipped
>over it (I was reading for curiosity anyway).  Quite likely, considering the
>complete lack of documentation in it.

Complete lack of documentation?
% man 2 select | wc -l
    198
The phrase near the top, "select waits for ..." and later "select can *block*
indefinitely [when timeout is NULL]" both imply that select is "blocking" just
like "read" from a socket with no data available.

But, anyway, I think it's just common knowledge that select() doesn't
merely sit and spin.  That's the sort of thing you have to have from
day one.  I don't think anybody would be serving websites from their
linux machine if there weren't a blocking, cpu-friendly select.

Jeff



More information about the Python-list mailing list