Unix 分叉树只分叉一个孩子

发布于 2024-12-08 18:01:40 字数 2980 浏览 1 评论 0原文

显然是作业,但我并不是要求任何人为我做,而是我只是想要指导。到目前为止,我已经将其编写为分叉进程树(这是一个挑战),

   /* 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 语句中创建另一个分叉,但它不起作用。我什至尝试将所有东西分开,但惨败。我只是没有从逻辑上看到解决方案。有什么提示或建议吗?

我尝试过递归:

     /* 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,有什么特殊原因吗?

Obviously homework, however I am not asking for anyone to do it for me but rather I just want direction. So far I have already written this as a fork process tree(which was a challenge to figure out)

   /* 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;

    }   

}

this program creates this tree

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

I need to create another fork tree that have a tree like this:

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

What modification should I look into making to my program?

I have tried creating a another fork inside the rightchild if statement and it didn't work. I even tried splitting everything up but that failed miserably. I am just not seeing the solution logically. any hints or suggestions?

I have tried recursion:

     /* 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();



}

This actually wont return the pid for anything, any particular reason why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2024-12-15 18:01:40

你的左孩子不应该继续循环;它应该跳出循环或退出。它没有子级,但这仅仅意味着 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);
}

Your left child should not continue the loop; it should break out of the loop, or exit. It has no children, but that simply means the wait() call will return with an error each time.

Your right child should continue the loop to continue the process down to the last level.

The parent process at each level should break out of the loop after launching the second (right) child, and wait for its children to die.


Example

This seems to work for me; it is a simple adaptation of the answer to SO 7624325, your previous related question, though I carved it out of your example code above.

Example output

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

Example code

#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);
}
青朷 2024-12-15 18:01:40

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

Why don't you use recursion instead of a for loop? Create a fork for the left side and call yourself for the right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文