create a process tree in C

后端 未结 2 1140
Happy的楠姐
Happy的楠姐 2021-01-27 05:36

How would I approach creating a process hierarchy that would look like a balanced ternary tree of depth N? ... meaning each process has 3 children so there would be (3^N-1)/2 pr

2条回答
  •  自闭症患者
    2021-01-27 06:02

    This bit does not look right to me:

    for(x=0; x<3; x++) {
       fork();
       createTernaryTree(n-1);
    }
    

    The problem is that both the parent and the child continue looping and do the recursion.

    Based on the return from fork (0 in the child, > 0 in the parent, -1 on error), you should decide whether to loop or recurse.

提交回复
热议问题