0
votes

I have the following code:

FD_SET(mc_sock, &readfds);

foo = FD_ISSET(mc_sock, &readfds); // returns 1

// Wait until some socket on the set is ready to be read 
while(select (FD_SETSIZE,&readfds,NULL,NULL,ptv))  { 

foo = FD_ISSET(mc_sock, &readfds); // returns 0

I add mc_sock to readfds and FD_ISSET returns 1 as expected. However later when inside while loop FD_ISSET returns 0 without calling FD_CLR. The code jumps into the while when I run a MobileC server but there isn't any FD_CLR in the code runned. I'm quite a newbie in sets and file descriptors and I haven't found out what's happening. Do you have an idea?

Thanks!

1
What is ptv? How do you initialize it? - n. 1.8e9-where's-my-share m.
Furthermore, select can return negative values and you must check for them and examine errno. - n. 1.8e9-where's-my-share m.

1 Answers

2
votes

Second, third, and forth arguments of select(2) are in-out parameters, meaning the call modifies them to let you know about what events had happened upon return. This is why you need to re-arm the file descriptor sets before every call to select(2).

Also look into other de-multiplexing facilities like poll(2) and epoll(7).