在 C 中创建进程树 [英] create a process tree in C

查看:21
本文介绍了在 C 中创建进程树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何创建一个看起来像深度为 N 的平衡三叉树的流程层次结构?...意味着每个进程有 3 个子进程,所以在深度为 N 的树中会有 (3^N-1)/2 个进程.要创建新进程,我只想使用 fork().

How would I approach creating a process hierarchy that would look like a balanced ternary tree of depth N? ... meaning each process has 3 children so there would be (3^N-1)/2 processes in a tree of depth N. To create the new processes, I only want to use fork().

这是我到目前为止所拥有的,但我认为它不起作用,因为我不处理进程 ID,而且我真的不认为我应该递归地执行此操作:

This is what I have so far but I don't think it works because I don't deal with process IDs and also I really don't think I should do this recursively:

void createTernaryTree(int n) {
   if((n-1) == 0) return;
   else {
      int x;
      for(x=0; x<3; x++) {
         fork();
         createTernaryTree(n-1);
      }
   }
}

谢谢,赫里斯托

推荐答案

显示的代码可以完成工作 显示的代码几乎可以完成工作(但有一个微妙的问题).另一个困难是证明它可以完成工作.

The code shown would do the job The code shown would nearly do the job (but there's a subtle problem). The other difficulty would be showing that it does the job.

问题是在 fork 之后,对于子进程和父进程,代码的行为没有不同.父进程需要完成它的循环.每个孩子都需要在下一级重新开始一个循环:

The problem is that the code doesn't behave differently for the child and the parent processes after the fork. The parent process needs to complete its loop. Each child needs to restart a loop at the next level:

  for (int x = 0; x < 3; x++) // C99
  {
     if (fork() == 0)
     {
         createTernaryTree(n-1);
         break;  // Per comment from R Samuel Klatchko
     }
  }
  pause();  // See below

你可以(应该)添加一个'pause();'循环后调用;这将使父进程进入假死状态,直到它收到信号.然后,您可以证明您有一个进程树.或者,使用sleep(30)"或其他暂停进程的方式.你不希望他们立即退出;他们会做得太快,以至于您无法演示树结构.

You could (should) add a 'pause();' call after the loop; this would send the parent process into suspended animation until it receives a signal. You could then show that you have a tree of processes. Alternatively, use 'sleep(30)' or some other way of suspending the processes. You don't want them to exit immediately; they'll do that too quickly for you to be able to demonstrate the tree structure.

理论上,您可能想要跟踪 'fork()' 是否成功;在实践中,不清楚你会做什么不同的事情,除非,如果第一个失败,不要尝试创建第二个孩子(但无论如何这很可能会失败,所以在这种情况下盲目尝试可能是最好的 - 但请记住在其他情况下,这通常很重要).

In theory, you might want to track whether 'fork()' succeeds; in practice, it isn't clear what you'd do differently except, perhaps, not try to create the second child if the first fails (but that's likely to fail anyway, so blindly trying is probably best in the circumstances - but remember that in other situations, it would usually matter a lot).

树本质上是递归结构;使用递归来管理它们通常是处理它们的最简洁的方法.这看起来像是尾递归,这意味着它可以很容易地转换为循环结构.然而,管理数据结构以密切关注正在发生的事情将比仅递归执行要困难.

Trees are inherently recursive structures; using recursion to manage them is often the neatest way to deal with them. This looks like it is tail recursion which means that it can be converted into a looping structure fairly easily. However, managing a data structure to keep tabs on what is happening is going to be harder than just doing it recursively.

这篇关于在 C 中创建进程树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