# Non-Preemptive Priority Scheduling Program in C++ | Gantt Chart

Non-Preemptive Priority Scheduling Program in C++ with Gantt Chart or Non-Preemptive Priority Scheduling Algorithm in C++ with output Priority Scheduling Algorithm is a Non-Primitive algorithm most commonly used in Batch System, In this type of system each process has Priority and according to Priority Process is executed By CPU If Two process has the same Priority then the first come first served to apply for executing the process.

Higher Priority is executed first and so on and the Priority of the process can be decided based on Memory requirements, and time requirements or there may be any other resource requirement. In simple words we can say that each and every process has a priority and based on priority process will be executed by the CPU or Processor.

## Priority Scheduling Algorithm

1. What is Priority Scheduling Algorithm?.
2. How to Calculate Turn Around Time?.
3. How to Calculate Waiting Time?.
4. Example.

### 1. What is Priority Scheduling Algorithm?.

Priority Scheduling Algorithm is a Non-Primitive algorithm In this Scheduling Algorithm priority is assigned for each and every process in the operating system and based upon some requirements(Memory, Time and Resource) process is executed and Higher Priority is Executed first and if the same Priority occurs then first come first serve to apply.

### 2. How to Calculate Turn-Around Time?.

Turn Around Time = Completion Time – Arrival Time
Total Turn Around Time = Turn Around Time / Total Number of Process

With the help of this formula, we can calculate a Turn Around Time of all processes in Queue.

### 3. How to Calculate Waiting Time?.

Waiting Time = (Turn Around Time – Burst Time)

Total Waiting Time = Waiting Time / Total Number of Process.

### 4. Priority Scheduling Algorithm Example

We are taking 4 processes whose CBT, or Burst Time Is 5, 3, 8, 6 and Priority Is 1, 2, 4, 3 respectively and the output is given below.

Enter The Number of Processes: 4

===============================
Enter The CBT (Burst Time) of the Process
===============================
5
3
8
6

========================
Enter The Priority of the Process
========================
1
2
4
3

=========================================
Process CBT(Burst Time) Wating Time Turn Around time
=========================================

P[3] 8 0 8
P[4] 6 8 14
P[2] 3 14 17
P[1] 5 17 22

Average Waiting Time: 9.75

Average Turn Around Time: 15.25

## Non-Preemptive Priority Scheduling Program in C++

``````#include<bits/stdc++.h>
using namespace std;

int main()
{
printf("=====================================");
printf("\nVisit - www.programmingwithbasics.com");
printf("\n=====================================");

/*  Here Integer Variables for loop, swapping and assigning values and float variables for waiting time, turn around time Average waiting time and Average turn around time.
*/

int n, I, j, temp, min;
float c1=0, c2=0, avwt, avtrt;

int *cbt  = new int[n];
int *p = new int[n];
int *s = new int[n];
int *wt = new int[n];
int *trt = new int[n];

/*  Here asking for the user to Enter the Number of Processes */

cout<<"\nEnter The Number of Processes: ";
cin>>n;

/* Storing the Burst time of each process in an array and process ID of the process. */

cout<<"\n=======================================================";
cout<<"\nEnter The CBT(Burst Time) of Processes";
cout<<"\n=======================================================\n";

for(i = 0; i< n; i++ )
{
cin>>cbt[i];
s[i] = i+1;
}

/* Storing the Priority of each process */

cout<<"\n========================================================";
cout<<"\nEnter The Priority of Process";
cout<<"\n========================================================\n";

for(i = 0; i< n; i++ )
{
cin>>p[i];
}

/* This is the main important part here we are sorting an array according to the priority given and if
we are sorting the priority array than the Burst Time array and the process ID array also needs to sort */

for(i = 0 ; i<n ; i++)
{
min = i;
for(j=i+1;j<n;j++)
{
if(p[j] > p[min] )
{
min=j;
}
}

temp = p[i];
p[i] = p[min];
p[min] = temp;

temp = cbt[i];
cbt[i] = cbt[min];
cbt[min] = temp;

temp = s[i];
s[i] = s[min];
s[min] = temp;
}

/* Starting waiting time of a process will be zero. */

wt[0] = 0;

/* Here We are calculating waiting time, after calculating the total waiting time we are calculating the Average Waiting time divide. Average Waiting Time = Total Waiting Time / Number of Processes. */

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]+=cbt[j];
}
c1+=wt[i];
}

avwt=c1/n; // Average Waitng Time

/* Here We are calculating turnaround time, after calculating the total turnaround time we are calculating the Average turnaround time divided. Average turnaround time = Total turnaround time / Number of Processes. */

for(i=0;i<n;i++)
{
trt[i]=cbt[i]+wt[i];     //calculate turnaround time
c2+=trt[i];
}
avtrt=c2/n; //Average turnaround time.

cout<<"\n====================================================================\n";
cout<<"Process\t"<<"CBT(Burst Time)\t"<<"  Wating Time\t"<<"Turn Around time";
cout<<"\n===================================================================\n";

for(i=0;i<n;i++)
{
cout<<"\nP["<<s[i]<<"]\t\t  "<<cbt[i]<<"\t\t    "<<wt[i]<<"\t\t\t"<<trt[i];
}

cout<<"\n==========================================================\n";
cout<<"\n\nAverage Waiting Time: "<<avwt;
cout<<"\n\nAverage Turn Around Time: "<<avtrt<<endl;

}``````

### More Scheduling Algorithm Programs in C++ with Gantt Chart

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.