76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
#include<iostream>
|
|
using namespace std;
|
|
|
|
struct sjf{
|
|
int burst, arrival, id, completion;
|
|
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){
|
|
meh[iddd].completion = k;
|
|
completed++;
|
|
}
|
|
} else{
|
|
k++;
|
|
}
|
|
}
|
|
}
|
|
|
|
void completion()
|
|
{
|
|
cout<<"\n\n | Completion time\n";
|
|
for(int j = 0; j < n; j++) {
|
|
cout<<"P"<<j<<"| "<<meh[j].completion<<"\n";
|
|
}
|
|
}
|
|
};
|
|
|
|
int main(){
|
|
lesgo obj;
|
|
obj.sjfIn();
|
|
obj.sjfProcess();
|
|
obj.completion();
|
|
return 0;
|
|
}
|