UNIX Socket FAQ

You are not logged in.

#1 2010-04-15 02:20 AM

JustSomeGuy
Member
Registered: 2010-04-14
Posts: 4

Re: Select won't unblock.

If I understand the documentation on select, select will block forever if the time out is set to zero.

My code looks like:

FD_ZERO(&readfds);
FD_SET(dns_sd_fd, &readfds);
tv.tv_sec = 1; //timeOut in 1 second;
tv.tv_usec = 0;
result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
if (result > 0)
...
}
else
{
   fprintf(stderr, "select() returned %d errno %d %s\n", result, errno, strerror(errno));
   if (errno != EINTR) stopNow = 1;
}
...

I never see a time out.... It just never happens.


Mac OS X 10.5
XCode 3.1.4

Offline

#2 2010-04-15 04:37 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Select won't unblock.

Actually, select() will block forever (or until the socket is ready-for-read) if you pass in NULL as the last argument (instead of a pointer to a timeval struct)

Passing in a timeval that has both tv_secs and tv_usecs set to zero, on the other hand, will force select() to return immediately no matter what.

In the code you posted, I would expect select() to return in 1 second, or earlier if your readfds socket has data available to read.

Note that you should test the return value to be "if (result >= 0)" instead of "if (result > 0)", since select() returning zero doesn't indicate an error.  select() returns zero if the timeout was reached without any sockets being ready-for-(whatever).

Offline

#3 2010-04-15 02:18 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,739
Website

Re: Select won't unblock.

I never see a time out.... It just never happens.

Are you saying that you end up blocking in that select() call for longer than 1 second?
Because, if that's true, that's a bug...  And, I really doubt such an obvious bug would
exist in any OS's select()...  So, it's probably something else going on with your code...
My guess is you're not "seeing" the timeout, because you just treat it the same as
an error condition, as jfriesne pointed out...  But, it's surely still actually happening...
Or else select() is always returning in less time than a second with a positive return
value, because the FD is always readable...

Offline

#4 2010-04-15 05:23 PM

JustSomeGuy
Member
Registered: 2010-04-14
Posts: 4

Re: Select won't unblock.

If the time limit expires, select() returns 0.

result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
if (result > 0)
{
   if (FD_ISSET(dns_sd_fd, &readfds)) ProcessResult();
}
else if (result == 0)
{
   printf("timeout\n");
}else{
   printf("select(  ) returned %d errno %d %s\n", result, errno, strerror(errno));
   if (errno != EINTR) stopNow = 1;
}

Offline

#5 2010-04-15 08:27 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,739
Website

Re: Select won't unblock.

That code won't work...  You only go into the first if() when select() returns >0, but
you only check for ==0 INSIDE that if(), so it'll never happen...

Offline

Board footer

Powered by FluxBB