|
发表于 2003-10-25 12:31:05
|
显示全部楼层
在子进程中屏蔽掉SIGINT,否则子进程收到SIGINT后会退出
like this:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/types.h>
void waiting(void);
void stop(int);
int wait_mark;
main()
{
pid_t p1,p2;
sigset_t nset,oset;
sigemptyset(&nset);
sigemptyset(&oset);
while((p1=fork())==-1);
if(p1>0)
{
while((p2=fork())==-1);
if(p2>0)
{
wait_mark=1;
signal(SIGINT,stop);
waiting();
kill(p1,SIGSTKFLT);
kill(p2,SIGCHLD);
wait(NULL);
wait(NULL);
fprintf(stderr,"parent process is killed!\n");
exit(0);
}
else
{
sigaddset(&nset,SIGINT);
sigprocmask(SIG_BLOCK,&nset,&oset);
wait_mark=1;
signal(17,stop);
waiting();
lockf(STDOUT_FILENO,1,0);
fprintf(stderr,"process two is killed by parent!\n");
lockf(STDOUT_FILENO,0,0);
sigprocmask(SIG_SETMASK,&oset,NULL);
exit(0);
}
}
else
{
sigaddset(&nset,SIGINT);
sigprocmask(SIG_BLOCK,&nset,&oset);
wait_mark=1;
signal(16,stop);
waiting();
lockf(STDOUT_FILENO,1,0);
fprintf(stderr,"process one is killed by parent!\n");
lockf(STDOUT_FILENO,0,0);
sigprocmask(SIG_SETMASK,&oset,NULL);
exit(0);
}
}
void waiting(void)
{
while(wait_mark!=0);
}
void stop(int sig)
{
printf("pid : %d got signal \" %d \" from parent : %d\n",getpid(),sig,getppid());
wait_mark=0;
} |
|