0% found this document useful (0 votes)
26 views9 pages

OS34

The document describes an experiment to implement and compare different CPU scheduling algorithms: FCFS, SJF, Round Robin, and Priority. It includes the theory behind CPU scheduling and algorithms for each approach. FCFS algorithm calculates waiting times and turnaround times. Round Robin uses time quanta and preemption. Priority scheduling sorts processes by priority then arrival time. The goal is to calculate and compare the waiting times and turnaround times for each algorithm.

Uploaded by

Aakash Singh
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)
26 views9 pages

OS34

The document describes an experiment to implement and compare different CPU scheduling algorithms: FCFS, SJF, Round Robin, and Priority. It includes the theory behind CPU scheduling and algorithms for each approach. FCFS algorithm calculates waiting times and turnaround times. Round Robin uses time quanta and preemption. Priority scheduling sorts processes by priority then arrival time. The goal is to calculate and compare the waiting times and turnaround times for each algorithm.

Uploaded by

Aakash Singh
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/ 9

Experiment No:3

Aim: Implementation of CPU Scheduling algorithm to find waiting time &


turnaround time for the following: -
a) FCFS
b) SJF
c) Round Robin (Preemptive)
d) Priority

Prerequisites: Knowledge about CPU Scheduling.

Theory: In Multiprogramming systems, the Operating system schedules the processes on


the CPU to have the maximum utilization of it and this procedure is called CPU scheduling.
The main task of CPU scheduling is to make sure that whenever the CPU remains idle, the
OS at least select one of the processes available in the ready queue for execution. The
selection process will be carried out by the CPU scheduler. It selects one of the processes in
memory that are ready for execution.

Algorithm for FCFS -


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.

Ayush 37 20272195
3.Round Robin(Preemptive):
Algorithm of Round Robin:
Step 1: Organize all processes according to their arrival time in the ready queue.
The queue structure of the ready queue is based on the FIFO structure to execute
all CPU processes.
Step 2: Now, we push the first process from the ready queue to execute its task
for a fixed time, allocated by each process that arrives in the queue.
Step 3: If the process cannot complete their task within defined time interval or
slots because it is stopped by another process that pushes from the ready queue
to execute their task due to arrival time of the next process is reached. Therefore,
CPU saved the previous state of the process, which helps to resume from the point
where it is interrupted. (If the burst time of the process is left, push the process
end of the ready queue).
Step 4: Similarly, the scheduler selects another process from the ready queue to
execute its tasks. When a process finishes its task within time slots, the process
will not go for further execution because the process's burst time is finished.
Step 5: Similarly, we repeat all the steps to execute the process until the work has
finished.
Source code:
#include<iostream>
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{ int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

Ayush 38 20272195
int t = 0;
while (1)
{ bool done = true;
for (int i = 0 ; i < n; i++)
{ if (rem_bt[i] > 0)
{ done = false;
if (rem_bt[i] > quantum)
{ t += quantum;
rem_bt[i] -= quantum; }
else
{ t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0; }
}
}
if (done == true)
break; }
}
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 quantum)
{ int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes "<< " Burst time "

Ayush 39 20272195
<< " 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];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "<< wt[i] <<"\t\t "
<<tat[i]<<endl; }
cout << "Average waiting time = "<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "<< (float)total_tat / (float)n; }
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
int quantum = 2;
cout<<"Quantum="<<quantum<<"\n";
findavgTime(processes, n, burst_time, quantum);
return 0;
}

Ayush 40 20272195
Output:

Ayush 41 20272195
3. Priority:
1. First input the processes with their arrival time, burst time and priority.
2. First process will schedule, which have the lowest arrival time, if two or
more processes will have lowest arrival time, then whoever has higher priority
will schedule first.
3. Now further processes will be schedule according to the arrival time and
priority of the process. (Here we are assuming that lower the priority number
having higher priority). If two process priority are same then sort according to
process number.
4. Once all the processes have been arrived, we can schedule them based on
their priority.
Source Code:
#include<iostream>
using namespace std;

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:";
cin>>n;

cout<<"\nEnter Burst Time and Priority\n";


for(i=0;i<n;i++)
{
Ayush 42 20272195
cout<<"\nP["<<i+1<<"] : \n";
cout<<"Burst Time: ";
cin>>bt[i];
cout<<"Priority: ";
cin>>pr[i];
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
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;

Ayush 43 20272195
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;

cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];

Ayush 44 20272195
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t"<<bt[i]<<"\t\t\t\t"<<wt[i]<<"\t\t\t\t"<<tat[i];
}

avg_tat=total/n;
cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;

return 0;
}

Output:

Ayush 45 20272195

You might also like