66 lines
1.5 KiB
C++
66 lines
1.5 KiB
C++
|
#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;
|
||
|
}
|