0
我想創建一個遞歸函數,使用fork()創建一個父子進程的二叉樹結構,給定樹的層數。到目前爲止,我有:進程的二叉樹
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void createTree(int level){
pid_t leftson;
pid_t rightson;
if (level > 1){
if ((leftson = fork()) < 0) {
perror("fork:");
exit(1);
} // Create the first son
if (leftson == 0){
createTree(level--);
} // If I'm the left son, continue biulding the structure
else { // I'm father
if ((rightson = fork()) < 0) {
perror("fork:");
exit(1);
} // Create right son
if (rightson == 0){
createTree(level--);
} // I'm right, continue building
else printf("created my 2 sons"); // I'm the father
}
}
else if (level == 1){
printf("end of tree");
}
}
void main(){
createTree(3);
}
的問題是,該方案在創造,因爲水平變量不減少,我想使用管道的過程的無限循環進入,但我不知道如何使用它們當有這麼多的進程。
此外,有沒有辦法讓新的進程參數像我從bash?而不是使用管道?