3
1

added waiting, turnaround and response time in FCFS and SJF code. Thanks to Ayush Kalaskar for their contribution.

This commit is contained in:
K 2024-07-30 11:59:54 +05:30
parent 19018c27d6
commit ba4a29ebf4
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
2 changed files with 59 additions and 18 deletions

33
Codes/Group B/Assignment - 5/FCFS (Non-Preemptive).cpp Normal file → Executable file
View File

@ -1,12 +1,14 @@
#include<iostream> #include<iostream>
using namespace std; using namespace std;
struct fcfs{ struct fcfs
int burst, arrival, id, completion; {
int burst, arrival, id, completion, waiting, turnaround, response;
}; };
fcfs meh[30]; fcfs meh[30];
class FCFS{ class FCFS
{
public: public:
int n; int n;
void fcfsIn(){ void fcfsIn(){
@ -24,6 +26,7 @@ public:
cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n"; cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n";
} }
} }
void process() { void process() {
cout<<"\nSequence of processes is: "; cout<<"\nSequence of processes is: ";
int currentTime = 0; int currentTime = 0;
@ -36,18 +39,33 @@ public:
} }
} }
meh[i].response = currentTime - meh[i].arrival;
cout<<"P"<<meh[i].id<<" "; cout<<"P"<<meh[i].id<<" ";
currentTime += meh[i].burst; currentTime += meh[i].burst;
meh[i].completion = currentTime; meh[i].completion = currentTime;
meh[i].turnaround = meh[i].completion - meh[i].arrival;
meh[i].waiting = meh[i].turnaround - meh[i].burst;
} }
} }
void completion() void displayMetrics()
{ {
cout<<"\n\n | Completion time\n"; double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
for(int j = 0; j < n; j++) { for(int j = 0; j < n; j++) {
cout<<"P"<<j<<"| "<<meh[j].completion<<"\n"; 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";
} }
cout<<"\nAverage completion time: "<<totalCompletion/n;
cout<<"\nAverage waiting time: "<<totalWaiting/n;
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
} }
}; };
@ -56,6 +74,7 @@ int main()
FCFS obj; FCFS obj;
obj.fcfsIn(); obj.fcfsIn();
obj.process(); obj.process();
obj.completion(); obj.displayMetrics();
return 0; return 0;
} }

44
Codes/Group B/Assignment - 5/SJF (Preemptive).cpp Normal file → Executable file
View File

@ -1,10 +1,12 @@
#include<iostream> #include<iostream>
#include<limits.h> // for INT_MAX
using namespace std; using namespace std;
struct sjf{ struct sjf{
int burst, arrival, id, completion; int burst, arrival, id, completion, waiting, turnaround, response;
bool active; bool active;
}; };
sjf meh[30]; sjf meh[30];
class lesgo{ class lesgo{
@ -14,7 +16,7 @@ public:
void sjfIn(){ void sjfIn(){
cout<<"\nEnter number of processes: "; cout<<"\nEnter number of processes: ";
cin>>n; cin>>n;
for(int i = 0; i < n; i++){ for(int i = 1; i <= n; i++){
cout<<"\nEnter arrival time of P"<<i<<": "; cout<<"\nEnter arrival time of P"<<i<<": ";
cin>>meh[i].arrival; cin>>meh[i].arrival;
cout<<"\nEnter burst time of P"<<i<<": "; cout<<"\nEnter burst time of P"<<i<<": ";
@ -23,7 +25,7 @@ public:
meh[i].active = false; meh[i].active = false;
} }
cout<<"\n | Arrival | Burst\n"; cout<<"\n | Arrival | Burst\n";
for(int j = 0; j < n; j++) { for(int j = 1; j <= n; j++) {
cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n"; cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n";
} }
} }
@ -33,9 +35,9 @@ public:
int completed = 0; int completed = 0;
cout<<"\nSequence of processes is: "; cout<<"\nSequence of processes is: ";
while(completed < n){ while(completed < n){
int burst1 = 999; int burst1 = INT_MAX; // Initialize to the maximum possible value
int iddd = -1; int iddd = -1;
for(int i = 0; i < n; i++){ for(int i = 1; i <= n; i++){
if(meh[i].arrival <= k && meh[i].burst > 0){ if(meh[i].arrival <= k && meh[i].burst > 0){
if(meh[i].burst < burst1){ if(meh[i].burst < burst1){
burst1 = meh[i].burst; burst1 = meh[i].burst;
@ -44,11 +46,19 @@ public:
} }
} }
if(iddd != -1){ if(iddd != -1){
// Mark the process as active
if(!meh[iddd].active) {
meh[iddd].response = k - meh[iddd].arrival;
meh[iddd].active = true;
}
cout<<"P"<<iddd<<" "; cout<<"P"<<iddd<<" ";
meh[iddd].burst--; meh[iddd].burst--;
k++; k++;
if(meh[iddd].burst == 0){ if(meh[iddd].burst == 0){
meh[iddd].completion = k; meh[iddd].completion = k;
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
completed++; completed++;
} }
} else{ } else{
@ -57,12 +67,23 @@ public:
} }
} }
void completion() void displayMetrics(){
{ double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0;
cout<<"\n\n | Completion time\n";
for(int j = 0; j < n; j++) { cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n";
cout<<"P"<<j<<"| "<<meh[j].completion<<"\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";
} }
cout<<"\nAverage completion time: "<<totalCompletion/n;
cout<<"\nAverage waiting time: "<<totalWaiting/n;
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
} }
}; };
@ -70,6 +91,7 @@ int main(){
lesgo obj; lesgo obj;
obj.sjfIn(); obj.sjfIn();
obj.sjfProcess(); obj.sjfProcess();
obj.completion(); obj.displayMetrics();
return 0; return 0;
} }