Search code examples
linuxunixprocessfork

Multi Level Single Parent Single Child Process tree?


so I wanted to write a programme where i will create multi level child processes. With single parent and single child.

example: Parent->child1->child2->child3. like that. See image here enter image description here

But the problem is I want to take input from terminal how many child processes will be created (single parent - single Child processes).

So How Can I modify that Nested if statement to some loop such that it will create child processes as i wanted it to be.

so Here goes My code

#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() {

int a, b;

    {
        if(fork() == 0)
        {
        printf("child my pid is %d ppid is %d\n",getpid(),getppid());
            if(fork()== 0)
            {
             printf("child my pid is %d ppid is %d\n",getpid(),getppid());
              
               if(fork()== 0)
             {
             printf("child my pid is %d ppid is %d\n",getpid(),getppid());
                }
            }
       
        }
        else
        printf("father my pid is %d ppid is %d\n",getpid(),getppid());
    
    }
    for(int i=0;i<3;i++)
    wait(NULL);

return 0;
}

OUTPUT GOES HERE:

father my pid is 4496 ppid is 3621
child my pid is 4497 ppid is 4496
child my pid is 4498 ppid is 4497
child my pid is 4499 ppid is 4498

Thanks It's working finally.


Solution

  • Just loop it:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <assert.h>
    
    int main(int argc, char *argv[]) 
    {
        assert(argc == 2);
        int num = atoi(argv[1]);
        for (int i = 0; i < num; ++i) {
             int child;
             switch ((child = fork())) { 
             case 0: 
                printf("child my pid is %d ppid is %d\n", getpid(), getppid());
                break;
             case -1:
                fprintf(stderr, "fork failed\n");
                break;
             default:
                printf("father my pid is %d ppid is %d and i just created %d\n",getpid(), getppid(), child);
                i = num; // break of loop
                break;
             }
        }
        wait(NULL);
    
        return 0;
    }
    

    @edit:

    The break of loop was in the wrong place. Now the process creates a tree of processes as intended:
    Compiled the code using gcc and run:

    $ ./a.out 5
    father my pid is 21893 ppid is 21640 and i just created 21894
    child my pid is 21894 ppid is 21893
    father my pid is 21894 ppid is 21893 and i just created 21895
    child my pid is 21895 ppid is 21894
    father my pid is 21895 ppid is 21894 and i just created 21896
    child my pid is 21896 ppid is 21895
    father my pid is 21896 ppid is 21895 and i just created 21897
    child my pid is 21897 ppid is 21896
    father my pid is 21897 ppid is 21896 and i just created 21898
    child my pid is 21898 ppid is 21897
    

    The program:

    • starts as pid 21893
    • creates child with pid 21894
    • child 21894 creates child 21895
    • child 21895 creates child 21896
    • child 21896 creates child 21897
    • child 21907 creates child 21898