Study Korner

fork() system call – Operating System

fork

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().

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

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
Exit mobile version