linux - 多级单亲单子(monad)进程树?

标签 linux unix process fork

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

例子: parent -> child 1-> child 2-> child 3。像那样。 See image here enter image description 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

谢谢,终于可以用了。

最佳答案

只是循环它:

#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 的 child
  • child 21894 创建 child 21895
  • child 21895 创建 child 21896
  • child 21896 创建 child 21897
  • child 21907 创建 child 21898

关于linux - 多级单亲单子(monad)进程树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51957778/

相关文章:

c++ - 如何在多个项目(以及一个版本的 cpplint.py)中使用单个 makefile?

c - x86 中 stat 结构的布局

dll - 如何查找加载到进程中的 DLL 及其位置等

Unix 软链接(soft link)和路径

c - Linux 上的 read() 和页对齐缓冲区

c# - 如何在 Windows 窗体中打开进程

我可以从当前线程中保存一个值吗?

c++ - 使用 eclipse 和 ubuntu 13.04 构建 opencv 2.4.5 应用程序时出错

linux - 汇编程序 NASM 文件的格式是什么?

bash - getopts 在 bash 脚本中不起作用