#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
//子进程个数
#define SUB_PRO_COUNT 10
//处理子进程的退出信号
void sub_quit_signal_handle(int sig);
//父进程的事件循环
void ParentCycle();
//子进程的事件循环
void ChildCycle();
int main(void)
{
pid_t pid;
int i;
//创建SUB_PRO_COUNT个子进程
for(i=; i<SUB_PRO_COUNT; i++)
{
pid = fork();
if( == pid || - == pid )
break;
}
//创建子进程失败
if( - == pid )
{
printf("No. %d: fork error\n", i);
}
//子进程的事件循环
else if( == pid )
{
ChildCycle();
}
//父进程的事件循环
else
{
ParentCycle();
}
return ;
}
void sub_quit_signal_handle(int sig)
{
int status;
//获取退出的那个子进程的状态
int quit_pid = wait(&status);
printf("sub process %d quit, exit status %d\n", quit_pid, status);
//新创建一个子进程
pid_t pid = fork();
if( == pid )
ChildCycle();
}
void ParentCycle()
{
printf("Parent process %d\n", getpid());
signal(SIGCHLD, sub_quit_signal_handle);
while()
pause();
}
void ChildCycle()
{
printf("create sub process id: %d, parent id: %d\n", getpid(), getppid());
while()
pause();
}