資源簡介
廣工,操作系統實驗,銀行家算法,源碼
2 實驗要求
1. 假定系統有3類資源A(10個)、B(15個)、C(12個),系有5個進程并發執行,進程調度采用時間片輪轉調度算法。
2. 每個進程由一個進程控制塊( PCB)表示,進程控制塊可以包含如下信息:進程名、需要的資源總數、已分配的資源數、進程狀態。
3. 由程序自動生成進程(包括需要的數據,要注意數據的合理范圍)。
4. 進程在運行過程中會隨機申請資源(隨機生成請求的資源數),如果達到最大需求,表示該進程可以完成;如果沒有達到最大需求,則運行一個時間片后,調度其它進程運行。資源分配采用銀行家算法來避免死鎖。
5. 每個進程的狀態可以是就緒 W(Wait)、運行R(Run)、阻塞B(Block)或完成F(Finish)狀態之一。
6. 每進行一次調度,程序都要輸出一次運行結果:正在運行的進程、就緒隊列中的進程、阻塞隊列中的進程、完成的進程以及各個進程的 PCB,以便進行檢查。
代碼片段和文件信息
#include
#include
#include?
#include
#include?
#include?
#include
using?namespace?std;
/***************BCP數據結構**********************/
struct?PCB{
???string?name;
???int?id;
???int?abc;?//需要的資源總數
???int?allocation_aallocation_ballocation_c;??//已分配的資源總數
???int?need_aneed_bneed_c;??//還需要的資源
???char?state;?//進程狀態
}pcb[5];
struct?resource{
???int?abc;
}res;
/***************各隊列和全局變量、函數聲明*********************/
queue?prepare;
vector?wait;
vector?run;
vector?finish;
vector?block;
int?fini[5];?//表示是否有足夠的資源分配給進程,有為1,無為0
void?createProcess();
void?printProcesses();
void?BanderAlgorithm();
bool?Safe();
/***************主函數**********************/
int?main(){
????srand(time(NULL));
????cout.setf(std::ios::left);
????res.a?=?10;?res.b?=?15;?res.c?=?12;
????int?i;
????printf(“?\n\n\n\n\n\n\n??????????????????????????????????????????????????操作系統實驗報告?\n“);
printf(“????????????????????????################################################################\n“);
printf(“????????????????????????##????????????????????????????????????????????????????????????##\n“);
printf(“????????????????????????##???????????????????????實驗二銀行家算法?????????????????????##\n“);
printf(“????????????????????????##????????????????????????????????????????????????????????????##\n“);
printf(“????????????????????????##????????????????????????????????????????????????????????????##\n“);
printf(“????????????????????????##??軟件2班?????????????????邱偉鋒???????????????3115005282???##\n“);
printf(“????????????????????????################################################################\n\n“);
printf(“?請按任何鍵以繼續??“);
getchar();
system(“cls“);
printf(“\n\n\n\n\n\n\n????????????????????????????????????????????請選擇操作序號:\n“);
printf(“???????????????????????????????@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n“);
printf(“???????????????????????????????@???????????????????????????????????????@\n“);
printf(“???????????????????????????????@?????????**????1=開始??????**??????????@\n“);
printf(“???????????????????????????????@?????????**????2=退出??????**??????????@\n“);
printf(“???????????????????????????????@?注:可能需要2秒左右的時間才能執行完成?@\n“);
printf(“???????????????????????????????@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n“);
printf(“???????????????????????????????????????????請輸入您的選擇:“);
scanf(“%d“?&i);
getchar();
???while?(i?!=?2)
{
switch?(i)
{
case?1:????createProcess();BanderAlgorithm();system(“PAUSE“);
break;
}
????system(“cls“);
printf(“\n\n\n\n\n\n\n????????????????????????????????????????????請選擇操作序號:\n“);
printf(“???????????????????????????????@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n“);
printf(“???????????????????????????????@???????????????????????????????????????@\n“);
printf(“???????????????????????????????@?????????**????1=開始??????**??????????@\n“);
printf(“???????????????????????????????@?????
- 上一篇:C語言實現日歷陽歷+農歷輸出
- 下一篇:基于暗通道和導向濾波的圖像去霧算法C++實現
評論
共有 條評論