博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINUX C 父进程建立多个子进程循环非堵塞回收列子
阅读量:5899 次
发布时间:2019-06-19

本文共 2974 字,大约阅读时间需要 9 分钟。

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

点击(此处)折叠或打开

  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:%d\n",*(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 %d\n", *(pidhd.pidarr+i), WEXITSTATUS(psre));
  63.                                         else if (WIFSIGNALED(psre))//是否异常退出信号终止获得信号值
  64.                                                 printf("child %d cancel signal %d\n", *(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

转载地址:http://fzrsx.baihongyu.com/

你可能感兴趣的文章
HashMap源码分析
查看>>
关于Activity调用Ondestroy()方法之后内存管理器为什么没有释放占用资源
查看>>
Launcher之Dock细节篇
查看>>
Android Activity 之 startActivityForResult 的使用
查看>>
org.springframework.util 类 Assert的使用
查看>>
eshell中设置环境变量
查看>>
Spring 源码分析(三) —— AOP(三)实现思路
查看>>
javaScript设计模式——策略模式
查看>>
java提供类与cglib包实现动态代理
查看>>
flask上传多个文件,获取input中的数组
查看>>
更改UIView的背景
查看>>
webstorm快捷键
查看>>
工厂方法模式
查看>>
sun.misc.BASE64Encoder找不到jar包
查看>>
java.io.FileNotFoundException:文件名、目录名或卷标语法不正确
查看>>
配置IIS服务器支持Sencha Touch
查看>>
Oracle冷备份
查看>>
kindEditor或中的图片粘贴处理
查看>>
yii2 - Behavior 实例及源码分析
查看>>
如何去掉“来自Internet的文件......"提示框
查看>>