0

I'm just starting to fork() and I'm having some difficulties understanding the parallel execution. I've found this example code and I want to know if the first time it will go true or false (I know if pid1==0 it means it's a child, etc). I also want to know how many copies (children will be created) and some details on the general execution.

I have tried to run it and added the return 0; (my original source didn't have it) just to see if exits... but as you can see it "waits"

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

int main(void)
{
    int pid1, pid2, pid3, pid4;
    pid1=fork();
    if (pid1!=0) 
    { 
        pid2=fork(); 
        pid3=fork(); 
        printf("\t\t IF(TRUE) pid1=%d and pid2=%d and pid3=%d\n",
               pid1, pid2, pid3);
    }
    else
    {
        pid4=fork();  
        printf("\nIF(False) FATHER is talking with pid1=%d and pid4=%d\n",
               pid1, pid4);
    }
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Peter
  • 1
  • 4
  • Could you just add your screenshot code to your code? Also, include the output prepending with `>` for formatting. That would help the readability. Thanks. – Rick Smith May 08 '15 at 16:54
  • Note that the program isn't hung; the shell spat out its prompt and then the various other processes continued printing too, before they exited. Your shell is waiting for you to tell it what to do next! You could add a wait() loop so that processes do not exit until all their children have exited: `int corpse, status; while ((corpse = wait(&status)) != -1) printf("Child %d exited 0x%.4X\n", corpse, status);` – Jonathan Leffler May 08 '15 at 23:14
  • Although it is not exactly what you are looking for, but I think [this Q&A about forked processes](http://stackoverflow.com/q/16400820/2034041) might help. – Varaquilex May 11 '15 at 16:00

2 Answers2

0

Both true and false will be used on the first time.

fork() copies the program (creates 1 child) and both programs continue execution from that point. The child process will take one branch and the parent the other.

The number of fork()s is as follows:

You start with 1 process

After pid1 -> +1 processes

Parent Branch

After pid4 -> +1 processes

Child Branch

After pid2 -> +1 processes

BOTH of the newly created processes run fork() for pid3, so after pid3 -> +2 processes

You get 5 children + the original process.

Rick Smith
  • 9,031
  • 15
  • 81
  • 85
  • i tryed to "simulated" the programm execution and i think it wil create 3 childs, plus one of the child will make another, i really can't see why it will make 2^14, i think each fork creates a child plus the father, – Peter May 10 '15 at 09:33
  • my thought process: the first fork() will make child1 + father, now the child1 will go FALSE on the if() and it will create a grandchild, now the father will go on TRUE and will make two more childs i'm i wrong? – Peter May 10 '15 at 09:40
  • Sorry about that, the edit view doesn't show the question and I remembered the code wrong. See my updated answer. – Rick Smith May 11 '15 at 15:37
0

This program creates five descendant processes, and makes six calls to printf, of which four will be the IF(TRUE) message and two will be IF(FALSE). Here is an ASCII-art diagram of the control flow; every time it branches, both sides are executed, with the parent going straight down and the child to the right. The numbers are the fork calls initializing the pid1, pid2, ... variables, the letters T and F are the IF(TRUE) and IF(FALSE) messages, and the _ is the return 0 at the end of the function. This program does not wait for anything; all control flow paths reach the return 0 eventually.

|
1
|\------\
2       4
|\--\   |\
3   3   | |
|\  |\  | |
T T T T F F
| | | | | |
_ _ _ _ _ _

The output messages may appear in any order. And it's possible that the topmost parent process will exit (returning control to the shell) before all of the descendant processes have written their messages, so (as is visible in your screeenshot) the shell prompt may get jumbled up with the messages, too.

Note that, going by the text of the messages, you have your if conditional backward: pid1 != 0 is true in the parent, not the child.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • i asked one of my teacher's today and you are right, each fork() is giving to the child the source code from that specific line and below from where it whas called, and i remember drawing the tree like you have, thanks for your help! – Peter May 11 '15 at 16:51