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에서와 같이 새 프로세스 인수를 제공 할 수있는 방법이 있습니까? 파이프를 사용하는 대신?