Priority Based Process Scheduling in operating systems.
Priority scheduling is a preemptive algorithm so processes priority matters.
Each process has a priority number.
The process with high priority always gets the CPU on a priority basis.
Processes with the same priority are executed according to the first come first served(FCFS)scheduling algorithm.
Preferred in batch systems.
Process | Burst Time | Priority |
P1 | 4 | 2nd |
P2 | 2 | 3rd |
P3 | 8 | 1st |
P4 | 3 | 4th |
Gantt Chart of Preemptive Scheduling
Let us see the Gantt Chart of Preemptive Scheduling.
Process | Waiting Time |
P1 | 8 |
P2 | 12 |
P3 | 0 |
P4 | 14 |
Average Wait Time: (0+8+12+14) / 4 = 8.5
Priority Based Process Scheduling Program in C++ (C Plus Plus)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include<iostream> using namespace std; int main() { int x; int n; //n is number of process int p[10]; //p is process int ProcessesPriority[10],BurstTime[10]; int w[10]; //w is wait time int t[10]; // t is turnaround time int AverageWaitingTime,AverageTurnAroundTime,i; cout<<"Enter total number of process : "; cin>>n; cout<<"\n\t Enter burst time : time priorities \n"; for(i=0;i<n;i++) { cout<<"\nProcess["<<i+1<<"]:"; cin>>BurstTime[i]>>ProcessesPriority[i]; p[i]=i+1; } //sorting on the basis of priority for(i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(ProcessesPriority[i]<ProcessesPriority[j]) { x=ProcessesPriority[i]; ProcessesPriority[i]=ProcessesPriority[j]; ProcessesPriority[j]=x; x=BurstTime[i]; BurstTime[i]=BurstTime[j]; BurstTime[j]=x; x=p[i]; p[i]=p[j]; p[j]=x; } } } w[0]=0; AverageWaitingTime=0; t[
0]=BurstTime[0]; AverageTurnAroundTime=t[0]; for(i=1;i<n;i++) { w[i]=t[i-1]; AverageWaitingTime+=w[i]; t[i]=w[i]+BurstTime[i]; AverageTurnAroundTime+=t[i]; } //Show the process cout<<"\n\nProcess \t Burst Time \t Wait Time \t Turn Around Time Priority \n"; for(i=0;i<n;i++) cout<<"\n "<<p[i]<<"\t\t "<<BurstTime[i]<<"\t\t "<<w[i]<<"\t\t "<<t[i]<<"\t\t "<<ProcessesPriority[i]<<"\n"; AverageWaitingTime/=n; AverageTurnAroundTime/=n; cout<<"\n Average Wait Time : "<<AverageWaitingTime; cout<<"\n Average Turn Around Time : "<<AverageTurnAroundTime; } |
Output
Priority scheduling using friend class and friend function
Let us see the Priority scheduling using friend class and friend function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#include<iostream> using namespace std; class priority { protected: int burst[20],p[20],wt[20],tat[20],Priority[20],i,j,n,total,pos,temporary,avg_total,sort; float avg_wait; public: <a href="https://t4tutorials.com/c-friend-class/" >friend class</a> schedule; priority() { i=0; j=0; n=0; total=0; temporary=0; avg_wait=0; avg_total=0; sort=0; } void Process() { cout<<"Process count:"; cin>>n; } void burst_time() { cout<<"\nBurst and priority of Process\n"; for(i=0;i<n;i++) { cout<<"\nP["<<i+1<<"]\n"; cout<<"Burst Time:"; cin>>burst[i]; cout<<"Priority:"; cin>>Priority[i]; p[i]=i+1; } } void sorting() { for(i=0;i<n;i++) { sort=i; for(j=i+1;j<n;j++) { if(Priority[j]<Priority[sort]) sort=j; } temporary=Priority[i]; Priority[i]=Priority[sort]; Priority[sort]=temporary; temporary=burst[i]; burst[i]=burst[sort]; burst[sort]=temporary; temporary=p[i]; p[i]=p[sort]; p[sort]=temporary; } } void wait_time() { wt[0]=0; for(i=1;i<n;i++) { wt[i]=0; for(j=0;j<i;j++) wt[i]+=burst[j]; total+=wt[i]; avg_wait=total/n; } } }; class schedule { public: friend class priority; void display(priority f) { cout<<"\nProcess\t Burst_Time \tWaiting_Time\tTurn_around_Time"; for(f.i=0;f.i<f.n;f.i++) { f.total=0; f.tat[f.i]=f.burst[f.i]+f.wt[f.i]; f.total+=f.tat[f.i]; cout<<"\nP["<<f.p[f.i]<<"]\t\t "<<f.burst[f.i]<<"\t\t "<<f.wt[f.i]<<"\t\t\t"<<f.tat[f.i]; } f.avg_total=f.total/f.n; cout<<"\n\nAvg Waiting_Time="<<f.avg_wait; cout<<"\nAvg Turnaround_Time="<<f.avg_total; } }; int main() { priority obj; schedule obj1; obj.Process(); obj.burst_time(); obj.sorting(); obj.wait_time(); obj1.display(obj); return 0; } |