
Aero T.
asked 07/23/21Write a program simulating an alarm clock.
Write a program simulating an alarm clock. In the main function, you fork a child process, and the child process sleeps for 5 seconds (the number of seconds is an command line argument, see below for a sample run), after which the child process sends the signal (SIGALRM) to its parent process. The parent process, after forking the child process, pause, upon receiving the SIGALRM signal, process prints out a message “Ding!”.
The following is a sample run $./alarm 5
alarm application running
waiting for alarm to go off
<5 second pause>
Ding! Done!
1 Expert Answer

Shanek K. answered 08/13/21
Computer Science Professional Tutor
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
// this is signal handler for
void signalHandler(int signal)
{
if (signal == SIGALRM)
{
printf("Ding!\n");
wait(NULL);
}
}
int main(int argc, char *argv[])
{
signal(SIGALRM, signalHandler);
if (argc != 2)
{
printf("Invalid arguments\n");
return 0;
}
printf("Alarm application starting\n");
int delay;
// compute delay
sscanf(argv[1], "%d", &delay);
// start child process
if (fork() == 0)
{
printf("Waiting for alarm to go off\n");
sleep(delay);
kill(getppid(), SIGALRM);
exit(0);
}
wait(NULL);
printf("done\n");
}

Shanek K.
Feel free to reach out to me if you have any doubts or need more help08/13/21
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I'm interested, but only have cygwin unix emulator and Dev C++, and a shabby one at best, which do not support this type of IPC. The best I can offer is IPC via sockets using Java.07/23/21