0% found this document useful (0 votes)
16 views20 pages

Os Lab Work

The document outlines the implementation of various CPU scheduling algorithms, including First-Come-First-Serve (FCFS), Non-Preemptive Shortest-Job-First (SJF), and Preemptive Shortest-Remaining-Time First (SRTF). Each algorithm is explained with its theory, advantages, disadvantages, and sample code in C++. Additionally, it includes input/output examples and a set of viva questions related to CPU scheduling.

Uploaded by

akshat1shuklapw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

Os Lab Work

The document outlines the implementation of various CPU scheduling algorithms, including First-Come-First-Serve (FCFS), Non-Preemptive Shortest-Job-First (SJF), and Preemptive Shortest-Remaining-Time First (SRTF). Each algorithm is explained with its theory, advantages, disadvantages, and sample code in C++. Additionally, it includes input/output examples and a set of viva questions related to CPU scheduling.

Uploaded by

akshat1shuklapw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

AIM 1 : Write a program to implement First-Come-First-Serve (FCFS) CPU

Scheduling Algorithm.

THEORY
Other names of this algorithm are:
 First-In-First-Out (FIFO)
 Run-to-Completion
 Run-Until-Done
Perhaps, First-Come-First-Served algorithm is the simplest scheduling algorithm is the simplest
scheduling algorithm. Processes are dispatched according to their arrival time on the ready queue.
Being a non-preemptive discipline, once a process has a CPU, it runs to completion. The FCFS
scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long
jobs make short jobs wait and unimportant jobs make important jobs wait.
FCFS is more predictable than most of other schemes since it offers time. FCFS scheme is not
useful in scheduling interactive users because it cannot guarantee good response time. The code for
FCFS scheduling is simple to write and understand. One of the major drawbacks of this scheme is
that the average time is often quite long.

PROGRAM:
#include<conio.h>
#include<iostream.h>
void main()
{
float i,at[20],bt[20],dt[20],n,wt[20],tat[20],rt[20],avtat,avwt,avrt,ft=0,stat=0,swt=0,srt=0;
clrscr();
cout<<"\nenter the no of processess";
cin>>n;
cout<<"\nenter the information of all processess";
for(i=0;i<n;i++)
{
cout<<"\nenter the burst time of process"<<i+1<<":-";
cin>>bt[i];
}
start:
for(i=0;i<n;i++)
{
cout<<"\nenter the arrival time of process"<<i+1<<":-";
cin>>at[i];
if(at[0]!=0)
{
cout<<"\a\a\n\n\t first arrival time must be zero";
goto start;
}
if(i!=0)
if(at[i]<at[i-1])
{
cout<<"\a\a\n\n\tcheck input \nenter the arrival time in sorted order";
goto start;
}
}
for(i=0;i<n;i++)
{
rt[i]=ft-at[i];
srt=srt+rt[i];
ft=ft+bt[i];
tat[i]=ft-at[i];
stat=stat+tat[i];
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
}
avtat=stat/n;
avwt=swt/n;
avrt=srt/n;
clrscr();
cout<<"gantt chart:-";
cout<<"\n\n0";
ft=0;
for(i=0;i<n;i++)
{
ft=ft+bt[i];
cout<<"=>>p"<<i+1<<"=>>"<<ft;
}
cout<<"\nprocessname Turnaround Time waiting time Response time ";
for(i=0;i<n;i++)
{
cout<<"\n\tp"<<i+1<<"\t\t"<<tat[i]<<"\t\t"<<wt[i]<<"\t\t"<<rt[i];
}
cout<<"\nAverage Turnaround Time:-"<<avtat;
cout<<"\nAverage Waiting time:-"<<avwt;
cout<<"\nAverage Response time:-"<<avrt;
getch();
}
CASE 1 (with same arrival time)
INPUT
enter the no of processess4
enter the information of all processess
enter the burst time of process1:-10
enter the burst time of process2:-6
enter the burst time of process3:-2
enter the burst time of process4:-4
enter the arrival time of process1:-0
enter the arrival time of process2:-0
enter the arrival time of process3:-0
enter the arrival time of process4:-0

