The Wayback Machine - https://web.archive.org/web/20240816172750/https://www.geeksforgeeks.org/program-for-preemptive-priority-cpu-scheduling/
Open In App

Program for Preemptive Priority CPU Scheduling

Last Updated : 24 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

C++




// CPP program to implement preemptive priority scheduling
#include <bits/stdc++.h>
using namespace std;
 
struct Process {
    int processID;
    int burstTime;
    int tempburstTime;
    int responsetime;
    int arrivalTime;
    int priority;
    int outtime;
    int intime;
};
 
// 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 a record of
// the current CPU time.
void insert(Process Heap[], Process value, int* heapsize,
            int* currentTime)
{
    int start = *heapsize, i;
    Heap[*heapsize] = value;
    if (Heap[*heapsize].intime == -1)
        Heap[*heapsize].intime = *currentTime;
    ++(*heapsize);
 
    // Ordering the Heap
    while (start != 0
           && Heap[(start - 1) / 2].priority
                  > Heap[start].priority) {
        Process temp = Heap[(start - 1) / 2];
        Heap[(start - 1) / 2] = Heap[start];
        Heap[start] = temp;
        start = (start - 1) / 2;
    }
}
 
// It is used to reorder the heap according to
// priority if the processes after insertion
// of new process.
void order(Process Heap[], int* heapsize, int start)
{
    int smallest = start;
    int left = 2 * start + 1;
    int right = 2 * start + 2;
    if (left < *heapsize
        && Heap[left].priority
               < Heap[smallest].priority)
        smallest = left;
    if (right < *heapsize
        && Heap[right].priority
               < Heap[smallest].priority)
        smallest = right;
 
    // Ordering the Heap
    if (smallest != start) {
        Process temp = Heap[smallest];
        Heap[smallest] = Heap[start];
        Heap[start] = temp;
        order(Heap, heapsize, smallest);
    }
}
 
// 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.
Process extractminimum(Process Heap[], int* heapsize,
                       int* currentTime)
{
    Process min = Heap[0];
    if (min.responsetime == -1)
        min.responsetime
            = *currentTime - min.arrivalTime;
    --(*heapsize);
    if (*heapsize >= 1) {
        Heap[0] = Heap[*heapsize];
        order(Heap, heapsize, 0);
    }
    return min;
}
 
// Compares two intervals
// according to starting times.
bool compare(Process p1, Process p2)
{
    return (p1.arrivalTime < p2.arrivalTime);
}
 
// This function is responsible for executing
// the highest priority extracted from Heap[].
void scheduling(Process Heap[], Process array[], int n,
                int* heapsize, int* currentTime)
{
    if (heapsize == 0)
        return;
 
    Process min = extractminimum(
        Heap, heapsize, currentTime);
    min.outtime = *currentTime + 1;
    --min.burstTime;
    printf("process id = %d current time = %d\n",
           min.processID, *currentTime);
 
    // If the process is not yet finished
    // insert it back into the Heap*/
    if (min.burstTime > 0) {
        insert(Heap, min, heapsize, currentTime);
        return;
    }
 
    for (int i = 0; i < n; i++)
        if (array[i].processID == min.processID) {
            array[i] = min;
            break;
        }
}
 
// This function is responsible for
// managing the entire execution of the
// processes as they arrive in the CPU
// according to their arrival time.
void priority(Process array[], int n)
{
    sort(array, array + n, compare);
 
    int totalwaitingtime = 0, totalbursttime = 0,
        totalturnaroundtime = 0, i, insertedprocess = 0,
        heapsize = 0, currentTime = array[0].arrivalTime,
        totalresponsetime = 0;
 
    Process Heap[4 * n];
 
    // Calculating the total burst time
    // of the processes
    for (int i = 0; i < n; i++) {
        totalbursttime += array[i].burstTime;
        array[i].tempburstTime = array[i].burstTime;
    }
 
    // Inserting the processes in Heap
    // according to arrival time
    do {
        if (insertedprocess != n) {
            for (i = 0; i < n; i++) {
                if (array[i].arrivalTime == currentTime) {
                    ++insertedprocess;
                    array[i].intime = -1;
                    array[i].responsetime = -1;
                    insert(Heap, array[i],
                           &heapsize;, ¤tTime;);
                }
            }
        }
        scheduling(Heap, array, n,
                   &heapsize;, ¤tTime;);
        ++currentTime;
        if (heapsize == 0
            && insertedprocess == n)
            break;
    } while (1);
 
    for (int i = 0; i < n; i++) {
        totalresponsetime
            += array[i].responsetime;
        totalwaitingtime
            += (array[i].outtime
                - array[i].intime
                - array[i].tempburstTime);
        totalbursttime += array[i].burstTime;
    }
    printf("Average waiting time = %f\n",
           ((float)totalwaitingtime / (float)n));
    printf("Average response time =%f\n",
           ((float)totalresponsetime / (float)n));
    printf("Average turn around time = %f\n",
           ((float)(totalwaitingtime
                    + totalbursttime)
            / (float)n));
}
 
// Driver code
int main()
{
    int n, i;
    Process a[5];
    a[0].processID = 1;
    a[0].arrivalTime = 4;
    a[0].priority = 2;
    a[0].burstTime = 6;
    a[1].processID = 4;
    a[1].arrivalTime = 5;
    a[1].priority = 1;
    a[1].burstTime = 3;
    a[2].processID = 2;
    a[2].arrivalTime = 5;
    a[2].priority = 3;
    a[2].burstTime = 1;
    a[3].processID = 3;
    a[3].arrivalTime = 1;
    a[3].priority = 4;
    a[3].burstTime = 2;
    a[4].processID = 5;
    a[4].arrivalTime = 3;
    a[4].priority = 5;
    a[4].burstTime = 4;
    priority(a, 5);
    return 0;
}


Java




import java.util.Arrays;
import java.util.Comparator;
 
class Process {
    int processID;
    int arrivalTime;
    int priority;
    int burstTime;
    int tempBurstTime;
    int responseTime;
    int outTime;
    int inTime;
 
    Process(int processID, int arrivalTime, int priority, int burstTime) {
        this.processID = processID;
        this.arrivalTime = arrivalTime;
        this.priority = priority;
        this.burstTime = burstTime;
        this.tempBurstTime = burstTime;
        this.responseTime = -1;
        this.outTime = 0;
        this.inTime = -1;
    }
}
 
public class PriorityScheduling {
    // Function to insert a process into the heap
    static void insert(Process[] heap, Process value, int[] heapSize, int[] currentTime) {
        int start = heapSize[0];
        heap[heapSize[0]] = value;
        if (heap[heapSize[0]].inTime == -1) {
            heap[heapSize[0]].inTime = currentTime[0];
        }
        heapSize[0]++;
 
        // Ordering the Heap
        while (start != 0 && heap[(start - 1) / 2].priority > heap[start].priority) {
            Process temp = heap[(start - 1) / 2];
            heap[(start - 1) / 2] = heap[start];
            heap[start] = temp;
            start = (start - 1) / 2;
        }
    }
 
    // Function to reorder the heap based on priority
    static void order(Process[] heap, int heapSize, int start) {
        int smallest = start;
        int left = 2 * start + 1;
        int right = 2 * start + 2;
        if (left < heapSize && heap[left].priority < heap[smallest].priority) {
            smallest = left;
        }
        if (right < heapSize && heap[right].priority < heap[smallest].priority) {
            smallest = right;
        }
 
        // Ordering the Heap
        if (smallest != start) {
            Process temp = heap[smallest];
            heap[smallest] = heap[start];
            heap[start] = temp;
            order(heap, heapSize, smallest);
        }
    }
 
    // Function to extract the process with the highest priority from the heap
    static Process extractMinimum(Process[] heap, int[] heapSize, int[] currentTime) {
        Process minProcess = heap[0];
        if (minProcess.responseTime == -1) {
            minProcess.responseTime = currentTime[0] - minProcess.arrivalTime;
        }
        heapSize[0]--;
        if (heapSize[0] >= 1) {
            heap[0] = heap[heapSize[0]];
            order(heap, heapSize[0], 0);
        }
        return minProcess;
    }
 
    // Function to compare two processes based on arrival time
    static boolean compare(Process p1, Process p2) {
        return p1.arrivalTime < p2.arrivalTime;
    }
 
    // Function responsible for executing the highest priority process extracted from the heap
    static void scheduling(Process[] heap, Process[] array, int n, int[] heapSize, int[] currentTime) {
        if (heapSize[0] == 0) {
            return;
        }
 
        Process minProcess = extractMinimum(heap, heapSize, currentTime);
        minProcess.outTime = currentTime[0] + 1;
        minProcess.burstTime--;
 
        System.out.println("process id = " + minProcess.processID + " current time = " + currentTime[0]);
 
        // If the process is not yet finished, insert it back into the Heap
        if (minProcess.burstTime > 0) {
            insert(heap, minProcess, heapSize, currentTime);
            return;
        }
 
        for (int i = 0; i < n; i++) {
            if (array[i].processID == minProcess.processID) {
                array[i] = minProcess;
                break;
            }
        }
    }
 
    // Function responsible for managing the entire execution of processes based on arrival time
    static void priority(Process[] array, int n) {
        Arrays.sort(array, Comparator.comparingInt(o -> o.arrivalTime));
 
        int totalWaitingTime = 0;
        int totalBurstTime = 0;
        int totalTurnaroundTime = 0;
        int insertedProcess = 0;
        int[] heapSize = {0};
        int[] currentTime = {array[0].arrivalTime};
        int totalResponseTime = 0;
 
        Process[] heap = new Process[4 * n];
 
        // Calculating the total burst time of the processes
        for (int i = 0; i < n; i++) {
            totalBurstTime += array[i].burstTime;
            array[i].tempBurstTime = array[i].burstTime;
        }
 
        // Inserting the processes into Heap according to arrival time
        do {
            if (insertedProcess != n) {
                for (int i = 0; i < n; i++) {
                    if (array[i].arrivalTime == currentTime[0]) {
                        insertedProcess++;
                        array[i].inTime = -1;
                        array[i].responseTime = -1;
                        insert(heap, array[i], heapSize, currentTime);
                    }
                }
            }
            scheduling(heap, array, n, heapSize, currentTime);
            currentTime[0]++;
            if (heapSize[0] == 0 && insertedProcess == n) {
                break;
            }
        } while (true);
 
        for (int i = 0; i < n; i++) {
            totalResponseTime += array[i].responseTime;
            totalWaitingTime += (array[i].outTime - array[i].inTime - array[i].tempBurstTime);
            totalTurnaroundTime += (array[i].outTime - array[i].inTime);
            totalBurstTime += array[i].burstTime;
        }
 
        System.out.println("Average waiting time = " + ((float) totalWaitingTime / n));
        System.out.println("Average response time = " + ((float) totalResponseTime / n));
        System.out.println("Average turn around time = " + ((float) (totalWaitingTime + totalBurstTime) / n));
    }
 
    // Driver code
    public static void main(String[] args) {
        int n = 5;
        Process[] a = {
                new Process(1, 4, 2, 6),
                new Process(4, 5, 1, 3),
                new Process(2, 5, 3, 1),
                new Process(3, 1, 4, 2),
                new Process(5, 3, 5, 4)
        };
 
        priority(a, n);
    }
}


Python3




class Process:
    def __init__(self, processID, arrivalTime, priority, burstTime):
        self.processID = processID
        self.arrivalTime = arrivalTime
        self.priority = priority
        self.burstTime = burstTime
        self.tempburstTime = burstTime
        self.responsetime = -1
        self.outtime = 0
        self.intime = -1
 
 
def insert(Heap, value, heapsize, currentTime):
    start = heapsize[0]
    Heap[heapsize[0]] = value
    if Heap[heapsize[0]].intime == -1:
        Heap[heapsize[0]].intime = currentTime[0]
    heapsize[0] += 1
 
    # Ordering the Heap
    while start != 0 and Heap[(start - 1) // 2].priority > Heap[start].priority:
        Heap[(start - 1) // 2], Heap[start] = Heap[start], Heap[(start - 1) // 2]
        start = (start - 1) // 2
 
 
def order(Heap, heapsize, start):
    smallest = start
    left = 2 * start + 1
    right = 2 * start + 2
    if left < heapsize[0] and Heap[left].priority < Heap[smallest].priority:
        smallest = left
    if right < heapsize[0] and Heap[right].priority < Heap[smallest].priority:
        smallest = right
 
    # Ordering the Heap
    if smallest != start:
        Heap[start], Heap[smallest] = Heap[smallest], Heap[start]
        order(Heap, heapsize, smallest)
 
 
def extract_minimum(Heap, heapsize, currentTime):
    min_process = Heap[0]
    if min_process.responsetime == -1:
        min_process.responsetime = currentTime[0] - min_process.arrivalTime
    heapsize[0] -= 1
    if heapsize[0] >= 1:
        Heap[0] = Heap[heapsize[0]]
        order(Heap, heapsize, 0)
    return min_process
 
 
def compare(p1, p2):
    return p1.arrivalTime < p2.arrivalTime
 
 
def scheduling(Heap, array, n, heapsize, currentTime):
    if heapsize[0] == 0:
        return
 
    min_process = extract_minimum(Heap, heapsize, currentTime)
    min_process.outtime = currentTime[0] + 1
    min_process.burstTime -= 1
    print(f"process id = {min_process.processID} current time = {currentTime[0]}")
 
    # If the process is not yet finished, insert it back into the Heap
    if min_process.burstTime > 0:
        insert(Heap, min_process, heapsize, currentTime)
        return
 
    for i in range(n):
        if array[i].processID == min_process.processID:
            array[i] = min_process
            break
 
 
def priority(array, n):
    array.sort(key=lambda x: x.arrivalTime)
 
    total_waiting_time = 0
    total_burst_time = 0
    total_turnaround_time = 0
    inserted_process = 0
    heap_size = [0]
    current_time = [array[0].arrivalTime]
    total_response_time = 0
 
    Heap = [None] * (4 * n)
 
    # Calculating the total burst time of the processes
    for i in range(n):
        total_burst_time += array[i].burstTime
        array[i].tempburstTime = array[i].burstTime
 
    # Inserting the processes in Heap according to arrival time
    while True:
        if inserted_process != n:
            for i in range(n):
                if array[i].arrivalTime == current_time[0]:
                    inserted_process += 1
                    array[i].intime = -1
                    array[i].responsetime = -1
                    insert(Heap, array[i], heap_size, current_time)
        scheduling(Heap, array, n, heap_size, current_time)
        current_time[0] += 1
        if heap_size[0] == 0 and inserted_process == n:
            break
 
    for i in range(n):
        total_response_time += array[i].responsetime
        total_waiting_time += (array[i].outtime - array[i].intime - array[i].tempburstTime)
        total_turnaround_time += (array[i].outtime - array[i].intime)
        total_burst_time += array[i].burstTime
 
    print(f"Average waiting time = {total_waiting_time / n}")
    print(f"Average response time = {total_response_time / n}")
    print(f"Average turn around time = {total_turnaround_time / n}")
 
 
# Driver code
if __name__ == "__main__":
    n = 5
    a = [
        Process(1, 4, 2, 6),
        Process(4, 5, 1, 3),
        Process(2, 5, 3, 1),
        Process(3, 1, 4, 2),
        Process(5, 3, 5, 4)
    ]
    priority(a, n)


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class Process
{
    public int ProcessID { get; set; }
    public int ArrivalTime { get; set; }
    public int Priority { get; set; }
    public int BurstTime { get; set; }
    public int TempBurstTime { get; set; }
    public int ResponseTime { get; set; } = -1;
    public int OutTime { get; set; }
    public int InTime { get; set; } = -1;
 
    // Constructor to initialize a Process object
    public Process(int processID, int arrivalTime, int priority, int burstTime)
    {
        ProcessID = processID;
        ArrivalTime = arrivalTime;
        Priority = priority;
        BurstTime = burstTime;
        TempBurstTime = burstTime;
    }
}
 
class Program
{
    // Function to insert a process into the heap
    static void Insert(List<Process> heap, Process value, ref int heapSize, ref int currentTime)
    {
        int start = heapSize;
        heap.Add(value);
        if (heap[heapSize].InTime == -1)
            heap[heapSize].InTime = currentTime;
        heapSize++;
 
        // Ordering the Heap
        while (start != 0 && heap[(start - 1) / 2].Priority > heap[start].Priority)
        {
            Process temp = heap[(start - 1) / 2];
            heap[(start - 1) / 2] = heap[start];
            heap[start] = temp;
            start = (start - 1) / 2;
        }
    }
 
    // Function to reorder the heap based on priority
    static void Order(List<Process> heap, int heapSize, int start)
    {
        int smallest = start;
        int left = 2 * start + 1;
        int right = 2 * start + 2;
        if (left < heapSize && heap[left].Priority < heap[smallest].Priority)
            smallest = left;
        if (right < heapSize && heap[right].Priority < heap[smallest].Priority)
            smallest = right;
 
        // Ordering the Heap
        if (smallest != start)
        {
            Process temp = heap[smallest];
            heap[smallest] = heap[start];
            heap[start] = temp;
            Order(heap, heapSize, smallest);
        }
    }
 
    // Function to extract the process with the highest priority from the heap
    static Process ExtractMinimum(List<Process> heap, ref int heapSize, ref int currentTime)
    {
        Process minProcess = heap[0];
        if (minProcess.ResponseTime == -1)
            minProcess.ResponseTime = currentTime - minProcess.ArrivalTime;
        heapSize--;
        if (heapSize >= 1)
        {
            heap[0] = heap[heapSize];
            Order(heap, heapSize, 0);
        }
        return minProcess;
    }
 
    // Function to compare two processes based on arrival time
    static bool Compare(Process p1, Process p2)
    {
        return p1.ArrivalTime < p2.ArrivalTime;
    }
 
    // Function responsible for executing the highest priority process extracted from the heap
    static void Scheduling(List<Process> heap, List<Process> array, int n, ref int heapSize, ref int currentTime)
    {
        if (heapSize == 0)
            return;
 
        Process minProcess = ExtractMinimum(heap, ref heapSize, ref currentTime);
        minProcess.OutTime = currentTime + 1;
        minProcess.BurstTime--;
 
        Console.WriteLine($"process id = {minProcess.ProcessID} current time = {currentTime}");
 
        // If the process is not yet finished, insert it back into the Heap
        if (minProcess.BurstTime > 0)
        {
            Insert(heap, minProcess, ref heapSize, ref currentTime);
            return;
        }
 
        for (int i = 0; i < n; i++)
        {
            if (array[i].ProcessID == minProcess.ProcessID)
            {
                array[i] = minProcess;
                break;
            }
        }
    }
 
    // Function responsible for managing the entire execution of processes based on arrival time
    static void Priority(List<Process> array, int n)
    {
        array = array.OrderBy(p => p.ArrivalTime).ToList();
 
        int totalWaitingTime = 0;
        int totalBurstTime = 0;
        int totalTurnaroundTime = 0;
        int insertedProcess = 0;
        int heapSize = 0;
        int currentTime = array[0].ArrivalTime;
        int totalResponseTime = 0;
 
        List<Process> heap = new List<Process>(4 * n);
 
        // Calculating the total burst time of the processes
        for (int i = 0; i < n; i++)
        {
            totalBurstTime += array[i].BurstTime;
            array[i].TempBurstTime = array[i].BurstTime;
        }
 
        // Inserting the processes in Heap according to arrival time
        do
        {
            if (insertedProcess != n)
            {
                for (int i = 0; i < n; i++)
                {
                    if (array[i].ArrivalTime == currentTime)
                    {
                        insertedProcess++;
                        array[i].InTime = -1;
                        array[i].ResponseTime = -1;
                        Insert(heap, array[i], ref heapSize, ref currentTime);
                    }
                }
            }
            Scheduling(heap, array, n, ref heapSize, ref currentTime);
            currentTime++;
            if (heapSize == 0 && insertedProcess == n)
                break;
        } while (true);
 
        for (int i = 0; i < n; i++)
        {
            totalResponseTime += array[i].ResponseTime;
            totalWaitingTime += (array[i].OutTime - array[i].InTime - array[i].TempBurstTime);
            totalTurnaroundTime += (array[i].OutTime - array[i].InTime);
            totalBurstTime += array[i].BurstTime;
        }
 
        Console.WriteLine($"Average waiting time = {totalWaitingTime / (float)n}");
        Console.WriteLine($"Average response time = {totalResponseTime / (float)n}");
        Console.WriteLine($"Average turn around time = {totalTurnaroundTime / (float)n}");
    }
 
    static void Main(string[] args)
    {
        int n = 5;
        List<Process> a = new List<Process>
        {
            new Process(1, 4, 2, 6),
            new Process(4, 5, 1, 3),
            new Process(2, 5, 3, 1),
            new Process(3, 1, 4, 2),
            new Process(5, 3, 5, 4)
        };
 
        Priority(a, n);
    }
}


Javascript




class Process {
    constructor(processID, arrivalTime, priority, burstTime) {
        this.ProcessID = processID;
        this.ArrivalTime = arrivalTime;
        this.Priority = priority;
        this.BurstTime = burstTime;
        this.TempBurstTime = burstTime;
        this.ResponseTime = -1;
        this.OutTime = 0;
        this.InTime = -1;
    }
}
 
// Function to insert a process into the heap
function Insert(heap, value, heapSize, currentTime) {
    let start = heapSize[0];
    heap[start] = value;
    if (heap[start].InTime === -1) {
        heap[start].InTime = currentTime[0];
    }
    heapSize[0]++;
 
    // Ordering the Heap
    while (start !== 0 && heap[Math.floor((start - 1) / 2)].Priority > heap[start].Priority) {
        [heap[Math.floor((start - 1) / 2)], heap[start]] = [heap[start], heap[Math.floor((start - 1) / 2)]];
        start = Math.floor((start - 1) / 2);
    }
}
 
// Function to reorder the heap based on priority
function Order(heap, heapSize, start) {
    let smallest = start;
    let left = 2 * start + 1;
    let right = 2 * start + 2;
    if (left < heapSize[0] && heap[left].Priority < heap[smallest].Priority) {
        smallest = left;
    }
    if (right < heapSize[0] && heap[right].Priority < heap[smallest].Priority) {
        smallest = right;
    }
 
    // Ordering the Heap
    if (smallest !== start) {
        [heap[start], heap[smallest]] = [heap[smallest], heap[start]];
        Order(heap, heapSize, smallest);
    }
}
 
// Function to extract the process with the highest priority from the heap
function ExtractMinimum(heap, heapSize, currentTime) {
    let minProcess = heap[0];
    if (minProcess.ResponseTime === -1) {
        minProcess.ResponseTime = currentTime[0] - minProcess.ArrivalTime;
    }
    heapSize[0]--;
    if (heapSize[0] >= 1) {
        heap[0] = heap[heapSize[0]];
        Order(heap, heapSize, 0);
    }
    return minProcess;
}
 
// Function to compare two processes based on arrival time
function Compare(p1, p2) {
    return p1.ArrivalTime < p2.ArrivalTime;
}
 
// Function responsible for executing the highest priority process extracted from the heap
function Scheduling(heap, array, n, heapSize, currentTime) {
    if (heapSize[0] === 0) {
        return;
    }
 
    let minProcess = ExtractMinimum(heap, heapSize, currentTime);
    minProcess.OutTime = currentTime[0] + 1;
    minProcess.BurstTime--;
 
    console.log(`process id = ${minProcess.ProcessID} current time = ${currentTime[0]}`);
 
    // If the process is not yet finished, insert it back into the Heap
    if (minProcess.BurstTime > 0) {
        Insert(heap, minProcess, heapSize, currentTime);
        return;
    }
 
    for (let i = 0; i < n; i++) {
        if (array[i].ProcessID === minProcess.ProcessID) {
            array[i] = minProcess;
            break;
        }
    }
}
 
// Function responsible for managing the entire execution of processes based on arrival time
function Priority(array, n) {
    array.sort((p1, p2) => p1.ArrivalTime - p2.ArrivalTime);
 
    let totalWaitingTime = 0;
    let totalBurstTime = 0;
    let totalTurnaroundTime = 0;
    let insertedProcess = 0;
    let heapSize = [0];
    let currentTime = [array[0].ArrivalTime];
    let totalResponseTime = 0;
 
    let heap = Array(4 * n);
 
    // Calculating the total burst time of the processes
    for (let i = 0; i < n; i++) {
        totalBurstTime += array[i].BurstTime;
        array[i].TempBurstTime = array[i].BurstTime;
    }
 
    // Inserting the processes into Heap according to arrival time
    do {
        if (insertedProcess !== n) {
            for (let i = 0; i < n; i++) {
                if (array[i].ArrivalTime === currentTime[0]) {
                    insertedProcess++;
                    array[i].InTime = -1;
                    array[i].ResponseTime = -1;
                    Insert(heap, array[i], heapSize, currentTime);
                }
            }
        }
        Scheduling(heap, array, n, heapSize, currentTime);
        currentTime[0]++;
        if (heapSize[0] === 0 && insertedProcess === n) {
            break;
        }
    } while (true);
 
    for (let i = 0; i < n; i++) {
        totalResponseTime += array[i].ResponseTime;
        totalWaitingTime += (array[i].OutTime - array[i].InTime - array[i].TempBurstTime);
        totalTurnaroundTime += (array[i].OutTime - array[i].InTime);
        totalBurstTime += array[i].BurstTime;
    }
 
    console.log(`Average waiting time = ${totalWaitingTime / n}`);
    console.log(`Average response time = ${totalResponseTime / n}`);
    console.log(`Average turn around time = ${totalTurnaroundTime / n}`);
}
 
// Driver code
let n = 5;
let a = [
    new Process(1, 4, 2, 6),
    new Process(4, 5, 1, 3),
    new Process(2, 5, 3, 1),
    new Process(3, 1, 4, 2),
    new Process(5, 3, 5, 4)
];
 
Priority(a, n);
//This code is contributed by Kishan


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.



Similar Reads

Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
Prerequisite - CPU Scheduling Priority Scheduling : In priority scheduling, each process has a priority which is an integer value assigned to it. The smallest integer is considered as the highest priority and the largest integer is considered as the lowest priority. The process with the highest priority gets the CPU first. In rare systems, the larg
4 min read
Difference Between Preemptive and Non-Preemptive CPU Scheduling Algorithms
Preemptive scheduling permits a high-priority task to interrupt a running process, When a process transitions from the running to the ready or from the waiting to the ready states, preemptive scheduling is used. Whereas in non-preemptive scheduling, any new process must wait until the running process completes its CPU cycle. In this article, we wil
5 min read
Preemptive Priority CPU Scheduling Algorithm
Preemptive Priority CPU Scheduling Algorithm is a pre-emptive method of CPU scheduling algorithm that works based on the priority of a process. In this algorithm, the scheduler schedules the tasks to work as per the priority, which means that a higher priority process should be executed first. In case of any conflict, i.e., when there is more than
15+ min read
Preemptive and Non-Preemptive Scheduling
In operating systems, scheduling is the method by which processes are given access to system resources, primarily the CPU. Efficient scheduling is essential for optimal system performance and user satisfaction. There are two primary types of CPU scheduling: preemptive and non-preemptive. Understanding the differences between preemptive and non-pree
6 min read
Difference between Priority Scheduling and Round Robin (RR) CPU scheduling
1. Priority Scheduling Algorithm : Priority scheduling algorithm executes the processes depending upon their priority. Each process is allocated a priority and the process with the highest priority is executed first. Priorities can be defined internally as well as externally. Internal priorities are decided by the system depending upon the number o
3 min read
Difference between Priority scheduling and Shortest Job First (SJF) CPU scheduling
1. Priority Scheduling Algorithm : Priority scheduling algorithm executes the processes depending upon their priority. Each process is allocated a priority and the process with the highest priority is executed first. Priorities can be defined internally as well as externally. Internal priorities are decided by the system depending upon the number o
3 min read
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling: Shortest Job first has the advantage of having a minimum average waiting time
13 min read
Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment Tree
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.   Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.It is a Greedy Algorithm.It may cause starvation i
15+ min read
Longest Remaining Time First (LRTF) or Preemptive Longest Job First CPU Scheduling Algorithm
Longest Remaining Time First (LRTF) is a preemptive version of Longest Job First (LJF) scheduling algorithm. In this scheduling algorithm, we find the process with the maximum remaining time and then process it, i.e. check for the maximum remaining time after some interval of time(say 1 unit each) to check if another process having more Burst Time
7 min read
Relation in FCFS and Preemptive Priority Scheduling Algorithm
In this article, we will see how FCFS is a special kind of Priority Preemptive Scheduling Algorithm. Also, we will cover the relation with each other. Let us discuss one by one. 1. First Come First Serve (FCFS) Scheduling Algorithm : FCFS is the simplest of CPU Scheduling Algorithm which executes the process that comes first. It is a non-preemptive
4 min read
Relation between Preemptive Priority and Round Robin Scheduling Algorithm
In this article, we'll try to establish the relation between Round Robin and Preemptive Priority Scheduling Algorithm. Let's discuss the algorithms one by one first and then establish how Round Robin is a special kind of Preemptive Priority Scheduling Algorithm. Prerequisite - CPU Scheduling | Difference between Priority and Round Robin (RR) Schedu
2 min read
Program for Priority CPU Scheduling | Set 1
Priority scheduling is one of the most common scheduling algorithms in batch systems. Each process is assigned a priority. The process with the highest priority is to be executed first and so on. Processes with the same priority are executed on a first-come first served basis. Priority can be decided based on memory requirements, time requirements
11 min read
Difference between Preemptive and Non-Preemptive Kernel in OS
Introduction : A kernel is the central component of an operating system that manages system resources and provides a layer of abstraction between the hardware and software. One of the key design decisions for a kernel is whether it is preemptive or non-preemptive. A non-preemptive kernel, also known as a cooperative kernel, relies on processes yiel
4 min read
Difference between FCFS and Priority CPU scheduling
1. First Come First Served (FCFS) : First Come First Served (FCFS) is the simplest type of algorithm. It is a non-preemptive algorithm i.e. the process cannot be interrupted once it starts executing. The FCFS is implemented with the help of a FIFO queue. The processes are put into the ready queue in the order of their arrival time. The process that
3 min read
Priority CPU Scheduling with different arrival time - Set 2
Prerequisite - Program for Priority Scheduling - Set 1Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. Each process is assigned first arrival time (less arrival time process first) if two processes have same arrival time, then compare to priorities (highest process first). Also, if
11 min read
Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling
Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling is like Multilevel Queue(MLQ) Scheduling but in this process can move between the queues. And thus, much more efficient than multilevel queue scheduling. Characteristics of Multilevel Feedback Queue Scheduling: In a multilevel queue-scheduling algorithm, processes are permanently assigned t
5 min read
Difference between Multi Level Queue Scheduling (MLQ) and Priority Scheduling
1. Multi Level Queue Scheduling (MLQ) : It is quite difficult to have just one queue and schedule all processes. This is where multi-level queue scheduling is used. In this, processes are divided into various classes depending upon property of processes such as system process, I/O process, etc. Thus we get 'n' number of queues for n classes of proc
3 min read
Implementation of Non-Preemptive Shortest Job First using Priority Queue
Read here for Shortest Job First Scheduling algorithm for same arrival times.Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next.In this article, we will implement the Shortest Job First Scheduling algorithm (SJF) using a priority queue, so that we c
12 min read
Shortest Remaining Time First (Preemptive SJF) Scheduling Algorithm
In previous post, we have discussed Set 1 of SJF i.e. non-pre-emptive. In this post we will discuss the pre-emptive version of SJF known as Shortest Remaining Time First (SRTF). In the Shortest Remaining Time First (SRTF) scheduling algorithm, the process with the smallest amount of time remaining until completion is selected to execute. Since the
15+ min read
Longest Remaining Time First (LRTF) CPU Scheduling Program
We have given some processes with arrival time and Burst Time and we have to find the completion time (CT), Turn Around Time(TAT), Average Turn Around Time (Avg TAT), Waiting Time(WT), Average Waiting Time (AWT) for the given processes. Prerequisite: CPU Scheduling | Longest Remaining Time First (LRTF) algorithm LRTF is a preemptive scheduling algo
15+ min read
Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times)
We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenarios, when processes have different arrival times, are discussed. Given n processes with their burst times and arrival times, the task is to find the average waiting time and an average turn around time using FCFS scheduling algorithm. FIFO simply queu
15+ min read
Program for FCFS CPU Scheduling | Set 1
First come - First served (FCFS), is the simplest scheduling algorithm. FIFO simply queues processes according to the order they arrive in the ready queue. In this algorithm, the process that comes first will be executed first and next process starts only after the previous gets fully executed.  Terminologies Used in CPU Scheduling Arrival Time: Th
15+ min read
Difference Between User-CPU-Time and System-CPU-Time in UNIX
Unix systems have a time utility that allows a user to see where their application took significant time to process. The syntax of this utility is as follows: time &lt;command-to-be-timed&gt;Its result generally has three categories as follows: real &lt;time&gt; user &lt;time&gt; sys &lt;time&gt;User CPU time and system CPU time are both measures o
3 min read
Difference between Priority Inversion and Priority Inheritance
Both of these concepts come under Priority scheduling in Operating System. But are they the same? In one line, Priority Inversion is a problem while Priority Inheritance is a solution. Priority Inversion means that the priority of tasks gets inverted and Priority Inheritance means that the priority of tasks gets inherited. Both of these phenomena h
2 min read
Should we declare as Queue or Priority Queue while using Priority Queue in Java?
Queue: Queue is an Interface that extends the collection Interface in Java and this interface belongs to java.util package. A queue is a type of data structure that follows the FIFO (first-in-first-out ) order. The queue contains ordered elements where insertion and deletion of elements are done at different ends. Priority Queue and Linked List are
3 min read
What is Priority Queue | Introduction to Priority Queue
A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher priority values are typically retrieved or removed before elements with lower priority values. Each element has a priority value associated with it. When we add an item, it is inserted in a position based on its priority value. There are
15+ min read
Operating Systems | CPU Scheduling | Question 1
Consider three processes (process id 0, 1, 2 respectively) with compute time bursts 2, 4 and 8 time units. All processes arrive at time zero. Consider the longest remaining time first (LRTF) scheduling algorithm. In LRTF ties are broken by giving priority to the process with the lowest process id. The average turn around time is: (A) 13 units (B) 1
1 min read
Operating Systems | CPU Scheduling | Question 2
Consider three processes, all arriving at time zero, with total execution time of 10, 20 and 30 units, respectively. Each process spends the first 20% of execution time doing I/O, the next 70% of time doing computation, and the last 10% of time doing I/O again. The operating system uses a shortest remaining compute time first scheduling algorithm a
2 min read
Operating Systems | CPU Scheduling | Question 3
Consider three CPU-intensive processes, which require 10, 20 and 30 time units and arrive at times 0, 2 and 6, respectively. How many context switches are needed if the operating system implements a shortest remaining time first scheduling algorithm? Do not count the context switches at time zero and at the end. (A) 1 (B) 2 (C) 3 (D) 4 Answer: (B)
1 min read
Operating Systems | CPU Scheduling | Question 4
Which of the following process scheduling algorithm may lead to starvation (A) FIFO (B) Round Robin (C) Shortest Job Next (D) None of the above Answer: (C) Explanation: Shortest job next may lead to process starvation for processes which will require a long time to complete if short processes are continually added.Quiz of this Question
1 min read
three90RightbarBannerImg