When a process executes a fork command it’s whole heap, stack and data is duplicated into the child process, it created.

Things to note

  • The Program Counter value is also copied into the child process. Thus, the program execution is resumed from where the parent issued the fork command
  • The fork command returns the parent (the process calling it) the child id and to child an ID of
OriginFork Command
Child ProcessReturn 0
Parent ProcessReturns > 0
Error While CreatingReturns -1

Examples - Multiple Forks

int main()
{
	fork();
	printf("Hello World!\n");
	return 0;
}
 
// Ouput
Hello World // by Child Process
Hello World // by Parent Process
- Parent
	- Child at 4
		- Hello World
	- Hello World
int main()
{
	fork();
	fork();
	printf("Hello World!\n");
	return 0;
}
 
// Ouput
Hello World // by Child 1.1 Process
Hello World // by Child 1 Process
Hello World // by Child 2 Process
Hello World // by Parent Process
- Parent
	- Child 1 at 3
		- Child 1.1 at 4
			- Hello World
		- Hello World
	- Child 2 at 4
		- Hello World
	- Hello World
int main()
{
	fork();
	fork();
	fork();
	printf("Hello World!\n");
	return 0;
}
- Parent
	- Child 1 at 1
		- Child 1.1 at 2
			- Child 1.1.1 at 3
				- Hello World
			- Hello World
		- Child 1.2 at 3
			- Hello World
		- Hello World
	- Child 2 at 2
		- Child 2.1 at 3
			- Hello World
		- Hello World
	- Child 3 at 3
		- Hello World
	- Hello World