LINUX C 父程式建立多個子程式迴圈非堵塞回收列子
下面 程式碼主要用於複習,留於此
輸出如下可以捕獲子執行緒由於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
點選(此處)摺疊或開啟
-
/*************************************************************************
-
> 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 退出
- }
▽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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux系統程式設計 - 07. 迴圈建立N個子程式分析Linux程式設計
- PHP多程式學習(二)__fork起多個子程式,父程式的阻塞與非阻塞PHP
- 使用 Python 迴圈建立多個列表Python
- fork、父程式和子程式
- fork同時建立多個子程式的方法
- Linux socke server程式設計:父程式和子程式關係LinuxServer程式設計
- C# 迴圈時,操作另外一個程式直到操作完成,迴圈繼續執行C#
- linux 子程式可以繼承父程式正在監聽的埠嗎? 如何子程式關閉了繼承的埠,父程式還能使用這個埠嗎?Linux繼承
- Linux Shell程式設計(16)——迴圈Linux程式設計
- linux下建立迴圈連線Linux
- 子程式等待父程式退出及fork使用
- C#程式設計基礎第七課:C#中的基本迴圈語句:while迴圈、do-while迴圈、for迴圈、foreach迴圈的使用C#程式設計While
- C語言程式設計學習中while迴圈和do……while迴圈C語言程式設計While
- Linux Shell程式設計(18)—— 迴圈控制Linux程式設計
- PHP多程式學習(三)__程式碼案例來了解父程式與子程式的執行順序PHP
- 非迴圈單連結串列的建立、遍歷、排序等排序
- Linux Shell程式設計(17)——巢狀迴圈Linux程式設計巢狀
- Linux 建立子程式執行任務Linux
- C語言——迴圈結構(for迴圈,while迴圈,do-while迴圈)C語言While
- C# 迴圈C#
- C#迴圈C#
- linux系統程式設計之程式(六):父程式查詢子程式的退出,wait,waitpidLinux程式設計AI
- Linux 迴圈Linux
- 接上節我們來了解了解多程式的一些基礎程式 / 執行緒 / 多程式 / 父程式 / 子程式 / 會話 / 控制終端等執行緒會話
- python多程式與子程式Python
- 漢諾塔非遞迴棧程式碼遞迴
- 高質量的C程式碼.關於迴圈遍歷薦C程式
- 程式流程 (順序,選擇,迴圈)
- Java 中建立子類物件會建立父類物件麼?Java物件
- Linux shell迴圈Linux
- linux while 迴圈LinuxWhile
- 14,javase程式碼實戰-迴圈控制——foreach迴圈的使用(五)Java
- 子承父業-C#繼承C#繼承
- [iOS]C語言技術視訊-06-程式迴圈結構(for{})iOSC語言
- 非同步程式設計 101:寫一個事件迴圈非同步程式設計事件
- 微信小程式 swiper 迴圈遍歷N個資料內容微信小程式
- C 語言使用非迴圈雙向連結串列實現佇列佇列
- 怎樣用 Bash 程式設計:迴圈程式設計