|
|
A process can suspend itself for a specific period of time with the function sleep or suspend itself indefinitely with the function pause until a signal arrives to reactivate the process. The function alarm schedules a signal to arrive at a specific time, so a pause suspension need not be indefinite.
#include <stdio.h> #include <signal.h>struct sigaction new_act, old_act; int alarm_count = 5; /* initialize number of alarms */
main () { void alarm_action(); /* * pass signal and function to sigaction */ new_act.sa_handler = alarm_action; sigaction(SIGALRM, &new_act, &old_act);
alarm(5); /* set alarm clock for 5 seconds */
pause(); /* suspend process until receipt of signal */ }
void alarm_action() { /* * print the number of alarms remaining */ printf("\t<%d\007>", alarm_count); /* * pass signal and function to sigaction */ new_act.sa_handler = alarm_action; sigaction(SIGALRM, &new_act, &old_act);
alarm(5); /* set alarm clock for 5 seconds */ if (--alarm_count) /* decrement alarm count */ pause(); /* suspend process */ }
The preceding example shows how you can use the signal, alarm and pause system calls to alternately suspend and resume a program.