Hey i want to know what will be output of following code:-
main()
{
fork();
fork();
fork();
printf("hello world");
}
I think it should print hello world 4 times.
Plz help me.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It will print it 2^3=8 times. Remember, every time you call fork, you are creating a child process, that child process will continue the execution from right after it got forked so it can itself also fork. The tree will look like this.
Forked4—Forked5 ————– Forked6
Forked7
So we will have total of 8 process (main process and 7 forked processes)running and printing the print statement.
As a side note: if the print statement was before a fork, it would not get executed by newly forked process.