I have to fork two child processes in which the SIGINT command is blocked, but one of them should unblock it when receiving a SIGTERM signal, while the other child process and the parent both print their PID's as a result of the same SIGTERM signal. The second child process should terminate right away, but the parent process shall wait for its child processes to end and then stop.
I just started to learn C programming in Linux, and I don't really understand how forking and signals work. As far as I understand, this code I've written so far will fork a process, and the child will block the Ctrl-C command, and the whole thing will run forever unless I kill it with kill -9 pid.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void handler(int n) {
write(STDOUT_FILENO, "I won't die!! Haha\n",13);
}
int main()
{
pid_t pid = fork();
if (pid < 0) {
perror("Fork() error!");
exit(-1);
}
if (pid == 0)
signal(SIGINT,handler);
while(1) {
printf("Wasting the iteration. %d\n", getpid());
sleep(1);
}
return 0;
}
- How do I use the SIGTERM though? Where should I call it?
- How can I make the parent create another child process which blocks the Ctrl-C command too?