LINUX C 父程式建立多個子程式迴圈非堵塞回收列子

gaopengtttt發表於2017-06-12
下面 程式碼主要用於複習,留於此

點選(此處)摺疊或開啟

  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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2140651/,如需轉載,請註明出處,否則將追究法律責任。

相關文章