3
1

Merge remote-tracking branch 'origin/main'

This commit is contained in:
K 2024-11-06 15:06:56 +05:30
commit f489de94f7
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -1,64 +1,52 @@
#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, waiting, turnaround, response; int id, arr, bur, comp, turn, wait, orgBur;
bool active;
}; };
sjf meh[30]; sjf meh[30];
class lesgo{ class SJF{
public: public:
int n; int n;
void getIn()
void sjfIn(){ {
cout<<"\nEnter number of processes: "; cout<<"Enter number of processes: ";
cin>>n; cin>>n;
for(int i = 1; i <= n; i++){ for(int i = 0; i < n; i++){
cout<<"\nEnter arrival time of P"<<i<<": "; cout<<"\nEnter arrival time for process "<<i<<": ";
cin>>meh[i].arrival; cin>>meh[i].arr;
cout<<"\nEnter burst time of P"<<i<<": "; cout<<"Enter burst time for process "<<i<<": ";
cin>>meh[i].burst; cin>>meh[i].bur;
meh[i].orgBur = meh[i].bur;
meh[i].id = i; meh[i].id = i;
meh[i].active = false;
}
cout<<"\n | Arrival | Burst\n";
for(int j = 1; j <= n; j++) {
cout<<"P"<<j<<"| "<<meh[j].arrival<<" | "<<meh[j].burst<<"\n";
} }
} }
void sjfProcess(){ void process()
{
int k = 0; int k = 0;
int completed = 0; int completed = 0;
cout<<"\nSequence of processes is: "; cout<<"\nSequence of processes is: ";
while(completed < n){ while(completed < n){
int burst1 = INT_MAX; // Initialize to the maximum possible value int burst = 999;
int iddd = -1; int idd = -1;
for(int i = 1; i <= n; i++){ for(int i = 0; i < n; i++){
if(meh[i].arrival <= k && meh[i].burst > 0){ if(meh[i].arr <= k && meh[i].bur > 0){
if(meh[i].burst < burst1){ if(meh[i].bur < burst){
burst1 = meh[i].burst; burst = meh[i].bur;
iddd = i; idd = i;
} }
} }
} }
if(iddd != -1){ if(idd != -1){
// Mark the process as active cout<<"P"<<idd<<" ";
if(!meh[iddd].active) {
meh[iddd].response = k - meh[iddd].arrival;
meh[iddd].active = true;
}
cout<<"P"<<iddd<<" ";
meh[iddd].burst--;
k++; k++;
if(meh[iddd].burst == 0){ meh[idd].bur--;
meh[iddd].completion = k; if(meh[idd].bur == 0){
meh[iddd].turnaround = meh[iddd].completion - meh[iddd].arrival; meh[idd].comp = k;
meh[iddd].waiting = meh[iddd].turnaround - (meh[iddd].response + 1); // +1 for the final unit burst time meh[idd].turn = meh[idd].comp - meh[idd].arr;
meh[idd].wait = meh[idd].turn - meh[idd].orgBur;
completed++; completed++;
} }
} else{ } else{
@ -67,31 +55,28 @@ public:
} }
} }
void displayMetrics(){ void display()
double totalWaiting = 0, totalTurnaround = 0, totalCompletion = 0; {
double turn = 0, comp = 0, wait = 0;
cout<<"\n\n | Completion time | Waiting time | Turnaround time | Response time\n"; cout<<"\n Completed | Waiting | Turnaround |";
for(int j = 1; j <= n; j++) { for(int i = 0; i < n; i++){
totalWaiting += meh[j].waiting; turn += meh[i].turn;
totalTurnaround += meh[j].turnaround; wait += meh[i].wait;
totalCompletion += meh[j].completion; comp += meh[i].comp;
cout<<"P"<<j<<"| "<<meh[j].completion cout<<"\nP"<<i<<"| "<<meh[i].comp<<" | "<<meh[i].wait<<" | "<<meh[i].turn;
<<" | "<<meh[j].waiting
<<" | "<<meh[j].turnaround
<<" | "<<meh[j].response<<"\n";
} }
cout<<"\nAverage completion time: "<<totalCompletion/n; cout<<"\n\nAvg completion time: "<<comp/n;
cout<<"\nAverage waiting time: "<<totalWaiting/n; cout<<"\nAvg turnaround time: "<<turn/n;
cout<<"\nAverage turnaround time: "<<totalTurnaround/n; cout<<"\nAvg waiting time: "<<wait/n;
} }
}; };
int main(){ int main()
lesgo obj; {
obj.sjfIn(); SJF ob;
obj.sjfProcess(); ob.getIn();
obj.displayMetrics(); ob.process();
ob.display();
return 0; return 0;
} }