Code:
#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
};
// Sort processes based on arrival time
bool comp(Process P1, Process P2)
{
return (P1.a < P2.a); // required condition
}
// Sort processes based on position
bool compP(Process P1, Process P2)
{
return (P1.n < P2.n); // required condition
}
// Print RR Gantt Chart and Table
void RR(vector<struct Process> V, int q)
{
cout << endl << "Gantt Chart :-" << endl << 0;
sort(V.begin(), V.end(), comp); // sort based on arrival time
int t = 0; // current timesnap
int AP = -1; // arrival pointer
int s = V.size(); // number of processes
int c = s; // processes left to schedule
int BT[s]; // array of burst times
for (int i = 0; i < s; i++)
BT[i] = V[i].b; // copy burst times
double s_tt = 0, s_wt = 0, s_rt = 0, s_it = 0; // sum of times variable
int preempted = -1; // preempted process
queue<int> Q; // queue
while (c)
{
int prev_AP = AP; // previous position of AP
// Set position of arrival pointer
for (int i = AP+1; i < s; i++)
{
if (V[i].a > t) break;
else AP++;
}
// Processes till AP's position have now arrived
// Push newly arrived processes into queue
for (int i = prev_AP+1; i <= AP; i++)
{
if (BT[i]) // process is not over
Q.push(i); // push process into queue
}
if (preempted != -1)
Q.push(preempted); // push the previously preempted process
preempted = -1; // reset
if (Q.empty()) // idle time has to be spent
{
s_it += V[AP+1].a-t; // add idle time
t = V[AP+1].a; // increment time
cout << " [-] " << t;
continue; // continue to next iteration
}
// Now queue is not empty
int p = Q.front(); // get process to be scheduled
Q.pop();
if (BT[p] == V[p].b) // first time schedule
V[p].ir = t; // record initial response moment
if (BT[p] <= q) // finish off the process
{
c--;
t += BT[p]; // increment time by burst time left
BT[p] = 0; // reset burst time
V[p].c = t; // completing time
V[p].t = V[p].c - V[p].a; // turnaround time
V[p].w = V[p].t - V[p].b; // waiting time
V[p].r = V[p].ir - V[p].a; // response time
s_tt += V[p].t;
s_wt += V[p].w;
s_rt += V[p].r; // add times
}
else // partially finish process and put back in queue
{
t += q; // increment time by one quantum
BT[p] -= q; // decrement burst time of process
preempted = p; // preempted process
}
cout << " [P" << V[p].n << "] " << t;
}
sort(V.begin(), V.end(), compP); // sort back based on position
cout << endl << endl << "\t" << "AT" << "\t" << "BT" << "\t" "CT" << "\t"
<< "TT" << "\t" << "WT" << "\t" << "RT" << endl;
for (int i = 0; i < s; i++)
cout << "P" << V[i].n << "\t" << V[i].a << "\t" << V[i].b << "\t" <<
V[i].c << "\t" << V[i].t << "\t" << V[i].w << "\t" << V[i].r << endl;
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;
int num; // number of processes
cout << "Enter the number of processes : ";
cin >> num;
vector<struct Process> V; // vector of processes
cout << "For each process, enter (pid arrival_time burst_time) :-" <<
endl;
for (int i = 0; i < num; i++)
{
struct Process P; // process
cout << "Process " << i << " : ";
cin >> P.n >> P.a >> P.b; // get times as user input
P.c = P.t = P.w = P.r = P.ir = 0; // other times
V.push_back(P);
}
int q; // time quantum
cout << "Enter time quantum : ";
cin >> q;
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
int c; // completion time
int t; // turnaround time
int w; // waiting time
int r; // response time
};
// Sort processes based on arrival time //comparator
bool compA(Process P1, Process P2)
{
return (P1.a < P2.a); // required condition
}
// Sort processes based on position
bool compP(Process P1, Process P2)
{
return (P1.n < P2.n); // required condition
}
// Print SJF Gantt Chart and Table
void SJF(vector<struct Process> V)
{
cout << endl
<< "Gantt Chart :-" << endl
<< 0;
sort(V.begin(), V.end(), compA); // sort based on arrival time
int t = 0; // current timesnap
int AP = -1; // arrival pointer
int s = V.size(); // number of processes
int c = s; // processes left to schedule
bool over[s]; // schedule array
memset(over, false, sizeof(over)); // -> default false
double s_tt = 0, s_wt = 0, s_rt = 0, s_it = 0; // sum of times variable
while (c)
{
// Set position of arrival pointer
for (int i = AP + 1; i < s; i++)
{
if (V[i].a > t)
break;
else
AP++;
}
int minJ = INT_MAX, p = -1; // store minimum job burst time
for (int i = 0; i <= AP; i++)
{
if (over[i])
continue; // process has been scheduled earlier
bool c1 = (V[i].b < minJ);
bool c2 = (p != -1) && (V[i].b == minJ) && (V[i].a < V[p].a);
bool c3 = (p != -1) && (V[i].b == minJ) && (V[i].a == V[p].a) &&
(V[i].n < V[p].n);
if (c1 || c2 || c3) // swap conditions
{
minJ = V[i].b;
p = i; // update position
}
}
if (p == -1) // idle time needs to be spent
{
s_it += V[AP + 1].a - t; // add idle time
t = V[AP + 1].a; // increment time
cout << " [-] " << t;
}
else // Process 'p' is to be scheduled
{
c--;
t += V[p].b; // increment time by burst time
over[p] = true; // current process is over
V[p].c = t; // completing time
V[p].t = V[p].c - V[p].a; // turnaround time
V[p].w = V[p].t - V[p].b; // waiting time
V[p].r = V[p].w; // response time
s_tt += V[p].t;
s_wt += V[p].w;
s_rt += V[p].r; // add times
cout << " [P" << V[p].n << "] " << t;
}
}
sort(V.begin(), V.end(), compP); // sort back based on position
cout << endl
<< endl
<< "\t"
<< "AT"
<< "\t"
<< "BT"
<< "\t"
"CT"
<< "\t"
<< "TT"
<< "\t"
<< "WT"
<< "\t"
<< "RT" << endl;
for (int i = 0; i < s; i++)
cout << "P" << V[i].n << "\t" << V[i].a << "\t" << V[i].b << "\t" <<
V[i].c << "\t" << V[i].t << "\t" << V[i].w << "\t" << V[i].r << endl;
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++ Shortest Job First (SJF) CPU Scheduler !" << endl
<< endl;
int num; // number of processes
cout << "Enter the number of processes : ";
cin >> num;
vector<struct Process> V; // vector of processes
cout << "For each process, enter (pid arrival_time burst_time) :-" <<
endl;
for (int i = 0; i < num; i++)
{
struct Process P; // process
cout << "Process " << i << " : ";
cin >> P.n >> P.a >> P.b; // get times as user input
P.c = P.t = P.w = P.r = 0; // other times
V.push_back(P);
}
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
int c; // completion time
int t; // turnaround time
int w; // waiting time
int r; // response time
int ir; // initial response time
};
// Sort processes based on arrival time
bool compA(Process P1, Process P2)
{
return (P1.a < P2.a); // required condition
}
// Sort processes based on position
bool compP(Process P1, Process P2)
{
return (P1.n < P2.n); // required condition
}
// Print SRTF Gantt Chart and Table
void SRTF(vector<struct Process> V)
{
cout << endl
<< "Gantt Chart :-" << endl
<< 0;
sort(V.begin(), V.end(), compA); // sort based on arrival time
int t = 0; // current timesnap
int AP = -1; // arrival pointer
int s = V.size(); // number of processes
int c = s; // processes left to schedule
int BT[s]; // array of burst times
for (int i = 0; i < s; i++)
BT[i] = V[i].b; // copy burst times
double s_tt = 0, s_wt = 0, s_rt = 0, s_it = 0; // sum of times variable
while (c)
{
// Set position of arrival pointer
for (int i = AP + 1; i < s; i++)
{
if (V[i].a > t)
break;
else
AP++;
}
// Processes till AP's position have now arrived
int minJ = INT_MAX, p = -1; // store minimum job burst time
for (int i = 0; i <= AP; i++)
{
if (!BT[i])
continue; // process has been scheduled earlier
bool c1 = (V[i].b < minJ);
bool c2 = (p != -1) && (V[i].b == minJ) && (V[i].a < V[p].a);
bool c3 = (p != -1) && (V[i].b == minJ) && (V[i].a == V[p].a) &&
(V[i].n < V[p].n);
if (c1 || c2 || c3) // swap conditions
{
minJ = V[i].b;
p = i; // update position
}
}
if (p == -1) // idle time needs to be spent
{
s_it += V[AP + 1].a - t; // add idle time
t = V[AP + 1].a; // increment time
cout << " [-] " << t;
}
// In these 2 cases, process 'p' is to be scheduled
else if ((AP == s - 1) || (V[AP + 1].a - t >= BT[p])) // process can
be fully scheduled
{
if (BT[p] == V[p].b) // process is being scheduled for the first
time
V[p].ir = t; // record initial response time
c--;
t += BT[p]; // increment time by burst time
BT[p] = 0; // current process is over
V[p].c = t; // completing time
V[p].t = V[p].c - V[p].a; // turnaround time
V[p].w = V[p].t - V[p].b; // waiting time
V[p].r = V[p].ir - V[p].a; // response time
s_tt += V[p].t;
s_wt += V[p].w;
s_rt += V[p].r; // add times
cout << " [P" << V[p].n << "] " << t;
}
else // process is partially scheduled
{
if (BT[p] == V[p].b) // process is being scheduled for the first
time
V[p].ir = t; // record initial response time
BT[p] -= (V[AP + 1].a - t); // decrement burst time
t = V[AP + 1].a; // update time
cout << " [P" << V[p].n << "] " << t;
}
}
sort(V.begin(), V.end(), compP); // sort back based on position
cout << endl
<< endl
<< "\t"
<< "AT"
<< "\t"
<< "BT"
<< "\t"
"CT"
<< "\t"
<< "TT"
<< "\t"
<< "WT"
<< "\t"
<< "RT" << endl;
for (int i = 0; i < s; i++)
cout << "P" << V[i].n << "\t" << V[i].a << "\t" << V[i].b << "\t" <<
V[i].c << "\t" << V[i].t << "\t" << V[i].w << "\t" << V[i].r << endl;
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++ Shortest Remaining Time First (SRTF) CPU Scheduler
!" << endl
<< endl;
int num; // number of processes
cout << "Enter the number of processes : ";
cin >> num;
vector<struct Process> V; // vector of processes
cout << "For each process, enter (pid arrival_time burst_time) :-" <<
endl;
for (int i = 0; i < num; i++)
{
struct Process P; // process
cout << "Process " << i << " : ";
cin >> P.n >> P.a >> P.b; // get times as user input
P.c = P.t = P.w = P.r = P.ir = 0; // other times
V.push_back(P);
}
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];
}
for (i = 0; i < n; i++)
x[i] = b[i]; // copying burst times
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: