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

查看:130
本文介绍了在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);
      }
   }
}

谢谢, 赫里斯托(Hristo)

Thanks, Hristo

推荐答案

所显示的代码可以完成这项工作所显示的代码几乎可以完成这项工作(但是存在一个细微的问题).另一个困难将是证明它确实起作用.

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.

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

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天全站免登陆