OS PracticeLab SchedulingAlgorithms
OS PracticeLab SchedulingAlgorithms
PRACTICE LAB
SCHEDULING ALGORITHMS
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:
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: