3
1
SystemsProgrammingAndOperat.../Codes/Group B/Assignment - 5/SJF (Preemptive).cpp

83 lines
2.0 KiB
C++
Raw Normal View History

2024-11-06 08:29:55 +05:30
#include <iostream>
2024-07-27 11:18:03 +05:30
using namespace std;
struct sjf{
2024-11-06 08:29:55 +05:30
int id, arr, bur, comp, turn, wait, orgBur;
2024-07-27 11:18:03 +05:30
};
sjf meh[30];
2024-11-06 08:29:55 +05:30
class SJF{
2024-07-27 11:18:03 +05:30
public:
int n;
2024-11-06 08:29:55 +05:30
void getIn()
{
cout<<"Enter number of processes: ";
2024-07-27 11:18:03 +05:30
cin>>n;
2024-11-06 08:29:55 +05:30
for(int i = 0; i < n; i++){
cout<<"\nEnter arrival time for process "<<i<<": ";
cin>>meh[i].arr;
cout<<"Enter burst time for process "<<i<<": ";
cin>>meh[i].bur;
meh[i].orgBur = meh[i].bur;
2024-07-27 11:18:03 +05:30
meh[i].id = i;
}
}
2024-11-06 08:29:55 +05:30
void process()
{
2024-07-27 11:18:03 +05:30
int k = 0;
int completed = 0;
cout<<"\nSequence of processes is: ";
while(completed < n){
2024-11-06 08:29:55 +05:30
int burst = 999;
int idd = -1;
for(int i = 0; i < n; i++){
if(meh[i].arr <= k && meh[i].bur > 0){
if(meh[i].bur < burst){
burst = meh[i].bur;
idd = i;
2024-07-27 11:18:03 +05:30
}
}
}
2024-11-06 08:29:55 +05:30
if(idd != -1){
cout<<"P"<<idd<<" ";
2024-07-27 11:18:03 +05:30
k++;
2024-11-06 08:29:55 +05:30
meh[idd].bur--;
if(meh[idd].bur == 0){
meh[idd].comp = k;
meh[idd].turn = meh[idd].comp - meh[idd].arr;
meh[idd].wait = meh[idd].turn - meh[idd].orgBur;
2024-07-27 11:18:03 +05:30
completed++;
}
} else{
k++;
}
}
}
2024-07-27 11:46:42 +05:30
2024-11-06 08:29:55 +05:30
void display()
{
double turn = 0, comp = 0, wait = 0;
cout<<"\n Completed | Waiting | Turnaround |";
for(int i = 0; i < n; i++){
turn += meh[i].turn;
wait += meh[i].wait;
comp += meh[i].comp;
cout<<"\nP"<<i<<"| "<<meh[i].comp<<" | "<<meh[i].wait<<" | "<<meh[i].turn;
2024-07-27 11:46:42 +05:30
}
2024-11-06 08:29:55 +05:30
cout<<"\n\nAvg completion time: "<<comp/n;
cout<<"\nAvg turnaround time: "<<turn/n;
cout<<"\nAvg waiting time: "<<wait/n;
2024-07-27 11:46:42 +05:30
}
2024-07-27 11:18:03 +05:30
};
2024-11-06 08:29:55 +05:30
int main()
{
SJF ob;
ob.getIn();
ob.process();
ob.display();
2024-07-27 11:18:03 +05:30
return 0;
}