Program for Priority CPU Scheduling | Set 1

You are given the ‘N’ processes with their “burst times”, and the “arrival time” for all processes is ‘0’. You are also given the ‘priority’ of each process.

Your task is to find the “waiting time” and the “turn-around time” of each process using the ‘Priority CPU Scheduling’ algorithm.

Note:

1. The highest priority process will be scheduled first, followed by the next highest priority, and so on.

2. If the two processes have the same priority, then you have to run the process with the lowest number (id) first.
Input Format:
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the 'T' test cases follow. 

The first line of each test case contains a single positive integer, ‘N’, denoting the number of Processes.

The second line of each test case contains ‘N’ space-separated non-negative integer denoting the ‘burst’ time of each process.

The last line of each test case contains ‘N’ space-separated non-negative integers denoting the ‘priority’ of each process.
Output Format:
For each test case, return an array of arrays, where the 1st array will represent the ‘waiting time, and the 2nd array will denote the ‘turn-around time’ of each process.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^4
1 <= BURST[i],PRIORITY[i] <= 10^4

Time Limit: 1sec
CodingNinjas
author
2y
Sorting

Approach:

 

  • Recall the following:
    • Finish Time: The time at which the process complete and exits from the system.
    • Turn Around Time: The total time taken by the process. In other words, Turn Around time = Finish Time - Arrival Time.
    • Waiting Time: The total time taken by the process in the ready queue. In other words, Waiting Time = Turn Around Time - Burst Time.
  • We also have the ’priority’ of each process. The highest priority process will be scheduled first, followed by the next highest priority, and so on.
  • First, we need to calculate each process’s waiting time, and then we calculate the turn-around time of each process, which is nothing but the Waiting Time + Burst Time.
  • You can refer to this, to read more about the Priority CPU Scheduling.
     

Steps:

  • We can create a 2D array named 'PROCESS_DATA', where the 1st element of the ith row denotes the id of the process, the 2nd element of the ith row represents the burst time, and the 3rd element indicates the priority of the ith process.
  • Sort the array 'PROCESS_DATA' according to the priority in descending order.
  • Now, we have to apply only the FCFS algorithm.
  • Create 2 new arrays named WAITING_TIME[], and TURN_AROUND_TIME[].
  • Call the function FIND_WAITING_TIME(PROCESS_DATA, WAITING_DATA), and both these parameters are already defined.
  • Now, call the function FIND_TURN_AROUND_TIME(PROCESS_DATA, WAITING_TIME, TURN_AROUND_TIME).
  • After completing the above function calls, we have found the waiting and turn-around time of each process. So, we just need to add them into the new array and return that array.

 

void findWaitingTime(PROCESS_DATA[][], WAITING_TIME[]):

  • The first process in the array 'PROCESS_DATA' doesn’t have to wait, so WAITING_TIME[PROCESS_DATA[0][0]] = 0.
  • Now, the ith process will be executed after the completion of (i-1)th process. So, run a loop from ‘i’ = 1 to ‘i’ < |'PROCESS_DATA'|, in each iteration do:
    • Waiting time of ith will become the waiting time plus the burst time of (i-1)th process. So, WAITING_TIME[PROCESS_DATA[i][0]] = WAITING_TIME[PROCESS_DATA[i-1][0]] + PROCESS_DATA[i-1][1].

 

void findTurnAroundTime(PROCESS_DATA[][], WAITING_TIME[], TURN_AROUND_TIME[]):

  • The Turn around time is the summation of the ‘WAITING’ time and the ‘BURST’ time.
  • Run a loop from ‘i’ = 0 to ‘i’ < |PROCESS_DATA|, in each iteration do:
    • TURN_AROUND_TIME[PROCESS_DATA[i][0]] = PROCESS_DATA[i][1] + WAITING_TIME[PROCESS_DATA[i][0]].
Space Complexity: O(n)Explanation:

O(N), where ‘N’ is the number of processes.

 

As we are creating an extra array of the size ‘N’ to store the ‘burst time’ and ‘priority’ of each process. Hence, space complexity will be O(N).

Time Complexity: O(nlogn)Explanation:

O(N*logN), where ‘N’ is the number of processes.

 

We are sorting the process according to their ‘priority’, and the sorting takes O(N*logN) time.


C++ (g++ 5.4)
/*
Time Complexity: O(N*logN)
Space Complexity: O(N),

where 'N' is the number of processes.
*/

// This function is used to sort the processData vector.
bool compare(vector &a, vector &b)
{
if (a[2] != b[2])
{
return a[2] > b[2];
}

return a[0] < b[0];
}

// This function is used to find the waiting time of each process.
void findWaitingTime(vector> &processData, vector &waitingTime)
{
// The Waiting time of the highest priorty process is 0.
waitingTime[processData[0][0]] = 0;
// Fill the waiting time of the remaining processes.
for (int i = 1; i < processData.size(); i++)
{
waitingTime[processData[i][0]] = waitingTime[processData[i - 1][0]] + processData[i - 1][1];
}

}

// This function is used to find the turn around time of each process.
void findTurnAroundTime(vector > &processData, vector &waitingTime, vector &turnAroundTime)
{
// Turn around time is the summation of the waiting time and the burst time.
for (int i = 0; i < processData.size(); i++)
{
turnAroundTime[processData[i][0]] = processData[i][1] + waitingTime[processData[i][0]];
}

}

