简体   繁体   English

叉树C程序

[英]Fork Tree C Programs

I'm trying to create fork tree diagram, but still with no success. 我正在尝试创建叉形树图,但仍然没有成功。 Here is my code: 这是我的代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void procStatus(int level) {
   printf("L%d: PID[%d] (PPID[%d])\n", level, getpid(), getppid());
   fflush(NULL);
}

void levelFork(int *level) {
   if (fork() == 0)
      (*level)++;
   wait(NULL);
}

void main() {
   int level = 0;
   procStatus(level);
   levelFork(&level);
   procStatus(level);
}

I want to create like this picture below: 我想创建如下图所示:

叉树图

And this is output look like: 输出如下:

输出量

Any help would be appreciated. 任何帮助,将不胜感激。

Code will be like this, you should fork two child for every new child process until reached target depth level ,after forking two child,parent process must exit system, only new child process should create new processes , 代码是这样的,您应该为每个新的子进程派生两个子进程,直到达到目标深度级别为止,在分叉两个子进程之后,父进程必须退出系统,只有新的子进程才应该创建新进程,

you can discard parent processes by looking childpid(return value of fork) 您可以通过查看childpid(fork的返回值)来丢弃父进程

  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <sys/wait.h>
  #include <math.h>

int main(int argc, char *argv[])
{

pid_t childpid;
int i, n;
if (argc != 2) {
    fprintf(stderr, "Usage: %s n\n", argv[0]); return 1;
}
n = atoi(argv[1]);
childpid=-1;
for (i = 1; i <= n; i++){

    int b;
    for(b=0;b<2;b++)
    {
        childpid=fork();
        if (childpid <= 0) break;

    }
    if (childpid > 0) break;

}
while(wait(NULL) > 0) ; /* wait for all of your children */


fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n",i, (long)getpid(), (long)getppid(), (long)childpid);
return 0;
}

output of code is this 代码的输出是这个

└──╼ $./fork.o 2
i:3 process ID:23913 parent ID:23911 child ID:0
i:3 process ID:23915 parent ID:23911 child ID:0
i:3 process ID:23914 parent ID:23912 child ID:0
i:3 process ID:23916 parent ID:23912 child ID:0
i:2 process ID:23911 parent ID:23910 child ID:23915
i:2 process ID:23912 parent ID:23910 child ID:23916
i:1 process ID:23910 parent ID:23277 child ID:23912

You need a way of specifying the max depth and then using that to fork new processes. 您需要一种方法来指定最大深度,然后使用它来派生新进程。 Once you are done with the forking you can start the printing. 完成分叉之后,就可以开始打印了。 The snippet below should work 下面的代码段应该可以工作

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void procStatus(int level) {
   printf("L%d: PID[%d] (PPID[%d])\n", level, getpid(), getppid());
   fflush(NULL);
}

void levelFork(int *level,int maxlevel) {
  int locallevel= *level;
   while(locallevel!=maxlevel){
      int pid = fork();
      if (pid  == 0){
         (*level)++; // childs level is higher
         levelFork(level,maxlevel);
         return;
      }
     locallevel++;
     wait(NULL);
   }
}

void main() {
        int level = 0;
        int maxlevel=3;

        levelFork(&level,maxlevel);
        procStatus(level);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM