3

我有一个看起来像这样的 C 文件:

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h> 
int main () 
{ 
    pid_t child_pid; 
    printf ("The PID is %d\n", (int) getpid ()); 
    child_pid = fork (); 
    if (child_pid != 0)
    { 
        printf ("this is the parent process, with PID %d\n", 
        (int)getpid()); 
        printf ("the child's PID is %d\n", (int) child_pid); 
    } 
    else 
        printf ("this is the child process, with PID %d\n", 
        (int)getpid()); 
    return 0; 
} 

我需要修改它以产生一个看起来像的层次结构

parent (0)
  |
  +---> child (1)
  |
  +---> child (2)
          |
          +----> child (3)
          |
          +----> child (4)
                  |
                  +----> child (5)
                  |

基本上是一个树结构,其中每个第二个孩子产生两个新孩子。据我了解,当我fork()一个进程时,每个进程都会同时运行。fork()在语句中添加 aif似乎可以正常工作并正确创建进程 0 到 2,因为只有父进程会创建一个新的分叉。但我不知道如何制作流程 2 的分叉而不是流程 1。有什么想法吗?

4

4 回答 4

2

我不知道你为什么要这样做,但通常只有父进程执行分叉。这也可能更容易设计。当您在 for 循环中执行 fork() 时,您将直接控制创建的进程。

请注意 fork() 是一个相对昂贵的操作,特别是如果您想创建许多进程。可以使用更轻量级的替代品 vfork 和线程,但我无法判断它们是否也符合您的需求。

于 2009-08-06T13:16:57.067 回答
2

那么,进程 1 将由第一个分叉创建。进程 2 将由 if 语句中的 fork 创建。所以为了让进程 2 也分叉,如果第二个分叉没有返回 0,你在 if 语句中再次分叉。

插图:

if(fork) {
    // Inside process 0
    if(fork) {
        // still in process 0
    } else {
        // in process 2
        if(fork) {
          // still in process 2
        } else {
          // in prcess 3
        }
        // and so on
    }
} else {
    // Inside process 1
}
于 2009-08-06T13:13:49.333 回答
2

孩子在分叉时获得父母状态的副本。

因此,如果父级有一个计数器或其他属性,那么子级将在分叉时看到该值(但如果父级随后更改它,则不会看到)。

于 2009-08-06T13:15:06.273 回答
1

一个古老的问题,但仍然是一个有趣的问题。的手册fork()告诉我们返回值:

成功时,父进程返回子进程的PID,子进程返回0。失败时,在父进程中返回 -1,不创建子进程,并适当设置 errno。

所以我们知道fork()返回一个 0 给孩子,我们可以使用它作为控制机制:

int main()
{
   // Here we're in the parent (0) process

   if(fork())  // This starts the chain, only the parent will do this
    if(!fork()) //the child will pass this, the parent will skip it
     for(count = 0; counter < number_of_processes; counter++) //now the 2nd child loops
     {
         if(fork())
             if(!fork());
         else
             break;
     }

让我们给它一些数字:

//parent (18402)
if(fork()) // spawns 18403, parent 18402 keeps going
  if(!fork()) // spawns 18404, parent 18402 drops to the end of main()
    for(count = 0; count < number_of_processes; conter++) // 18404 enters here
      if(fork())  // 18404 forks 18405 and then goes on
        if(!fork());  // 18404 spawns 18406 before leaving, 18406 restarts the loop
        else
          break; // 18404 breaks out there
      else
          break; //18405 leaves the loop here

所以在一次迭代之后,我们有:

  18402
    |
    +---> 18403
    |
    +---> 18404
            |
            +----> 18405
            |
            +----> 18406
                     |

在此之后,我们将继续循环两个新过程,其中第二个过程将继续迭代,直到您完成所需的次数。

于 2012-12-03T19:19:07.620 回答