如何使用fork()创建特定的进程树

时间:2016-12-01 17:07:23

标签: unix parallel-processing fork

流程树:

Image

我想制作一张如上图所示的流程树。我写了下面的代码,但是如果看一下PID,你会发现有问题!

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int a ;
    int b ;
    int c ;
    int d ;
    int e ;
    int f ;
    int g ;
    int h ;
    int i ;

    b=fork();

    if (b == 0) //it's child
    {
        d= fork();
        if(d==0)
        {
            h=fork();
            if(h==0)
            {
                i=fork();
                if(i==0)
                    printf("%d: I\n", getpid());
                else
                    printf("%d: H\n", getpid());
            }
            else
                printf("%d: D\n", getpid());
        }
        else
        {
            e=fork();
            if(e==0)
                printf("%d: E\n", getpid());

            else
            {
                f=fork();
                if(f==0)
                    printf("%d: F\n", getpid());
                else
                    printf("%d: B\n", getpid());
            }
        }
    }
    else
    {
        c=fork();
        if(c==0){
            g=fork();
            if(g==0)
                printf("%d: G\n", getpid());
            else
                printf("%d: C\n", getpid());
        }
        else
            printf("%d: A\n", getpid());
    }   
    return 0;
}

输出(UNIX):

    10201: A
    10203: C
    10202: B
    10204: G
    10207: F
    10206: E
    10205: D
    10208: H
    10209: I

你可以看到G(pid)= 04,这意味着它早于D(pid)= 05

  • 我该如何改进?
  • 另一个问题是,是否有任何方法可以按顺序打印PID(A,B,C,D,E,...)?

我要创建一个订单:

        10201: A
        10203: C
        10202: B
        10204: D
        10207: G
        10206: F
        10205: E
        10208: H
        10209: I

2 个答案:

答案 0 :(得分:1)

嘿,请看一下这个流程树。

https://i.stack.imgur.com/G5QLy.png

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

int main(){

pid_t pid1, pid2, pid3, pid4;
printf("Parent of all: %d\n",getpid());

pid1 = fork();

if(pid1 == 0){   // A child Process. Lets say B.
    printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
    pid2 = fork();
    if(pid2 == 0){ // A child process. Lets say D.
        printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
    }
}
if(pid1 > 0){
    pid3 = fork();
    if(pid3 == 0){ // A child process. Lets say C.
        printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
        pid4 = fork();
        if(pid4 == 0){ // A child process. Lets say E.
            printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
        }

    }
}
for(int i=0; i<3; i++) 
  wait(NULL);  
}

<强>输出:

Parent of all: 47891
Child with id: 47892 and its Parent id: 47891 
Child with id: 47893 and its Parent id: 47891 
Child with id: 47894 and its Parent id: 47892 
Child with id: 47895 and its Parent id: 47893 

希望,代码是自我解释的。

答案 1 :(得分:0)

这是我上面树的代码 -

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

int main(){

  pid_t pid1, pid2, pid3, pid4,pid5,pid6,pid7,pid8,pid9,pid10,pid11;
  printf("Parent of all: %d\n",getpid());

  pid1 = fork();

  if(pid1 == 0)
  {   // A child Process. Lets say B.
      printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
      pid2 = fork();
      if(pid2 == 0){ // A child process. Lets say D.
        printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
}
else{
    // A child process. Lets say E.
    pid4 = fork();
    if(pid4 == 0){ 
        printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
        // A child process. Lets say H.
        pid6=fork();
        if(pid6 == 0)
        { 
            printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
            // A child process. Lets say I.
            pid7=fork();
            if(pid7==0)
            {   
            printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
            }
        }
        
    }
    else{
        pid5=fork();
        if(pid5 == 0)
        { // A child process. Lets say F.
        printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());   
        } 
    }

  }
 }
 if(pid1 > 0){
   pid3 = fork();
  if(pid3 == 0){ // A child process. Lets say C.
    printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
    // A child process. Lets say G.
    pid10=fork();
    if(pid10 == 0)
    {
        printf("Child with id: %d and its Parent id: %d \n", getpid(),getppid());
    }
   }
  }
for(int i=0; i<3; i++) 
   wait(NULL);  
}

输出是 -

The output of the above code