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();
}
Here's what happens in the parent:
- Fork a child.
- Send SIGUSR1 to the child.
- Wait for a signal.
Here's what happens in the child:
- Wait for a signal.
- Print
Child
. - Call
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
. - Wait for a signal.
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:
- Parent sends SIGUSR1 to the child.
- Child receives SIGUSR1 and prints
Catched
. - Child calls
pause
. - Parent calls
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:
- Child calls
pause
. - Parent sends SIGUSR1 to the child.
- Parent calls
pause
. - Child is unblocked and prints
Catched
. - Child prints
Child
. - Child sends SIGUSR1 to the process group.
- Child prints
Catched
. - Child calls
pause
. - Parent is unblocked and prints
Catched
. - Parent exits.
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 from kill(0,SIGUSR1)
) and that one is delivered synchronously, not during the execution of pause
.
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:
pid = fork();
if (pid < 0) {
/*error handling*/
} else if (pid == 0) {
/*child code*/
exit(...); /*Usually, what follows the if is specific to the parent.*/
}
Try this:
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
void
sigusr1( int pidno )
{
fprintf(stderr, "Caught\n");
}
int
main()
{
pid_t pid;
signal( SIGINT, sigusr1 );
if( (pid = fork()) == 0 ){
pause();
fprintf(stderr, "Child\n");
}
else
{
fprintf(stderr, "Parent\n");
kill( pid , SIGINT ); //parent sends signal to child
}
pause();
return 0;
}
printf
buffers input: perhaps it's being called, but it isn't being displayed when you expect. One workaround isfflush()
. A better workaround isfprintf (stderr)
, which doesn't buffer in the first place.You're calling
kill()
in both the parent and the child. I added anelse
to eliminate this.Here's sample output:
gcc -Wall -g -o z z.c
./z
Parent
Caught
Child
来源:https://stackoverflow.com/questions/7546828/why-doesnt-child-process-continue-running-after-receiving-signal