C++優先順序排程程式
我們給定n個程序,即P1、P2、P3、……、Pn,以及每個程序對應的突發時間和優先順序。任務是使用優先順序CPU排程演算法找到平均等待時間、平均週轉時間和程序執行順序。
什麼是等待時間和週轉時間?
週轉時間是從提交一個程序到完成該程序的時間間隔。
週轉時間 = 程序完成時間 – 程序提交時間
等待時間是週轉時間和突發時間之間的差值
等待時間 = 週轉時間 – 突發時間
什麼是優先順序排程?
在優先順序排程中,每個程序都與一個優先順序相關聯,範圍從0到10,其中整數0表示最低優先順序,10表示最高優先順序。優先順序可以透過兩種方式定義,即內部和外部。此外,優先順序排程可以是搶佔式的或非搶佔式的。
在搶佔式優先順序排程中,如果新到達程序的優先順序高於正在執行的程序的優先順序,則排程程式將搶佔CPU。
在非搶佔式優先順序排程中,排程程式將新程序排隊到就緒佇列的頭部。
使用優先順序排程演算法的缺點是不確定阻塞或飢餓。將會有一個低優先順序程序可能不得不無限期地等待資源,因為高優先順序程序會導致飢餓問題。
示例
假設有4個程序P1、P2、P3和P4,以及每個程序對應的突發時間和優先順序,其中0表示最低優先順序,10表示最高優先順序。
| 程序 | 突發時間 | 優先順序 |
|---|---|---|
| P1 | 15 | 2 |
| P2 | 13 | 0 |
| P3 | 10 | 4 |
| P4 | 11 | 1 |
執行多個程序的順序將使用下面給出的甘特圖表示

演算法
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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP