Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
f489de94f7
@ -1,64 +1,52 @@
|
||||
#include<iostream>
|
||||
#include<limits.h> // for INT_MAX
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct sjf{
|
||||
int burst, arrival, id, completion, waiting, turnaround, response;
|
||||
bool active;
|
||||
int id, arr, bur, comp, turn, wait, orgBur;
|
||||
};
|
||||
|
||||
sjf meh[30];
|
||||
|
||||
class lesgo{
|
||||
class SJF{
|
||||
public:
|
||||
int n;
|
||||
|
||||
void sjfIn(){
|
||||
cout<<"\nEnter number of processes: ";
|
||||
void getIn()
|
||||
{
|
||||
cout<<"Enter number of processes: ";
|
||||
cin>>n;
|
||||
for(int i = 1; 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;
|
||||
for(int i = 0; i < n; i++){
|
||||
cout<<"\nEnter arrival time for process "<<i<<": ";
|
||||
cin>>meh[i].arr;
|
||||
cout<<"Enter burst time for process "<<i<<": ";
|
||||
cin>>meh[i].bur;
|
||||
meh[i].orgBur = meh[i].bur;
|
||||
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 completed = 0;
|
||||
cout<<"\nSequence of processes is: ";
|
||||
while(completed < n){
|
||||
int burst1 = INT_MAX; // Initialize to the maximum possible value
|
||||
int iddd = -1;
|
||||
for(int i = 1; i <= n; i++){
|
||||
if(meh[i].arrival <= k && meh[i].burst > 0){
|
||||
if(meh[i].burst < burst1){
|
||||
burst1 = meh[i].burst;
|
||||
iddd = i;
|
||||
int burst = 999;
|
||||
int idd = -1;
|
||||
for(int i = 0; i < n; i++){
|
||||
if(meh[i].arr <= k && meh[i].bur > 0){
|
||||
if(meh[i].bur < burst){
|
||||
burst = meh[i].bur;
|
||||
idd = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
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<<" ";
|
||||
meh[iddd].burst--;
|
||||
if(idd != -1){
|
||||
cout<<"P"<<idd<<" ";
|
||||
k++;
|
||||
if(meh[iddd].burst == 0){
|
||||
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
|
||||
meh[idd].bur--;
|
||||
if(meh[idd].bur == 0){
|
||||
meh[idd].comp = k;
|
||||
meh[idd].turn = meh[idd].comp - meh[idd].arr;
|
||||
meh[idd].wait = meh[idd].turn - meh[idd].orgBur;
|
||||
completed++;
|
||||
}
|
||||
} else{
|
||||
@ -67,31 +55,28 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
void display()
|
||||
{
|
||||
double turn = 0, comp = 0, wait = 0;
|
||||
cout<<"\n Completed | Waiting | Turnaround |";
|
||||
for(int i = 0; i < n; i++){
|
||||
turn += meh[i].turn;
|
||||
wait += meh[i].wait;
|
||||
comp += meh[i].comp;
|
||||
cout<<"\nP"<<i<<"| "<<meh[i].comp<<" | "<<meh[i].wait<<" | "<<meh[i].turn;
|
||||
}
|
||||
|
||||
cout<<"\nAverage completion time: "<<totalCompletion/n;
|
||||
cout<<"\nAverage waiting time: "<<totalWaiting/n;
|
||||
cout<<"\nAverage turnaround time: "<<totalTurnaround/n;
|
||||
cout<<"\n\nAvg completion time: "<<comp/n;
|
||||
cout<<"\nAvg turnaround time: "<<turn/n;
|
||||
cout<<"\nAvg waiting time: "<<wait/n;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
lesgo obj;
|
||||
obj.sjfIn();
|
||||
obj.sjfProcess();
|
||||
obj.displayMetrics();
|
||||
int main()
|
||||
{
|
||||
SJF ob;
|
||||
ob.getIn();
|
||||
ob.process();
|
||||
ob.display();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user