fork() system call – Operating System

Ankur Kulhari

When a process invokes/calls fork() function, a new process is created which has the same address space.
As the address space is copied => PC (program counter) will also has the same value as old process has. That means the newly created process (also called as child process) will start executing from the instruction, parent process is currently executing => just after the fork().

process creation using fork

Fig. 3: process creation using fork- memory aspect

C program to create a new process using fork() system call
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    int a=5;
	int pid=fork();  //Child process is created and will start executing from this statement
	if(pid>0)      //Process ID of a process is &gt;=0
	{
	    printf("The chid process is not created successfully");
	}
	else if(pid==0)   //fork() returns 0 in the child process
	{
	    printf("Hello I am the child process and a=%d\n",a); // initially a is copied from parent
	    a=a+3;  //This update is made in the child process's address space in the RAM will not reflect in parent process
	    printf("The updated value of a in child is: a=%d\n",a);
	    exit(0);
	}
	else   //fork() returns non-zero value in the parent process
	{
	    printf("Hello I am the parent process and a=%d\n",a);
	    a=a+10;    //This change is done at parent process's address space and will not be affected to child process
	    printf("The updated value of a in child is: a=%d\n",a);
	}
	return 0;
}
output
Hello I am the parent process and a=5
The updated value of a in child is: a=15
Hello I am the child process and a=5
The updated value of a in child is: a=8
Note
  • The order in which parent and child processes will be executed depends on the scheduler i.e. you may get different answer every time you execute same program.
  • fork() returns 0 to the child process.
  • fork() returns process ID of the child process to the parent process. A parent should have information of its child as it is responsible to clear/unallocate the memory space occupied by its children processes.
  • If parent process process terminates before a child process, the address space of child process will remain in memory even after it is terminated/completed.
  • wait() function can be used in parent process, so that it suspends its execution until child process is terminated => parent process will not proceed further after wait() statement until child process executed completely/get terminated.
  • It is always advisable to use wait() in parent process

Where parent process is one which invokes fork() system call, and child process is one which is newly created because of execution of fork().

Use of wait() system call

The calling process’s execution is suspended after wait() until one of its children process is terminated.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    int a=5;
	int pid=fork();  //Child process is created and will start executing from this statement
	if(pid<0)      //Process ID of a process is >=0
	{
	    printf("The chid process is not created successfully");
	}
	else if(pid==0)   //fork() returns 0 in the child process
	{
	    printf("Hello I am the child process and a=%d\n",a); // initially a is copied from parent
	    a=a+3;  //This update is made in the child process's address space in the RAM will not reflect in parent process
	    printf("The updated value of a in child is: a=%d\n",a);
	    exit(1);
	}
	else   //fork() returns non-zero value in the parent process
	{
	    printf("Hello I am the parent process and a=%d\n",a);
	    wait(NULL);
	    a=a+10;    //This change is done at parent process's address space and will not be affected to child process
	    printf("The updated value of a in parent is: a=%d\n",a);
	}
	return 0;
}
output
Hello I am the parent process and a=5
Hello I am the child process and a=5
The updated value of a in child is: a=8
The updated value of a in parent is: a=15  //executed in the last after completion of child process
Process creation using fork() and execlp() system calls
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    int a=5;
	int pid=fork();  //Child process is created and will start executing from this statement
	if(pid<0)      //Process ID of a process is &gt;=0
	{
	    printf("The chid process is not created successfully");
	}
	else if(pid==0)   //fork() returns 0 in the child process
	{
	    printf("Hello I am the child process and a=%d\n",a); // initially a is copied from parent
	    a=a+3;  //This update is made in the child process's address space in the RAM will not reflect in parent process
	    printf("The updated value of a in child is: a=%d\n",a);
	    execlp("/bin/ls","ls",NULL); //loads ls command to address space of calling process
	    exit(1);
	}
	else   //fork() returns non-zero value in the parent process
	{
	    printf("Hello I am the parent process and a=%d\n",a);
	    wait(NULL);
	    a=a+10;    //This change is done at parent process's address space and will not be affected to child process
	    printf("The updated value of a in parent is: a=%d\n",a);
	}
	return 0;
}
output
Hello I am the parent process and a=5
Hello I am the child process and a=5
The updated value of a in child is: a=8
main  main.c    //result of ls.
The updated value of a in parent is: a=15

Comments 2

  1. Pingback: Process Management: Operating Systems - Study Korner

What do you think about the article?

This site uses Akismet to reduce spam. Learn how your comment data is processed.