Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why can't I block a SIGALRM signal?

412 views
Skip to first unread message

Tim Grunewald

unread,
Jan 28, 1997, 3:00:00 AM1/28/97
to

I am trying to write a program that uses alarm() to do some maintenance
tasks with a SIGALRM handler. There are critical sections in the code that
I do not want to be interrupted. It seems that I cannot block the SIGALRM
signal as shown by the included sample program.

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);
}
}


Eduardo Saltelli

unread,
Jan 29, 1997, 3:00:00 AM1/29/97
to Tim Grunewald

You should check the man pages on sleep() for Solaris 2.5. In the Notes
section it states that the SIGALRM signal should not be blocked. In
your code segment, you've blocked the signal that will be delivered by
sleep(). Try replacing your sleep(15) with a for loop; something that
keeps the process busy for an extended period of time, but doesn't rely
on the SIGALRM signal to be delivered.

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.

Tim Grunewald

unread,
Jan 30, 1997, 3:00:00 AM1/30/97
to

Ok, it seems that the sleep() is interfering with my attempt to block the
SIGALRM signal. In the man page for sleep() is states that the SIGALRM
should not be blocked or ignored during a call to sleep().

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

: }
: }


Guy Harris

unread,
Jan 30, 1997, 3:00:00 AM1/30/97
to

Tim Grunewald <tgru...@earth.execpc.com> wrote:
>Ok, it seems that the sleep() is interfering with my attempt to block the
>SIGALRM signal. In the man page for sleep() is states that the SIGALRM
>should not be blocked or ignored during a call to sleep().

"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.

0 new messages