C 从孩子那里分出3个孙子的麻烦,顺序、PID和PPID不正确

C 从孩子那里分出3个孙子的麻烦,顺序、PID和PPID不正确,c,linux,unix,fork,C,Linux,Unix,Fork,您好,我有一个关于使用fork()创建更多子对象的问题,这个问题是基于我之前提出的一个问题 我希望我的输出看起来像这样(#s被简化,仅用于说明顺序) 但我的输出如下所示: 最后关于爸爸的ppid似乎还行,除了最后一个,大多数PID似乎都出了问题。我不知道为什么会有一个儿子,然后是5个,然后是3个儿子。这是我的密码: int grandforking(null) { Gen1 (null); return 0; } int Gen1 (null) {

您好,我有一个关于使用fork()创建更多子对象的问题,这个问题是基于我之前提出的一个问题

我希望我的输出看起来像这样(#s被简化,仅用于说明顺序)

但我的输出如下所示:

最后关于爸爸的ppid似乎还行,除了最后一个,大多数PID似乎都出了问题。我不知道为什么会有一个儿子,然后是5个,然后是3个儿子。这是我的密码:

int grandforking(null)
{
    Gen1 (null);       
    return 0;
}

int Gen1 (null)
{    
  void about(char *);
  int i=0;
  int j=0;  
  about("grandpa");    
  for(i = 0; i < 3; i++ )
  {
        pid_t child = 0;
        child = fork();
        if (child < 0) 
        { //unable to fork error
            perror ("Unable to fork");
            exit(-1);
        }    
        else if (child == 0)
        { //child process
             Gen2 (null);
             exit(0);
        }    
        else 
        { //parent process    
              //(do nothing)
        }    
  }   
  for(j = 0; j < 3; j++ )
  {
     wait(NULL);//wait for parent to acknowledge child process
  }
return 0;
}

int Gen2 (null)
{   
  int i=0;
  int j=0;
  about("dad");
  for(i = 0; i < 3; i++ )
  {
        pid_t child = 0;
        child = fork();
        if (child < 0)
        { //unable to fork error
            perror ("Unable to fork");
            exit(-1);
         }    
         else if (child == 0)
         { //child process
             about ("son");
             exit(0);
         }   
         else 
         { //parent process
            //(do nothing)
         }
  }
  for(j = 0; j < 3; j++ )
  {
    wait(NULL);//wait for parent to acknowledge child process
  }
   return 0;    
 }
int-grandforking(空)
{
第1代(空);
返回0;
}
int第1代(空)
{    
空约(字符*);
int i=0;
int j=0;
关于(“爷爷”);
对于(i=0;i<3;i++)
{
pid_t child=0;
child=fork();
if(子级<0)
{//无法分叉错误
佩罗尔(“无法叉叉”);
出口(-1);
}    
else if(子项==0)
{//子进程
第2代(空);
出口(0);
}    
其他的
{//父进程
//(什么也不做)
}    
}   
对于(j=0;j<3;j++)
{
wait(NULL);//等待父进程确认子进程
}
返回0;
}
int第2代(空)
{   
int i=0;
int j=0;
关于(“爸爸”);
对于(i=0;i<3;i++)
{
pid_t child=0;
child=fork();
if(子级<0)
{//无法分叉错误
佩罗尔(“无法叉叉”);
出口(-1);
}    
else if(子项==0)
{//子进程
关于(“儿子”);
出口(0);
}   
其他的
{//父进程
//(什么也不做)
}
}
对于(j=0;j<3;j++)
{
wait(NULL);//等待父进程确认子进程
}
返回0;
}

进程启动后,调度程序可以并将按照其希望的任何顺序运行它们。如果您有多个处理器,这包括同时运行多个处理器。(像现在的每个人一样。)

它当然可以启动一个子系统,运行该子系统一段时间,然后返回父系统打印消息

如果在父对象生成其子对象之前具有标识printf,则排序会稍微好一些

但获得lockstep订单的唯一方法是执行以下操作:

  • 自认
  • 循环起点
    • 生孩子
    • 等待孩子完成
  • 在所有子项完成后退出

您不需要澄清它是什么语言,只需使用标签即可。你认为社区意识到了这一点的差异,事实上,看到混合标签的问题是很常见的,我们不断告诉他们C不是C++。AHH只是想在我的标题中有所帮助,因为有时我会问,当我把CSEC代码工作正常时,是否是C++。你有三个父亲是从5841产生的,三个儿子是从5842产生的,三个儿子是从5843产生的,三个儿子是从5850产生的。叔叔和侄子之间printfs的顺序不是代码试图控制的。我发现所有多余的单词和英语使得调试和查看发生了什么变得更加困难。我只想列出
(父:)
,并根据需要缩进以指示父/子关系并显示流程树。好的,谢谢大家。如果有人对如何更好地格式化输出有任何想法(有点像Arlie所说的),我想现在就让它保持原样。你认为祖父母>>>父母>>>孙辈的实际分组是正确的,还是仅仅因为他们没有以正确的顺序显示在屏幕上?从你所说的听上去像是后者
int grandforking(null)
{
    Gen1 (null);       
    return 0;
}

int Gen1 (null)
{    
  void about(char *);
  int i=0;
  int j=0;  
  about("grandpa");    
  for(i = 0; i < 3; i++ )
  {
        pid_t child = 0;
        child = fork();
        if (child < 0) 
        { //unable to fork error
            perror ("Unable to fork");
            exit(-1);
        }    
        else if (child == 0)
        { //child process
             Gen2 (null);
             exit(0);
        }    
        else 
        { //parent process    
              //(do nothing)
        }    
  }   
  for(j = 0; j < 3; j++ )
  {
     wait(NULL);//wait for parent to acknowledge child process
  }
return 0;
}

int Gen2 (null)
{   
  int i=0;
  int j=0;
  about("dad");
  for(i = 0; i < 3; i++ )
  {
        pid_t child = 0;
        child = fork();
        if (child < 0)
        { //unable to fork error
            perror ("Unable to fork");
            exit(-1);
         }    
         else if (child == 0)
         { //child process
             about ("son");
             exit(0);
         }   
         else 
         { //parent process
            //(do nothing)
         }
  }
  for(j = 0; j < 3; j++ )
  {
    wait(NULL);//wait for parent to acknowledge child process
  }
   return 0;    
 }