vector> priorityCPUScheduling(vector &burstTime, vector &priority)
{
int n = burstTime.size();
vector>processData(n, vector(3));
// Insert all the process data into the new vector.
for (int i = 0; i < n; i++)
{
processData[i][0] = i;
processData[i][1] = burstTime[i];
processData[i][2] = priority[i];
}

// Sort this vector according to the priority by the help of compare function.
sort(processData.begin(), processData.end(), compare);
// Create two new vectors for the Waiting time and the Trun Around time of each process.
vectorwaitingTime(n), turnAroundTime(n);
// Find the waiting time.
findWaitingTime(processData, waitingTime);
// Find the turn around time.
findTurnAroundTime(processData, waitingTime, turnAroundTime);

vector> result;
result.push_back(waitingTime);
result.push_back(turnAroundTime);
return result;
}

Java (SE 1.8)
/*

Time Complexity: O(N*logN)
Space Complexity: O(N),

where 'N' is the number of processes.
*/

import java.util.*;

public class Solution {
// This function is used to sort the processData vector.
public static class CustomComparator implements Comparator {

public int compare(int[] a, int[] b) {
if (a[2] != b[2])
{
return b[2] - a[2];
}

return a[0] - b[0];
}
}

// This function is used to find the waiting time of each process.
public static void findWaitingTime(int[][] processData, int[] waitingTime)
{
// The Waiting time of the highest priorty process is 0.
waitingTime[processData[0][0]] = 0;
// Fill the waiting time of the remaining processes.
for (int i = 1; i < processData.length; i++)
{
waitingTime[processData[i][0]] = waitingTime[processData[i - 1][0]] + processData[i - 1][1];
}

}

// This function is used to find the turn around time of each process.
public static void findTurnAroundTime(int[][] processData, int[] waitingTime, int[] turnAroundTime)
{
// Turn around time is the summation of the waiting time and the burst time.
for (int i = 0; i < processData.length; i++)
{
turnAroundTime[processData[i][0]] = processData[i][1] + waitingTime[processData[i][0]];
}

}

public static ArrayList> priorityCPUScheduling(ArrayList burstTime, ArrayList priority) {
int n = burstTime.size();
int[][] processData = new int[n][3];
// Insert all the process data into the new vector.
for (int i = 0; i < n; i++)
{
processData[i][0] = i;
processData[i][1] = burstTime.get(i);
processData[i][2] = priority.get(i);
}

// Sort this vector according to the priority by the help of compare function.

Arrays.sort(processData, new CustomComparator());
// Create two new vectors for the Waiting time and the Trun Around time of each process.

int[] waitingTime = new int[n];
int[] turnAroundTime = new int[n];

// Find the waiting time.
findWaitingTime(processData, waitingTime);

// Find the turn around time.
findTurnAroundTime(processData, waitingTime, turnAroundTime);

ArrayList> result = new ArrayList>();
ArrayList tmp = new ArrayList<>();
for(int i=0;i tmp.add(waitingTime[i]);
}

result.add(tmp);
tmp = new ArrayList();
for(int i=0;i tmp.add(turnAroundTime[i]);
}

result.add(tmp);
return result;
}
}

Python (3.5)
'''

Time Complexity: O(N*logN)
Space Complexity: O(N),

where N is the number of processes.
'''

from functools import cmp_to_key
# This function is used to sort the processData vector.
def compare(a, b):
if (a[2] != b[2]):
if a[2] < b[2]:
return 1
elif a[2] > b[2]:
return -1
else:
return 0


if a[0] > b[0]:
return 1
elif a[0] < b[0]:
return -1
else:
return 0


# This function is used to find the waiting time of each process.
def findWaitingTime(processData, waitingTime):

# The Waiting time of the highest priorty process is 0.
waitingTime[processData[0][0]] = 0
# Fill the waiting time of the remaining processes.
for i in range(1,len(processData)):
waitingTime[processData[i][0]] = waitingTime[processData[i - 1][0]] + processData[i - 1][1]


# This function is used to find the turn around time of each process.
def findTurnAroundTime(processData, waitingTime, turnAroundTime):

# Turn around time is the summation of the waiting time and the burst time.
for i in range(len(processData)):
turnAroundTime[processData[i][0]] = processData[i][1] + waitingTime[processData[i][0]]


def priorityCPUScheduling(burstTime, priority):

n = len(burstTime)
processData = [[0 for i in range(3)] for j in range(n)]

# Insert all the process data into the new vector.
for i in range(n):
processData[i][0] = i
processData[i][1] = burstTime[i]
processData[i][2] = priority[i]

# Sort this list according to the priority by the help of compare function.
processData = sorted(processData,key = cmp_to_key(compare))

# Create two new lists for the Waiting time and the Trun Around time of each process.
waitingTime = [0 for i in range(n)]
turnAroundTime = [0 for i in range(n)]
# Find the waiting time.
findWaitingTime(processData, waitingTime)
# Find the turn around time.
findTurnAroundTime(processData, waitingTime, turnAroundTime)

result = []
result.append(waitingTime)
result.append(turnAroundTime)
return result
Help your peers!
Add answer anonymously...
Paytm Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter