進程調度實驗
實驗目的
多道程序設計中,經常是若干個進程同時處於就緒狀態,必須依照某種策略來決定那個進程優先占有處理機。因而引起進程調度。本實驗模擬在單處理機情況下的處理機調度問題,加深對進程調度的理解。
實驗內容
- 優先權法-動態優先權
- 輪轉法
流程圖
實驗要求
- 產生的各種隨機數的取值范圍加以限制,如所需的CPU時間限制在1~20之間。
- 進程數n不要太大通常取4~8個
- 使用動態數據結構
- 獨立編程
- 兩種調度算法
運行結果
實驗代碼
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n;
const int N = 10;
struct PCB {
string name;
int time;
int priority;
int status;
int runtime;
int lefttime;
};
struct cmp {
bool operator()(PCB a, PCB b) {
if (a.priority == b.priority) {
return a.time > b.time;
}
return a.priority < b.priority;
}
};
//動態優先權隊列
priority_queue<PCB, vector<PCB>, cmp> q1; //ready
queue<PCB> q3; //finish
void init_priority() {
PCB t;
for (int i = 1; i <= n; i++) {
t.name = 'a' + i - 1;
t.priority = (rand() % 20) + 50;
t.time = (rand() % 20) + 1;
t.status = -1;
t.lefttime = t.time;
t.runtime = 0;
q1.push(t);
}
}
void print_priority(priority_queue<PCB, vector<PCB>, cmp> q) {
while (!q.empty()) {
PCB t = q.top();
cout << t.name << '\t' << t.time << '\t' << t.priority << '\t' << t.status << '\t' << t.runtime + 1 << '\t' << '\t' << t.lefttime - 1 << endl;
q.pop();
}
}
void print_finish1(queue<PCB> q) {
cout << "完成隊列過程如下: " << endl;
cout << "名字" << '\t' << "時間" << '\t' << " 優先級" << '\t' << "狀態" << '\t' << "運行時間" << '\t' << "剩余時間" << endl;
while (!q.empty()) {
PCB t = q.front();
cout << t.name << '\t' << t.time << '\t' << t.priority << '\t' << t.status << '\t' << t.runtime << '\t' << '\t' << t.lefttime << endl;
q.pop();
}
}
void run_priority() {
while (!q1.empty()) {
print_priority(q1);
PCB t = q1.top();
t.priority -= 3;
t.lefttime -= 1;
t.runtime += 1;
t.status = 0;
if (t.lefttime <= 0) {
t.status = 1;
q3.push(t); //插入到finish隊列
q1.pop();
}
else {
t.status = -1;
q1.pop();
q1.push(t); //插入到就緒隊列
}
}
cout << "所有進程均已執行完畢!" << endl;
cout << endl << endl << endl;
}
//時間片輪轉法隊列
queue<PCB> q2;
void init_timeturn() {
PCB t;
for (int i = 1; i <= n; i++) {
t.name = 'a' + i - 1;
t.time = (rand() % 20) + 1;
t.status = -1;
t.lefttime = t.time;
t.runtime = 0;
q2.push(t);
}
}
void print_timeturn(queue<PCB> q) {
while (!q.empty()) {
PCB t = q.front();
cout << t.name << '\t' << t.time << '\t' << t.status << '\t' << t.runtime + 1 << '\t' << '\t' << t.lefttime - 1 << endl;
q.pop();
}
}
void print_finish2(queue<PCB> q) {
cout << "完成隊列過程如下: " << endl;
cout << "名字" << '\t' << "時間" << '\t' << "狀態" << '\t' << "運行時間" << '\t' << "剩余時間" << endl;
while (!q.empty()) {
PCB t = q.front();
cout << t.name << '\t' << t.time << '\t' << t.status << '\t' << t.runtime << '\t' << '\t' << t.lefttime << endl;
q.pop();
}
}
void run_timeturn() {
while (!q2.empty()) {
print_timeturn(q2);
PCB t = q2.front();
t.runtime += 1;
t.lefttime -= 1;
t.status = 0;
if (t.lefttime <= 0) {
t.status = 1;
q3.push(t);
q2.pop();
}
else {
t.status = -1;
q2.push(t);
q2.pop();
}
}
cout << "所有進程均已執行完畢!" << endl;
cout << endl << endl << endl;
}
int main() {
int c;
cout << "選擇調度算法 : 1.優先級調度 2.時間片輪轉 " << endl;
cin >> c;
cout << "輸入進程個數 : " << endl;
cin >> n;
cout << endl;
cout << "狀態欄 ‘-1’代表就緒, ‘1’代表完成" << endl << endl;
if (c == 1) {
init_priority();
cout << "就緒隊列過程如下: " << endl;
cout << "名字" << '\t' << "時間" << '\t' << "優先級" << '\t' << "狀態" << '\t' << "運行時間" << '\t' << "剩余時間" << endl;
run_priority();
print_finish1(q3);
}
else if (c == 2) {
init_timeturn();
cout << "就緒隊列過程如下: " << endl;
cout << "名字" << '\t' << "時間" << '\t' << "狀態" << '\t' << "運行時間" << '\t' << "剩余時間" << endl;
run_timeturn();
print_finish2(q3);
}
else {
cout << "輸入錯誤 ,自動退出 ! : " << endl;
}
return 0;
}