【问题标题】:trouble with forking 3 grandchildren from children, order and PID and PPIDs not right从孩子中分叉 3 个孙子的麻烦,订单和 PID 和 PPID 不正确
【发布时间】:2015-09-30 00:38:19
【问题描述】:

您好,我有一个关于使用 fork() 创建更多子代的问题,这是基于我之前提出的 using fork() to make 3 children out of 1 parent in C (not C++) 的一个问题

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

[grandpa]hi am I PID 1234 and I come from ####(dont care what this number is)
  [dad] hi i am PID 2111 and I come from PPID 1234
    [son] hi i am PID 3111 and I come from PPID 2111
    [son] hi i am PID 3112 and I come from PPID 2111
    [son] hi i am PID 3113 and I come from PPID 2111
  [dad] hi i am PID 2112 and I come from PPID 1234
    [son] hi i am PID 3111 and I come from PPID 2112
    [son] hi i am PID 3112 and I come from PPID 2112
    [son] hi i am PID 3113 and I come from PPID 2112
  [dad] hi i am PID 2113 and I come from PPID 1234
    [son] hi i am PID 3111 and I come from PPID 2113
    [son] hi i am PID 3112 and I come from PPID 2113
    [son] hi i am PID 3113 and I come from PPID 2113

但我的输出如下所示:

除了最后一个之外,关于爸爸 ppid,最后似乎还可以,而且大多数 PID 似乎都乱序了。我不知道为什么有一个儿子,然后是五个,然后是三个儿子。这是我的代码:

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;    
 }

【问题讨论】:

  • 您不需要明确它是什么语言,只需使用标签即可。您认为社区已经意识到 cc++ 之间的区别,事实上,混合标签的问题很常见,我们一直告诉他们 c 不是 c++。
  • 啊,我只是想对我的标题有所帮助,因为有时我在输入 C 时会被问到是否是 C++
  • 代码运行正常。您有三个从 5841 产生的父亲,三个来自 5842 的儿子,三个来自 5843 的儿子和三个来自 5850 的儿子。叔侄之间 printfs 的顺序不是代码试图控制的东西。该代码仅保证父亲在其任何儿子之前打印。
  • 我发现所有多余的单词和英语都使调试和查看发生了什么变得更加困难。我只是列出&lt;pid&gt; (parent: &lt;pid&gt;) 并根据需要缩进以指示父/子关系并显示流程树。
  • 好的,谢谢大家。如果有人对如何更好地格式化输出有任何想法(有点像 Arlie 所说的),我想我现在就让它保持原样。

标签: c linux unix fork


【解决方案1】:

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

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

如果您在父级生成子级之前有标识 printf,您的排序会稍微好一些。

但获得同步排序的唯一方法是执行以下操作:

  • 表明自己的身份
  • 循环开始
    • 启动孩子
    • 等待孩子完成
  • 在所有孩子完成后退出

【讨论】:

  • 您认为祖父母 >>> 父母 >>> 孙子女的实际分组是正确的,还是只是他们没有以正确的顺序显示在屏幕上?你说的好像是后者
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多