Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fork() start of execution

I am pretty new to unix programming and came across something I do not understand.
Here it is the following code snippet:

#include <stdio.h>
#include <sys/types.h>

int main()
{
     printf("%d ", fork());
     return 0;
}

The output is: 9298 0.
Why does the child process call this printf ? At my course I was told that it executes everything after the fork() call. What am I getting wrong ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The result of fork is passed to printf so it is executed before printf. Your code is equivalent to:

#include <stdio.h>
#include <sys/types.h>

int main()
{
     pid_t pid = fork();
     printf("%d ", pid);
     return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading