Please send me e-mail at tgru...@execpc.com if you know why this does not
work as intended. The machine that I am developing this on is a Sun
workstation running Solaris 2.5.
(Note that if the SIGALRM is replaced with a SIGINT and the alarm() calls
are taken out, that the program runs as expected)
Thanks,
Tim
Example Program:
--------------------------------------------------------------------------------
#include <stdio.h>
#include <signal.h>
#include <errno.h>
void alarm_handler(int signo)
{
printf("in alarm_handler()\n");
alarm(1); /* reset alarm to go off again */
}
int main()
{
sigset_t sigmask;
struct sigaction sigact;
/* set up alarm handler */
sigact.sa_handler=alarm_handler;
sigact.sa_flags=0;
if (sigaction(SIGALRM, &sigact, NULL) < 0)
{
perror("sigaction()");
return 1;
}
/* start timer to call alarm handler */
alarm(1);
while (1)
{
printf("\nStart of loop:\n");
/* block SIGALRM signal */
/* clear signal mask */
sigemptyset(&sigmask);
/* Add SIGALRM to list of signals to block */
sigaddset(&sigmask, SIGALRM);
/* block SIGALRM signal */
if (sigprocmask(SIG_BLOCK, &sigmask, NULL) < 0)
{
perror("Error blocking signal");
return 1;
}
printf("Should not get a SIGALRM\n");
sleep(15);
/* unblock SIGALRM signal */
if (sigprocmask(SIG_UNBLOCK, &sigmask, NULL) < 0)
{
perror("error unblocking signal");
return 1;
}
printf("Ok to get a SIGALRM\n");
sleep(15);
}
}
Hope this helps.
Ed
--
Eduardo Saltelli esal...@fallschurch.esys.com
Raytheon E-Systems, Falls Church Div.
Ph: 703 560-5000 ext. 4959
The opinions expressed here are mine and not that of my employer.
Replacing the sleep() call with a for loop or getchar() solved the problem.
Thanks to those how replied.
Tim
Tim Grunewald (tgru...@earth.execpc.com) wrote:
: I am trying to write a program that uses alarm() to do some maintenance
: Thanks,
: Tim
: }
: }
"sleep()", on many UNIX systems, catches SIGALRM, sets a timer to cause
a SIGALRM to be delivered in N seconds (where "N" is the argument to
"sleep()"), and then uses "pause()" or "sigpause()" to block until a
signal is delivered. To quote the SunOS 5.5.1 "sleep(3C)" man page, for
example:
The routine is implemented by setting an alarm signal and
pausing until it (or some other signal) occurs. The previ-
ous state of the alarm signal is saved and restored. The
calling program may have set up an alarm signal before cal-
ling sleep(). If the sleep() time exceeds the time until
such alarm signal, the process sleeps only until the alarm
signal would have occurred. The caller's alarm catch rou-
tine is executed just before the sleep() routine returns.
But if the sleep() time is less than the time till such
alarm, the prior alarm time is reset to go off at the same
time it would have without the intervening sleep().
"An alarm signal" means SIGALRM.
This means that "sleep()" may not work correctly in programs that use
SIGALRM for their own purposes.