0% found this document useful (0 votes)
11 views

OS PracticeLab SchedulingAlgorithms

The document describes algorithms and programs for implementing three CPU scheduling algorithms: first come first serve, priority, and round robin. It includes the aims, algorithms, programs, and sample outputs for each scheduling algorithm. The first come first serve algorithm calculates waiting times and average waiting/turnaround times. The priority algorithm rearranges processes by priority before calculating times. The round robin algorithm uses a time slice to rotate between processes and calculates waiting/turnaround times.

Uploaded by

f2021266175
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

OS PracticeLab SchedulingAlgorithms

The document describes algorithms and programs for implementing three CPU scheduling algorithms: first come first serve, priority, and round robin. It includes the aims, algorithms, programs, and sample outputs for each scheduling algorithm. The first come first serve algorithm calculates waiting times and average waiting/turnaround times. The priority algorithm rearranges processes by priority before calculating times. The round robin algorithm uses a time slice to rotate between processes and calculates waiting/turnaround times.

Uploaded by

f2021266175
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

OPERATING SYSTEM

PRACTICE LAB
SCHEDULING ALGORITHMS

School of Science and


Technology

UMT Lahore Pakistan


Lab Manual
First Come First Serve Scheduling
AIM:
To write the program to implement CPU & scheduling algorithm for first come first serve
scheduling.
ALGORITHM:
1. Start the program.
2. Get the number of processes and their burst time.
3. Initialize the waiting time for process 1 to 0.
4. Process for(i=2;i<=n;i++),wt.p[i]=p[i-1]+bt.p[i-1].
5. The waiting time of all the processes is summed then average value time is calculated.
6. The waiting time of each process and average times are displayed
7. Stop the program
PROGRAM:
#include <iostream>
#include <iomanip>
using namespace std;
struct Process{
int pid;
int bt;
int st;
int tt;
};

int main ()
{
Process p[10];
// nop = number of process
//twt = total wait time, ttt = total turnaround time
int nop;
double twt=0, ttt=0;
cout << "Enter number of process: ";
cin >> nop;
for (int i = 0 ; i<nop; i++)
{
p[i].pid = i;
cout << "Enter the burst time of process : " << i <<endl;
cin >> p[i].bt;
}
// computing waiting and termination time
p[0].st = 0;
p[0].tt = p[0].st + p[0].bt;
cout <<setw(5) <<"PID" << setw(5) <<"BT"
<<setw(5) <<"ST"<<setw(5)<<"TT" << endl;
cout << setw (5) << p[0].pid << setw(5) << p[0].bt
<< setw(5) << p[0].st << setw(5)<< p[0].tt<<endl;
for (int i = 1 ; i<nop; i++)
{
p[i].st = p[i-1].tt;
p[i].tt = p[i].st + p[i].bt;
twt = twt + p[i].st;
ttt = ttt + p[i].tt;
cout << setw (5) << p[i].pid << setw (5)<< p[i].bt
<< setw (5)<< p[i].st << setw(5) << p[i].tt << endl;
}
cout << "Average Waiting Time: " << twt / (nop) << endl;
cout << "Average Turnaround Time: " << ttt /(nop) << endl;
}

