Non Preemptive Priority Scheduling In C++

Non-Preemptive Priority Scheduling is a CPU process Scheduling algorithm in which all the processes are executed by the CPU based on their priority. In this, the scheduling of processes depends upon a third factor called Priority. This PRIORITY is a number provided by the user, which determines when a process will be executed. Process with highest PRIORITY will be executed first and after that the processes with decreasing order of PRIORITY will be executed till the process with the least PRIORITY is executed. If two processes have the same PRIORITY then their order of execution is decided by their burst time, i.e. the process with lower burst time will be executed first. Here, we are talking about Non-Preemptive Priority Scheduling so the process arriving first will be executed first irrespective of its priority and will not be interrupted till it finishes executing even if a process with higher priority arrives during its execution.

Non Preemptive Priority Scheduling In C++

/* ******************************
AUTHOR: EXPLORING BITS
****************************** */
#include <iostream>
#include <iomanip>
using namespace std;
int n, pMat[100][7];
void swap(int a, int b){
for (int i = 0; i < 6; i++){
int temp = pMat[a][i];
pMat[a][i] = pMat[b][i];
pMat[b][i] = temp;
}
}
void arrangeArrival()
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i-1; j++)
{
if(pMat[j][1] > pMat[j+1][1])
{
for(int k = 0; k < 5; k++)
{
swap(pMat[j][k], pMat[j+1][k]);
}
}
if (pMat[j][1] == pMat[j+1][1]){
if(pMat[j][2] > pMat[j+1][2])
{
swap(j, j+1);
}
}
}
}
}
void prioritySchedule(){
double tt = 0.0;
int x = 1;
tt = tt + pMat[0][2];
pMat[0][4] = tt;
pMat[0][6] = tt – pMat[0][1];
pMat[0][5] = pMat[0][6] – pMat[0][2];
for (int i = 1; i < n; i++){
for (int j = i; j < n-1; j++){
for (int k = i; k < n-1; k++){
if ((pMat[k][1] – tt) > 0 || (pMat[k+1][1] – tt) > 0)
continue;
if (pMat[k][3] < pMat[k+1][3])
swap(k, k+1);
}
}
tt = tt + pMat[i][2];
pMat[i][4] = tt;
pMat[i][6] = tt – pMat[i][1];
pMat[i][5] = pMat[i][6] – pMat[i][2];
}
}
void displayGanttChart(){
cout << “\nGantt Chart: \n”;
cout << “\n+”;
for (int i = 0; i < n; i++){
for (int j = 0; j < pMat[i][2]*2; j++){
cout << “-“;
}
cout << “+”;
}
cout << “\n”;
for (int i = 0; i < n; i++){
cout << “|”;
for (int j = 0; j < pMat[i][2]-1; j++){
cout << ” “;
}
cout << pMat[i][0];
for (int j = 0; j < pMat[i][2]; j++){
cout << ” “;
}
}
cout << “|”;
cout << “\n”;
cout << “+”;
for (int i = 0; i < n; i++){
for (int j = 0; j < pMat[i][2]*2; j++){
cout << “-“;
}
cout << “+”;
}
cout << “\n”;
cout << 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < pMat[i][2]*21; j++){
cout << ” “;
}
cout << setw(2) << pMat[i][4];
}
cout << “\n”;
}
void displayFinal(){
double wt = 0.0, tat = 0.0;
cout << “\nFinal Result: \n\n”;
cout << “+—–+————+————–+—————–+\n”;
cout << “| PID | BURST TIME | WAITING TIME | TURNAROUND TIME |\n”;
cout << “+—–+————+————–+—————–+\n”;
for (int i = 0; i < n; i++){
cout << “| “ << setw(2) << pMat[i][0] << ” “;
cout << “| “ << setw(2) << pMat[i][2] << ” “;
cout << “| “ << setw(2) << pMat[i][5] << ” “;
cout << “| “ << setw(2) << pMat[i][6] << ” |\n”;
cout << “+—–+————+————–+—————–+\n”;
wt = wt + pMat[i][5];
tat = tat + pMat[i][6];
}
cout << “\nTotal Waiting Time: “ << wt << endl;
cout << “Average Waiting Time: “ << wt/n << endl;
cout << “Total Turnaround Time: “ << tat << endl;
cout << “Average Turnaround Time: “ << tat/n << endl;
}
int main(){
cout << “Enter the number of processes: “;
cin >> n;
cout << “Enter the details of the processes: \n”;
for (int i = 0; i < n; i++){
cout << “\nProcess “ << i+1 << “…\n”;
cout << “Enter Process ID: “;
cin >> pMat[i][0];
cout << “Enter Arrival Time: “;
cin >> pMat[i][1];
cout << “Enter Burst Time: “;
cin >> pMat[i][2];
cout << “Enter Priority: “;
cin >> pMat[i][3];
}
prioritySchedule();
displayGanttChart();
displayFinal();
return 0;
}

 

Leave a Comment