Re: select() problems
Re: select() problems
- Subject: Re: select() problems
- From: Steve Checkoway <email@hidden>
- Date: Wed, 3 Dec 2008 05:40:07 -0800
On Dec 2, 2008, at 7:42 PM, Jamil Weatherbee wrote:
This may be a simple one but I haven't figured it out.
Suppose you have a server that does (in pseudo code):
Something like this?
#include <sys/types.h>
#include <sys/select.h>
#include <sys/errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
static sig_atomic_t runserver = 1;
static void handler( int sig )
{
runserver = 0;
}
int main()
{
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = SA_RESTART;
sa.sa_mask = 0;
if( sigaction(SIGINT, &sa, NULL) == -1 )
{
perror( "sigaction" );
return 1;
}
while( runserver )
{
fd_set set;
struct timeval tv = { 1, 0 };
char buffer[1024];
FD_ZERO( &set );
FD_SET( 0, &set );
int ret = select( 1, &set, NULL, NULL, &tv );
if( ret == -1 )
{
if( errno == EINTR )
continue; // interrupted by a signal
perror( "select" );
return 1;
}
if( ret == 0 )
continue; // timeout
ssize_t amount = read( 0, buffer, sizeof(buffer)-1 );
if( amount < 0 )
{
perror( "read" );
return 1;
}
buffer[amount] = '\0';
fputs( buffer, stdout );
}
puts( "Done!" );
return 0;
}
Now if you send a TERM signal to this process but for whatever
reason there is no activity on the file descriptors. Then the
process will
hang into the select() until a descriptor becomes ready. You would
think that the 1 second should timeout and then it should exit
gracefully (which is the intent).
Works for me.
[PBG4-3:~/temp] steve$ gcc -O3 -Wall s.c
[PBG4-3:~/temp] steve$ ./a.out
Testing the read.
Testing the read.
^CDone!
It seems the best I can tell is that the select() call is getting
restarted due to the signal but somehow it is restarted without the
timeout.
I didn't check if EINTR was actually being set or not, but as you can
see, when I send SIGINT, it does quit.
Any easy solutions?
What you're describing seems like it should work. Maybe you can
provide a test case that isn't working for you.
--
Steve Checkoway
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden