📜  在循环调度中找到给定的N个进程的执行顺序

📅  最后修改于: 2021-04-23 07:22:31             🧑  作者: Mango

给定一个数组arr [],它表示使用Round Robin算法以给定的量子时间Q调度的N个进程的突发时间。假设所有进程都在时间t = 0到达,任务就是找到进程执行的顺序。

例子:

方法:想法是创建一个辅助数组,其中包含一个进程与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


输出:
0  2  1

时间复杂度: O(N * log N)
辅助空间: O(N)