- 论坛徽章:
- 0
|
我又做了一下修改
加了两句来忽略子进程接受SIGINT信号
现在用Ctrl+C就可以达到要求了
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void waiting();
void stop();
int wait_mark;
main(){
int p1,p2;
while((p1=fork())==-1);
if(p1>0)
{
while((p2=fork())==-1);
if(p2>0){
wait_mark=1;
signal(SIGINT,stop);
waiting();
kill(p1,16);
kill(p2,17);
wait(0);
wait(0);
printf("parent is killed!\n");
exit(0);
}
else{
signal(SIGINT,SIG_IGN);
wait_mark=1;
signal(17,stop);
waiting();
lockf(stdout,1,0);
printf("child 2 is killed by parent!\n");
lockf(stdout,0,0);
exit(0);
}
}
else{
signal(SIGINT,SIG_IGN);
wait_mark=1;
signal(16,stop);
waiting();
lockf(stdout,1,0);
printf("child process 1 is killed by parent!\n");
lockf(stdout,0,0);
exit(0);
}
}
void waiting(){
while(wait_mark!=0);
}
void stop(){
wait_mark=0;
}
[ 本帖最后由 剑胆琴心 于 2006-4-10 19:19 编辑 ] |
|