Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/forums/unix-forum/)
-   -   Fork() - help me understand (http://www.go4expert.com/forums/fork-help-understand-t20622/)

neo_vi 7Jan2010 19:55

Fork() - help me understand
 
Fork
Definition of wikipedia says that

"The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process, though if copy-on-write semantics are implemented actual physical memory may not be assigned (i.e., both processes may share the same physical memory segments for a while). Both the parent and child processes possess the same code segments, but execute independently of each other."

And also in Let us C book, by Yashvant kanetkar it is mentioned as
"Fork doesn't mean that the child process contains the data and code below the fork() call."

Then why does this code prints only the statements below the fork twice compared to printing all the statements twice.
Code:

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

int main()
{
printf("Before forking\n");
fork();
printf("After forking\n");
}


venami 8Jan2010 07:18

Re: Fork() - help me understand
 
What is the compiler that you used and in which Operating System did you run this program?

venami 8Jan2010 07:39

Re: Fork() - help me understand
 
According to my understanding, this is what might have happened. Since the fork()ed child process shares the same memory segment, standard file streams(STDIN, STDERR, STDOUT) and the printf() writes the content to the STDOUT buffer. The child process again prints it since it has the copy of the content written by its parent in the STDOUT buffer.

Could you let me know what happens when running the program by issuing flush() of STDOUT after every printf() statements.

neo_vi 8Jan2010 11:02

Re: Fork() - help me understand
 
Quote:

Originally Posted by venami (Post 62816)
What is the compiler that you used and in which Operating System did you run this program?

Ubuntu 9.04, GCC

neo_vi 8Jan2010 11:16

Re: Fork() - help me understand
 
Quote:

Originally Posted by venami (Post 62817)
According to my understanding, this is what might have happened. Since the fork()ed child process shares the same memory segment, standard file streams(STDIN, STDERR, STDOUT) and the printf() writes the content to the STDOUT buffer. The child process again prints it since it has the copy of the content written by its parent in the STDOUT buffer.

Could you let me know what happens when running the program by issuing flush() of STDOUT after every printf() statements.

So the code will become like this.
Code:

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

int main()
{
printf("Before forking\n");
fflush(stdout);
fork();
printf("After forking\n");
fflush(stdout);
}

Again i got the same output
Code:

Before forking
After forking
After forking


venami 9Jan2010 10:05

Re: Fork() - help me understand
 
When the code is executed, the first printf() is executed once before another process is created using fork(). After fork(), the new process gets the copy of the code part after fork(). So each process(parent and child) executes the second printf() once each.

This is how a fork()-ed process works.

venami 9Jan2010 10:07

Re: Fork() - help me understand
 
Previously I thought the reason as something else and posted. But after seeing this link, I came to know the behaviour of fork(). Hope this might help you :).

shankar.489 25Aug2010 12:21

Re: Fork() - help me understand
 
#include<stdio.h>
#include<sys/types.h>

int main()
{
printf("Before forking\n");
if(fork()==0)
printf("After forking\n");
}
try this for ur required output.
note:If successful, fork() returns 0 in the child process, and returns the process ID of the child process to the parent process.
and the remaining analyzy i left to you...good luck

teacher 15Apr2011 16:55

Re: Fork() - help me understand
 
yes it is correct that fork() makes the image of parent process but it is also correct that the execution of child process starts after the fork call....
fork() returns two values 0 in the child process and pid in the parent process..
Code:

1.int main(){
2.printf("before fork");
3.fork();
4.printf("after fork");
5.}

upto line 3 only parent process was executing
after line 3 both parent and child process are executing
line 1 to 3 executed only by parent
line 3 to 5 executed by both child and parent.


All times are GMT +5.5. The time now is 00:19.