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?而不是使用管道?