模擬銀行家算法

介紹

data.h

#ifndef _Data_h_
#define _Data_h_#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define ElemType PCB
#define Status int
#define true				1
#define false				0
#define OK					1
#define	ERROR				0
#define RESOURCE_NUM		3
#define	MAX_RESOURCE_A_NUM	10
#define	MAX_RESOURCE_B_NUM	5
#define	MAX_RESOURCE_C_NUM	7
#define NAME_MAXSIZE		20
#define PCB_Num 5
typedef struct{int MaxNum[RESOURCE_NUM];			//需要每項資源個數int AllocationNum[RESOURCE_NUM];	//已占用每項資源個數int NeedNum[RESOURCE_NUM];			//還需要的每項資源個數
}ResourceList;typedef struct 
{char Name[NAME_MAXSIZE];			//進程名ResourceList resList;				//資源清單
}PCB;typedef struct Node
{ElemType data;struct Node * Next;		
}LNode,*LinkList;#endif

chainlist.h

#ifndef _ChainList_h_
#define _ChainList_h_#include "Data.h"Status Init(LinkList *L);
void Assignment(ElemType *e1, ElemType e2);
Status ListInsert_L(LinkList L,ElemType e);#endif

實現

ProPCB.h

#ifndef _ProPCB_h_
#define _ProPCB_h_#include "ChainList.h"
#include <string.h>
//上隊列
Status GetProcess(LinkList Q,ElemType e);		
//銀行家算法
Status BankerAlgorithm(int *Allocation, int *Request,int i, int *Need, int *Available);
//安全性檢測算法
Status SecurityCheck(int *Allocation,int *Need, int *Available);
//分配資源
Status AllocateResource(LinkList PCBdata , int pos , int *Request);
//獲取資源矩陣
void GetMatrixData(LinkList PCBdata,int *Max,int *Allocation,int *Need,int *Available);
//打印進程資源信息
void PrintProQueue(LinkList L, int *A);
//得到指定PCB名的位置
void GetPos(LinkList L, char *name, int len, int *pos);
//對當前的請求進行預分配
void PreGrant(int* Allocation, int *Request,int pos,int *Need, int *Available);
//正式分配算法
void GrantSource(LinkList L, int *Request, int pos, int *Available);#endif

chainlist.c

#include "ChainList.h"
extern int CPUUsedTime;
Status Init(LinkList *L)
{*L = (LinkList)malloc(sizeof(LNode));strcpy((*L)->data.Name, "");(*L)->Next = NULL;return OK;
}void Assignment(ElemType *e1, ElemType e2)
{int i = 0;strcpy(e1->Name,e2.Name);for(i = 0; i < RESOURCE_NUM; ++i){e1->resList.AllocationNum[i] = e2.resList.AllocationNum[i];e1->resList.MaxNum[i] = e2.resList.MaxNum[i];e1->resList.NeedNum[i] = e2.resList.NeedNum[i];}
}Status ListInsert_L(LinkList L,ElemType e)	//這樣修改應該不對 p = *L出錯
{LinkList p = L, s;while (p->Next)	p = p->Next;s = (LinkList)malloc(sizeof(LNode));Assignment(&s->data, e);s->Next = p->Next;p->Next = s;return OK;
}

ProPCB.c

