利用銀行家算法避免死鎖(C++實現)

算法思想:

代碼:

#include<iostream>
#include<string>
using namespace std;
#define  numberOfProcess 5
#define  numberOfReSource 3
int *available=new int[numberOfReSource];
int *work=new int[numberOfReSource];
string *securitySequence=new string[numberOfProcess];
typedef struct pcb {
	string pName;//進程名
	string *nameOfReSource = new string[numberOfReSource];//依次存放各類資源的名稱
	int *max = new int[numberOfReSource];//max[j]=K表示進程需要Rj類資源的最大數目爲K
	int *allocation = new int[numberOfReSource];//allocation[j]=K表示進程已分得Rj類資源的數目爲K
	int *need = new int[numberOfReSource];//need[j]=K表示進程還需要Rj類資源K個方能完成任務
	bool finish;
	struct pcb *next;//鏈接指針指向下一個進程
}PCB;

void printProcess(PCB* p) {//輸出進程的相關信息
	PCB *q = p->next;//q指向第一個進程
	cout << endl << "所有進程的相關信息" << endl;
	cout << "         Need        Allocation    Finish" << endl;
	cout << "進程  A   B   C      A   B   C           " << endl;
	while (q) {//遍歷進程
		cout << q->pName << "    ";
		cout << q->need[0] << "   " << q->need[1] << "   " << q->need[2] << "      ";
		cout << q->allocation[0] << "   " << q->allocation[1] << "   " << q->allocation[2] << "     ";
		cout << (q->finish ? "true" : "false") << endl;
		q = q->next;//q指向下一個進程
	}
	cout << endl;
}

void initProcess(PCB *p) {//初始化進程
	cout << "初始化進程" << endl;
	cout << "請依次輸入資源的名稱:";
	string *nameOfReSource = new string[numberOfReSource];//該變量裏依次存放資源的名稱
	for (int i = 0; i<numberOfReSource; i++)//依次存放各類資源的名稱
		cin >> nameOfReSource[i];
	cout << "請依次輸入當前可利用資源量:";
	for (int i = 0; i<numberOfReSource; i++)
		cin >> available[i];
	PCB *q = p;
	PCB *r = new PCB;//當前工作指針
	for (int i = 0; i<numberOfProcess; i++) {
		cout << "請輸入進程的名字:";
		cin >> r->pName;
		cout << "請依次輸入該進程的各類資源的最大需求量(例如:2 3 4 ...):";
		for (int j = 0; j<numberOfReSource; j++)
			cin >> r->max[j];
		cout << "請依次輸入該進程的各類資源的已經分配量(例如:2 3 4 ...):";
		for (int j = 0; j<numberOfReSource; j++) {
			cin >> r->allocation[j];
			r->need[j] = r->max[j] - r->allocation[j];
			r->finish = false;
		}
		q->next = r;
		q = r;
		r->next = new PCB;
		r = r->next;//指針後移
	}
	r->next = NULL;
	q->next = NULL;
	q = NULL;
	delete q;//釋放結點
	delete r;
	printProcess(p);//輸出此時進程的相關信息
	cout << endl;
}

bool securityAlgorithm(PCB *p) {//安全性檢查算法
	PCB *m = p->next;//m指向第一個結點
	PCB *s = p->next;//s指向第一個結點
	int count = 0;
	bool flag = true;
	for (int i = 0; i<numberOfReSource; i++)//初始化work變量
		work[i] = available[i];
	cout << "初始時刻work變量的值:";
	for (int j = 0; j<numberOfReSource; j++)
		cout << work[j] << " ";
	cout << endl;
	while (s) {//將所有進程的finish初始化爲false
		s->finish = false;
		s = s->next;
	}
	while (true) {//安全性檢測
		flag = true;
		for (int j = 0; j<numberOfReSource; j++)
			if (m->need[j]>work[j] && !m->finish) {
				flag = false;
				break;
			}
		if (flag && !m->finish) {//找到符合條件的進程
			securitySequence[count++] = m->pName;//將該進程的名稱加到安全序列中
			for (int j = 0; j<numberOfReSource; j++)//修改work變量的值
				work[j] = work[j] + m->allocation[j];
			m->finish = true;//修改finish
			cout << endl << "當前選中的進程:" << m->pName << endl;
			cout << "該進程預分配後work變量的值:";//輸出此刻work變量的值
			for (int j = 0; j<numberOfReSource; j++)
				cout << work[j] << " ";
			cout << endl;
		}
		m = m->next;//m指向下一個進程
		if (m == NULL)//m指向第一個進程
			m = p->next;
		s = p->next;
		flag = false;
		for (int i = 0; i<numberOfProcess; i++) {
			if (s->finish) {
				s = s->next;
				continue;
			}
			flag = true;
			for (int j = 0; j<numberOfReSource; j++)
				if (s->need[j]>work[j]) {
					flag = false;
					break;
				}
			if (flag)//當前進程符合條件
				break;
			s = s->next;
		}
		if (!flag)//退出整個循環
			break;
	}
	flag = true;
	s = p->next;
	for (int i = 0; i<numberOfProcess; i++) {
		if (!s->finish) {
			flag = false;
			break;
		}
		s = s->next;
	}
	if (flag) {//全爲true
		cout << endl << "當前系統是安全的" << endl;
		cout << "安全序列:";
		for (int i = 0; i<numberOfProcess; i++)
			cout << securitySequence[i] << " ";
		cout << endl;
		printProcess(p);
		return true;
	}
	else {//不全爲false
		cout << "當前系統是不安全的" << endl;
		printProcess(p);
		return false;
	}
}

int main() {
	PCB *p = new PCB;
	PCB *q;
	initProcess(p);//初始化操作
	securityAlgorithm(p);//安全性檢測
	string nameOfRequestResource;//請求資源的進程名
	int *requestResource = new int[numberOfReSource];
	bool flag = true;
	while (true) {
		cout << endl << "請輸入發出請求資源的進程的名字:";
		cin >> nameOfRequestResource;
		cout << "請依次輸入請求向量:";
		for (int i = 0; i<numberOfReSource; i++) {
			cin >> requestResource[i];
		}
		q = p->next;
		while (q) {//找到當前進程
			if (q->pName == nameOfRequestResource)
				break;
			q = q->next;
		}
		cout << endl << "銀行家算法開始進行檢查" << endl << endl;
		flag = true;
		for (int i = 0; i<numberOfReSource; i++) {
			if (requestResource[i]>q->need[i] || requestResource[i]>available[i]) {
				flag = false;
				break;
			}
		}
		if (flag) {//符合條件
			cout << "銀行家算法檢查過後發現符合要求" << endl << endl;
			for (int i = 0; i<numberOfReSource; i++) {
				available[i] -= requestResource[i];
				q->allocation[i] += requestResource[i];
				q->need[i] -= requestResource[i];
			}
			cout << "下面進行安全性算法檢查" << endl << endl;
			if (!securityAlgorithm(p))//當前系統處於不安全狀態
				for (int i = 0; i<numberOfReSource; i++) {
					available[i] += requestResource[i];
					q->allocation[i] -= requestResource[i];
					q->need[i] += requestResource[i];
				}
		}
		else//不符合條件
			cout << "銀行家算法檢查之後發現不符合要求" << endl << endl;
		cout << endl;
		string choose;
		cout << endl << "是否還有進程要申請資源,若是則輸入YES,否則輸入NO:";
		cin >> choose;
		if (choose == "NO")
			break;
	}
	q = NULL;
	delete q;
	return 0;
}

結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章