根据楼上的指点我又做了一下修改
加了两句来忽略子进程接受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{
- [color=Red] signal(SIGINT,SIG_IGN);[/color] 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{
- [color=Red]signal(SIGINT,SIG_IGN);[/color]
- 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;
- }
-
-
复制代码 |