给定一个数组arr[] ,表示使用循环算法调度的N 个进程的突发时间,并具有给定的量子时间Q 。假设所有进程在时间t = 0到达,任务是找到进程执行的顺序。
例子:
Input: arr[] = {3, 7, 4}, q = 3
Output: 0 2 1
Explanation:
The order of execution is as follows P0, P1, P2, P1, P2, P1
Since, P0 has burst time of 3 and quantum time is also 3, it gets completed first.
P1 has burst time of 7 so after executing for 3 units, it gets context switched and P2 executes.
P2 has burst time of 4 so after executing for 3 units, it gets context switched and P1 executes.
Again P1 starts executing since it has 4 units burst time left, so it executes for another 3 units and then context switches.
Now process P2 executes for 1 unit and gets completed.
In the end process P1 is completed.
They complete the execution in the order P0, P2, P1.
Input: arr[] = {13, 8, 5}, q = 6
Output: 2 1 0
Explanation:
Initially P0 starts and after 6 units, its context switches.
P1 executes for 6 units and context switches.
Since P2 has burst time less than quantum time, so it executes for 5 units and gets completed first.
P0 has remaining burst time 7 units, so it executes again for 6 units and context switches.
P1 has remaining burst time as 2 units and it gets completed second.
In the end process P0 gets completed.
They complete the execution in the order P2, P1, P0.
方法:这个想法是创建一个辅助数组,其中包含进程与 CPU 交互次数的频率。然后freq[]数组可以按频率的递增顺序排序。首先完成频率最低的过程,然后是第二个过程,依此类推。排序后的数组给出了过程的完成顺序。以下是步骤:
- 初始化一个数组freq[] ,其中freq[i]是第i个进程与 CPU 交互的次数。
- 初始化数组order[]以存储进程的完成顺序并存储order[i] = i 。
- 按照freq[]的递增顺序对数组order[]进行排序,使得freq[order[i]] ≤ freq[order[i + 1]] 。
- 打印数组order[] ,这是进程完成执行的顺序。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to sort the array order[]
// on the basis of the array freq[]
void merge(int* order, int* freq, int i,
int mid, int j)
{
int tempOrder[j - i + 1];
int temp = mid + 1, index = -1;
while (i <= mid && temp <= j) {
// If order[i]th is less than
// order[temp]th process
if (freq[order[i]]
<= freq[order[temp]]) {
tempOrder[++index] = order[i++];
}
// Otherwise
else {
tempOrder[++index] = order[temp++];
}
}
// Add the left half to tempOrder[]
while (i <= mid) {
tempOrder[++index] = order[i++];
}
// Add right half to tempOrder[]
while (temp <= j) {
tempOrder[++index] = order[temp++];
}
// Copy the tempOrder[] array to
// order[] array
for (index; index >= 0; index--) {
order[j--] = tempOrder[index];
}
}
// Utility function to sort the array
// order[] on the basis of freq[]
void divide(int* order, int* freq,
int i, int j)
{
// Base Case
if (i >= j)
return;
// Divide array into 2 parts
int mid = i / 2 + j / 2;
// Sort the left array
divide(order, freq, i, mid);
// Sort the right array
divide(order, freq, mid + 1, j);
// Merge the sorted arrays
merge(order, freq, i, mid, j);
}
// Function to find the order of
// processes in which execution occurs
void orderProcesses(int A[], int N, int q)
{
int i = 0;
// Store the frequency
int freq[N];
// Find elements in array freq[]
for (i = 0; i < N; i++) {
freq[i] = (A[i] / q)
+ (A[i] % q > 0);
}
// Store the order of completion
// of processes
int order[4];
// Initialize order[i] as i
for (i = 0; i < N; i++) {
order[i] = i;
}
// Function Call to find the order
// of execution
divide(order, freq, 0, N - 1);
// Print order of completion
// of processes
for (i = 0; i < N; i++) {
cout << order[i] << " ";
}
}
// Driver Code
int main()
{
// Burst Time of the processes
int arr[] = { 3, 7, 4 };
// Quantam Time
int Q = 3;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
orderProcesses(arr, N, Q);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to sort the array order[]
// on the basis of the array freq[]
static void merge(int order[], int freq[], int i,
int mid, int j)
{
int tempOrder[] = new int[j - i + 1];
int temp = mid + 1, index = -1;
while (i <= mid && temp <= j)
{
// If order[i]th is less than
// order[temp]th process
if (freq[order[i]]
<= freq[order[temp]])
{
tempOrder[++index] = order[i++];
}
// Otherwise
else
{
tempOrder[++index] = order[temp++];
}
}
// Add the left half to tempOrder[]
while (i <= mid)
{
tempOrder[++index] = order[i++];
}
// Add right half to tempOrder[]
while (temp <= j)
{
tempOrder[++index] = order[temp++];
}
// Copy the tempOrder[] array to
// order[] array
int ind= index;
for (index= ind; index >= 0; index--)
{
order[j--] = tempOrder[index];
}
}
// Utility function to sort the array
// order[] on the basis of freq[]
static void divide(int order[], int freq[],
int i, int j)
{
// Base Case
if (i >= j)
return;
// Divide array into 2 parts
int mid = i / 2 + j / 2;
// Sort the left array
divide(order, freq, i, mid);
// Sort the right array
divide(order, freq, mid + 1, j);
// Merge the sorted arrays
merge(order, freq, i, mid, j);
}
// Function to find the order of
// processes in which execution occurs
static void orderProcesses(int A[], int N, int q)
{
int i = 0;
// Store the frequency
int freq[] = new int[N];
// Find elements in array freq[]
for (i = 0; i < N; i++)
{
freq[i] = (A[i] / q);
if (A[i] % q > 0)
freq[i] += 1;
}
// Store the order of completion
// of processes
int order[] = new int[4];
// Initialize order[i] as i
for (i = 0; i < N; i++) {
order[i] = i;
}
// Function Call to find the order
// of execution
divide(order, freq, 0, N - 1);
// Print order of completion
// of processes
for (i = 0; i < N; i++) {
System.out.print( order[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Burst Time of the processes
int arr[] = { 3, 7, 4 };
// Quantam Time
int Q = 3;
int N = arr.length;
// Function Call
orderProcesses(arr, N, Q);
}
}
// This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
class GFG{
// Function to sort the array order[]
// on the basis of the array freq[]
static void merge(int[] order, int[] freq, int i,
int mid, int j)
{
int[] tempOrder = new int[j - i + 1];
int temp = mid + 1, index = -1;
while (i <= mid && temp <= j)
{
// If order[i]th is less than
// order[temp]th process
if (freq[order[i]] <= freq[order[temp]])
{
tempOrder[++index] = order[i++];
}
// Otherwise
else
{
tempOrder[++index] = order[temp++];
}
}
// Add the left half to tempOrder[]
while (i <= mid)
{
tempOrder[++index] = order[i++];
}
// Add right half to tempOrder[]
while (temp <= j)
{
tempOrder[++index] = order[temp++];
}
// Copy the tempOrder[] array to
// order[] array
int ind = index;
for(index = ind; index >= 0; index--)
{
order[j--] = tempOrder[index];
}
}
// Utility function to sort the array
// order[] on the basis of freq[]
static void divide(int[] order, int[] freq,
int i, int j)
{
// Base Case
if (i >= j)
return;
// Divide array into 2 parts
int mid = i / 2 + j / 2;
// Sort the left array
divide(order, freq, i, mid);
// Sort the right array
divide(order, freq, mid + 1, j);
// Merge the sorted arrays
merge(order, freq, i, mid, j);
}
// Function to find the order of
// processes in which execution occurs
static void orderProcesses(int[] A, int N, int q)
{
int i = 0;
// Store the frequency
int[] freq = new int[N];
// Find elements in array freq[]
for(i = 0; i < N; i++)
{
freq[i] = (A[i] / q);
if (A[i] % q > 0)
freq[i] += 1;
}
// Store the order of completion
// of processes
int[] order = new int[4];
// Initialize order[i] as i
for(i = 0; i < N; i++)
{
order[i] = i;
}
// Function Call to find the order
// of execution
divide(order, freq, 0, N - 1);
// Print order of completion
// of processes
for(i = 0; i < N; i++)
{
Console.Write( order[i] + " ");
}
}
// Driver Code
public static void Main()
{
// Burst Time of the processes
int[] arr = { 3, 7, 4 };
// Quantam Time
int Q = 3;
int N = arr.Length;
// Function Call
orderProcesses(arr, N, Q);
}
}
// This code is contributed by sanjoy_62
Javascript
0 2 1
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live