|
发表于 2003-5-25 14:00:32
|
显示全部楼层
写了一个,还有些问题,主要是父进程没有处理其他信号,如果父进程收到其他信号结束子进程不会结束。
[code:1]
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
static int create_child(void);
static void sig_int(int);
static void sig_usr(int);
static int child_no;
static pid_t child_pid[2];
int
main(void)
{
if(create_child()){
printf("cannot create child\n");
exit(-1);
}
if(create_child()){
printf("cannot create chld\n");
kill(child_pid[0], SIGUSR1);
}
if(signal(SIGINT, sig_int) == SIG_ERR){
perror("cannot catch SIGINT");
exit(-1);
}
while(child_no > 0){
wait(NULL);
--child_no;
}
printf("parent process in killed\n");
exit(0);
}
static int
create_child(void)
{
pid_t pid;
if((pid = fork()) < 0){
perror("fork error");
return(-1);
}else if(pid == 0){
++child_no;
if(signal(SIGUSR1, sig_usr) == SIG_ERR){
perror("cannot catch SIGINT");
return(-1);
}
signal(SIGINT, SIG_IGN);
for(;;)
pause();
}
child_pid[child_no] = pid;
++child_no;
return(0);
}
static void
sig_int(int signo)
{
kill(child_pid[0], SIGUSR1);
kill(child_pid[1], SIGUSR1);
}
static void
sig_usr(int signo)
{
printf("child process%d is killed by parent!\n", child_no);
exit(0);
}
[/code:1] |
|