Tuesday, September 13, 2011

Predict the output of the following program code.


main()
{
fork(); fork(); fork();
printf("Hello World!");
}

Answer : "Hello World" will be printed 8 times.

Explanation:
2^n times where n is the number of calls to fork()
The fork creates a child that is a duplicate of the parent process. The child begins from the fork().All the statements after the call to fork() will be executed twice.(once by the parent process and other by child). The statement before fork() is executed only by the parent process.

No comments :