added sjf code
This commit is contained in:
parent
fb0e0e15df
commit
81c27ea1b1
65
Codes/Group B/Assignment - 5/SJF (Premptive).cpp
Normal file
65
Codes/Group B/Assignment - 5/SJF (Premptive).cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct sjf{
|
||||
int burst, arrival, id;
|
||||
bool active;
|
||||
};
|
||||
sjf meh[30];
|
||||
|
||||
class lesgo{
|
||||
public:
|
||||
int n;
|
||||
|
||||
void sjfIn(){
|
||||
cout<<"\nEnter number of processes: ";
|
||||
cin>>n;
|
||||
for(int i = 0; i < n; i++){
|
||||
cout<<"\nEnter arrival time of P"<<i<<": ";
|
||||
cin>>meh[i].arrival;
|
||||
cout<<"\nEnter burst time of P"<<i<<": ";
|
||||
cin>>meh[i].burst;
|
||||
meh[i].id = i;
|
||||
meh[i].active = false;
|
||||
}
|
||||
cout<<"\n | Arrival | Burst\n";
|
||||
for(int j = 0; j < n; j++) {
|
||||
cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
void sjfProcess(){
|
||||
int k = 0;
|
||||
int completed = 0;
|
||||
cout<<"\nSequence of processes is: ";
|
||||
while(completed < n){
|
||||
int burst1 = 999;
|
||||
int iddd = -1;
|
||||
for(int i = 0; i < n; i++){
|
||||
if(meh[i].arrival <= k && meh[i].burst > 0){
|
||||
if(meh[i].burst < burst1){
|
||||
burst1 = meh[i].burst;
|
||||
iddd = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(iddd != -1){
|
||||
cout<<"P"<<iddd<<" ";
|
||||
meh[iddd].burst--;
|
||||
k++;
|
||||
if(meh[iddd].burst == 0){
|
||||
completed++;
|
||||
}
|
||||
} else{
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
lesgo obj;
|
||||
obj.sjfIn();
|
||||
obj.sjfProcess();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user