如何建立某個程序的多個子程序 方式一

2021-08-31 05:55:53 字數 432 閱讀 3145

相信大家都知道,在linux環境中,使用fork()函式來建立子程序.然而市面上的書籍往往都是告訴大家如何建立乙個子程序,那麼如何建立某個程序的多個子程序呢?

好了,廢話不多數了,請看如下**(這裡我們採用遞迴實現):

#include#include#includepid_t pid;

/* * num:當前已經建立的子程序數

* max:需要建立的子程序數

*/void createsubprocess(int num,int max)

//子程序

else if(pid==0)

//父程序

else

{ num++;

if(num==1)printf("父程序id=%d\n",getpid());

if(num執行結果如下所示:

Linuxc 建立與監控多個子程序

include include include include include 子程序個數 define sub pro count 10 處理子程序的退出訊號 void sub quit signal handle int sig 父程序的事件迴圈 void parentcycle 子程序的事件迴...

linux中建立多個子程序的方法

今天,師兄問我乙個問題說 小濤啊,建立兩個程序太簡單了,怎麼建立多個程序呢?我說那還不容易,看下邊 省略必要標頭檔案 int main if pid 0 0 printf this is parent d,child is d n getppid getpid else wait 5 return ...

linux中fork同時建立多個子程序的方法(二)

fork 同時建立多個子程序方法 第一種方法 驗證通過 特點 同時建立多個子程序,每個子程序可以執行不同的任務,程式 可讀性較好,便於分析,易擴充套件為多個子程序 int main void pid t p2 fork if p2 0 int st1,st2 waitpid p1,st1,0 wai...