I'm doing some practice questions for an exam and one of the questions gives two pieces of code called parent.c and child.c . Parent creates a child and fires signals at it and child displays a message every time it receives a signal. Child will spend rest of it's time printing a message from main. The question is to describe what is wrong with the signal handling in child.c and to re-write the code to correct it. I get the general idea of signals but have a lot of difficulty implementing them. I'm not sure if procmask
in child.c is working properly, I'm not completely comfortable with signals but I can't see why you'd put NULL
as the last parameter so maybe that's part of why it's wrong? Could someone please point me in the right direction and give me an idea of what part of the code is wrong and why.
Parent.c
#include <unistd.h>
#include <signal.h>
int
main(int argc, char *argv[])
{
pid_t pid;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigprocmask(SIG_BLOCK, &set, NULL);
pid = fork();
if (pid == 0) {
execlp("./child", "./child", NULL);
}
while (1) {
kill(pid, SIGUSR1);
}
return (0);
}
Child.c
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
static void
handler(int signo)
{
printf("This is the SIGUSR1 signal handler!\n");
}
int
main(void)
{
sigset_t set;
sigemptyset(&set);
sigset(SIGUSR1, handler);
sigprocmask(SIG_SETMASK, &set, NULL);
while (1) {
printf("This is main()!\n");
}
return (0);
}