The alarm
function allows us to set a timer that will expire at a specified time in the
future. When the timer expires, the SIGALRM signal is generated. If we ignore
or don't catch this signal, its default action is to terminate the process.
#include <unistd.h>
unsigned
int alarm(unsigned int seconds);
Returns:
0 or number of seconds until previously set alarm
The seconds
value is the number of clock seconds in the future when the signal should
be generated. Be aware that when that time occurs, the signal is generated by
the kernel, but there could be additional time before the process gets control
to handle the signal, because of processor scheduling delays.
Earlier
UNIX System implementations warned that the signal could also be sent up to 1
second early. POSIX.1 does not allow this.
There is
only one of these alarm clocks per process. If, when we call alarm, a
previously registered alarm clock for the process has not yet expired, the
number of seconds left for that alarm clock is returned as the value of this
function. That previously registered alarm clock is replaced by the new value.
If a
previously registered alarm clock for the process has not yet expired and if
the seconds value is 0, the previous alarm clock is canceled. The number
of seconds left for that previous alarm clock is still returned as the value of
the function.
Although
the default action for SIGALRM is to terminate the process, most processes that
use an alarm clock catch this signal. If the process then wants to terminate,
it can perform whatever cleanup is required before terminating. If we intend to
catch SIGALRM, we need to be careful to install its signal handler before
calling alarm. If we call alarm first and are sent SIGALRM before we can
install the signal handler, our process will terminate.
The pause function suspends the calling process until a signal is caught.
No comments:
Post a Comment