下面 代码主要用于复习,留于此
点击(此处)折叠或打开
- /*************************************************************************
- > File Name: fork5.c
- > Author: gaopeng QQ:22389860 all right reserved
- > Mail: gaopp_200217@163.com
- > Created Time: Sun 02 Jul 2017 02:39:16 AM CST
- ************************************************************************/
- #include<stdio.h>
- #include<unistd.h>
- #include <stdlib.h>
- #define MAXPNUM 3
- typedef struct handler
- {
- int* pidarr;
- int childnum;
- } HANDLER;
- int main(void)
- {
- int i = 0;
- int m = 0;
- int psre = 0;
- HANDLER pidhd;
- pidhd.pidarr = (int*)calloc(MAXPNUM+1,sizeof(int));//初始化内存
- pidhd.childnum = 0;//初始化进程数量
- for(i = 0 ;i<MAXPNUM;i++)//循环建立
- {
- m = fork();
- if(m == -1)
- {
- perror("fork:");
- }
- else if( m == 0 )
- {
- printf("CHILD: I is child process pid: %d parent process pid: %d \n",getpid(),getppid());
- sleep(60);
- break;
- }
- else
- {
- sleep(1);
- pidhd.childnum ++;//进程num+1
- *(pidhd.pidarr+i) = m;//指针移动+1
- printf("PARENT: I is parent process pid: %d i fock chlid pid: %d \n",getpid(),m);
- }
- }
- if(i == MAXPNUM)//一定为父进程
- {
- for(i=0;*(pidhd.pidarr+i);i++)
- {
- printf("child process is pid:%d\n",*(pidhd.pidarr+i));
- }
- }
- if(i == MAXPNUM)//一定为父进程
- {
- printf("parent pid %d fock child process number is %d finsh!! \n",getpid(),pidhd.childnum);
- while(pidhd.childnum > 0)
- {
- for(i = 0;i< MAXPNUM ;i++) //WNOHANG非堵塞循环回收
- {
- if(*(pidhd.pidarr+i) != 0 && waitpid(*(pidhd.pidarr+i),&psre,WNOHANG) > 0 )
- {
- if (WIFEXITED(psre))//是否正常退出获取其退出值
- printf("child %d exit %d\n", *(pidhd.pidarr+i), WEXITSTATUS(psre));
- else if (WIFSIGNALED(psre))//是否异常退出信号终止获得信号值
- printf("child %d cancel signal %d\n", *(pidhd.pidarr+i), WTERMSIG(psre));
- *(pidhd.pidarr+i) == 0;
- pidhd.childnum--;
- break;
- }
- }
- }
- free(pidhd.pidarr);
- }
- return 1;//子进程父进程均已return 1 退出
- }
输出如下可以捕获子线程由于kill 发信号终止:
▽aopeng@bogon:~/linux0411/process$ ./a.out
CHILD: I is child process pid: 2588 parent process pid: 2587
PARENT: I is parent process pid: 2587 i fock chlid pid: 2588
CHILD: I is child process pid: 2589 parent process pid: 2587
PARENT: I is parent process pid: 2587 i fock chlid pid: 2589
CHILD: I is child process pid: 2590 parent process pid: 2587
PARENT: I is parent process pid: 2587 i fock chlid pid: 2590
child process is pid:2588
child process is pid:2589
child process is pid:2590
parent pid 2587 fock child process number is 3 finsh!!
child 2588 cancel signal 9
child 2589 cancel signal 15
child 2590 cancel signal 11
可以捕获正常终止
gaopeng@bogon:~/linux0411/process$ ./a.out
CHILD: I is child process pid: 2597 parent process pid: 2596
PARENT: I is parent process pid: 2596 i fock chlid pid: 2597
CHILD: I is child process pid: 2598 parent process pid: 2596
PARENT: I is parent process pid: 2596 i fock chlid pid: 2598
CHILD: I is child process pid: 2599 parent process pid: 2596
PARENT: I is parent process pid: 2596 i fock chlid pid: 2599
child process is pid:2597
child process is pid:2598
child process is pid:2599
parent pid 2596 fock child process number is 3 finsh!!
child 2599 cancel signal 1
child 2597 exit 1
child 2598 exit 1