Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Select System hangs

218 views
Skip to first unread message

khan

unread,
Sep 12, 2011, 8:30:21 AM9/12/11
to
Hi,
I had code where select waits on only one File-descriptor, in while
loop. If FD_SET and FD_ZERO is done outside while loop thread hangs in
select system call , when moved FD_ZERO and FD_SET inside while-loop
it is working for more than a week testing.
I didn't understand this behavior.
Here is code snippet : FD_SET and FD_ZERO outside loop
/*FD_ZERO and FD_SET*/
FD_ZERO(&rfds);
FD_SET(chipset_p->fd, &rfds);

while(1) {
/*FD_ZERO and FD_SET*/
FD_ZERO(&rfds);
FD_SET(chipset_p->fd, &rfds);
select((chipset_p->fd + 1), &rfds, NULL, NULL,NULL);
if (FD_ISSET(chipset_p->fd, &rfds)) {
/*Calle handler*/
}
}
In above code thread hangs in select system call.

Code snippet: FD_ZERO and FD_SET in while-loop
while(1) {
select((chipset_p->fd + 1), &rfds, NULL, NULL,NULL);
if (FD_ISSET(chipset_p->fd, &rfds)) {
/*Calle handler*/
}
}

But i didn't understand why select() didn't work if we just do
FD_ZER() and FD_SET() once and pass the same readfds to select()?

Thanks,
Mushtaq Khan

Tauno Voipio

unread,
Sep 12, 2011, 9:59:46 AM9/12/11
to
----

From man select:

Three independent sets of file descriptors are watched. Those listed
in readfds will be watched to see if characters become available for
reading (more precisely, to see if a read will not block; in particu‐
lar, a file descriptor is also ready on end-of-file), those in writefds
will be watched to see if a write will not block, and those in
exceptfds will be watched for exceptions. On exit, the sets are modi‐
fied in place to indicate which file descriptors actually changed sta‐
tus. Each of the three file descriptor sets may be specified as NULL
if no file descriptors are to be watched for the corresponding class of
events.

---

Please note that the system call will modify the descriptor sets.

--

Tauno Voipio


David Schwartz

unread,
Sep 17, 2011, 8:05:44 AM9/17/11
to
On Sep 12, 5:30 am, khan <mushtaqk...@gmail.com> wrote:

> But i didn't understand why select() didn't work if we just do
> FD_ZER() and FD_SET() once and pass the same readfds to select()?

Because then you only 'select' on the socket once. You have to pass
the correct parameters to 'select' every time, not just the first
time.

DS

Rainer Weikusat

unread,
Sep 18, 2011, 3:44:47 PM9/18/11
to

To be more precise: The fd sets are so-called 'input/ output'
parameters meaning they are used by the caller to pass information to
the kernel (the file descriptors supposed to be monitored) and used by
the kernel to pass information back to this caller (the file
descriptors found to be 'ready for something'). This implies that none
of the fd sets will keep its value accross the select-invocation.

0 new messages