Fork() - help me understand

neo_vi's Avatar, Join Date: Feb 2008
Invasive contributor
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's Avatar, Join Date: Dec 2008
Ambitious contributor
What is the compiler that you used and in which Operating System did you run this program?
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
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's Avatar, Join Date: Feb 2008
Invasive contributor
Quote:
Originally Posted by venami View Post
What is the compiler that you used and in which Operating System did you run this program?
Ubuntu 9.04, GCC
neo_vi's Avatar, Join Date: Feb 2008
Invasive contributor
Quote:
Originally Posted by venami View Post
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's Avatar, Join Date: Dec 2008
Ambitious contributor
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's Avatar, Join Date: Dec 2008
Ambitious contributor
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's Avatar, Join Date: Aug 2010
Go4Expert Member
#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's Avatar, Join Date: Mar 2011
Contributor
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.