#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void waiting(),stop();
int wait_mark;
void main() {
int p1,p2;
if (p1=fork()) { /*创建子进程 p1*/
if (p2=fork()) { /*创建子进程 p2*/
wait_mark=1;
signal(SIGINT,stop); /*接收到^c 信号,转 stop*/
waiting();
kill(p1,16); /*向 p1 发软中断信号 16*/
kill(p2,17); /*向 p2 发软中断信号 17*/
wait(0); /*同步*/
wait(0);
printf("Parent process is terminated!\n");
exit(0);
}
else {
wait_mark=1;
signal(SIGINT,stop);
waiting();
lockf(1,0,0);
printf("Child process2 is killed by parent!\n");
lockf(1,0,0);
exit(0);
}
}
else {
wait_mark=1;
signal(SIGINT,stop);
waiting();
lockf(1,0,0);
printf("Child process1 is killed by parent!\n");
lockf(1,0,0);
exit(0);
}
}
void waiting() {
while (wait_mark!=0);
}
void stop() {
wait_mark=0;
}