循环中给定进程的完成时间
我们以数组的形式给出了 n 个进程及其完成时间。如果调度过程是循环的并且时间片是 1 秒,我们需要找到给定进程 p 结束的时刻。
注意:数组索引从 0 开始。
例子 :
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}
蛮力:解决这个问题的基本方法是使用时间片 1 的循环算法。但是这种方法的时间复杂度将是 O(ΣAi),即所有进程时间的总和,这是相当高的。
有效方法:这个想法基于以下观察。
1) 所有 CPU 时间小于 arr[p] 的进程都会在 arr[p] 之前完成。我们只需要添加这些过程的时间。
2) 我们还需要加上 arr[p] 的时间。
3) 对于每个 CPU 时间超过 arr[p] 的进程 x,会出现两种情况:
.....(i) 如果 x 在 arr[p] 的左侧(安排在 arr[p] 之前),则此过程在 p 完成之前占用 CPU 的 arr[p] 时间。
.....(ii) 如果 x 在 arr[p] 的右边(安排在 arr[p] 之后),那么这个过程在 p 完成之前需要 arr[p]-1 次 CPU 时间。
算法 :
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
C++
// Program to find end time of a process
// p in round robin scheduling with unit
// time slice.
#include
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
Javascript
输出 :
Completion time = 9
时间复杂度: O(n)