I will signal changed position () function is a before the process creation, one after the parent process to create, is all hope to parent and child process was achieved by the interrupt signal communication.
But a different output information, a child process output, without a child process output, think what is the cause of this?
This code directly before they can set the signal process creation () function, capture interrupt signal. Can success to let two child process output information.
Pid_t pid1, pid2;
Signal (2, stop);
Pid1=fork ();
I made a little adjustment. The signal () on the call of the parent process, the results of input interrupt signal, but the child did not output information.
Pid_t pid1, pid2;
Pid1=fork ();
If (pid1 & gt; 0 {
Pid2=fork ();
If (pid2 & gt; 0 {
Signal (2, stop);
Printf (" in the parent \ n ");
Sleep (10);
The kill (pid1, 16);
Wait (0);
The kill (pid2, 17);
Wait (0);
Printf (" \ nParent process is killed! \n");
Sleep (100);
exit(0);
The following is a successful output information of the complete code for the child process.
#include
#include
#include
#include
#include
#include
#include
Int wait_flag=1;
Void the stop () {
}
Void stop2 () {
Wait_flag=0;
}
Int main () {
Pid_t pid1, pid2;
Signal (2, stop);
Pid1=fork ();
If (pid1 & gt; 0 {
Pid2=fork ();
If (pid2 & gt; 0 {
Printf (" in the parent \ n ");
Sleep (10);
The kill (pid1, 16);
Wait (0);
The kill (pid2, 17);
Wait (0);
Printf (" \ nParent process is killed! \n");
exit(0);
} else {
Signal (17, stop2);
While (wait_flag)
;
Printf (" in the second child process: ");
Printf (" Child Processl2 is killed by the parent! \n");
exit(0);
}
{} else if (pid1==0)
Signal (16, stop2);
While (wait_flag)
;
Printf (" in the process of the first child: ");
Printf (" Child Processl1 is killed by the parent! \n");
exit(0);
}
}
CodePudding user response:
You fork the child process has been created, so you set a semaphore is also for the child to set.