Term work
on
Operating Systems
(PCS 502)
2023-24
Submitted to: Submitted by:
Arun Uniyal
Department of Computer University Roll. No.: 2118319
Science and Engineering Class Roll. No./Section: 17/C
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
GRAPHIC ERA HILL UNIVERSITY, DEHRADUN
1|Page
ACKNOWLEDGMENT
I would like to particularly thank my Operating Systems Lab Faculty Dr.
Pradeep for his patience, support and encouragement throughout the completion
of this Term work.
At last but not the least I greatly indebted to all other persons who directly
or indirectly helped me during this course.
Arun Uniyal
University. Roll No.-2118319
B.Tech CSE-C-V Sem
Session: 2023-24
GEHU, Dehradun
2|Page
Table of Contents
Program Program Name Page No
No.
1 C Program to demonstrate the working of fork() system call
2 C Program in which Parent Process Computes the SUM OF
EVEN NUMBERS and Child Process Computes the sum of ODD
NUMBERS stored in array using fork () . First the child process
should print its answer i.e sum of odd numbers, then parent
should print its answer, i.e sum of even numbers.
3 C program to Implement the Orphan Process and Zombie
Process.
4 C program to Implement FCFS CPU Scheduling Algorithm
5 C program to implement SRTF algorithm.
6 C program to Implement Round Robin CPU scheduling algo.
7 C program to Implement Preemptive Priority CPU scheduling algo
8 C program to Implement Interprocess Communication using PIPE
9 C program to Implement Interprocess Communication using
shared memory
10 C program to demonstrate working of execl() where parent
process executes "ls" command and child process executes
"date" command
11
C program to Implement Banker’s Algo for Deadlock Avoidance to
check for the safe and unsafe state.
12
C program to Implement First In First Out page replacement
policy
13
C program to Implement Least recently used page replacement
3|Page
policy
14
C program to Implement FCFS Disk Scheduling Algorithm
15.
C program to Implement SCAN Disk Scheduling Algorithm
4|Page
Program 1
Q. Write a C program to demonstrate the use of fork() System call
fork() :
It is a system call in Unix based system. (Write 5-6 lines for its description)
Fork system call is used for creating a new process, which is called child
process, which runs concurrently with the process that makes the fork() call
(parent process). After a new child process is created, both processes will
execute the next instruction following the fork() system call. A child
process uses the same pc(program counter), same CPU registers,same open
files which use in the parent process.
It takes no parameters and returns an integer value.
SOURCE CODE:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int p;
p=fork();
if(p<0){
printf("Error occured\n");
exit(1);
}
else if(p==0){
printf("I am child\t%d\n", getpid());
printf("%d\n",p);
//exit(1);
}
else{
printf("I am parent\t%d\n", getpid());
printf("%d\n",p);
//exit(1);
}
return 0;
}
5|Page
Output
6|Page
Practical 2
Q. C Program in which Parent Process Computes the SUM OF EVEN
NUMBERS and Child Process Computes the sum of ODD NUMBERS stored in
array using fork ().
First the child process should print its answer i.e., sum of odd numbers, then
parent should print its answer, i.e., sum of even numbers.
When fork() is called, it returns a value. If the value is greater than 0, then
currently it is in parent process, otherwise it is in child process. So using
this we can distinguish between theprocesses.
SOURCE CODE:
#include <stdio.h> #include <sys/types.h> #include <unistd.h>
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sumOdd = 0, sumEven = 0, i;
pid_t n = fork();
printf("%d\n",n); if (n >0){
for (i = 0; i< 10; i++) { if (a[i] % 2 == 0)
sumEven = sumEven + a[i];
}
printf("Sum of even no. is %d", sumEven);
}
else{
for (i = 0; i< 10; i++){ if (a[i] % 2 != 0)
sumOdd = sumOdd + a[i];
}
printf("\nSum of odd no. is %d", sumOdd);
}
return 0;
}
7|Page
OUTPUT :
8|Page
Practical 03
Q. C program to Implement the Orphan Process and Zombie Process.
Zombie Process: Zombie process is also known as "dead" process. Ideally when a
process completes its execution, it's entry from the process table should be removed
but this does not happen in case of zombie a process.
Orphan Process: a process which is executing (is alive) but it's parent process has
terminated (dead) is called an orphan process.
SOURCE CODE: (Orphan Process)
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t p;
p=fork();
if(p==0) //child
{
sleep(5);
printf("I am child having PID %d\n",getpid());
printf("My parent PID is %d\n",getppid());
}
else //parent
{
printf("I am parent having PID %d\n",getpid());
printf("My child PID is %d\n",p);
}
return 0;
}
9|Page
SOURCE CODE: (Zombie Process)
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int pid=fork();
if(pid==0)
{
printf("Child process id: %d has Parent id: %d\n",getpid(),getppid());
}
else if(pid>0)
{
wait(NULL);
sleep(5);
printf("Parent process id: %d has Grand Parent id:
%d\n",getpid(),getppid());
}
else
{
printf("Process not created");
}
return 0;
}
10 | P a g e
OUTPUT :
(Orphan Process)
(Zombie Process)
11 | P a g e
Practical 04
Q. C program to Implement FCFS CPU Scheduling Algorithm.
Algorithm:
1- Input the processes along with their burst time (bt).
2- Find waiting time (wt) for all processes.
3- As first process that comes need not to wait so
waiting time for process 1 will be 0 i.e. wt[0] = 0.
4- Find waiting time for all other processes i.e. for
process i ->
wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time
for all processes.
6- Find average waiting time =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.
SOURCE CODE:
#include<stdio.h>
void findWaitingTime(int processes[], int n,int bt[], int wt[])
{
wt[0] = 0;
for (int i = 1; i < n ; i++ ) wt[i] = bt[i-1] + wt[i-1] ;
}
void findTurnAroundTime( int processes[], int n, int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
12 | P a g e
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst time Waiting time Turn around time\n");
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT:
13 | P a g e
Practical 05
Q. C program to implement SRTF algorithm.
ALGORITHM:
At the 0th unit of the CPU, there is only one process that is P1, so P1 gets
executed for the 1 time unit.
At the 1st unit of the CPU, Process P2 arrives. Now, the P1 needs 6 more
units more to be executed, and the P2 needs only 3 units. So, P2 is executed
first by preempting P1.
At the 3rd unit of time, the process P3 arrives, and the burst time of P3 is 4
units which is more than the completion time of P2 that is 1 unit, so P2
continues its execution.
Now after the completion of P2, the burst time of P3 is 4 units that means it
needs only 4 units for completion while P1 needs 6 units for completion.
So, this algorithm picks P3 above P1 due to the reason that the completion
time of P3 is less than that of P1
P3 gets completed at time unit 8, there are no new processes arrived.
So again, P1 is sent for execution, and it gets completed at the 14th unit.
As Arrival Time and Burst time for three processes P1, P2, P3 are given in the above
diagram. Let us calculate Turn around time, completion time, and waiting time.
SOURCE CODE:
#include<stdio.h>
int main()
{
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter at for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter bt for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
14 | P a g e
printf("\n\nProcess\t Turnaround Time Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && rt[i])
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\nP[%d]\t \t%d\t \t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
return 0;
}
OUTPUT :
15 | P a g e
Practical 06
Q. C program to Implement Round Robin CPU scheduling algorithm.
ALGORITHM :
Step 1) The execution begins with process P1, which has burst time 4. Here, every
process
executes for 2 seconds. P2 and P3 are still in the waiting queue.
Step 2) At time =2, P1 is added to the end of the Queue and P2 starts executing
Step 3) At time=4 , P2 is preempted and add at the end of the queue. P3 starts
executing.
Step 4) At time=6 , P3 is preempted and add at the end of the queue. P1 starts
executing.
Step 5) At time=8 , P1 has a burst time of 4. It has completed execution. P2 starts
execution
Step 6) P2 has a burst time of 3. It has already executed for 2 interval. At time=9,
P2 completes execution. Then, P3 starts execution till it completes.
SOURCE CODE:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
16 | P a g e
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
17 | P a g e
OUTPUT :
18 | P a g e
Practical 07
Q. C program to Implement Preemptive Priority CPU scheduling algorithm.
ALGORITHM:
Step-1: Select the first process whose arrival time will be 0, we need to select
that process because that process is only executing at time t=0.
Step-2: Check the priority of the next available process. Here we need to
check for 3 conditions.
if priority(current_process) > priority(prior_process) :- then execute the current
process.
if priority(current_process) < priority(prior_process) :- then execute the prior
process.
if priority(current_process) = priority(prior_process) :- then execute the process
which arrives first i.e., arrival time should be first.
Step-3: Repeat Step-2 until it reaches the final process.
Step-4: When it reaches the final process, choose the process which is having
the highest priority & execute it. Repeat the same step until all processes
complete their execution.
SOURCE CODE:
#include<stdio.h>
int main(){
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n"); for(i=0;i<n;i++)
printf("\nP[%d]\n",i+1); printf("Burst Time:"); scanf("%d",&bt[i]); printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
pos=i; for(j=i+1;j<n;j++)
19 | P a g e
{
if(pr[j]<pr[pos]) pos=j;
temp=pr[i]; pr[i]=pr[pos]; pr[pos]=temp;
temp=bt[i]; bt[i]=bt[pos]; bt[pos]=temp;
temp=p[i];
p[i]=p[pos]; p[pos]=temp;
wt[0]=0;
for(i=1;i<n;i++)
wt[i]=0; for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
avg_wt=total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;i++)
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);}
avg_tat=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
20 | P a g e
}
OUTPUT :
21 | P a g e