日期:2014-05-16 浏览次数:20698 次
#include <stdio.h> #include <signal.h> #include <stdlib.h> int wait_pid=0; int main(int args,char *av[]) { //signal(SIGCHLD,report_quit); if(args==1) { printf("You should declare the number of the child process that you want to build!"); return 1; } else { int num_p=atoi(av[1]); int child_pid=1; int i=0; for(i=0;i<num_p;i++) { if(child_pid!=0) { child_pid=fork(); printf("%d :%d \n",getpid(),child_pid); if(child_pid==0) { sleep(2); exit(17); } else if(child_pid==-1) { perror("fork error!"); exit(2); } } } while((wait_pid=wait(NULL))!=-1) printf("the child process %d exited!\n",wait_pid); } return 0; }
11240 :11241 11241 :0 11240 :11242 11242 :0 11240 :11243 11243 :0 11240 :11244 11244 :0 the child process 11241 exited! the child process 11242 exited! the child process 11243 exited! the child process 11244 exited!
#include <stdio.h> #include <signal.h> #include <stdlib.h> void report_quit(); int wait_pid=0; int main(int args,char *av[]) { signal(SIGCHLD,report_quit); if(args==1) { printf("You should declare the number of the child process that you want to build!"); return 1; } else { int num_p=atoi(av[1]); int child_pid=1; int i=0; for(i=0;i<num_p;i++) { if(child_pid!=0) { child_pid=fork(); printf("%d :%d \n",getpid(),child_pid); if(child_pid==0) { sleep(2); exit(17); } else if(child_pid==-1) { perror("fork error!"); exit(2); } } } while(1) wait_pid=wait(NULL); // printf("the child process %d exited!\n",wait_pid); } return 0; } void report_quit() { printf("the child process %d exited.\n",wait_pid); }
11266 :1