Q:

C++ Program for Priority Scheduling Algorithm (Non Preemptive)

0
Explanation:-
 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 and If Two process has same Priority then first come first serve to apply for executing the process. Higher Priority is executed first and so on and Priority of the process can be decided based on Memory requirements, and time requirements or there may be any other resource requirement. In simple word we can say that each and every process has a priority and based on priority process will be executed by CPU or Processor.
 
1. What is Priority Scheduling Algorithm?.
2. How to Calculate Turn Around Time?.
3. How to Calculate Waiting Time?.
4. Example.
 
What is Priority Scheduling Algorithm?.
 
Priority Scheduling Algorithm is a Non-Primitive algorithm and 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 same Priority occurs then first come first serve to apply.
 
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 process in Queue.
 
How to Calculate Waiting Time?.
 
Waiting Time = (Turn Around Time – Burst Time)
 
Total Waiting Time = Waiting Time / Total Number of Process.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

int main()
{  
        
 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 user Enter Number of Process 
 */
 
 cout<<"\nEnter The Number of Process: ";
 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 the 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 priority given and if 
 we are sorting priority array than Burst Time array and process ID array also need 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 total waiting time we are calculating Average
 Waiting time divide. Average Waiting Time = Total Waiting Time / Number of Process.
 */ 
 
 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 total turnaround time we are calculating Average turnaround time divide. Average turnaround time = Total turnaround time / Number of Process.
 */
 
 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; 

}

 

Output:

Enter The Number of Processes: 4
 
========================================================
Enter The CBT(Burst Time) of Process
========================================================
5
3
8
6
 
========================================================
Enter The Priority of 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program For PRIORITY WITH PREEMPTIVE Schedulin... >>
<< C++ Program For First Come First Served (FCFS) Sch...