Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. Each process is assigned a priority. Process with the highest priority is to be executed first and so on.
Processes with the same priority are executed on first come first served basis. Priority can be decided based on memory requirements, time requirements or any other resource requirement.

Implementation :

1- First input the processes with their burst time 
   and priority.
2- Sort the processes, burst time and priority
   according to the priority.
3- Now simply apply FCFS algorithm.
[ad type=”banner”]

Program for Priority Scheduling | Set 1

Note: A major problem with priority scheduling is indefinite blocking or starvation. A solution to the problem of indefinite blockage of the low-priority process is aging. Aging is a technique of gradually increasing the priority of processes that wait in the system for a long period of time.

[ad type=”banner”]

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20implementation%20of%20FCFS%0A%2F%2F%20scheduling%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Astruct%20Process%0A%7B%0A%20%20%20%20int%20pid%3B%20%20%2F%2F%20Process%20ID%0A%20%20%20%20int%20bt%3B%20%20%20%2F%2F%20CPU%20Burst%20time%20required%0A%20%20%20%20int%20priority%3B%20%2F%2F%20Priority%20of%20this%20process%0A%7D%3B%0A%20%0A%2F%2F%20Function%20to%20sort%20the%20Process%20acc.%20to%20priority%0Abool%20comparison(Process%20a%2C%20Process%20b)%0A%7B%0A%20%20%20%20return%20(a.priority%20%3E%20b.priority)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20the%20waiting%20time%20for%20all%0A%2F%2F%20processes%0Avoid%20findWaitingTime(Process%20proc%5B%5D%2C%20int%20n%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20wt%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20waiting%20time%20for%20first%20process%20is%200%0A%20%20%20%20wt%5B0%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20calculating%20waiting%20time%0A%20%20%20%20for%20(int%20%20i%20%3D%201%3B%20i%20%3C%20n%20%3B%20i%2B%2B%20)%0A%20%20%20%20%20%20%20%20wt%5Bi%5D%20%3D%20%20proc%5Bi-1%5D.bt%20%2B%20wt%5Bi-1%5D%20%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20calculate%20turn%20around%20time%0Avoid%20findTurnAroundTime(%20Process%20proc%5B%5D%2C%20int%20n%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20wt%5B%5D%2C%20int%20tat%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20calculating%20turnaround%20time%20by%20adding%0A%20%20%20%20%2F%2F%20bt%5Bi%5D%20%2B%20wt%5Bi%5D%0A%20%20%20%20for%20(int%20%20i%20%3D%200%3B%20i%20%3C%20n%20%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20tat%5Bi%5D%20%3D%20proc%5Bi%5D.bt%20%2B%20wt%5Bi%5D%3B%0A%7D%0A%20%0A%2F%2FFunction%20to%20calculate%20average%20time%0Avoid%20findavgTime(Process%20proc%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20wt%5Bn%5D%2C%20tat%5Bn%5D%2C%20total_wt%20%3D%200%2C%20total_tat%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2FFunction%20to%20find%20waiting%20time%20of%20all%20processes%0A%20%20%20%20findWaitingTime(proc%2C%20n%2C%20wt)%3B%0A%20%0A%20%20%20%20%2F%2FFunction%20to%20find%20turn%20around%20time%20for%20all%20processes%0A%20%20%20%20findTurnAroundTime(proc%2C%20n%2C%20wt%2C%20tat)%3B%0A%20%0A%20%20%20%20%2F%2FDisplay%20processes%20along%20with%20all%20details%0A%20%20%20%20cout%20%3C%3C%20%22%5CnProcesses%20%20%22%3C%3C%20%22%20Burst%20time%20%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20%22%20Waiting%20time%20%20%22%20%3C%3C%20%22%20Turn%20around%20time%5Cn%22%3B%0A%20%0A%20%20%20%20%2F%2F%20Calculate%20total%20waiting%20time%20and%20total%20turn%0A%20%20%20%20%2F%2F%20around%20time%0A%20%20%20%20for%20(int%20%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20total_wt%20%3D%20total_wt%20%2B%20wt%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20total_tat%20%3D%20total_tat%20%2B%20tat%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22%20%20%20%22%20%3C%3C%20proc%5Bi%5D.pid%20%3C%3C%20%22%5Ct%5Ct%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%3C%20proc%5Bi%5D.bt%20%3C%3C%20%22%5Ct%20%20%20%20%22%20%3C%3C%20wt%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%3C%20%22%5Ct%5Ct%20%20%22%20%3C%3C%20tat%5Bi%5D%20%3C%3Cendl%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnAverage%20waiting%20time%20%3D%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20(float)total_wt%20%2F%20(float)n%3B%0A%20%20%20%20cout%20%3C%3C%20%22%5CnAverage%20turn%20around%20time%20%3D%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20(float)total_tat%20%2F%20(float)n%3B%0A%7D%0A%20%0Avoid%20priorityScheduling(Process%20proc%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Sort%20processes%20by%20priority%0A%20%20%20%20sort(proc%2C%20proc%20%2B%20n%2C%20comparison)%3B%0A%20%0A%20%20%20%20cout%3C%3C%20%22Order%20in%20which%20processes%20gets%20executed%20%5Cn%22%3B%0A%20%20%20%20for%20(int%20%20i%20%3D%200%20%3B%20i%20%3C%20%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20proc%5Bi%5D.pid%20%3C%3C%22%20%22%20%3B%0A%20%0A%20%20%20%20findavgTime(proc%2C%20n)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main()%0A%7B%0A%20%20%20%20Process%20proc%5B%5D%20%3D%20%7B%7B1%2C%2010%2C%202%7D%2C%20%7B2%2C%205%2C%200%7D%2C%20%7B3%2C%208%2C%201%7D%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof%20proc%20%2F%20sizeof%20proc%5B0%5D%3B%0A%20%20%20%20priorityScheduling(proc%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

[ad type=”banner”]

Output:

Order in which processes gets executed 
1 3 2 
Processes  Burst time  Waiting time  Turn around time
 1		10	 0		 10
 3		8	 10		 18
 2		5	 18		 23

Average waiting time = 9.33333
Average turn around time = 17