Start
Start
#include "bits/stdc++.h"
using namespace std;
struct Process
{
int n; // process number
int b; // burst time
int a; // arrival time
int c; // completion time
int t; // turnaround time
int w; // waiting time
int r; // response time
int ir; // initial response time
};
if (preempted != -1)
Q.push(preempted); // push the previously preempted process
preempted = -1; // reset
s_tt += V[p].t;
s_wt += V[p].w;
s_rt += V[p].r; // add times
}
cout << " [P" << V[p].n << "] " << t;
}
cout << endl << "Average Turnaround Time = " << (s_tt / s);
cout << endl << "Average Waiting Time = " << (s_wt / s);
cout << endl << "Average Response Time = " << (s_rt / s);
cout << endl << "Total Idle Time = " << s_it;
}
int main()
{
cout << "Welcome to C++ Round Robin (RR) CPU Scheduler !" << endl << endl;
RR(V, q);
}
-----------------------------------------------------------------------------
Input:
Output:
Code:
#include <bits/stdc++.h>
using namespace std;
struct Process
{
int n; // process number
int a; // arrival time
int b; // burst time
s_tt += V[p].t;
s_wt += V[p].w;
s_rt += V[p].r; // add times
cout << " [P" << V[p].n << "] " << t;
}
}
int main()
{
cout << "Welcome to C++ Shortest Job First (SJF) CPU Scheduler !" << endl
<< endl;
SJF(V);
cout << endl
<< endl
<< "Thank you for using C++ SJF CPU Scheduler. Bye Bye !";
}
Input/Output:
Code:
#include <bits/stdc++.h>
using namespace std;
struct Process
{
int n; // process number
int b; // burst time
int a; // arrival time
s_tt += V[p].t;
s_wt += V[p].w;
s_rt += V[p].r; // add times
cout << " [P" << V[p].n << "] " << t;
}
int main()
{
cout << "Welcome to C++ Shortest Remaining Time First (SRTF) CPU Scheduler
!" << endl
<< endl;
SRTF(V);
cout << endl
<< endl
<< "Thank you for using C++ SRTF CPU Scheduler. Bye Bye !";
}
Input/Output
Code:
#include <bits/stdc++.h>
#define MAX_P 100
using namespace std;
int main()
{
int a[MAX_P], b[MAX_P], x[MAX_P], pid[MAX_P];
int waiting[MAX_P], turnaround[MAX_P], completion[MAX_P], p[MAX_P];
int i, j, smallest, count = 0, time, n;
double avg = 0, tt = 0, end;
cout<<"Welcome to Preemptive Priority Scheduling"<<endl;
cout << "\nEnter the number of Processes: ";
cin >> n;
cout << "\nFor each process, enter(PID Priority Arrival_Time Burst_Time):-
\n";
for (i = 0; i < n; i++)
{
cout << "Process " << i + 1 << " : ";
cin >> pid[i] >> p[i] >> a[i] >> b[i];
}
p[MAX_P - 1] = -1;
for (time = 0; count != n; time++)
{
smallest = MAX_P - 1;
for (i = 0; i < n; i++)
{
if (a[i] <= time && p[i] > p[smallest] && b[i] > 0)
smallest = i;
}
b[smallest]--;
if (b[smallest] == 0)
{
count++;
end = time + 1;
completion[smallest] = end;
waiting[smallest] = end - a[smallest] - x[smallest];
turnaround[smallest] = end - a[smallest];
}
}
cout << "PID"
<< "\t"
<< "AT"
<< "\t"
<< "BT"
<< "\t"
<< "CT"
<< "\t"
<< "TAT"
<< "\t"
<< "WT"
<< "\t"
<< "Priority" << endl;
for (i = 0; i < n; i++)
{
cout << "P" << i + 1 << "\t" << a[i] << "\t" << x[i] << "\t" <<
completion[i] << "\t" << turnaround[i] << "\t" << waiting[i] << "\t" << p[i]
<< endl;
avg = avg + waiting[i];
tt = tt + turnaround[i];
}
cout << "\n\nAverage waiting time =" << avg / n;
cout << " Average Turnaround time =" << tt / n << endl;
}
Input/Output: