Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

alarm - Set alarm clock

&pagelevel(4)&pagelevel

Definition

#include <signal.h>

unsigned int alarm(unsigned int sec);

alarm triggers the signal SIGALRM for the calling program when the specified time span
sec (passed as an argument) has elapsed. SIGALRM corresponds to the STXIT event class
RTIMER (real-time interval timer). The program is terminated with exit(-1) if the signal is
not intercepted (see also signal).
alarm calls with the value 0 - alarm(0) - do not trigger an alarm but set the alarm clock to
0 and cancel any pending alarms.

Return val.

Notes

Time remaining in the alarm clock before execution of the alarm call.

A number of alarm calls in succession resets the alarm clock with each call.

Since the alarm clock has a 1-second pulse, there may be time shifts of up to a second
when the signal is triggered.

If the signal is intercepted (see signal), the restart of the interrupted program (i.e. the base
process) may be delayed on priority grounds.

With the assignment: i = alarm(0) you can turn off the alarm clock and additionally
ascertain how much time would have remained since the last alarm request.

Example

The following program sends an asterisk to the standard output approx. every two seconds:

#include <stdio.h>
#include <signal.h>
void f(int sig)          /* Signal handling for SIGALRM */
{
  printf("*\n");
  alarm(2);            /* Resetting of alarm clock; all further asterisks */
}
int main(void)
{
  signal(SIGALRM + SIG_PS, f);
  alarm(2);            /* First asterisk */
  for(;;)
    ;
  return 0;
}

See also

signal, sleep