【问题标题】:Creating n number of child processes from one parent process从一个父进程创建 n 个子进程
【发布时间】:2021-09-14 20:39:32
【问题描述】:

我试图从单个父进程创建n(比如n=3)子进程,但无法找到解决方案。我试过这段代码:

for(i=0; i<3; i++)
{
    fork();
}

但不幸的是它正在创建 7 个子进程。

【问题讨论】:

  • @anastaciu 但那将是错误的信息
  • fork 的返回值告诉您代码是在父级还是在子级中运行。孩子不应该继续循环。
  • 你确定不是总共7个进程吗?
  • @anastaciu 当i=0,父级创建child1,然后当i=1 父级和child1 创建child2child3,然后当i=2,父级,@987654332 @, child2, 和 child3 创建 child4,child5,child6,child7 ---> totak 7 个子进程
  • @AgrudgeAmicus,洗澡数学我猜,我想,你是对的。

标签: c


【解决方案1】:

您必须进行检查,以便只有父进程才能创建子进程,这是执行此操作的示例:

pid_t pid1;

 pid1 = getpid();

 for(i = 0; i < 3; i++)
      if(pid1 == getpid())
           fork();

【讨论】:

  • fork()的返回值告诉你需要知道什么时,调用getpid()四次似乎效率不高。
【解决方案2】:

发生这种情况是因为每个孩子都继续从它派生时的位置执行循环:

  • 根进程分叉 3 个子进程:abc
  • a 看到 i=0 并创建子 de
  • d 看到 i=1 并创建 f
  • b 看到 i=1 并创建 g
  • cefg 参见 i=2,然后退出循环。

这样就有 7 个孩子。你需要检查fork()的返回值,确保你在父进程中:

if (!fork()) {  // returns 0 to the child
    break;
}

【讨论】:

    【解决方案3】:

    您首先需要考虑所有这些分叉都在父级和子级中返回,因此它们都在三层二叉树中创建了三个进程(导致七个最终进程)

    正确的做法是检查父级的返回值,即:

    • -1 如果出现错误(您必须始终检查),您需要检查 errno 以了解 fork() 失败的原因。
    • 0 在这种情况下你是孩子,你不必再次fork(),否则你会以很多进程结束。我只是暂停了 10 秒,然后死在下面的示例代码中。
    • &gt; 0 允许父母知道孩子的 pid(并保存它以了解每个 wait() 返回的孩子中的哪个 exit()ed)。 (你是父进程)

    这导致一些类似这样的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    int main()
    {
        int i;
        for (i = 0; i < 5; i++) { /* repeat five times */
            int child_pid;
            if ((child_pid = fork()) < 0) {  /* error */
                perror("fork");
                exit(1);
            } else if (child_pid > 0) { /* we are the parent */
                printf("Parent, forked child %d with pid = %d\n",
                        i, child_pid);
                /* we continue with the next child until no more
                 * children to start, then we will wait(2) for them
                 * all, before exiting. */
            } else { /* we are the child */
                /* our pid is got from getpid(2) system call */
                printf("I'm the %d child, with pid = %d\n",
                        i, getpid());
                sleep(10); /* do something useful, like sleeping :) */
                exit(0); /* and exit */
            }
            /* the only process that gets here is the parent, as the child
             * does an exit in its block above */
            /* go for next child */
        }
        printf("... now, we need to wait until no children are alive\n");
        int child_exit_pid;
        while ((child_exit_pid = wait(NULL)) >= 0) {  /* wait until we get an error */
            printf("Child with pid == %d exit\n",
                    child_exit_pid);
        }
        printf("Parent exit after all children died\n");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2023-04-04
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      相关资源
      最近更新 更多