23,217
社区成员




pid_t pid1,pid2;
signal(2,stop);
pid1=fork();
pid_t pid1,pid2;
pid1=fork();
if(pid1>0){
pid2=fork();
if(pid2>0){
signal(2,stop);
printf("在父进程中\n");
sleep(10);
kill(pid1,16);
wait(0);
kill(pid2,17);
wait(0);
printf("\nParent process is killed!\n");
sleep(100);
exit(0);
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sched.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
int wait_flag=1;
void stop(){
}
void stop2(){
wait_flag = 0;
}
int main(){
pid_t pid1,pid2;
signal(2,stop);
pid1=fork();
if(pid1>0){
pid2=fork();
if(pid2>0){
printf("在父进程中\n");
sleep(10);
kill(pid1,16);
wait(0);
kill(pid2,17);
wait(0);
printf("\nParent process is killed!\n");
exit(0);
}else{
signal(17,stop2);
while(wait_flag)
;
printf("在第二个子进程中:");
printf("Child Processl2 is killed by parent!\n");
exit(0);
}
}else if(pid1==0){
signal(16,stop2);
while(wait_flag)
;
printf("在第一个子进程中:");
printf("Child Processl1 is killed by parent!\n");
exit(0);
}
}