8 processes containing 3 parents and 2 of them raise 3 children in C

Asked

Viewed 41 times

0

I’m having a hard time solving an exercise. I have to do a program that manages 3 parent lawsuits that in turn, two of them give rise to the creation of 3 child lawsuits. And in total, I have to generate 8 processes.

My problem is that I’m generating 9 processes and I can’t figure out why. Can you help me try to see the light? :)

Thank you!


    #include <stdio.h> 
    #include <sys/types.h> 
    #include <unistd.h> 
    
    int main(){
        int x = 2;
        pid_t p = fork(); 
        
        if (p == 0) {
            pid_t w = fork();
            x--;
        }  pid_t t = fork();
             if (t == 0){
                
            x+=3;
        }   else {
            pid_t s = fork(); 
    
        }      
        
        printf("x = %d -> PID = %d -> pai: %d \n", x, getpid(), getppid()); 
    
        sleep(1);
        return 0;
    }

  • 1

    By its description and by my accounts the total of processes is really 9: 3 parent processes: P1, P2 and P3 and 6 child processes: P1.1, P1.2, P1.3, P2.1, P2.2 and P2.3.

  • Exactly. And I need to have eight lawsuits in total, one parent who generates three children, one of the children who generates three more children and one child of child or child, to make three parents. But I’m already very confused with what I’ve done.

  • So I understood it should be: Processes: 1, 1.1, 1.2, 1.3, 1.1.1, 1.1.2, 1.1.3 and 1.1.1 Total 8 processes being 3 parents: 1 (father of 1.1), 1.1 (father of 1.1.1) and 1.1 (father of 1.1.1). Cases 1 and 1.1 have 3 children each.

  • I can’t understand it. There are no parent processes or child processes. Every program is a process. Every process is a child of at least init .So your program is a process. It will generate 3 processes. Of these 2 will generate 3 children. So there are ten lawsuits, nine of them children of your program, six of them without lawsuits son

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.