***********************************PREFER C++***********************************
ID: 673150 • Letter: #
Question
***********************************PREFER C++********************************************************
Write a program in which a parent process creates a Child process:
If the user sends ctr+c to the parent process before 10 seconds from start running, then the parent catches the signal and a message indicates that the system is protected.
After 10 seconds, the parent process could be terminating using ctr+c, before the parent terminates it must send a signals to the Child. The Child then prints its process id and terminates.
Some useful system calls:
int pause ( )
pause ( ) suspends the calling process and returns when the calling process receives a signal. It is most often used to wait efficiently for an alarm signal. pause ( ) doesn't return anything useful.
int kill(pid, sig)
The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal.
pid is the process-ID of the process to receive the signal;
sig is the signal number. The effective user-IDs of the sending and receiving processes must be the same, or else the effective user-ID of the sending process must be the super user.
If pid is equal to zero, the signal is sent to every process in the same process group as the sender. This feature is frequently used with the kill command (kill 0) to kill all background processes without referring to their process-IDs. Processes in other process groups (such as a DBMS you happened to have started) won't receive the signal.
Explanation / Answer
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
void signup();
void signint();
void signquit();
main()
{ int pid;
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0)
{
signal(SIGNUP,signup);
signal(SIGNINT,signint);
signal(SIGNQUIT, signquit);
for(;;);
}
else
{
printf(" parent: sending SIGNUP ");
kill(pid,SIGNUP);
sleep(10);
printf(" parent: sending SIGNINT ");
kill(pid,SIGNINT);
sleep(10);
printf(" parent: sending SIGNQUIT ");
kill(pid,SIGNQUIT);
sleep(10);
}
}
void signup()
{ signal(SIGNUP,signup);
printf("child: received a SIGNUP ");
}
void signint()
{ signal(SIGNINT,signint);
printf("child: received a SIGNINT ");
}
void signquit()
{ printf("parent has Killed ");
exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.