The following is my code. Parent forks a child. Child pause until parent sends a signal to it, then it continues running.
My question is why doesn't child process continue
running after parent sending signal to him. Did I miss or misunderstand something?
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
void
sigusr1( int pidno )
{
printf("Catched\n");
}
int
main()
{
pid_t pid;
signal( SIGUSR1, sigusr1 );
if( (pid = fork()) == 0 ){
pause();
printf("Child\n");
}
kill( pid , SIGUSR1 ); //parent sends signal to child
pause();
}
Best Answer
Here's what happens in the parent:
Here's what happens in the child:
Child
.kill(0, SIGUSR1)
(0 being the value ofpid
in the child). Callingkill
with a process ID of 0 sends the signal to every process in the process group of the process that callskill
.There are several possible behaviors for your program, depending on the order in which the parent and the child system calls are executed. Depending on the exact version of your operating system, on the fine-tuning of various kernel parameters, on how loaded your system is, and on random chance, you may or may not observe different behavior if you run the program several times or under a debugger.
If the parent starts faster than the child, you may see this:
Catched
.pause
.pause
.With this execution order, both the parent and the child end up waiting forever (it's a deadlock).
If the child starts faster than the parent, you may see this:
pause
.pause
.Catched
.Child
.Catched
.pause
.Catched
.I don't think there's a way for the child to exit: it calls
pause
twice, and while it can receive up to two signals, one of these is sent from itself (the one fromkill(0,SIGUSR1)
) and that one is delivered synchronously, not during the execution ofpause
.This program is probably not what you meant to write, but since you don't describe the expected behavior, it's impossible to tell what you did mean to write. I do note that you do not follow the usual structure of a program that forks: