📌  相关文章
📜  抢占式优先 CPU 调度程序

📅  最后修改于: 2021-10-28 02:12:22             🧑  作者: Mango

实现优先CPU调度。在这个问题中,我们使用最小堆作为实现优先级调度的数据结构。
在这个问题中,较小的数字表示较高的优先级。
在下面给定的代码中使用了以下函数:

struct process {
   processID,
   burst time,
   response time,
   priority,
   arrival time.
} 

void quicksort(process array[], low, high) – 该函数用于根据进程的到达时间按升序排列进程。
int partition(process array[], int low, int high) – 该函数用于对数组进行分区以进行排序。
void insert(process Heap[], process value, int *heapsize, int *currentTime) – 用于将所有有效且符合条件的进程包含在堆中以供执行。 heapsize 根据当前时间定义正在执行的进程数 currentTime 记录当前 CPU 时间。
void order(process Heap[], int *heapsize, int start) – 用于在插入新进程后的进程根据优先级重新排序堆。
void extractminimum(process Heap[], int *heapsize, int *currentTime) – 该函数用于从堆中查找优先级最高的进程。它还在提取最高优先级进程后重新排序堆。
void schedule(process Heap[], process array[], int n, int *heapsize, int *currentTime) – 该函数负责执行从 Heap[] 中提取的最高优先级。
void process(process array[], int n) – 该函数负责根据到达时间管理进程到达 CPU 时的整个执行过程。

// CPP program to implement preemptive priority scheduling
#include 
using namespace std;
  
struct Process {
    int processID;
    int burstTime;
    int tempburstTime;
    int responsetime;
    int arrivalTime;
    int priority;
    int outtime;
    int intime;
};
  
// It is used to include all the valid and eligible
// processes in the heap for execution. heapsize defines
// the number of processes in execution depending on
// the current time currentTime keeps a record of
// the current CPU time.
void insert(Process Heap[], Process value, int* heapsize,
            int* currentTime)
{
    int start = *heapsize, i;
    Heap[*heapsize] = value;
    if (Heap[*heapsize].intime == -1)
        Heap[*heapsize].intime = *currentTime;
    ++(*heapsize);
  
    // Ordering the Heap
    while (start != 0 && Heap[(start - 1) / 2].priority > 
                                  Heap[start].priority) {
        Process temp = Heap[(start - 1) / 2];
        Heap[(start - 1) / 2] = Heap[start];
        Heap[start] = temp;
        start = (start - 1) / 2;
    }
}
  
// It is used to reorder the heap according to
// priority if the processes after insertion
// of new process.
void order(Process Heap[], int* heapsize, int start)
{
    int smallest = start;
    int left = 2 * start + 1;
    int right = 2 * start + 2;
    if (left < *heapsize && Heap[left].priority <
                            Heap[smallest].priority)
        smallest = left;
     if (right < *heapsize && Heap[right].priority < 
                           Heap[smallest].priority)
        smallest = right;
  
    // Ordering the Heap
    if (smallest != start) {
        Process temp = Heap[smallest];
        Heap[smallest] = Heap[start];
        Heap[start] = temp;
        order(Heap, heapsize, smallest);
    }
}
  
// This function is used to find the process with
// highest priority from the heap. It also reorders
// the heap after extracting the highest priority process.
Process extractminimum(Process Heap[], int* heapsize,
                       int* currentTime)
{
    Process min = Heap[0];
    if (min.responsetime == -1)
        min.responsetime = *currentTime - min.arrivalTime;
    --(*heapsize);
    if (*heapsize >= 1) {
        Heap[0] = Heap[*heapsize];
        order(Heap, heapsize, 0);
    }
    return min;
}
  
// Compares two intervals according to staring times.
bool compare(Process p1, Process p2)
{
    return (p1.arrivalTime < p2.arrivalTime);
}
  
