c++实现银行家算法

青旅半醒 2022-07-20 15:25 256阅读 0赞

大三上学期的时候自己写的银行家算法的代码,复制粘贴到编译器即可运行,希望帮助到其他人!

  1. /* Financier algorithm.(银行家算法) */
  2. //=====<head file>==============================================================
  3. #include<stdio.h>
  4. #include<windows.h>
  5. #include<conio.h>
  6. //=====<data structure>=========================================================
  7. struct systemInformation
  8. {
  9. int proceeding; //number of preceedings.
  10. int resource; //number of resources.
  11. int available[6];//six kinds of resources.
  12. int maxResource[6];//each max element stand for the max number of one kind of resources .
  13. int allocation[10][6];//allocation[i][j]=k stand for proceeding[i] has been allocated k resource[j].
  14. int need[10][6];//need[i][j]=k stands for proceeding[i] need k resource[j] for finishing.
  15. int finish[10];//finish[i]=1 => proceeding[i] have been finished .
  16. };
  17. struct currentProceedingInformation
  18. {
  19. int proceedingID;
  20. int need[6];
  21. };
  22. //=====<static variable>========================================================
  23. struct systemInformation information;
  24. struct currentProceedingInformation informationForCheck;
  25. int haveBeenInitializedTag = 0;
  26. //=====<function declare>=======================================================
  27. char menu();//show menu UI.
  28. void drawTable();//to show users the current system information.
  29. void defaultInput();//use default information to run this algorithm.
  30. void input();
  31. void financier();//financier algorithm
  32. int securityCheck();//a subordinate function of financier .
  33. //=====<mian function>==========================================================
  34. int main()
  35. {
  36. do
  37. {
  38. char option = menu();
  39. switch(option)
  40. {
  41. case'1':
  42. {
  43. input();
  44. drawTable();
  45. break;
  46. }
  47. case'2':
  48. {
  49. defaultInput();
  50. system("cls");
  51. drawTable();
  52. printf("\n[Enter]继续\n");
  53. getch();
  54. break;
  55. }
  56. case'3':
  57. {
  58. ;//nothing
  59. break;
  60. }
  61. case'4':
  62. {
  63. system("cls");
  64. drawTable();
  65. printf("\n[Enter]继续\n");
  66. getch();
  67. goto sign;
  68. }
  69. }
  70. system("cls");
  71. financier();
  72. sign:;
  73. }while(1);
  74. return 0;
  75. }
  76. //=====<functions>==============================================================
  77. char menu()
  78. {
  79. do
  80. {
  81. system("cls");
  82. printf(" 银行家算法\n");
  83. printf(" ----------------------------\n");
  84. printf("\n");
  85. printf(" (1)---输入数据\n");
  86. printf("\n");
  87. printf(" (2)---使用默认数据\n");
  88. printf("\n");
  89. if(haveBeenInitializedTag == 1)
  90. {
  91. printf(" (3)--继续\n");
  92. printf("\n");
  93. printf(" (4)--显示系统状态\n");
  94. printf("\n");
  95. }
  96. printf(" (0)---退出程序\n");
  97. printf("\n");
  98. printf(" ----------------------------\n");
  99. printf("\n请选择:");
  100. char optionTag = getch();
  101. if(optionTag == '0')
  102. {
  103. system("cls");
  104. printf("\n\n\t\t<欢迎下次使用>\n\n\t\t");
  105. Sleep(1000);
  106. exit(0);
  107. }
  108. else if(optionTag == '1' || optionTag == '2')
  109. return optionTag;
  110. else if((optionTag == '3' ||optionTag == '4') && haveBeenInitializedTag == 1)
  111. return optionTag;
  112. else
  113. {
  114. printf("\n#输入错误#");
  115. Sleep(500);
  116. }
  117. }while(1);
  118. }
  119. void defaultInput()
  120. {
  121. information.proceeding = 5;
  122. information.resource = 3;
  123. information.available[0] = 3;
  124. information.available[1] = 3;
  125. information.available[2] = 2;
  126. information.maxResource[0] = 10;
  127. information.maxResource[1] = 5;
  128. information.maxResource[2] = 7;
  129. information.allocation[0][0] = 0;
  130. information.allocation[0][1] = 1;
  131. information.allocation[0][2] = 0;
  132. information.allocation[1][0] = 2;
  133. information.allocation[1][1] = 0;
  134. information.allocation[1][2] = 0;
  135. information.allocation[2][0] = 3;
  136. information.allocation[2][1] = 0;
  137. information.allocation[2][2] = 2;
  138. information.allocation[3][0] = 2;
  139. information.allocation[3][1] = 1;
  140. information.allocation[3][2] = 1;
  141. information.allocation[4][0] = 0;
  142. information.allocation[4][1] = 0;
  143. information.allocation[4][2] = 2;
  144. information.need[0][0] = 7;
  145. information.need[0][1] = 4;
  146. information.need[0][2] = 3;
  147. information.need[1][0] = 1;
  148. information.need[1][1] = 2;
  149. information.need[1][2] = 2;
  150. information.need[2][0] = 6;
  151. information.need[2][1] = 0;
  152. information.need[2][2] = 0;
  153. information.need[3][0] = 0;
  154. information.need[3][1] = 1;
  155. information.need[3][2] = 1;
  156. information.need[4][0] = 4;
  157. information.need[4][1] = 3;
  158. information.need[4][2] = 1;
  159. //finish[]=0;
  160. for(int i=0;i<5;i++)
  161. {
  162. information.finish[i] = 0;
  163. }
  164. haveBeenInitializedTag = 1;
  165. }
  166. void input()
  167. {
  168. system("cls");
  169. printf("输入信息\n");
  170. printf("---------------------------\n");
  171. printf("\n");
  172. //input proceeding
  173. printf("\n请输入进程的个数:");
  174. int temporary=0;
  175. scanf("%d",&temporary);
  176. if(temporary<=0||temporary>10)
  177. {
  178. printf("\n#输入错误,再见#\n");
  179. getch();
  180. exit(0);
  181. }
  182. else
  183. information.proceeding = temporary;
  184. printf("\n#以下用 P0-p%d 来表示\n",temporary);
  185. //input resource
  186. printf("\n请输入资源的个数:");
  187. scanf("%d",&temporary);
  188. if(temporary<=0 ||temporary>6)
  189. {
  190. printf("\n#输入错误,再见#\n");
  191. getch();
  192. exit(0);
  193. }
  194. else
  195. information.resource = temporary;
  196. printf("\n#以下用 A-%c 来表示",temporary+65);
  197. //input maxResource
  198. for(int i=0;i<information.resource;i++)
  199. {
  200. printf("\n请输入%c类资源总数:",i+65);
  201. scanf("%d",&temporary);
  202. if(temporary<=0)
  203. {
  204. printf("\n#输入错误,再见#");
  205. getch();
  206. exit(0);
  207. }
  208. else
  209. information.maxResource[i] = temporary;
  210. }
  211. //input allocation
  212. for(int i=0;i<information.proceeding;i++)
  213. {
  214. for(int j=0;j<information.resource;j++)
  215. {
  216. printf("\n请输入P%d 当前已占有%c 类资源的数目:",i,j+65);
  217. if(temporary<=0)
  218. {
  219. printf("\n#输入错误,再见#");
  220. getch();
  221. exit(0);
  222. }
  223. else
  224. information.allocation[i][j] = temporary;
  225. }
  226. }
  227. //input need
  228. for(int i=0;i<information.proceeding;i++)
  229. {
  230. for(int j=0;j<information.resource;j++)
  231. {
  232. printf("\n请输入P%d 当前尚需要%c 类资源的数目:",i,j+65);
  233. if(temporary<=0)
  234. {
  235. printf("\n#输入错误,再见#");
  236. getch();
  237. exit(0);
  238. }
  239. else
  240. information.allocation[i][j] = temporary;
  241. }
  242. }
  243. //finish[]=0;
  244. for(int i=0;i<information.proceeding;i++)
  245. {
  246. information.finish[i] = 0;
  247. }
  248. haveBeenInitializedTag = 1;
  249. }
  250. void drawTable()
  251. {
  252. printf("\n\n当前系统状态:\n\n");
  253. printf("P :A allication/need ...\n");
  254. for(int i=0;i<information.proceeding;i++)
  255. {
  256. printf("\n");
  257. printf("P%d: ",i);
  258. if(information.finish[i] == 0)
  259. {
  260. //
  261. for(int j=0;j<information.resource;j++)
  262. {
  263. printf("%c %d/%d ",j+65,information.allocation[i][j],information.need[i][j]);
  264. }
  265. printf(";");
  266. }
  267. else
  268. printf("已完成");
  269. }
  270. printf("\n系统剩余: ");
  271. for(int i=0;i<information.resource;i++)
  272. {
  273. printf("%c:%d ",i+65,information.available[i]);
  274. }
  275. printf("\n");
  276. }
  277. void financier()
  278. {
  279. system("cls");
  280. printf("银行家算法\n");
  281. printf("----------------------------\n\n");
  282. fflush(stdin);//empty input block.
  283. printf("请输入要申请资源的程序(如 P1): ");
  284. int temporary = -1;
  285. char p;
  286. scanf("%c%d",&p,&temporary);
  287. if(temporary<0||temporary>=information.proceeding)
  288. {
  289. printf("\n#输入错误,再见#");
  290. Sleep(1000);
  291. exit(0);
  292. }
  293. else
  294. informationForCheck.proceedingID = temporary;
  295. printf("\n");
  296. for(int i=0;i<information.resource;i++)
  297. {
  298. printf("请输入 P%d 要申请 %c 类资源的数目:",informationForCheck.proceedingID,i+65);
  299. scanf("%d",&temporary);
  300. if(temporary < 0)
  301. {
  302. printf("\n#输入错误,再见#");
  303. Sleep(1000);
  304. exit(0);
  305. }
  306. else if(temporary > information.need[informationForCheck.proceedingID][i])
  307. {
  308. printf("\n#输入错误#\n错误信息: 申请资源量大于该进程需求量\n");
  309. Sleep(1000);
  310. exit(0);
  311. }
  312. else
  313. informationForCheck.need[i] = temporary;
  314. }
  315. if(1 == securityCheck())//the request can be satisfied .
  316. {
  317. for(int i=0;i<information.resource;i++)//allocate
  318. {
  319. //refresh need
  320. information.need[informationForCheck.proceedingID][i] = information.need[informationForCheck.proceedingID][i] - informationForCheck.need[i];
  321. //refresh allocation
  322. information.allocation[informationForCheck.proceedingID][i] = information.allocation[informationForCheck.proceedingID][i]+informationForCheck.need[i];
  323. //refresh available
  324. information.available[i] = information.available[i] - informationForCheck.need[i];
  325. }
  326. }
  327. else
  328. {
  329. ;//nothing.
  330. }
  331. }
  332. int securityCheck()
  333. {
  334. system("cls");
  335. printf("\n安全性检查\n");
  336. printf("----------------------------\n");
  337. struct systemInformation copyInformation = information,temporaryInformation;//to protect original data.
  338. //STEP ONE:experimental allocate
  339. for(int i=0;i<copyInformation.resource;i++)
  340. {
  341. //refresh need
  342. copyInformation.need[informationForCheck.proceedingID][i] = copyInformation.need[informationForCheck.proceedingID][i] - informationForCheck.need[i];
  343. //refresh allocation
  344. copyInformation.allocation[informationForCheck.proceedingID][i] = copyInformation.allocation[informationForCheck.proceedingID][i]+informationForCheck.need[i];
  345. //refresh available
  346. copyInformation.available[i] = copyInformation.available[i] - informationForCheck.need[i];
  347. }
  348. printf("\n\n第一步:试探性分配");
  349. //temporaryInformation <- information <- copyInformation --- to drawTable
  350. temporaryInformation = information;
  351. information = copyInformation;
  352. drawTable();
  353. //copyInformation <- information <- temporaryInformation -- return to the original condition.
  354. copyInformation = information;
  355. information = temporaryInformation;
  356. printf("\n[Enter]下一步\n");
  357. getch();
  358. //STEP TWO:to check whether all of proceedings can finish .
  359. system("cls");
  360. printf("\n安全性检查\n");
  361. printf("----------------------------\n");
  362. printf("\n\n第二步:检测是否所有进程都能够完成.\n");
  363. int haveUnfinishedProceedingTag = 0;//Are there unfinished proceedings ?
  364. int changeTag = 0;//whether finishTag has changed.
  365. int eachSatisfiedTag = 0;//eachSatisfiedTag = resource => changeTag = 1;
  366. for(int i=0;i<copyInformation.proceeding;i++)//scan proceedings for those times.
  367. {
  368. //set variables in order
  369. changeTag = 0;
  370. eachSatisfiedTag = 0;
  371. for(int j=0;j<copyInformation.proceeding;j++)//scan all proceedings each time.
  372. {
  373. if(copyInformation.finish[j] == 0)//this proceeding is unfinished now .
  374. {
  375. eachSatisfiedTag = 0;
  376. //if(need:<=available)
  377. for(int k=0;k<copyInformation.resource;k++)
  378. {
  379. if(copyInformation.need[j][k] <= copyInformation.available[k])
  380. eachSatisfiedTag++;
  381. }
  382. if(eachSatisfiedTag == copyInformation.resource)//this proceeding can finish.
  383. {
  384. copyInformation.finish[j] = 1;//to record its finish .
  385. changeTag = 1;//finish[] was changed .
  386. //then refresh copyInformation
  387. for(int k=0;k<copyInformation.resource;k++)//retrieve its resource .
  388. {
  389. copyInformation.available[k] = copyInformation.available[k] + copyInformation.allocation[j][k];
  390. }
  391. break;
  392. }//if-finish
  393. }//if
  394. }//for-eachTime.
  395. //whether are all proceedings finished ?
  396. haveUnfinishedProceedingTag = 1;
  397. for(int k=0;k<copyInformation.proceeding;k++)//have unfinished proceedings ?
  398. {
  399. if(copyInformation.finish[k] == 0)
  400. {
  401. haveUnfinishedProceedingTag = 0;//yes
  402. break;
  403. }
  404. }
  405. if(haveUnfinishedProceedingTag == 0 && changeTag == 0)//have no way to continue running.
  406. {
  407. printf("\n无法完成分配!\n");
  408. getch();
  409. return 0;
  410. }
  411. else
  412. {
  413. //temporaryInformation <- information <- copyInformation --- to drawTable
  414. temporaryInformation = information;
  415. information = copyInformation;
  416. drawTable();
  417. //copyInformation <- information <- temporaryInformation -- return to the original condition.
  418. copyInformation = information;
  419. information = temporaryInformation;
  420. printf("\n[Enter]继续\n");
  421. getch();
  422. }
  423. }//for-scan
  424. system("cls");
  425. printf("\n所有程序都可以运行完成!\n");
  426. Sleep(500);
  427. printf("\n#系统为安全状态,可以分配#");
  428. Sleep(500);
  429. printf("\n\n[Enter]完成");
  430. getch();
  431. return 1;
  432. }

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 银行家算法实现代码

    银行家算法代码 简介:本文为大家奉献银行家算法的java代码实现,拿去吧,我的算法思路就是深度优先遍历(DFS),对深度优先遍历不熟悉的,可以看看我的这篇博客。 理解D