CASE 1 (with same arrival time)


OUTPUT
Gantt chart

0=>>p1=>>10=>>p2=>>16=>>p3=>>18=>>p4=>>22
Process name Turnaround Time waiting time Response time
p1 10 0 0
p2 16 10 10
p3 18 16 16
p4 22 18 18
Average Turnaround Time:-16.5
Average Waiting time:-11
Average Response time:-11
CASE 2 (with different arrival time)
INPUT
enter the no of processess4
enter the information of all processes
enter the burst time of process1:-10
enter the burst time of process2:-6
enter the burst time of process3:-2
enter the burst time of process4:-4
enter the arrival time of process1:-0
enter the arrival time of process2:-1
enter the arrival time of process3:-3
enter the arrival time of process4:-5
CASE 2 (with different arrival time)
OUTPUT
Gantt chart
0=>>p1=>>10=>>p2=>>16=>>p3=>>18=>>p4=>>22
processname Turnaround Time waiting time Response time
p1 10 0 0
p2 15 9 9
p3 15 13 13
p4 17 13 13
Average Turnaround Time:-14.25
Average Waiting time:-8.75
Average Response time:-8.75

Viva Questions
Que.1 What do you understand by CPU scheduling?
Que.2 What do you understand by disk and drum scheduling?
Que.3 What is the function of a ready queue?
Que.4 What is preemptive and Non Preemptive Scheduling?
Que5 What do you mean by kernel?
Que.6 What is drawback of FCFS Algorithm?
Que.7 what is the function of handler & a Scheduler?
Que.8 What do you understand by an interrupt?
AIM 2: Write a program to implement Non-Preemptive-Shortest-Job-First
(SJF) CPU Scheduling Algorithm.
THEORY
Other name of this algorithm is Shortest-Process-Next (SPN).
Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process) with
the smallest estimated run-time-to-completion is run next. In other words, when CPU is available, it
is assigned to the process that has smallest next CPU burst.
The SJF scheduling is especially appropriate for batch jobs for which the run times are known
in advance. Since the SJF scheduling algorithm gives the minimum average time for a given set of
processes, it is probably optimal.
The SJF algorithm favors short jobs (or processors) at the expense of longer ones.
The obvious problem with SJF scheme is that it requires precise knowledge of how long a job
or process will run, and this information is not usually available.
The best SJF algorithm can do is to rely on user estimates of run times.
In the production environment where the same jobs run regularly, it may be possible to provide
reasonable estimate of run time, based on the past performance of the process. But in the
development environment users rarely know how their program will execute.
Like FCFS, SJF is non preemptive therefore, it is not useful in timesharing environment in which
reasonable response time must be guaranteed.