// This function is responsible for executing
// the highest priority extracted from Heap[].
void scheduling(Process Heap[], Process array[], int n,
                int* heapsize, int* currentTime)
{
    if (heapsize == 0)
        return;
  
    Process min = extractminimum(Heap, heapsize, currentTime);
    min.outtime = *currentTime + 1;
    --min.burstTime;
    printf("process id = %d current time = %d\n",
           min.processID, *currentTime);
  
    // If the process is not yet finished
    // insert it back into the Heap*/
    if (min.burstTime > 0) {
        insert(Heap, min, heapsize, currentTime);
        return;
    }
  
    for (int i = 0; i < n; i++)
        if (array[i].processID == min.processID) {
            array[i] = min;
            break;
        }
}
  
// This function is responsible for
// managing the entire execution of the
// processes as they arrive in the CPU
// according to their arrival time.
void priority(Process array[], int n)
{
    sort(array, array + n, compare);
  
    int totalwaitingtime = 0, totalbursttime = 0,
        totalturnaroundtime = 0, i, insertedprocess = 0,
        heapsize = 0, currentTime = array[0].arrivalTime,
        totalresponsetime = 0;
  
    Process Heap[4 * n];
  
    // Calculating the total burst time
    // of the processes
    for (int i = 0; i < n; i++) {
        totalbursttime += array[i].burstTime;
        array[i].tempburstTime = array[i].burstTime;
    }
  
    // Inserting the processes in Heap
    // according to arrival time
    do {
        if (insertedprocess != n) {
            for (i = 0; i < n; i++) {
                if (array[i].arrivalTime == currentTime) {
                    ++insertedprocess;
                    array[i].intime = -1;
                    array[i].responsetime = -1;
                    insert(Heap, array[i], &heapsize, ¤tTime);
                }
            }
        }
        scheduling(Heap, array, n, &heapsize, ¤tTime);
        ++currentTime;
        if (heapsize == 0 && insertedprocess == n)
            break;
    } while (1);
  
    for (int i = 0; i < n; i++) {
        totalresponsetime += array[i].responsetime;
        totalwaitingtime += (array[i].outtime - array[i].intime -
                                         array[i].tempburstTime);
        totalbursttime += array[i].burstTime;
    }
    printf("Average waiting time = %f\n",
           ((float)totalwaitingtime / (float)n));
    printf("Average response time =%f\n",
           ((float)totalresponsetime / (float)n));
    printf("Average turn around time = %f\n",
           ((float)(totalwaitingtime + totalbursttime) / (float)n));
}
  
// Driver code
int main()
{
    int n, i;
    Process a[5];
    a[0].processID = 1;
    a[0].arrivalTime = 4;
    a[0].priority = 2;
    a[0].burstTime = 6;
    a[1].processID = 4;
    a[1].arrivalTime = 5;
    a[1].priority = 1;
    a[1].burstTime = 3;
    a[2].processID = 2;
    a[2].arrivalTime = 5;
    a[2].priority = 3;
    a[2].burstTime = 1;
    a[3].processID = 3;
    a[3].arrivalTime = 1;
    a[3].priority = 4;
    a[3].burstTime = 2;
    a[4].processID = 5;
    a[4].arrivalTime = 3;
    a[4].priority = 5;
    a[4].burstTime = 4;
    priority(a, 5);
    return 0;
}
输出:
process id = 3 current time = 1
process id = 3 current time = 2
process id = 5 current time = 3
process id = 1 current time = 4
process id = 4 current time = 5
process id = 4 current time = 6
process id = 4 current time = 7
process id = 1 current time = 8
process id = 1 current time = 9
process id = 1 current time = 10
process id = 1 current time = 11
process id = 1 current time = 12
process id = 2 current time = 13
process id = 5 current time = 14
process id = 5 current time = 15
process id = 5 current time = 16
Average waiting time = 4.400000
Average response time =1.600000
Average turn around time = 7.200000

输出显示进程在内存中执行的顺序,还显示每个进程的平均等待时间、平均响应时间和平均周转时间。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程