What would be the output of the following program ?main (){printf ("welcome to the world");fork();}how many times "welcome to the world" message will print ?

Showing Answers 1 - 5 of 5 Answers

dineshraop

  • Aug 31st, 2006
 

The following statement
printf ("welcome to the world");
will be printed twice.

Once it will be executed by the Parent PID later by the child PID after the system call fork.

  Was this answer useful?  Yes

vikas

  • Sep 11th, 2006
 

it ll keep on printing the given string (welcome to the world").. because once parent executes the printf.. then fork creats a separate task..which calls printf as well as creates one more child. This way, innumerable child process are created, which call printf()

  Was this answer useful?  Yes

jay

  • Sep 23rd, 2006
 

main (){ printf ("welcome to the world"); fork();}will output : "welcome to the world"that's it.When you use fork, you separate current process(thread) into two subprocesses from the point where fork() was called.If however you do : main (){ fork(); printf ("welcome to the world");}you'll end up with two : welcome to the world's

wizard

  • Feb 27th, 2007
 

it will print one time only, bzc after fork there is no printf statments. the printf statement is before fork so for parent it will print.

The fork() system call will spawn a new child process which is an identical process to the parent except that has a new system process ID.  Hence

printf ("welcome to the world");

this statement execute infinite time

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions