VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-for-preemptive-priority-cpu-scheduling/

⇱ Program for Preemptive Priority CPU Scheduling - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for Preemptive Priority CPU Scheduling

Last Updated : 24 Feb, 2024

Implementing priority CPU scheduling. In this problem, we are using Min Heap as the data structure for implementing priority scheduling. 

In this problem smaller numbers denote higher priority.

The following functions are used in the given code below: 

struct process {
processID,
burst time,
response time,
priority,
arrival time.
}
  • void quicksort(process array[], low, high): This function is used to arrange the processes in ascending order according to their arrival time. 
  • int partition(process array[], int low, int high): This function is used to partition the array for sorting. 
  • void insert(process Heap[], process value, int *heapsize, int *currentTime): It is used to include all the valid and eligible processes in the heap for execution. heapsize defines the number of processes in execution depending on the current time currentTime keeps record of the current CPU time. 
  • void order(process Heap[], int *heapsize, int start): It is used to reorder the heap according to priority if the processes after insertion of new process. 
  • void extractminimum(process Heap[], int *heapsize, int *currentTime): This function is used to find the process with highest priority from the heap. It also reorders the heap after extracting the highest priority process. 
  • void scheduling(process Heap[], process array[], int n, int *heapsize, int *currentTime): This function is responsible for executing the highest priority extracted from Heap[]. 
  • void process(process array[], int n): This function is responsible for managing the entire execution of the processes as they arrive in the CPU according to their arrival time.

Implementation:


Output
process id = 3 current time = 1
process id = 3 current time = 2
process id = 5 current time = 3
process id = 1 current time = 4
process id = 4 current time = 5
process id = 4 current time = 6
process id = 4 current time = 7
process id = 1 current time = 8
process id = 1 current time = 9
process id = 1 current time = 10
process id = 1 current time = 11
process id = 1 current time = 12
process id = 2 current time = 13
process id = 5 current time = 14
process id = 5 current time = 15
process id = 5 current time = 16
Average waiting time = 4.200000
Average response time =1.600000
Average turn around time = 7.400000

The output displays the order in which the processes are executed in the memory and also shows the average waiting time, average response time and average turn around time for each process.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Comment
Article Tags:
Article Tags: