2024-07-27 11:18:03 +05:30
|
|
|
#include<iostream>
|
2024-07-30 11:59:54 +05:30
|
|
|
#include<limits.h> // for INT_MAX
|
2024-07-27 11:18:03 +05:30
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
struct sjf{
|
2024-07-30 11:59:54 +05:30
|
|
|
int burst, arrival, id, completion, waiting, turnaround, response;
|
2024-07-27 11:18:03 +05:30
|
|
|
bool active;
|
|
|
|
};
|
2024-07-30 11:59:54 +05:30
|
|
|
|
2024-07-27 11:18:03 +05:30
|
|
|
sjf meh[30];
|
|
|
|
|
|
|
|
class lesgo{
|
|
|
|
public:
|
|
|
|
int n;
|
|
|
|
|
|
|
|
void sjfIn(){
|
|
|
|
cout<<"\nEnter number of processes: ";
|
|
|
|
cin>>n;
|
2024-07-30 11:59:54 +05:30
|
|
|
for(int i = 1; i <= n; i++){
|
2024-07-27 11:18:03 +05:30
|
|
|
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";
|
2024-07-30 11:59:54 +05:30
|
|
|
for(int j = 1; j <= n; j++) {
|
2024-07-27 11:18:03 +05:30
|
|
|
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){
|
2024-07-30 11:59:54 +05:30
|
|
|
int burst1 = INT_MAX; // Initialize to the maximum possible value
|
2024-07-27 11:18:03 +05:30
|
|
|
int iddd = -1;
|
2024-07-30 11:59:54 +05:30
|
|
|
for(int i = 1; i <= n; i++){
|
2024-07-27 11:18:03 +05:30
|
|
|
if(meh[i].arrival <= k && meh[i].burst > 0){
|
|
|
|
if(meh[i].burst < burst1){
|
|
|
|
burst1 = meh[i].burst;
|
|
|
|
iddd = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(iddd != -1){
|
2024-07-30 11:59:54 +05:30
|
|
|
// Mark the process as active
|
|
|
|
if(!meh[iddd].active) {
|
|
|
|
meh[iddd].response = k - meh[iddd].arrival;
|
|
|
|
meh[iddd].active = true;
|
|
|
|
}
|
|
|
|
|
2024-07-27 11:18:03 +05:30
|
|
|
cout<<"P"<<iddd<<" ";
|
|
|
|
meh[iddd].burst--;
|
|
|
|
k++;
|
|
|
|
if(meh[iddd].burst == 0){
|
2024-07-27 11:46:42 +05:30
|
|
|
meh[iddd].completion = k;
|
2024-07-30 11:59:54 +05:30
|
|
|
meh[iddd].turnaround = meh[iddd].completion - meh[iddd].arrival;
|
|
|
|
meh[iddd].waiting = meh[iddd].turnaround - (meh[iddd].response + 1); // +1 for the final unit burst time
|
2024-07-27 11:18:03 +05:30
|
|
|
completed++;
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-27 11:46:42 +05:30
|
|
|
|
2024-07-30 11:59:54 +05:30
|
|
|
void displayMetrics(){
|
|
|
|
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
|
|
|
|
|
|
|
|
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
|
|
|
|
for(int j = 1; j <= n; j++) {
|
|
|
|
totalWaiting += meh[j].waiting;
|
|
|
|
totalTurnaround += meh[j].turnaround;
|
|
|
|
totalCompletion += meh[j].completion;
|
|
|
|
cout<<"P"<<j<<"| "<<meh[j].completion
|
|
|
|
<<" | "<<meh[j].waiting
|
|
|
|
<<" | "<<meh[j].turnaround
|
|
|
|
<<" | "<<meh[j].response<<"\n";
|
2024-07-27 11:46:42 +05:30
|
|
|
}
|
2024-07-30 11:59:54 +05:30
|
|
|
|
|
|
|
cout<<"\nAverage completion time: "<<totalCompletion/n;
|
|
|
|
cout<<"\nAverage waiting time: "<<totalWaiting/n;
|
|
|
|
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
|
2024-07-27 11:46:42 +05:30
|
|
|
}
|
2024-07-27 11:18:03 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
lesgo obj;
|
|
|
|
obj.sjfIn();
|
|
|
|
obj.sjfProcess();
|
2024-07-30 11:59:54 +05:30
|
|
|
obj.displayMetrics();
|
2024-07-27 11:18:03 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2024-07-30 11:59:54 +05:30
|
|
|
|