Re: Question on handling SIGALRM.

Richard B. Johnson (root@analogic.com)
Wed, 7 May 1997 15:21:08 -0400 (EDT)


alarm(0)
Will reset any pending alarm if, and only if it has not been
delivered. Unfortunately, It could be delivered at any time.

You have to plan for this. For instance, I could set an alarm
as alarm(200);

It is possible that my program may not get control back from the
system call to alarm for 201 seconds, unlikely but possible. Therefore
the results may not be expected. For instance, using alarm to
unblock a blocking socket may give unexpected results:

signal(SIGALRM, handler); /* Dummy handler */
alarm(10); /* If the return from this call takes more than 10
seconds, you lose */
status = read(fd, buffer, len); /* You get stuck here forever */

If you want to keep things simple, you could do something like:

void handler(int sig)
{
global_variable= TRUE;
}

signal(SIGALRM, handler);
global_variable = FALSE;
alarm(timeout);
if(global_variable == FALSE)
status = read(fd, buffer, len);
else
......

alarm(0); /* Always cancel */

Cheers,
Dick Johnson
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Richard B. Johnson
Project Engineer
Analogic Corporation
Voice : (508) 977-3000 ext. 3754
Fax : (508) 532-6097
Modem : (508) 977-6870
Ftp : ftp@boneserver.analogic.com
Email : rjohnson@analogic.com, johnson@analogic.com
Penguin : Linux version 2.1.35 on an i586 machine (66.15 BogoMips).
Warning : I read unsolicited mail for $350.00 per hour. Supply billing address.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-