OUTPUT:
Priority Scheduling
AIM:
To write a C++ program to perform priority scheduling.
PROGRAM: (PRIORITY SCHEDULING)
include <iostream>
#include <iomanip>
using namespace std;
struct Process{
int pid;
int bt;
int st;
int tt;
int prior;
void operator= (const Process & p)
{
pid = p.pid;
bt = p.bt;
st = p.st;
tt = p.tt;
prior = p.prior;
}
};
int main ()
{
Process temp, p[10];
// nop = number of process, twt = total wait time, ttt = total termination time
int nop;
double twt=0, ttt=0;
cout << "Enter number of process: ";
cin >> nop;
for (int i = 0 ; i<nop; i++)
{
p[i].pid = i;
cout << "Enter the burst time of process : " << i <<endl;
cin >> p[i].bt;
cout << "Enter the priority of process : " << i <<endl;
cin >> p[i].prior;
}
// Rearrange Processes
for (int i = 0 ; i<nop; i++)
{
for (int j = i+1; j < nop ; j++)
{
if(p[i].prior > p[j].prior)
{
temp = p[i];
p[i]= p[j];
p[j]= temp;
}
}
}
// computing waiting and termination time
p[0].st = 0;
p[0].tt = p[0].bt;
cout <<setw(5) <<"PID" << setw(5) <<"BT"<<setw(5)
<<"ST"<<setw(5)<<"TT" << endl;
cout << setw (5) << p[0].pid << setw(5) << p[0].bt
<< setw(5) << p[0].st << setw(5)<< p[0].tt<<endl;
for (int i = 1 ; i<nop; i++)
{
p[i].st = p[i-1].st + p[i-1].bt;
p[i].tt = p[i].st + p[i].bt;
twt = twt + p[i].st;
ttt = ttt + p[i].tt;
cout << setw (5) << p[i].pid << setw (5)<< p[i].bt
<< setw (5)<< p[i].st << setw(5) << p[i].tt << endl;
}
cout << "Average Waiting Time: " << twt / (nop) << endl;
cout << "Average Turnaround Time: " << ttt /(nop) << endl;
}
OUTPUT:

ROUND ROBIN SCHEDULING


AIM:
To write a program to implement cpu scheduling for Round Robin Scheduling.
PROGRAM: (ROUND ROBIN SCHEDULING)
#include <iostream>
#include <iomanip>
using namespace std;
struct Process{
int pid;
int bt;
int rbt;
int st;
int tt;
Process(){
pid = 0, bt = 0;
st = 0, tt = 0;
}
};
int main ()
{
Process p[10];
// nop = number of process
int nop, slice;
//twt = total wait time, ttt = total termination time
double twt=0, ttt=0;
cout << "Enter number of process: ";
cin >> nop;
for (int i = 0 ; i<nop; i++){
p[i].pid = i;
cout << "Enter the burst time of process : " << i <<endl;
cin >> p[i].bt;
p[i].rbt = p[i].bt;
}
cout << "Enter the time slice : " <<endl;
cin >> slice;
cout <<setw(5) <<"PID" << setw(5) <<"ST"<<setw(5)
<<"TT"<<setw(5)<<"RBT" << endl;
//Computing Wait timings & Displaying details
int prev = 0;
while(1){
bool done = true;
for (int j = 0 ; j < nop ; j++){
if(p[j].rbt>0){
done = false;
prev = j-1;
if(j==0)
prev = nop-1;
if((p[j].rbt-slice) <=0){

p[j].st = p[prev].tt;
p[j].tt = p[j].st + p[j].rbt;
p[j].rbt = 0;
}else
{
p[j].st = p[prev].tt;
p[j].tt = p[j].st + slice;
p[j].rbt = p[j].rbt - slice;
}
cout << setw(5)<<p[j].pid<<setw(5)<< p[j].st<<setw (5)
<< p[j].tt<<setw(5)<<p[j].rbt<<setw(5)<< endl;
}
}
if(done==true)
break;
}
// computing average waiting and termination time
cout <<setw(5) <<"PID" << setw(5) <<"BT"<<setw(5)
<<"TT"<<setw(5)<<"WT" << endl;
for(int i = 0 ; i<nop; i++)
{
int wt = p[i].tt - p[i].bt;
cout << setw(5)<<p[i].pid<<setw(5)<< p[i].bt<<setw (5)
<< p[i].tt<<setw(5)<<wt<<setw(5)<< endl;
twt = twt + wt;
ttt = ttt + p[i].tt;
}
cout << "Average Waiting Time: " << twt / nop << endl;
cout << "Average turnaround Time: " << ttt /nop << endl;
}

Output:

You might also like