C++优先级调度程序


我们给定n个进程,即P1、P2、P3、……、Pn,以及每个进程对应的突发时间和优先级。任务是使用优先级CPU调度算法找到平均等待时间、平均周转时间和进程执行顺序。

什么是等待时间和周转时间?

周转时间是从提交一个进程到完成该进程的时间间隔。

周转时间 = 进程完成时间 – 进程提交时间

等待时间是周转时间和突发时间之间的差值

等待时间 = 周转时间 – 突发时间

什么是优先级调度?

在优先级调度中,每个进程都与一个优先级相关联,范围从0到10,其中整数0表示最低优先级,10表示最高优先级。优先级可以通过两种方式定义,即内部和外部。此外,优先级调度可以是抢占式的或非抢占式的。

抢占式优先级调度中,如果新到达进程的优先级高于正在执行的进程的优先级,则调度程序将抢占CPU。

在非抢占式优先级调度中,调度程序将新进程排队到就绪队列的头部。

使用优先级调度算法的缺点是不确定阻塞或饥饿。将会有一个低优先级进程可能不得不无限期地等待资源,因为高优先级进程会导致饥饿问题。

示例

假设有4个进程P1、P2、P3和P4,以及每个进程对应的突发时间和优先级,其中0表示最低优先级,10表示最高优先级。

进程突发时间优先级
P1152
P2130
P3104
P4111

执行多个进程的顺序将使用下面给出的甘特图表示

算法

Start
Step 1-> Make a structure Process with variables pid, bt, priority
Step 2-> In function bool compare(Process a, Process b)
   Return (a.priority > b.priority)
Step 3-> In function waitingtime(Process pro[], int n, int wt[])
   Set wt[0] = 0
   Loop For i = 1 and i < n and i++
      Set wt[i] = pro[i-1].bt + wt[i-1]
   End
Step 4-> In function turnarround( Process pro[], int n, int wt[], int tat[])
   Loop For i = 0 and i < n and i++
      Set tat[i] = pro[i].bt + wt[i]
   End Loop
Step 5-> In function avgtime(Process pro[], int n)
   Declare and initialize wt[n], tat[n], total_wt = 0, total_tat = 0
   Call function waitingtime(pro, n, wt)
   Call function turnarround(pro, n, wt, tat)
   Print “Processes, Burst time, Waiting time, Turn around time"
   Loop For i=0 and i<n and i++
      Set total_wt = total_wt + wt[i]
      total_tat = total_tat + tat[i]
   End Loop
   Print values of “Processes, Burst time, Waiting time, Turn around time"
   Print Average waiting time, Average turn around time
Step 6-> In function scheduling(Process pro[], int n)
   Call function sort(pro, pro + n, compare)
   Loop For i = 0 and i < n and i++
      Print the order.
   End Loop
   Call function avgtime(pro, n)
Step 7-> In function int main()
   Declare and initialize Process pro[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}}
   Declare and initialize n = sizeof pro / sizeof pro[0]
   Call function scheduling(pro, n)
Stop

示例

 实时演示

#include<bits/stdc++.h>
using namespace std;
struct Process {
   int pid; // Process ID
   int bt; // CPU Burst time required
   int priority; // Priority of this process
};
// sorting the Process acc. to the priority
bool compare(Process a, Process b) {
   return (a.priority > b.priority);
}
void waitingtime(Process pro[], int n, int wt[]) {
   // Initial waiting time for a process is 0
   wt[0] = 0;
   // calculating waiting time
   for (int i = 1; i < n ; i++ )
      wt[i] = pro[i-1].bt + wt[i-1] ;
}
 // Function to calculate turn around time
void turnarround( Process pro[], int n, int wt[], int tat[]) {
   // calculating turnaround time by adding
   // bt[i] + wt[i]
   for (int i = 0; i < n ; i++)
      tat[i] = pro[i].bt + wt[i];
}
//Function to calculate average time
void avgtime(Process pro[], int n) {
   int wt[n], tat[n], total_wt = 0, total_tat = 0;
   //Function to find waiting time of all processes
   waitingtime(pro, n, wt);
   //Function to find turn around time for all processes
   turnarround(pro, n, wt, tat);
   //Display processes along with all details
   cout << "\nProcesses "<< " Burst time " << " Waiting time " << " Turn around time\n";
   // Calculate total waiting time and total turn
   // around time
   for (int i=0; i<n; i++) {
      total_wt = total_wt + wt[i];
      total_tat = total_tat + tat[i];
      cout << " " << pro[i].pid << "\t\t" << pro[i].bt << "\t " << wt[i] << "\t\t " << tat[i] <<endl;
   }
   cout << "\nAverage waiting time = " << (float)total_wt / (float)n;
   cout << "\nAverage turn around time = " << (float)total_tat / (float)n;
}
void scheduling(Process pro[], int n) {
   // Sort processes by priority
   sort(pro, pro + n, compare);
   cout<< "Order in which processes gets executed \n";
   for (int i = 0 ; i < n; i++)
      cout << pro[i].pid <<" " ;
   avgtime(pro, n);
}
// main function
int main() {
   Process pro[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
   int n = sizeof pro / sizeof pro[0];
   scheduling(pro, n);
   return 0;
}

输出

Order in which processes gets executed
1 3 2
Processes  Burst time  Waiting time  Turn around time
 1              10         0              10
 3              8          10             18
 2              5          18             23
 
Average waiting time = 9.33333
Average turn around time = 17

更新于: 2019年12月20日

11K+浏览量

开启您的职业生涯

通过完成课程获得认证

开始学习
广告