[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Signal doesn't interrupt read() and wait() - help!
I think this short program might help to explain the problem. Try it out
and see that when the alarm signal arrives, the read() call does not
return like it seems it should.
This just shows the problem in read(), it also happens in wait() and
perhaps others.
So now that you can see the problem happening in front of you, perhaps
someone has some other ideas about why this is.
--- SNIP ---
#include <stdio.h>
#include <signal.h>
#include <errno.h>
int signal_was_received = 0;
static void handle_signal( void );
void main( void )
{
char buffer;
int ok;
printf( "Signal bug demonstration.\n" );
printf(
"\nThis signal bug demonstration will set an alarm for 5 seconds, then wait\n"
"in a read(). AFAICS, the read should return with errno = EINTR when the alarm\n"
"signal is received, that is after 5 seconds. But if you wait for 5 seconds\n"
"without typing any keys, the read() does not return. If you then press a key\n"
"you can see that the signal was received.\n\n");
printf( "Installing signal handler\n" );
signal( SIGALRM, handle_signal );
printf( "Setting alarm for 5 seconds\n" );
alarm( 5 );
printf( "Currently, signal_was_received = %d (should be 0)\n", signal_was_received );
printf( "Waiting for input with read()\n" );
ok = read( fileno(stdin), &buffer, 1 );
printf( "Read returned %d, errno = %d\n", ok, errno );
printf( "Currently, signal_was_received = %d (should be 1)\n", signal_was_received );
}
static void handle_signal( void )
{
signal_was_received = 1;
}
--- SNIP ---
--
Mario Becroft Auckland, New Zealand
mb@tos.pl.net http://www.pl.net/~mario/ |\__/,| (`\
Tariland, Atari Support in New Zealand _.|o o |_ ) )
tariland@tos.pl.net http://www.pl.net/~mario/tariland/ --(((---(((--------