Doubt with fork system call ()

0

I have read about the operation of the call fork () and so far I have understood that it makes a copy of the process that is running and through a fork it is possible to know if we are in the process father or son as in the next example.

Now that I understand how this function works, I am trying to make a program that creates two children processes in the following way

According to my logic, the output of this program should be: "I'm in the child 1" "I'm in the child 2" However, the program does not always print that on the screen. Sometimes it prints only "I'm in the child 1" And I would like to know why this happens? If, according to me, the parent process creates a child process and in the part of the where the father code should go, I create another process (child 2) and this should print its message without problems. Can anyone help me understand why this happens, please?

    
asked by Leonardo Herrera Ornelas 03.03.2018 в 17:55
source

1 answer

0

I respond to myself with the answer, I comment, I'm using code blocks as IDE and I had not noticed this: It turns out that the message "I'm in the child 2" If it appears, it's just that as they commented before, the order of the on-screen printing is not always the same, so when I thought that the second message was not printed, it turns out I just was not seeing it

    
answered by 03.03.2018 в 19:34