PROGRAM:
#include<conio.h>
#include<iostream.h>
struct sjfs
{
int at,bt,dt,wt,tat,rt,pno;
}p[20];
void main()
{
float n,avtat,avwt,avrt,stat,swt,srt,ft=0,min;
int i,k,j;
struct sjfs temp;
clrscr();
cout<<"\nenter the no of processess";
cin>>n;
cout<<"\nenter the information of all processess";
for(i=0;i<n;i++)
{
p[i].pno=i+1;
cout<<"\nenter the burst time of process"<<i+1<<":-";
cin>>p[i].bt;
}
start:
for(i=0;i<n;i++)
{
cout<<"\nenter the arrival time of process"<<i+1<<":-";
cin>>p[i].at;
if(p[0].at!=0)
{
cout<<"\a\a\n\n\t first arrival time must be zero";
goto start;
}
if(i!=0)`
if(p[i].at<p[i-1].at)
{
cout<<"\a\a\n\n\tcheck input \nenter the arrival time in sorted order";
goto start;
}
}
for(i=0;i<n;i++)
{
min=p[i].bt;
for(j=i+1;j<n;j++)
{
if(ft>=p[j].at && min>p[j].bt)
{
min=p[j].bt;
k=j;
}
}
if(min!=p[i].bt)
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
ft=ft+p[i].bt;
}
ft=0;
for(i=0;i<n;i++)
{
p[i].rt=ft-p[i].at;
srt=srt+p[i].rt;
ft=ft+p[i].bt;
p[i].tat=ft-p[i].at;
stat=stat+p[i].tat;
p[i].wt=p[i].tat-p[i].bt;
swt=swt+p[i].wt;
}
avtat=stat/n;
avwt=swt/n;
avrt=srt/n;
clrscr();
cout<<"gantt chart:-";
cout<<"\n\n0";
ft=0;
for(i=0;i<n;i++)
{
ft=ft+p[i].bt;
cout<<"=>>p"<<p[i].pno<<"=>>"<<ft;
}
cout<<"\nprocessname Turnaround Time waiting time Response time ";
for(i=0;i<n;i++)
{
cout<<"\n\tp"<<p[i].pno<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].wt<<"\t\t"<<p[i].rt;
}
cout<<"\nAverage Turnaround Time:-"<<avtat;
cout<<"\nAverage Waiting time:-"<<avwt;
cout<<"\nAverage Response time:-"<<avrt;
getch();
}
CASE 1 (with same arrival time)
INPUT
enter the no of processess4
enter the information of all processes
enter the burst time of process1:-10
enter the burst time of process2:-6
enter the burst time of process3:-2
enter the burst time of process4:-4
enter the arrival time of process1:-0
enter the arrival time of process2:-0
enter the arrival time of process3:-0
enter the arrival time of process4:-0
CASE 1 (with same arrival time)
OUTPUT
Gantt chart:-
0=>>p3=>>2=>>p4=>>6=>>p2=>>12=>>p1=>>22
Process name Turnaround Time waiting time Response time
p3 2 0 0
p4 6 2 2
p2 12 6 6
p1 22 12 12
Average Turnaround Time:-10.5
Average Waiting time:-5
Average Response time:-5
CASE 2 (with different arrival time)
INPUT

enter the no of processess4


enter the information of all processes
enter the burst time of process1:-10
enter the burst time of process2:-6
enter the burst time of process3:-2
enter the burst time of process4:-4
enter the arrival time of process1:-0
enter the arrival time of process2:-1
enter the arrival time of process3:-3
enter the arrival time of process4:-5
CASE 2 (with different arrival time)
OUTPUT
Gantt chart:-
0=>>p1=>>10=>>p3=>>12=>>p4=>>16=>>p2=>>22
processname Turnaround Time waiting time Response time
p1 10 0 0
p3 9 7 7
p4 11 7 7
p2 21 15 15
Average Turnaround Time:-12.75
Average Waiting time:-7.25
Average Response time:-7.25
Viva Questions

Que.1 What do you mean by Scheduling?


Que.2 What do you understand by SJF?
Que.3 What are the disadvantages of SJF?
Que.4 Explain CPU Scheduling criteria?
Que.5 What do you mean by Interrupt Service Routine?
Que.6 Explain the function of an Interrupt Controller?
Que.7 What do you understand by Directory System?
Que.8 Explain the necessity of Scheduling?
AIM 3 : Write a program to implement Preemptive SJF(Shortest-Remaining-Time
First(SRTF)) CPU Scheduling Algorithm.
THEORY
The SRTF is the preemptive counterpart of SJF and useful in time-sharing environment.
In SRTF scheduling, the process with the smallest estimated run-time to completion is run next,
including new arrivals.
In SJF scheme, once a job begin executing, it run to completion.
In SJF scheme, a running process may be preempted by a new arrival process with shortest estimated
run-time.
The algorithm SRTF has higher overhead than its counterpart SJF.
The SRTF must keep track of the elapsed time of the running process and must handle occasional
preemptions.

PROGRAM
#include<conio.h>
#include<iostream.h>
struct sjfs
{
int at,bt,bt1,wt,tat,rt,pno,r1,rf;
}p[20];
void main()
{
float n,avtat,avwt,avrt,stat,swt,srt,ft=0,min;
int i,k,j,f1=0;
struct sjfs temp;
clrscr();
cout<<"\nenter the no of processess";
cin>>n;
cout<<"\nenter the information of all processess";
for(i=0;i<n;i++)
{
p[i].pno=i+1;
cout<<"\nenter the burst time of process"<<i+1<<":-";
cin>>p[i].bt;
p[i].bt1=p[i].bt;
p[i].r1=0;
p[i].rf=0;
}
start:
for(i=0;i<n;i++)
{
cout<<"\nenter the arrival time of process"<<i+1<<":-";
cin>>p[i].at;
if(p[0].at!=0)
{
cout<<"\a\a\n\n\t first arrival time must be zero";
goto start;
}
if(i!=0)
if(p[i].at<p[i-1].at)
{
cout<<"\a\a\n\n\tcheck input \nenter the arrival time in sorted order";
goto start;
}
}
clrscr();
cout<<"gantt chart:-";
cout<<"\n\n0";
while(f1==0)
{
f1=1;
min=100;
for(i=0;i<n;i++)
{
if(ft>=p[i].at && min>p[i].bt && p[i].bt!=0 && p[i].rf==0)
{
min=p[i].bt;
k=i;
}
}
if (p[k].bt!=0)
{
f1=0;
if(p[k].r1==0)
{
p[k].r1=1;
p[k].rt=ft;
srt=srt+p[k].rt;
}
ft=ft+1;
p[k].bt=p[k].bt-1;
if(p[k].bt==0 && p[k].rf==0)
{
p[k].tat=ft-p[k].at;
stat=stat+p[k].tat;
p[k].wt=p[k].tat-p[k].bt1;
swt=swt+p[k].wt;
p[k].rf=1;
}
cout<<"=>>p"<<p[k].pno<<"=>>"<<ft;
}
}
avtat=stat/n;
avwt=swt/n;
avrt=srt/n;
cout<<"\nprocessname Turnaround Time waiting time Response time ";
for(i=0;i<n;i++)
{
cout<<"\n\tp"<<p[i].pno<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].wt<<"\t\t"<<p[i].rt;
}
cout<<"\nAverage Turnaround Time:-"<<avtat;
cout<<"\nAverage Waiting time:-"<<avwt;
cout<<"\nAverage Response time:-"<<avrt;
getch();
}
In this scheme, arrival of small processes will run almost immediately. However, longer jobs have
even longer mean waiting time.

INPUT
enter the no of processess4
enter the information of all processes
enter the burst time of process1:-10
enter the burst time of process2:-6
enter the burst time of process3:-2
enter the burst time of process4:-4
enter the arrival time of process1:-0
enter the arrival time of process2:-1
enter the arrival time of process3:-3
enter the arrival time of process4:-5

OUTPUT

Gantt chart:-
0=>>p1=>>1=>>p2=>>2=>>p2=>>3=>>p3=>>4=>>p3=>>5=>>p2=>>6=>>p2=>>7=>>p2=>>8=>>p2=
>>9=>>p4=>>10=>>p4=>>11=>>p4=>>12=>>p4=>>13=>>p1=>>14=>>p1=>>15=>>p1=>>16=>>p1=
>>17=>>p1=>>18=>>p1=>>19=>>p1=>>20=>>p1=>>21=>>p1=>>22
processname Turnaround Time waiting time Response time
p1 22 12 0
p2 8 2 1
p3 2 0 3
p4 8 4 9
Average Turnaround Time:-10
Average Waiting time:-4.5
Average Response time:-3.25
Viva Questions

Que.1 What is the role of the Device Driver?


Que.2 What do you understand by SRTF?
Que.3 Explain Time Sharing Environment?
Que.4 What do you mean by Sub Modules??
Que.5 What do you mean by I/O Buffering?
Que.6 What do you mean by Real Time Scheduling?
Que.7 Distinguish between SJF and SRTF.
Que.8 What do you understand by Path Management?
AIM.4 : Write a program to implement Non-Preemptive-Priority CPU
Scheduling Algorithm.

THEORY
The basic idea is straightforward: each process is assigned a priority, and priority is allowed to
run. Equal-Priority processes are scheduled
In FCFS order. The shortest-Job-First (SJF) algorithm is a special case of general priority
scheduling algorithm.
An SJF algorithm is simply a priority algorithm where the priority is the inverse of the (predicted)
next CPU burst. That is, the longer the CPU burst, the lower the priority and vice versa.
Priority scheduling can be either preemptive or non preemptive
 A preemptive priority algorithm will preemptive the CPU if the priority of the newly arrival
process is higher than the priority of the currently running process.
 A non-preemptive priority algorithm will simply put the new process at the head of the ready
queue.
A major problem with priority scheduling is indefinite blocking or starvation. A solution to the problem
of indefinite blockage of the low-priority process is aging. Aging is a technique of gradually increasing
the priority of processes that wait in the system for a long period of time.

PROGRAM:
#include<conio.h>
#include<iostream.h>
struct sjfs
{
int at,bt,dt,wt,tat,rt,pno,pr;
}p[20];
void main()
{
float n,avtat,avwt,avrt,stat,swt,srt,ft=0,min;
int i,k,j;
struct sjfs temp;
clrscr();
cout<<"\nenter the no of processess";
cin>>n;
cout<<"\nenter the information of all processess";
for(i=0;i<n;i++)
{
p[i].pno=i+1;
cout<<"\nenter the burst time of process"<<i+1<<":-";
cin>>p[i].bt;
cout<<"\nenter the priorty of process"<<i+1<<":-";
cin>>p[i].pr;
}
start:
for(i=0;i<n;i++)
{
cout<<"\nenter the arrival time of process"<<i+1<<":-";
cin>>p[i].at;
if(p[0].at!=0)
{
cout<<"\a\a\n\n\t first arrival time must be zero";
goto start;
}
if(i!=0)
if(p[i].at<p[i-1].at)
{
cout<<"\a\a\n\n\tcheck input \nenter the arrival time in sorted order";
goto start;
}
}
for(i=0;i<n;i++)
{
min=p[i].pr;
for(j=i+1;j<n;j++)
{
if(ft>=p[j].at && min>p[j].pr)
{
min=p[j].pr;
k=j;
}
}
if(min!=p[i].pr)
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
ft=ft+p[i].bt;
}
ft=0;
for(i=0;i<n;i++)
{
p[i].rt=ft-p[i].at;
srt=srt+p[i].rt;
ft=ft+p[i].bt;
p[i].tat=ft-p[i].at;
stat=stat+p[i].tat;
p[i].wt=p[i].tat-p[i].bt;
swt=swt+p[i].wt;
}
avtat=stat/n;
avwt=swt/n;
avrt=srt/n;
clrscr();
cout<<"grant chart:-";
cout<<"\n\n0";
ft=0;
for(i=0;i<n;i++)
{
ft=ft+p[i].bt;
cout<<"=>>p"<<p[i].pno<<"=>>"<<ft;
}
cout<<"\nprocessname priorty Turnaround Time waiting time Response time ";
for(i=0;i<n;i++)
{
cout<<"\n\tp"<<p[i].pno<<"\t"<<p[i].pr<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].wt<<"\t\t"<<p[i].rt;
}
cout<<"\nAverage Turnaround Time:-"<<avtat;
cout<<"\nAverage Waiting time:-"<<avwt;
cout<<"\nAverage Response time:-"<<avrt;
getch();
}
CASE 1 (with same arrival time)

INPUT

enter the no of processess4


enter the information of all processes
enter the burst time of process1:-10
enter the priority of process1:-3
enter the burst time of process2:-6
enter the priority of process2:-2
enter the burst time of process3:-2
enter the priority of process3:-1
enter the burst time of process4:-4
enter the priority of process4:-0
enter the arrival time of process1:-0
enter the arrival time of process2:-0
enter the arrival time of process3:-0
enter the arrival time of process4:-0

CASE 1 (with same arrival time)


OUTPUT
Grant chart:-

0=>>p4=>>4=>>p3=>>6=>>p2=>>12=>>p1=>>22
processname priorty Turnaround Time waiting time Response time
p4 0 4 0 0
p3 1 6 4 4
p2 2 12 6 6
p1 3 22 12 12
Average Turnaround Time:-11
Average Waiting time:-5.5
Average Response time:-5.5
CASE 2 (with different arrival time)
INPUT
enter the no of processess4
enter the information of all processes
enter the burst time of process1:-10
enter the priority of process1:-3
enter the burst time of process2:-6
enter the priority of process2:-2
enter the burst time of process3:-2
enter the priority of process3:-1
enter the burst time of process4:-4
enter the priority of process4:-0
enter the arrival time of process1:-0
enter the arrival time of process2:-1
enter the arrival time of process3:-3
CASE 2 (with different arrival time)
OUTPUT
Grant chart:-

0=>>p1=>>10=>>p4=>>14=>>p3=>>16=>>p2=>>22
processname priorty Turnaround Time waiting time Response time
p1 3 10 0 0
p4 0 9 5 5
p3 1 13 11 11
p2 2 21 15 15
Average Turnaround Time:-13.25
Average Waiting time:-7.75
Average Response time:-7.75
Viva Questions
Que.1 Explain multiple processor scheduling.
Que.2 What should we do in case of equal priority process?
Que.3 What is major problem with priority scheduling?
Que.4 How is algorithm evaluation done?
Que.5 What do you mean by precedence graph?
Que.6 What are the criteria for Non Preemptive Process Scheduling?
Que.7 What is the use of Grant chart?
Que.8 What do you understand by threads?
AIM 5: Write a program to implement Preemptive Priority CPU Scheduling
Algorithm.

THEORY
A preemptive priority algorithm will preemptive the CPU if the priority of the newly arrival process is
higher than the priority of the currently running process.

PROGRAM:
#include<conio.h>
#include<iostream.h>
struct pri
{
int at,bt,dt,wt,tat,rt,pno,pr,r1,rf,bt1;
}p[20];
void main()
{
float n,avtat,avwt,avrt,stat,swt,srt,ft=0,min;
int i,k,j,f1=0;
struct pri temp;
clrscr();
cout<<"\nenter the no of processess";
cin>>n;
cout<<"\nenter the information of all processess";
for(i=0;i<n;i++)
{
p[i].pno=i+1;
cout<<"\nenter the burst time of process"<<i+1<<":-";
cin>>p[i].bt;
p[i].bt1=p[i].bt;
p[i].r1=0;
p[i].rf=0;
cout<<"\nenter the priorty of process"<<i+1<<":-";
cin>>p[i].pr;
}
start:
for(i=0;i<n;i++)
{
cout<<"\nenter the arrival time of process"<<i+1<<":-";
cin>>p[i].at;
if(p[0].at!=0)
{
cout<<"\a\a\n\n\t first arrival time must be zero";
goto start;
}
if(i!=0)
if(p[i].at<p[i-1].at)
{
cout<<"\a\a\n\n\tcheck input \nenter the arrival time in sorted order";
goto start;
}
}
clrscr();
cout<<"gantt chart:-";
cout<<"\n\n0";
while(f1==0)
{
f1=1;
min=100;
for(i=0;i<n;i++)
{
if(ft>=p[i].at && min>p[i].pr && p[i].bt!=0 && p[i].rf==0)
{
min=p[i].bt;
k=i;
}
}
if (p[k].bt!=0)
{

f1=0;
if(p[k].r1==0)
{
p[k].r1=1;
p[k].rt=ft;
srt=srt+p[k].rt;
}

ft=ft+1;
p[k].bt=p[k].bt-1;
if(p[k].bt==0 && p[k].rf==0)
{
p[k].tat=ft-p[k].at;
stat=stat+p[k].tat;
p[k].wt=p[k].tat-p[k].bt1;
swt=swt+p[k].wt;
p[k].rf=1;
}
cout<<"=>>p"<<p[k].pno<<"=>>"<<ft;
}

}
avtat=stat/n;
avwt=swt/n;
avrt=srt/n;
cout<<"\nprocessname priorty Turnaround Time waiting time Response time ";
for(i=0;i<n;i++)
{
cout<<"\n\tp"<<p[i].pno<<"\t"<<p[i].pr<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].wt<<"\t\t"<<p[i].rt;
}
cout<<"\nAverage Turnaround Time:-"<<avtat;
cout<<"\nAverage Waiting time:-"<<avwt;
cout<<"\nAverage Response time:-"<<avrt;
getch();
}

INPUT
enter the no of processess4
enter the information of all processess
enter the burst time of process1:-10
enter the priority of process1:-3
enter the burst time of process2:-6
enter the priority of process2:-2
enter the burst time of process3:-2
enter the priority of process3:-1
enter the burst time of process4:-4
enter the priority of process4:-0
enter the arrival time of process1:-0
enter the arrival time of process2:-1
enter the arrival time of process3:-3
enter the arrival time of process4:-5

OUTPUT
Gantt chart:-
0=>>p1=>>1=>>p2=>>2=>>p2=>>3=>>p3=>>4=>>p3=>>5=>>p4=>>6=>>p4=>>7=>>p4=>>8=>>p4=
>>9=>>p2=>>10=>>p2=>>11=>>p2=>>12=>>p2=>>13=>>p1=>>14=>>p1=>>15=>>p1=>>16=>>p1=
>>17=>>p1=>>18=>>p1=>>19=>>p1=>>20=>>p1=>>21=>>p1=>>22
processname priorty Turnaround Time waiting time Response time
p1 3 22 12 0
p2 2 12 6 1
p3 1 2 0 3
p4 0 4 0 5
Average Turnaround Time:-10
Average Waiting time:-4.5
Average Response time:-2.25
Viva Questions

Que.1 What is the difference between disk scheduling & process scheduling?
Quw.2 What will you do when priority of the newly arrived process is higher than the priority of the
currently running process?
Que.3 What do you mean by process control blocks?
Que.4 Explain the concept of inter process communication?
Que.5 What is critical section problem?
Que.6 What are the necessary conditions for deadlock to occur?
Que.7 What do you mean by process management?
Que.8 Differentiate between deadlock & starvation?
AIM 6: Write a program to implement Round Robin CPU Scheduling
Algorithm.
THEORY
One of the oldest, simplest, fairest and most widely used algorithm is round robin (RR).
In the round robin scheduling, processes are dispatched in a FIFO manner but are given a
limited amount of CPU time called a time-slice or a quantum.
If a process does not complete before its CPU-time expires, the CPU is preempted and given
to the next process waiting in a queue. The preempted process is then placed at the back of the ready
list.
Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in time-
sharing environments in which the system needs to guarantee reasonable response times for
interactive users.
The only interesting issue with round robin scheme is the length of the quantum. Setting the
quantum too short causes too many context switches and lower the CPU efficiency. On the other
hand, setting the quantum too long may cause poor response time and appoximates FCFS.
In any event, the average waiting time under round robin scheduling is often quite long.
PROGRAM:
#include<conio.h>
#include<iostream.h>
struct sjfs
{
int at,bt,bt1,wt,tat,rt,pno,r1;
}p[20];
void main()
{
float n,avtat,avwt,avrt,stat=0,swt=0,srt=0,ft=0;
int i,qt,f1=0,r1=0;

clrscr();
cout<<"\nenter the no of processess";
cin>>n;
cout<<"\nenter the information of all processess";
for(i=0;i<n;i++)
{
p[i].pno=i+1;
p[i].r1=0;
cout<<"\nenter the burst time of process"<<i+1<<":-";
cin>>p[i].bt;
p[i].bt1=p[i].bt;
}
start:
for(i=0;i<n;i++)
{
cout<<"\nenter the arrival time of process"<<i+1<<":-";
cin>>p[i].at;
if(p[0].at!=0)
{
cout<<"\a\a\n\n\t first arrival time must be zero";
goto start;
}
if(i!=0)
if(p[i].at<p[i-1].at)
{
cout<<"\a\a\n\n\tcheck input \nenter the arrival time in sorted order";
goto start;
}
}
cout<<"enter the time quantum";
cin>>qt;
clrscr();
cout<<"gantt chart:-";
cout<<"\n\n0";
ft=0;
while(f1==0)
{
f1=1;
for(i=0;i<n;i++)
{
if(qt<p[i].bt && p[i].bt!=0 && ft>=p[i].at)
{
f1=0;
if(p[i].r1==0)
{
p[i].r1=1;
p[i].rt=ft;
srt=srt+p[i].rt;
}
ft=ft+qt;
p[i].bt=p[i].bt-qt;
cout<<"=>>p"<<p[i].pno<<"=>>"<<ft;
}
else if(qt>=p[i].bt && p[i].bt!=0 && ft>=p[i].at)
{
f1=0;
if(p[i].r1==0)
{
p[i].r1=1;
p[i].rt=ft;
srt=srt+p[i].rt;
}
ft=ft+p[i].bt;
p[i].bt=0;
p[i].tat=ft-p[i].at;
stat=stat+p[i].tat;
p[i].wt=p[i].tat-p[i].bt1;
swt=swt+p[i].wt;
cout<<"=>>p"<<p[i].pno<<"=>>"<<ft;
}
}
}
avtat=stat/n;
avwt=swt/n;
avrt=srt/n;
cout<<"\nprocessname Turnaround Time waiting time Response time ";
for(i=0;i<n;i++)
{
cout<<"\n\tp"<<p[i].pno<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].wt<<"\t\t"<<p[i].rt;
}
cout<<"\nAverage Turnaround Time:-"<<avtat;
cout<<"\nAverage Waiting time:-"<<avwt;
cout<<"\nAverage Response time:-"<<avrt;
getch();
}

INPUT
enter the no of processess3
enter the information of all processes
enter the burst time of process1:-30
enter the burst time of process2:-6
enter the burst time of process3:-4
enter the arrival time of process1:-0
enter the arrival time of process2:-0
enter the arrival time of process3:-0
enter the time quantum4
OUTPUT
Gantt chart-0=>>p1=>>4=>>p2=>>8=>>p3=>>12=>>p1=>>16=>>p2=>>18=>>p1=>>22=>>p1=>>26
=>>p1=>>30=>>p1=>>34=>>p1=>>38=>>p1=>>40
Process name Turnaround Time waiting time Response time
p1 40 10 0
p2 18 12 4
p3 12 8 8
Average Turnaround Time:-23.333334
Average Waiting time:-10
Average Response time:-4
Viva Questions
Que.1 explain the concept of Round Robin Scheduling?
Que.2 Explain the classical problem of Synchronization?
Que.3 What is Time Slice?
Que.4 Is there any disadvantage of Round Robin Scheduling?
Que.5 What is the use of quantum?
Que6 What will happen in case if quantum too long?
Que.7 What is Semaphores?
Que.8 What do you mean by Deadlock Avoidance?

You might also like