Completion time of a given process in round robin
Last Updated :
03 Aug, 2022
We are given n-processes with their completion times in form of an array. We need to find the time instant when a given process p ends if the scheduling process is round robin and time slice is 1-sec.
note : Array index start with 0.
Examples :
Input : arr[] = {3, 2, 4, 2}, p = 1
Output : Completion time = 6
Explanation : Snap of process for every second is as:
Time | Process Array
0 | {3, 2, 4, 2}
1 | {2, 2, 4, 2}
2 | {2, 1, 4, 2}
3 | {2, 1, 3, 2}
4 | {2, 1, 3, 1}
5 | {1, 1, 3, 1}
6 | {1, 0, 3, 1}
Input : arr[] = {2, 4, 1, 3}, p = 2
Output :Completion time = 3
Explanation : Snap of process for every second is as:
Time | Process Array
0 | {2, 4, 1, 3}
1 | {1, 4, 1, 3}
2 | {1, 3, 1, 3}
3 | {1, 3, 0, 3}
Brute Force :The basic approach for solving this problem is to apply round robin algorithm with time slice 1. But the time complexity of that approach will be O(?Ai) i.e. summation of all process's time, which is quite high.
Efficient Approach: The idea is based on below observations.
- All processes with CPU time less than arr[p] would complete before arr[p]. We simply need to add time of these processes.
- We also need to add time of arr[p].
- For every process x with CPU time more than arr[p], two cases arise :
- If x is on left of arr[p] (scheduled before arr[p]), then this process takes arr[p] time of CPU before p finishes.
- If x is on right of arr[p] (scheduled after arr[p]), then this process takes arr[p]-1 time of CPU before p finishes.
Algorithm :
time_req = 0;
// Add time for process on left of p
// (Scheduled before p in a round of
// 1 unit time slice)
for (int i=0; i<p; i++)
{
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}
// step 2 : Add time of process p
time_req += arr[p];
// Add time for process on right
// of p (Scheduled after p in
// a round of 1 unit time slice)
for (int i=p+1; i<n; i++)
{
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p]-1;
}
Implementation:
C++
// Program to find end time of a process
// p in round robin scheduling with unit
// time slice.
#include <bits/stdc++.h>
using namespace std;
// Returns completion time of p.
int completionTime(int arr[], int n, int p) {
// Initialize result
int time_req = 0;
// Step 1 : Add time of processes on left
// of p (Scheduled before p)
for (int i = 0; i < p; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}
// Step 2 : Add time of p
time_req += arr[p];
// Step 3 : Add time of processes on right
// of p (Scheduled after p)
for (int i = p + 1; i < n; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p] - 1;
}
return time_req;
}
// driver program
int main() {
int arr[] = {3, 5, 2, 7, 6, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int p = 2;
cout << "Completion time = "
<< completionTime(arr, n, p);
return 0;
}
Java
// Program to find end time of a process
// p in round robin scheduling with unit
// time slice.
class GFG
{
// Returns completion time of p.
static int completionTime(int arr[], int n, int p) {
// Initialize result
int time_req = 0;
// Step 1 : Add time of processes on left
// of p (Scheduled before p)
for (int i = 0; i < p; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}
// Step 2 : Add time of p
time_req += arr[p];
// Step 3 : Add time of processes on right
// of p (Scheduled after p)
for (int i = p + 1; i < n; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p] - 1;
}
return time_req;
}
// Driver code
public static void main (String[] args)
{
int arr[] = {3, 5, 2, 7, 6, 1};
int n =arr.length;;
int p = 2;
System.out.print("Completion time = "+
completionTime(arr, n, p));
}
}
// This code is contributed by Anant Agarwal.
Python
# Program to find end time of a process
# p in round robin scheduling with unit
# time slice.
# Returns completion time of p.
def completionTime(arr, n, p) :
# Initialize result
time_req = 0
# Step 1 : Add time of processes on
# left of p (Scheduled before p)
for i in range(0, p):
if (arr[i] < arr[p]):
time_req += arr[i]
else:
time_req += arr[p]
# Step 2 : Add time of p
time_req += arr[p]
# Step 3 : Add time of processes on
# right of p (Scheduled after p)
for i in range(p + 1, n):
if (arr[i] < arr[p]):
time_req += arr[i]
else:
time_req += arr[p] - 1
return time_req
# driver program
arr = [3, 5, 2, 7, 6, 1]
n = len(arr)
p = 2
print("Completion time =",
completionTime(arr, n, p))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to find end time of a process
// p in round robin scheduling with unit
// time slice.
using System;
class GFG {
// Returns completion time of p.
static int completionTime(int []arr,
int n, int p)
{
// Initialize result
int time_req = 0;
// Step 1 : Add time of processes
// on left of p (Scheduled before p)
for (int i = 0; i < p; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}
// Step 2 : Add time of p
time_req += arr[p];
// Step 3 : Add time of processes on
// right of p (Scheduled after p)
for (int i = p + 1; i < n; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p] - 1;
}
return time_req;
}
// Driver code
public static void Main ()
{
int []arr = {3, 5, 2, 7, 6, 1};
int n =arr.Length;;
int p = 2;
Console.WriteLine("Completion time = "+
completionTime(arr, n, p));
}
}
// This code is contributed by vt_m.
PHP
<?php
// Program to find end time
// of a process p in round
// robin scheduling with
// unit time slice.
// Returns completion time of p.
function completionTime($arr, $n, $p)
{
// Initialize result
$time_req = 0;
// Step 1 : Add time of processes on
// left of p (Scheduled before p)
for ($i = 0; $i < $p; $i++)
{
if ($arr[$i] < $arr[$p])
$time_req += $arr[$i];
else
$time_req += $arr[$p];
}
// Step 2 : Add time of p
$time_req += $arr[$p];
// Step 3 : Add time of processes on
// right of p (Scheduled after p)
for ($i = $p + 1; $i < $n; $i++)
{
if ($arr[$i] < $arr[$p])
$time_req += $arr[$i];
else
$time_req += $arr[$p] - 1;
}
return $time_req;
}
// Driver Code
$arr = array(3, 5, 2, 7, 6, 1);
$n = count($arr);
$p = 2;
echo"Completion time = " ,
completionTime($arr, $n, $p);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Program to find end time of a process
// p in round robin scheduling with unit
// time slice.
// Returns completion time of p.
function completionTime(arr, n, p) {
// Initialize result
var time_req = 0;
// Step 1 : Add time of processes on left
// of p (Scheduled before p)
for (var i = 0; i < p; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p];
}
// Step 2 : Add time of p
time_req += arr[p];
// Step 3 : Add time of processes on right
// of p (Scheduled after p)
for (var i = p + 1; i < n; i++) {
if (arr[i] < arr[p])
time_req += arr[i];
else
time_req += arr[p] - 1;
}
return time_req;
}
// driver program
var arr = [3, 5, 2, 7, 6, 1];
var n = arr.length;
var p = 2;
document.write( "Completion time = "
+ completionTime(arr, n, p));
// This code is contributed by noob2000.
</script>
OutputCompletion time = 9
Time Complexity : O(n)
Similar Reads
Find the order of execution of given N processes in Round Robin Scheduling Given an array arr[] representing burst time of N processes scheduled using the Round Robin Algorithm with given quantum time Q. Assuming that all the process arrive at time t = 0, the task is to find the order in which the process execute. Examples: Input: arr[] = {3, 7, 4}, q = 3Output: 0 2 1Expla
12 min read
Find the time taken finish Processing of given processes Given N processes and two arrays, arr1[] and arr2[] of size N each. arr1[] contains time spent by any process in critical section and arr2[] denotes time taken by a process to complete processing after coming out of the critical section. The task is to find the time taken by all the processes to com
6 min read
Minimum operations to complete all tasks Given an array arr[] representing the time required to complete n tasks. Each task has a specified time required for its completion. In each operation, one task is selected as the primary task and is executed for x units of time, while all other tasks are executed for y units of time, where y < x
4 min read
Round Robin Scheduling Algorithm with Different Arrival Time Round Robin Scheduling is one of the most popular CPU scheduling algorithms used in operating systems. This algorithm is designed to handle processes efficiently by assigning a fixed time slice or quantum to each process. However, when processes arrive at different times, the scheduling becomes slig
15+ min read
Round Robin Scheduling Algorithm with Different Arrival Time Round Robin Scheduling is one of the most popular CPU scheduling algorithms used in operating systems. This algorithm is designed to handle processes efficiently by assigning a fixed time slice or quantum to each process. However, when processes arrive at different times, the scheduling becomes slig
15+ min read
Check if CPU will process given requests successfully or not Given an integer capacity the maximum number of the processes handled by a CPU at any given time and a 2-D array request[][] is given, each request has three parameters: a number of processes requiring CPU,start time,finish time. The task is to check if the CPU will entertain all the requests succes
6 min read