-
大小: 5KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-05-10
- 語言: C/C++
- 標(biāo)簽: 操作系統(tǒng)??C語言??
資源簡介
利用C語言實(shí)現(xiàn)操作系統(tǒng)中的銀行家算法(話說說的這么清楚還需要多少描述嗎?額,我這個(gè)是老師親自驗(yàn)收通過的!)
代碼片段和文件信息
#include?
using?namespace?std;
#define?maxprocess?30????????????????????????/*最大進(jìn)程數(shù)*/
#define?maxresource?80????????????????????????/*最大資源數(shù)*/
int?available[maxresource];????????????????????/*可用資源數(shù)組*/
int?max[maxprocess][maxresource];????????????/*最大需求矩陣*/
int?allocation[maxprocess][maxresource];????/*分配矩陣*/
int?need[maxprocess][maxresource];????????????/*需求矩陣*/
int?request[maxprocess][maxresource];????????/*進(jìn)程需要資源數(shù)*/
bool?finish[maxprocess];????????????????????????/*系統(tǒng)是否有足夠的資源分配*/
int?p[maxprocess];?????????????????????????????/*記錄序列*/
int?mn;????????????????????????????????????/*m個(gè)進(jìn)程n個(gè)資源*/
void?Init();
bool?Safe();
void?Bank();
void?main()
{
????Init();
????Safe();
????Bank();
}
void?Init()????????????????/*初始化算法*/
{
????int?ij;
cout<<“**********************************銀行家算法**********************************“< ????cout<<“請(qǐng)輸入進(jìn)程的數(shù)目:“< ????cin>>m;
????cout<<“請(qǐng)輸入資源的種類:“< ????cin>>n;
????cout<<“請(qǐng)輸入每個(gè)進(jìn)程最多所需的各資源數(shù)按照“< ????for(i=0;i ????for(j=0;j ????cin>>max[i][j];
????cout<<“請(qǐng)輸入每個(gè)進(jìn)程已分配的各資源數(shù)也按照“< ????for(i=0;i ????{
????????for(j=0;j ????????{
????????????cin>>allocation[i][j];
????????????need[i][j]=max[i][j]-allocation[i][j];
????????????if(need[i][j]<0)
????????????{
????????????????cout<<“您輸入的第“<????????????????j--;
????????????????continue;
????????????}
????????}
????}
????cout<<“請(qǐng)輸入各個(gè)資源現(xiàn)有的數(shù)目:“< ????for(i=0;i ????{
????????cin>>available[i];
????}
}
void?Bank()????????????????/*銀行家算法*/
{
????int?icusneed;
????char?again;
????while(1)
????{
????????cout<<“請(qǐng)輸入要申請(qǐng)資源的進(jìn)程號(hào)(請(qǐng)從0開始依次輸入)“< ????????cin>>cusneed;
????????cout<<“請(qǐng)輸入進(jìn)程所請(qǐng)求的各資源的數(shù)量“< ????????for(i=0;i ????????{
????????????cin>>request[cusneed][i];
????????}
????????for(i=0;i ????????{
????????????if(request[cusneed][i]>need[cusneed][i])
????????????{
????????????????cout<<“您輸入的請(qǐng)求數(shù)超過進(jìn)程的需求量!請(qǐng)重新輸入!“< ????????????????continue;
????????????}
????????????if(request[cusneed][i]>available[i])
????????????{
????????????????cout<<“您輸入的請(qǐng)求數(shù)超過系統(tǒng)有的資源數(shù)!請(qǐng)重新輸入!“< ???
- 上一篇:C++聊天室客戶端+服務(wù)端
- 下一篇:spath(A*算法的C語言源代碼)
評(píng)論
共有 條評(píng)論