#include "ProPCB.h"Status GetProcess(LinkList Q,ElemType e)
{return ListInsert_L(Q, e);
}Status AllocateResource(LinkList PCBdata , int pos , int *Request)
{int i = 1;LNode *p = PCBdata->Next;while (p && i < pos){p = p->Next;++i;}if(!p || i > pos)return ERROR;for (i = 0; i < RESOURCE_NUM; ++i){p->data.resList.AllocationNum[i] += Request[i];p->data.resList.NeedNum[i] -= Request[i];}return OK;
}
void GetMatrixData(LinkList PCBdata,int *Max,int *Allocation,int *Need,int *Available)
{LNode *p;int i, j, c = RESOURCE_NUM;Available[0] = Available[1] = Available[2] = 0;for(p = PCBdata->Next, i = 0; p; p = p->Next, ++i){for(j = 0; j < RESOURCE_NUM; ++j){Max[i * c + j] = p->data.resList.MaxNum[j];Allocation[i * c + j] = p->data.resList.AllocationNum[j];Need[i * c + j] = p->data.resList.NeedNum[j];}Available[0] += Allocation[i * c + 0];Available[1] += Allocation[i * c + 1];Available[2] += Allocation[i * c + 2];}Available[0] =  MAX_RESOURCE_A_NUM - Available[0];Available[1] =  MAX_RESOURCE_B_NUM - Available[1];Available[2] =  MAX_RESOURCE_C_NUM - Available[2];
}void PrintProQueue(LinkList L,int *available)
{int i = 0;L = L->Next;printf(" -------------------------------------------------------------\n");printf("|進程名 |     Max    |  Allocation |    Need    |  Available  |\n");printf("|       |  A   B   C |  A   B   C  | A   B   C  |  A   B   C  |\n");while(L){printf("|  %s   |  %d   %d   %d |  %d   %d   %d  | %d   %d   %d  |  %d   %d   %d  |\n",L->data.Name, L->data.resList.MaxNum[0], L->data.resList.MaxNum[1], L->data.resList.MaxNum[2],L->data.resList.AllocationNum[0],L->data.resList.AllocationNum[1],L->data.resList.AllocationNum[2],L->data.resList.NeedNum[0],L->data.resList.NeedNum[1],L->data.resList.NeedNum[2],available[0], available[1], available[2]);L = L->Next;}printf(" -------------------------------------------------------------\n");}//安全性檢測算法
Status SecurityCheck(int *Allocation,int *Need, int *Available)
{/ 以下補充  //int work[RESOURCE_NUM];int Finish[PCB_Num];int k, i, j, t, f;int flag;//初始化工作向量和標記數組memcpy(work, Available, sizeof work);memset(Finish, 0, sizeof Finish);//最多檢測PCB_Num次for(k = 0; k < PCB_Num; ++k){flag = 0;for(i = 0; i < PCB_Num; ++i){//已經被訪問if(Finish[i]){continue;}//檢測是否所有資源都能被分配for(j = 0; j < RESOURCE_NUM; ++j){if(!(Need[i * 3 + j] <= work[j])){break;}}//可以滿足,回收if(j == RESOURCE_NUM){for(t = 0; t < RESOURCE_NUM; ++t){work[t] += Allocation[i * 3 + t];}Finish[i] = 1;flag = 1;break;}}//為進行分配,跳出循環if(!flag){break;}}for(f = 0; f < PCB_Num; ++f){//只要有一個進程不滿足,跳出循環if(!Finish[f]){return ERROR;}}return OK;
}//銀行家算法
Status BankerAlgorithm(int* Allocation, int *Request,int pos,int *Need, int *Available)
{/ 以下補充  //int i;//檢查請求的是否大于需要的for(i = 0; i < RESOURCE_NUM; ++i){if(Request[i] > Need[pos*3 + i]){return ERROR;}}//檢查請求的是否大于可分配的for(i = 0; i < RESOURCE_NUM; ++i){if(Request[i] > Available[i]){return ERROR;}}//進行預分配PreGrant(Allocation, Request, pos, Need, Available);//進行安全性檢測if(!SecurityCheck(Allocation, Need, Available)){return ERROR;}return OK;
}//根據PCB的名字得到該PCB在鏈表中的位置
void GetPos(LinkList L, char *name, int len, int *pos)
{LinkList p = L->Next;char PcbName[NAME_MAXSIZE];memcpy(PcbName, name, (len + 1) * sizeof(char));(*pos) = 0;while(p){if(strcmp(p->data.Name, PcbName)){(*pos)++;p = p->Next;} else {break;}}
}//預分配算法
void PreGrant(int* Allocation, int *Request,int pos,int *Need, int *Available){int i;//1. Need減去請求的for(i = 0; i < RESOURCE_NUM; ++i){Need[pos*3 + i] -= Request[i];}//2. Available減去請求的for(i = 0; i < RESOURCE_NUM; ++i){Available[i] -= Request[i];}//3. Allocation加上請求的for(i = 0; i < RESOURCE_NUM; ++i){Allocation[pos*3 + i] += Request[i];}
}/*** 1.首先對請求資源的進程進行分配資源* 2.如果給該進程分配資源之后,該進程所需的資源等于已經得到的資源,那么對其擁有的資源進行回收*///正式分配算法,pos從0開始標記
void GrantSource(LinkList L, int *Request, int pos, int *Available){LinkList p = L->Next;int tag = 0;int i;int flag = 0;if(tag < pos && NULL != p){p = p->Next;tag++;}if(p){//已獲得的加上請求的for(i = 0; i < RESOURCE_NUM; ++i){p->data.resList.AllocationNum[i] += Request[i];}//還需要的減去請求的for(i = 0; i < RESOURCE_NUM; ++i){p->data.resList.NeedNum[i] -= Request[i];}//可利用的減去請求的for(i = 0; i < RESOURCE_NUM; ++i){Available[i] -= Request[i];}//如果進行分配之后該進程最大所需資源數目等于已獲得的資源數目,則對資源進行回收flag = 0;for(i = 0; i < RESOURCE_NUM; ++i){if(p->data.resList.AllocationNum[i] != p->data.resList.MaxNum[i]){flag = 1;break;}}if(!flag){for(i = 0; i < RESOURCE_NUM; ++i){Available[i] += p->data.resList.AllocationNum[i];}}}
}

main

#include "ProPCB.h"void InputData(LinkList * pPCBdata)
{ElemType e = {{0},{{0},{0},{0}}};strcpy(e.Name,"P0");e.resList.MaxNum[0] = 7;	e.resList.MaxNum[1] = 5;	e.resList.MaxNum[2] = 3;e.resList.AllocationNum[0] = 0;e.resList.AllocationNum[1] = 1;e.resList.AllocationNum[2] = 0;e.resList.NeedNum[0] = 7;	e.resList.NeedNum[1] = 4;	e.resList.NeedNum[2] = 3;	GetProcess(*pPCBdata,e);strcpy(e.Name,"P1");e.resList.MaxNum[0] = 3;	e.resList.MaxNum[1] = 2;	e.resList.MaxNum[2] = 2;e.resList.AllocationNum[0] = 2;e.resList.AllocationNum[1] = 0;e.resList.AllocationNum[2] = 0;e.resList.NeedNum[0] = 1;	e.resList.NeedNum[1] = 2;	e.resList.NeedNum[2] = 2;	GetProcess(*pPCBdata,e);strcpy(e.Name,"P2");e.resList.MaxNum[0] = 9;	e.resList.MaxNum[1] = 0;	e.resList.MaxNum[2] = 2;e.resList.AllocationNum[0] = 3;e.resList.AllocationNum[1] = 0;e.resList.AllocationNum[2] = 2;e.resList.NeedNum[0] = 6;	e.resList.NeedNum[1] = 0;	e.resList.NeedNum[2] = 0;	GetProcess(*pPCBdata,e);strcpy(e.Name,"P3");e.resList.MaxNum[0] = 2;	e.resList.MaxNum[1] = 2;	e.resList.MaxNum[2] = 2;e.resList.AllocationNum[0] = 2;e.resList.AllocationNum[1] = 1;e.resList.AllocationNum[2] = 1;e.resList.NeedNum[0] = 0;	e.resList.NeedNum[1] = 1;	e.resList.NeedNum[2] = 1;	GetProcess(*pPCBdata,e);strcpy(e.Name,"P4");e.resList.MaxNum[0] = 4;	e.resList.MaxNum[1] = 3;	e.resList.MaxNum[2] = 3;e.resList.AllocationNum[0] = 0;e.resList.AllocationNum[1] = 0;e.resList.AllocationNum[2] = 2;e.resList.NeedNum[0] = 4;	e.resList.NeedNum[1] = 3;	e.resList.NeedNum[2] = 1;	GetProcess(*pPCBdata,e);
}
int main(void)
{LinkList PCBdata;	//PCBdata里面存放原始數據ElemType e = {{0},{{0},{0},{0}}};char PcbName[NAME_MAXSIZE], chioce;int Max[PCB_Num][RESOURCE_NUM] = {0}, Allocation[PCB_Num][RESOURCE_NUM] = {0};int Need[PCB_Num][RESOURCE_NUM] = {0}, Available[RESOURCE_NUM] = {0};int Request[RESOURCE_NUM] = {0}, pos = 0;LNode *p = NULL;int i;/ 以下補充  ////初始化就緒隊列Init(&PCBdata);//數據輸入InputData(&PCBdata);while(1){//獲取所有PCB中的資源信息GetMatrixData(PCBdata, *Max, *Allocation, *Need, Available);//打印當前系統的狀態PrintProQueue(PCBdata, Available);//接受請求printf("請輸入申請資源的進程名,資源A,資源B,資源C申請量(空格隔開):");scanf("%s", PcbName);for(i = 0; i < RESOURCE_NUM; ++i){scanf("%d", &Request[i]);}//獲取相應的PCB在鏈表中的位置GetPos(PCBdata, PcbName, strlen(PcbName), &pos);//跑銀行家算法,根據返回值的狀態判斷是否安全,//如果安全,進行正式分配,否則僅僅打印不安全信息if(BankerAlgorithm(*Allocation, Request, pos, *Need, Available)){//正式分配資源GrantSource(PCBdata, Request, pos, Available);//分配完成后,打印資源的信息GetMatrixData(PCBdata, *Max, *Allocation, *Need, Available);PrintProQueue(PCBdata, Available);printf("請安任意鍵繼續. . . ");getchar();getchar();} else {printf("不安全,不可分配!\n");}}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445100.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445100.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445100.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Lua 與 C混合編程 .

本文通過程序實例說明C調用lua腳本和lua調用C的方法: 先建立一個 test.c文件: #include <stdio.h> #include <stdlib.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #pragma comment(lib, "lua5.1.lib&qu…

模擬固定分區分配

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PCB #define Status int…

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 list.h #ifndef _List_h_ #define _List_h_#include "Data.h"//******* 鏈表 *******// Status InitLinkList(LinkList *L); void PCBAssign(PCBType *e1, PCBType e2); Status GetElemt_L(LinkList L,int i,PCBType *e); Status ListIn…

python模塊(4)-Collections

collections1.collection.counter(list)2.collections.defaultdict()3.collection.dequecollections是Python內建的一個集合模塊&#xff0c;提供了許多有用的集合類。collections在python官方文檔中的解釋是High-performance container datatypes1.collection.counter(list) …

js知識點匯總

1.本門課的作用&#xff08;JavaScript的作用&#xff09;所有基于Web的程序開發基礎 2.一種計算機客戶端腳本語言&#xff0c;主要在Web瀏覽器解釋執行。 3.瀏覽器中Javascript&#xff0c;用于與用戶交互&#xff0c;以及實現頁面中各種動態特效 4.在HTML文件中&#xff0…

redis——內存概述

Redis通過自己的方法管理內存,&#xff0c;主要方法有zmalloc(),zrealloc()&#xff0c; zcalloc()和zfree(), 分別對應C中的malloc(), realloc()、 calloc()和free()。相關代碼在zmalloc.h和zmalloc.c中。 Redis自己管理內存的好處主要有兩個&#xff1a;可以利用內存池等手段…

Windows下如何用C語言清空特定文件夾中的所有文件

#include "iostream.h" //由于該博客系統發布是不能顯示正常&#xff0c;代碼如需調試&#xff0c;只需將改成""即可 #include "string.h" #include "stdlib.h" #include "time.h" #include "math.h" #include…

MachineLearning(5)-去量綱:歸一化、標準化

去量綱&#xff1a;歸一化、標準化1.歸一化(Normalization)1.1 Min-Max Normalization1.2 非線性Normalization2.標準化(Standardlization)2.1 Z-score Normalization3.標準化在梯度下降算法中的重要性本博文為葫蘆書《百面機器學習》閱讀筆記。去量綱化 可以消除特征之間量綱的…

GDB調試技術(一)

啟動GDB的方法有以下幾種: 1、gdb <program> program也就是你的執行文件,一般在當然目錄下。 2、gdb <program> core 用gdb同時調試一個運行程序和core文件,core是程序非法執行后core dump后產生的文件。 3、

GDB調試技術(二)

1) 恢復程序運行和單步調試 當程序被停住了,你可以用continue命令恢復程序的運行直到程序結束,或下一個斷點到來。也可以使用step或next命令單步跟蹤程序。 continue [ignore-count] c [ignore-count] fg [ignore-count] 恢復程序運行,直到程序結束,或是下一個斷點到…

關于Java中String的問題

String 對象的兩種創建方式&#xff1a; String str1 "abcd";//先檢查字符串常量池中有沒有"abcd"&#xff0c;如果字符串常量池中沒有&#xff0c;則創建一個&#xff0c;然后 str1 指向字符串常量池中的對象&#xff0c;如果有&#xff0c;則直接將 st…

學點數學(3)-函數空間

函數空間1.距離&#xff1a;從具體到抽象2.范數3.內積4.拓撲本博文為觀看《上海交通大學公開課-數學之旅-函數空間 》所整理筆記&#xff0c;公開課視頻連接&#xff1a;http://open.163.com/newview/movie/free?pidM8PTB0GHI&midM8PTBUHT0數學中的空間 是 大家研究工作的…

Makefile編寫詳解--項目開發

預備知識&#xff1a; gcc 的3個參數&#xff1a; 1. -o 指定目標文件 gcc sources/main.c -o bin/main 2. -c 編譯的時候只生產目標文件不鏈接 gcc -c sources/main.c -o obj/main.o 3. -I 主要指定頭文件的搜索路徑 gcc -I headers -c main.c -o main.o 4. -l 指定靜…

如何判斷對象已經死亡

引用計數 給對象中添加一個引用計數器&#xff0c;每當有一個地方引用它&#xff0c;計數器就加 1&#xff1b;當引用失效&#xff0c;計數器就減 1&#xff1b;任何時候計數器為 0 的對象就是不可能再被使用的。 這個方法實現簡單&#xff0c;效率高&#xff0c;但是目前主流…

XML常見的操作

1. 創建XML文檔 &#xff08;1&#xff09;創建一個XML文檔非常簡單&#xff0c;其流程如下&#xff1a; ① 用xmlNewDoc函數創建一個文檔指針doc。 ② 用xmlNewNode函數創建一個節點指針root_node。 ③ 用xmlDocSetRootElement將root_node設置為doc的根結點。…