【问题标题】:Multi Level Single Parent Single Child Process tree?多级单父单子进程树?
【发布时间】:2018-08-21 22:58:01
【问题描述】:

所以我想编写一个程序来创建多级子进程。单亲和独生子女。

示例:父->child1->child2->child3。像那样。 See image here

但问题是我想从终端获取输入,将创建多少子进程(单父 - 单子进程)。

那么我怎样才能将 嵌套 if 语句修改为某个循环,以便它可以创建我想要的子进程。

这是我的代码

#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() {

int a, b;

    {
        if(fork() == 0)
        {
        printf("child my pid is %d ppid is %d\n",getpid(),getppid());
            if(fork()== 0)
            {
             printf("child my pid is %d ppid is %d\n",getpid(),getppid());
              
               if(fork()== 0)
             {
             printf("child my pid is %d ppid is %d\n",getpid(),getppid());
                }
            }
       
        }
        else
        printf("father my pid is %d ppid is %d\n",getpid(),getppid());
    
    }
    for(int i=0;i<3;i++)
    wait(NULL);

return 0;
}

输出到这里:

father my pid is 4496 ppid is 3621
child my pid is 4497 ppid is 4496
child my pid is 4498 ppid is 4497
child my pid is 4499 ppid is 4498

谢谢,终于成功了。

【问题讨论】:

    标签: linux unix process fork


    【解决方案1】:

    循环一下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <assert.h>
    
    int main(int argc, char *argv[]) 
    {
        assert(argc == 2);
        int num = atoi(argv[1]);
        for (int i = 0; i < num; ++i) {
             int child;
             switch ((child = fork())) { 
             case 0: 
                printf("child my pid is %d ppid is %d\n", getpid(), getppid());
                break;
             case -1:
                fprintf(stderr, "fork failed\n");
                break;
             default:
                printf("father my pid is %d ppid is %d and i just created %d\n",getpid(), getppid(), child);
                i = num; // break of loop
                break;
             }
        }
        wait(NULL);
    
        return 0;
    }
    

    @编辑:

    循环中断在错误的位置。现在该流程按预期创建流程树:
    使用 gcc 编译代码并运行:

    $ ./a.out 5
    father my pid is 21893 ppid is 21640 and i just created 21894
    child my pid is 21894 ppid is 21893
    father my pid is 21894 ppid is 21893 and i just created 21895
    child my pid is 21895 ppid is 21894
    father my pid is 21895 ppid is 21894 and i just created 21896
    child my pid is 21896 ppid is 21895
    father my pid is 21896 ppid is 21895 and i just created 21897
    child my pid is 21897 ppid is 21896
    father my pid is 21897 ppid is 21896 and i just created 21898
    child my pid is 21898 ppid is 21897
    

    程序:

    • 以 pid 21893 开始
    • 使用 pid 21894 创建子节点
    • 子 21894 创建子 21895
    • 子 21895 创建子 21896
    • 子 21896 创建子 21897
    • 子 21907 创建子 21898

    【讨论】:

    • child == fork() 在多个层面上都是完全错误的。我不是在那里进行比较操作,而是进行分配。 fork() 返回 1 给孩子,-1 错误和孩子 pid 值给父母。 (child = fork()) 是一个赋值运算符,它将fork() 的返回值赋给子值,并返回这个值(即被赋值)。我稍后可以在 switch 中使用该值。使用child == fork() 是错误的。如果您希望father 行显示一次,只需在default: 大小写后添加if (i == 0),这样它只会在第一个循环中显示。
    • 我明白这一点。 == 是比较,= 是赋值,但赋值时它没有给出所需的输出。我确实运行了你的代码。但我没有得到想要的输出,所以我建议编辑..
    • 对不起,我昨天才加入堆栈溢出,所以我在这里不太了解。
    • 我还添加了 if (i==0) 所以父语句只工作一次。
    猜你喜欢
    • 2017-06-29
    • 1970-01-01
    • 2013-02-06
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多