1

so I am attempting to solve a problem in my book regarding the fork system call in C.

Here is the following code:

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

int main() {  

int i = 1;

   if (fork ()) //parent process will return PID of child.
        i++;
   else if (fork()) //child process (becomes parent)
            i--;
       else //grandchild process returns 0
            i++;

 printf("%d\n", i);
}

After going through the code, I got 2 0 2 as the solution. I am confused though since I don't know which order is correct? Can the child processes get printed out before the parent does? If so, another viable solution could be 0 2 2 or 2 2 0. How do I know if all the conditional statements will be executed? I know in the first conditional, the if statement will be executed and will be the parent process (which will return the process ID of the child, and the value of i will increment to 2). How would I know if the else if and elsecode will be executed? Sorry, I am just confused and trying to wrap my head around this as this is a new topic for me.

Any help would be greatly appreciated. Thank you in advance.

3
  • I would suggest adding comments to the code to preserve your understanding. For example, inside the first if (fork ()) block, you can add a comment "first fork returned non-zero, this is the parent". And continue through the whole program. Commented Sep 20, 2017 at 2:08
  • Made the edits, still looking for some further detailed assistance, thanks. Commented Sep 20, 2017 at 2:22
  • This code was asked about, presumably by someone else, in the last couple of weeks. Commented Sep 20, 2017 at 2:23

1 Answer 1

2

The child process can be printed before the parent finishes. Remember, in the parent process, the call to fork() returns a nonzero number, while in the child it will return 0.

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

int main() {  

int i = 1;

   if (fork ())
        i++;
   else if (fork())
            i--;
       else
            i++;

 printf("%d\n", i);
}

For the above code, the parent will execute the first if() statement, the child will execute the else if statement and the grandchild will execute the else statement. The child/grandchild/parent can be printed in any order. So, 2 0 2, 2 2 0 and 0 2 2 are all viable solutions.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi @JohnDoe thanks for your reply. So in the ` else if ` statement, is that process the child process or parent?
@Derek : it is the child of the initial process and the parent of the final process.
Thanks for your reply. Also, I just want to confirm how we know that ALL conditional statements will be executed?
We know that all conditonal statements will be executed because the parent will always execute the if(), the child will always execute the else if() and the grandchild will always execute the else.
@JohnDoe how would I determine how many processes are created in total in this code? I know the general formula for total # of processes is 2^n where n is the number of fork system calls.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.