1. 首页
  2. IT资讯

LINUX C 父进程建立多个子进程循环非堵塞回收列子

下面 代码主要用于复习,留于此

点击(此处)折叠或打开

  1. /*************************************************************************
  2.   > File Name: fork5.c
  3.   > Author: gaopeng QQ:22389860 all right reserved
  4.   > Mail: gaopp_200217@163.com
  5.   > Created Time: Sun 02 Jul 2017 02:39:16 AM CST
  6.  ************************************************************************/
  7. #include<stdio.h>
  8. #include<unistd.h>
  9. #include <stdlib.h>
  10. #define MAXPNUM 3
  11. typedef struct handler
  12. {
  13.         int* pidarr;
  14.         int childnum;
  15. } HANDLER;
  16. int main(void)
  17. {
  18.         int i = 0;
  19.         int m = 0;
  20.         int psre = 0;
  21.         HANDLER pidhd;
  22.         pidhd.pidarr = (int*)calloc(MAXPNUM+1,sizeof(int));//初始化内存
  23.         pidhd.childnum = 0;//初始化进程数量
  24.         for(i = 0 ;i<MAXPNUM;i++)//循环建立
  25.         {
  26.                 m = fork();
  27.                 if(m == 1)
  28.                 {
  29.                         perror(“fork:”);
  30.                 }
  31.                 else if( m == 0 )
  32.                 {
  33.                         printf(“CHILD: I is child process pid: %d parent process pid: %d n”,getpid(),getppid());
  34.                         sleep(60);
  35.                         break;
  36.                 }
  37.                 else
  38.                 {
  39.                         sleep(1);
  40.                         pidhd.childnum ++;//进程num+1
  41.                         *(pidhd.pidarr+i) = m;//指针移动+1
  42.                         printf(“PARENT: I is parent process pid: %d i fock chlid pid: %d n”,getpid(),m);
  43.                 }
  44.         }
  45.         if(i == MAXPNUM)//一定为父进程
  46.         {
  47.                 for(i=0;*(pidhd.pidarr+i);i++)
  48.                 {
  49.                         printf(“child process is pid:%dn”,*(pidhd.pidarr+i));
  50.                 }
  51.         }
  52.         if(i == MAXPNUM)//一定为父进程
  53.         {
  54.                 printf(“parent pid %d fock child process number is %d finsh!! n”,getpid(),pidhd.childnum);
  55.                 while(pidhd.childnum > 0)
  56.                 {
  57.                         for(i = 0;i< MAXPNUM ;i++) //WNOHANG非堵塞循环回收
  58.                         {
  59.                                 if(*(pidhd.pidarr+i) != 0 && waitpid(*(pidhd.pidarr+i),&psre,WNOHANG) > 0 )
  60.                                 {
  61.                                         if (WIFEXITED(psre))//是否正常退出获取其退出值
  62.                                                 printf(“child %d exit %dn”, *(pidhd.pidarr+i), WEXITSTATUS(psre));
  63.                                         else if (WIFSIGNALED(psre))//是否异常退出信号终止获得信号值
  64.                                                 printf(“child %d cancel signal %dn”, *(pidhd.pidarr+i), WTERMSIG(psre));
  65.                                         *(pidhd.pidarr+i) == 0;
  66.                                         pidhd.childnum;
  67.                                         break;
  68.                                 }
  69.                         }
  70.                 }
  71.           free(pidhd.pidarr);
  72.         }
  73.         return 1;//子进程父进程均已return 1 退出
  74. }

输出如下可以捕获子线程由于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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2140651/,如需转载,请注明出处,否则将追究法律责任。

主题测试文章,只做测试使用。发布者:深沉的少年,转转请注明出处:http://www.cxybcw.com/183844.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code