C++: Priority Preemptive CPU Scheduling Algorithm

In Priority scheduling algorithm, a priority is associated with each process, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order

Priorities can be defined either internally or externally. Internally defined priorities use some measurable quantity or quantities to compute the priority of a process. For example, time limits, memory requirements, the number of open files, and the ratio of average I/O burst to average CPU burst have been used in computing priorities. External priorities are set by criteria outside the operating system, such as the importance of the process, the type and amount of funds being paid for computer use, the department sponsoring the work, and other, often political, factors.

A preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process. A nonpreemptive priority scheduling algorithm will simply put the new process at the head of the ready queue.

A major problem with priority scheduling algorithms is indefinite blocking, or starvation.

A solution to the problem of indefinite blockage of low-priority processes is aging. Aging involves gradually increasing the priority of processes that wait in the system for a long time.

Here we have used some features of C++11. Algorithm Library will help to understand them.


This program also uses the concept of Inheritance.

Data members and member functions which are common to all scheduling algorithm are declared in scheduling.h and member functions are declared in scheduling.cpp . virtual keyword is used before calcEndTime() because each scheduling algorithm has different algorithm to calculate end time.

Turnaround time and waiting time are calculated using these formulas.


turnAroundTime = endTime - arrivalTime
waitingTime = turnAroundTime - burstTime

Here is the C++ implementation.

You can compile with this command in Linux g++ -std=c++11 shortestjobfirst.cpp scheduling.cpp 
Output
Priority Preemptive Scheduling

                                                                  
                                                                  
Reference:
 

Other CPU Scheduling Algorithms
C++: Shortest Job First Preemptive CPU Scheduling Algorithm 
C++: Round Robin CPU Scheduling Algorithm  

Comments