Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Use of Fork( ) :

fork ( ) :
fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent. Characteristics of Fork ( ) : Different characteristics of fork() can be determined by considering the output of following programs:

Source code :
#include<stdio.h> int main(){ fork(); printf("p1"); printf("p2"); }

Output :
p1 p2 p1 p2

Source code :
#include<stdio.h> int main(){ printf("p1"); fork();

printf("p2"); }

Output :
p1 p2 p1 p2

Source code :
#include<stdio.h> int main(){ printf("p1"); printf("p2"); fork(); }

Output :
p1 p2 p1 p2

Decision :
By considering these source codes & outputs it can be said that fork() copies the entire calling (parent) process irrespective of the position of fork() in the program.

Source code :
#include<stdio.h> int main(){ fork(); printf("p1"); printf("p2");

fork(); }

Output :
p1 p2 p1 p2 p1 p2 p1 p2

Source code :
#include<stdio.h> int main(){ fork(); printf("p1"); fork(); printf("p2"); fork(); }

Output :
p1 p2 p1 p2 p1 p2 p1 p2 p1 p2 p1 p2 p1 p2 p1 p2

Decision :
If we use N number of fork() in the program then we will get 2N copies of original output(output of parent process [For this particular program it is: p1 p2]). Execution sequence ( Parent & Child ) :

Source code :
#include<stdio.h> int main(){ int pid; pid=fork();

if(pid==0){ printf("I'm child; my id is %d ",getpid()); printf("& My parent id is %d ",getppid()); } else{ printf("I'm parent; my id is %d ",getpid()); printf("& My parent id is %d ",getppid()); } }

Output :
I'm parent; my id is 2275 & My parent id is 2214 my id is 2276 & My parent id is 2275 I'm child;

Decision :
This program indicates that the parent process starts execution before the child process and the childs parent process ID is the same as the parents process ID. Sequence of execution : Parent > child1 > child2 > child2 > child1 > Parent

Orphan Process : When the parent process is compelled to finish execution before the child process then the childs parent process ID becomes 1. Then the child process is called an orphan process. For example :

Source code :
#include<stdio.h>

int main(){ int pid; pid=fork(); if(pid==0){ printf("I'm child; my id is %d ",getpid()); printf("& My parent id is %d ",getppid()); sleep(30); printf("After awaking my id is %d ",getpid()); printf("& my parent id is %d ",getppid()); } else{ printf("I'm parent; my id is %d ",getpid()); printf("& My parent id is %d ",getppid()); sleep(10); printf("After awaking my id is %d ",getpid()); printf("& my parent id is %d ",getppid()); } }

Output :
(10 sec later :) I'm parent; my id is 2358 & My parent id is 2214 After awaking my id is 2358 & my parent id is 2214 (30 sec later :) I'm child; my id is 2359 & My parent id is 2358 After awaking my id is 2359 & my parent id is 1

Decision :
Since sleep(30) function makes the child inactive for 30 seconds but the parent process finishes execution after 10 seconds so after

30 seconds the childs parent process ID becomes 1. So the child process can be called an orphan process.

You might also like