Unix fork树,只对一个孩子进行分叉

时间:2011-10-07 04:17:32

标签: c unix process fork

显然是家庭作业,但我不是要求任何人为我做,而是我只想要指导。到目前为止,我已经将它写成了一个fork进程树(这是一个很难理解的问题)

   /* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include<errno.h>

int main()
{   
    pid_t leftchild;
    pid_t rightchild;
    pid_t pid;
    int level=0;
    int max;


    printf("Enter in max level for process tree: ");
    scanf(" %d", &max);




    pid=getpid();
        fprintf(stderr,"I am the parent process with and id of: %ld\n", (long)getpid());
    for(; level<max; level++){

        leftchild=fork();
          if (leftchild == -1)
           {   

              fprintf(stderr, "can't fork, error %d\n", errno);
              exit(EXIT_FAILURE);
           }

        if(leftchild==0){
            fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());
            continue;
        }



        else{
            rightchild=fork();

            if(rightchild==0){
                fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

                continue;
            }
        }
    wait(NULL);
    wait(NULL);
    break;

    }   

}

此程序创建此树

    i
    /\
  i    i 
 /\    /\
i  i  i  i

我需要创建另一个具有如下树的叉树:

     i
    / \
  i     i
        /\
       i  i
          /\  
         i  i
            /\
            i  i

我应该对我的程序进行哪些修改?

我尝试在右边的if语句中创建另一个fork,但它不起作用。我甚至尝试将所有东西分开,但是失败了。我只是没有逻辑地看到解决方案。任何提示或建议?

我尝试过递归:

     /* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include<errno.h>
pid_t leftchild;
pid_t rightchild;
pid_t pid;
int level=0;
int max;



void recurse(){
if(level<2){
    leftchild= fork();
        if (leftchild == -1)
           {   

              fprintf(stderr, "can't fork, error %d\n", errno);
              exit(EXIT_FAILURE);
           }

        if(leftchild==0){
            fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

        }   

    rightchild=fork();
        if (rightchild == -1)
           {   

              fprintf(stderr, "can't fork, error %d\n", errno);
              exit(EXIT_FAILURE);
           }

        if(rightchild==0){
            fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

        }
        level++;
        recurse();
        }

    }




int main()
{   



    printf("Enter in max level for process tree: ");
    scanf(" %d", &max);


    pid=getpid();
        fprintf(stderr,"I am the parent process with and id of: %ld\n", (long)getpid());

    recurse();



}

这实际上不会为任何事情返回pid,有什么特别的原因吗?

2 个答案:

答案 0 :(得分:2)

你的左孩子不应该继续循环;它应该突破循环,或退出。它没有子节点,但这只是意味着wait()调用每次都会返回错误。

你的正确的孩子应该继续循环,将过程继续到最后一级。

每个级别的父进程应该在启动第二个(右)子进程后退出循环,并等待其子进程死亡。


实施例

这似乎对我有用;它只是简单地改编了SO 7624325的答案,你以前的相关问题,虽然我把它从上面的示例代码中删除了。

示例输出

I am the parent process with and id of: 13277
Level is 0, I am Left  child 13278, my parent is 13277
Exiting: 13278
Level is 0, I am Right child 13279, my parent is 13277
Level is 1, I am Left  child 13280, my parent is 13279
Level is 1, I am Right child 13281, my parent is 13279
Exiting: 13280
Level is 2, I am Right child 13283, my parent is 13281
Level is 2, I am Left  child 13282, my parent is 13281
Exiting: 13282
Level is 3, I am Left  child 13284, my parent is 13283
Level is 3, I am Right child 13285, my parent is 13283
Exiting: 13284
Level is 4, I am Left  child 13286, my parent is 13285
Exiting: 13286
Level is 4, I am Right child 13287, my parent is 13285
Level is 5, I am Left  child 13288, my parent is 13287
Exiting: 13288
Level is 5, I am Right child 13289, my parent is 13287
Level is 6, I am Right child 13291, my parent is 13289
Level is 6, I am Left  child 13290, my parent is 13289
Exiting: 13290
Exiting: 13291
Exiting: 13289
Exiting: 13287
Exiting: 13285
Exiting: 13283
Exiting: 13281
Exiting: 13279
Exiting: 13277

示例代码

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

static void run_process(const char *child, int level)
{
    fprintf(stderr, "Level is %d, I am %-5s child %ld, my parent is %ld\n",
            level, child, (long)getpid(), (long)getppid());
}

int main(void)
{   
    int max = 7;

    fprintf(stderr, "I am the parent process with and id of: %ld\n", (long)getpid());

    for (int level = 0; level < max; level++)
    {
        pid_t leftchild;
        pid_t rightchild;
        if ((leftchild = fork()) < 0)
        {   
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(EXIT_FAILURE);
        }
        else if (leftchild == 0)
        {
            run_process("Left", level);
            break;
        }
        else if ((rightchild = fork()) < 0)
        {   
            fprintf(stderr, "can't fork, error %d\n", errno);
            exit(EXIT_FAILURE);
        }
        else if (rightchild == 0)
        {
            run_process("Right", level);
            continue;
        }
        else
            break;
    }

    wait(NULL);
    wait(NULL);
    fprintf(stderr, "Exiting: %ld\n", (long)getpid());

    return(0);
}

答案 1 :(得分:1)

为什么不使用递归而不是for循环?为左侧创建一个分叉,并为自己调用右侧。