操作系統實驗一:進程調度


進程調度實驗

實驗目的

       多道程序設計中,經常是若干個進程同時處於就緒狀態,必須依照某種策略來決定那個進程優先占有處理機。因而引起進程調度。本實驗模擬在單處理機情況下的處理機調度問題,加深對進程調度的理解。

實驗內容

  1. 優先權法-動態優先權
  2. 輪轉法

流程圖


實驗要求

  1. 產生的各種隨機數的取值范圍加以限制,如所需的CPU時間限制在1~20之間。
  2. 進程數n不要太大通常取4~8個
  3. 使用動態數據結構
  4. 獨立編程
  5. 兩種調度算法

運行結果

實驗代碼

#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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM