#include <sys/types.h>//kill
#include <signal.h>//kill;signal
#include <stdio.h>
#include <unistd.h>//pause
#include <stdlib.h>
static int alarm_fired = 0;
void ding(int sig)
{
alarm_fired = 1;
}
int main()
{
pid_t pid;
printf("alarm application starting\n");
pid = fork();
switch(pid)
{
case -1: perror("fork failed\n");exit(1);
case 0: sleep(5); kill(getppid(), SIGALRM);exit(0);
}
//parent process wait
printf("waiting for alarm to go off\n");
(void) signal(SIGALRM, ding);
pause();//hang the parent process untill recieving a singal
if(alarm_fired == 1)
printf("Ding\n");
printf("done");
exit(0);
}
[Output]
$ cc -o alarm alarm.c
$ ./alarm
alarm application starting
waiting for alarm to go off
Ding // after 5 seconds