Possible Duplicate:
working of fork in c language
I have a very simple program that i am trying to understand fork() Now, in my program is fork() copying the whole program everytime it encounters fork() or the line above(as the parent)?
I am getting weird results which is making it harder to understand.
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout << "Ha! " << endl;
fork();
cout << "Ho! " << endl;
fork();
cout << "He! " << endl;
}
output:
apple.seed@wint:~$ ./program1
Ha!
Ho!
He!
Ho!
He!
apple.seed@wint:~$ He!
He!
At the fork() point, the operating system will create a new process that is exactly the same as the parent process(whatever that may be?). This means all the state that was talked about previously is copied, including open files, register state and all memory allocations, which includes the program code.(so when the program reaches fork() the whole program is copied?)