Problem with sleep and signal/alarm.

winnard at frith.msu.edu winnard at frith.msu.edu
Tue May 7 01:39:37 AEST 1991


I'm having trouble getting the sleep function to work
under C when I use signal/alarm function.  When the
signal/alarm call is taken out of the following code, the
program will print "Sleep 10..." then it will wait 10 seconds
before printing how long it did not sleep every time through the 
loop.  But with the signal/alarm function in place the sleep will 
return immediately and still indicate that it slept 10 seconds
every time through the loop.

#include <stdio.h>
#include <signal.h>
#include <errno.h>

main()
{
   int x;

   signal( SIGALRM, SIG_IGN );
   alarm( 1 );

   while( 1 )
   {
      printf("Sleep 10...");
      x = sleep( (unsigned)10 );
      if( x == -1 ) perror("");
      else printf("unslept %d\n", x );
   }
}

If you don't know why this is happening maybe you know a way 
to timeout a read function.  Either solution would be of great help.
Thanks.   
Jamie Winnard
winnard at frith.egr.msu.edu



More information about the Comp.unix.questions mailing list