資源簡介
操作系統課的實驗(銀行家算法)#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available /*可用資源數*/
{
int value;
struct available *next;
};
struct need /*需求資源數*/
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2;
printf("\n請輸入系統資源的種類數:");
scanf("%d",&colum);
printf("請輸入現時內存中的進程數:");
scanf("%d",&row);
printf("請輸入已分配資源矩陣:\n");
for(i=0;i<row;i++)
{
for (j=0;jnext=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("請輸入最大需求矩陣:\n");
for(i=0;i<row;i++)
{
for (j=0;jnext=maxium2->next=NULL;
scanf("%d",

代碼片段和文件信息
#include??
using?namespace?std;?
#define?MAXPROCESS?50?????????????????????????????????????/*最大進程數*/?
#define?MAXRESOURCE?100??????????????????????????????????/*最大資源數*/?
int?AVAILABLE[MAXRESOURCE];??????????????????????????/*可用資源數組*/?
int?MAX[MAXPROCESS][MAXRESOURCE];????????????????????/*最大需求矩陣*/?
int?ALLOCATION[MAXPROCESS][MAXRESOURCE];??????????????/*分配矩陣*/?
int?NEED[MAXPROCESS][MAXRESOURCE];???????????????????/*需求矩陣*/?
int?REQUEST[MAXPROCESS][MAXRESOURCE];????????????????/*進程需要資源數*/?
bool?FINISH[MAXPROCESS];??????????????????????????/*系統是否有足夠的資源分配*/?
int?p[MAXPROCESS];?????????????????????????????????????/*記錄序列*/?
int?mn;????????????????????????????????????????????????/*m個進程n個資源*/?
void?Init();?
bool?Safe();?
void?Bank();?
void?main()?
{?
????Init();?
????Safe();?
????Bank();?
}?
void?Init()???????????????????????????????????????????/*初始化算法*/?
{?
????????int?ij;?
????????cout<<“請輸入進程的數目:“;?
????????cin>>m;?
????????cout<<“請輸入資源的種類:“;?
????????cin>>n;?
????????cout<<“請輸入每個進程最多所需的各資源數按照“< ????????for(i=0;i for(j=0;j cin>>MAX[i][j];
cout<<“請輸入每個進程已分配的各資源數也按照“< 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<<“請輸入各個資源現有的數目:“< for(i=0;i {?cin>>AVAILABLE[i];}?
}?
void?Bank()?/*銀行家算法*/?
{??????int?icusneed;?
char?again;?
while(1)?
{?cout<<“請輸入要申請資源的進程號(注:第1個進程號為0依次類推)“< cin>>cusneed;?
cout<<“請輸入進程所請求的各資源的數量“< for(i=0;i {?cin>>REQUEST[cusneed][i];?}?
for(i=0;i {if(REQUEST[cusneed][i]>NEED[cusneed][i])?
{?cout<<“您輸入的請求數超過進程的需求量!請重新輸入!“< continue;?}?
if(REQUEST[cusneed][i]>AVAILABLE[i])?
{?cout<<“您輸入的請求數超過系統有的資源數!請重新輸入!“< continue;?}?
}?
for(i=0;i {?AVAILABLE[i]-=REQUEST[cusneed][i];?
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];?
NEED[cusneed][i]-=REQUEST[cusneed][i];?
}?
if(Safe())?
{?cout<<“同意分配請求!“< else?
{?cout<<“您的請求被拒絕!“< for(i=0;i {?AVAILABLE[i]+=REQUEST[cusneed][i];?
ALLOCATION[cusneed][i]-=REQUEST[cusneed][i];?
NEED[cusneed][i]+=REQUEST[cusneed][i];?}?
}?
for(i=0;i {?FINISH[i]=false;?}?
cout<<“您還想再次請求分配嗎?是請按y/Y否請按其它鍵“< cin>>again;?
if(again==‘y‘||again==‘Y‘)?
{?continue;?}?
break;?
}?
}?
bool?Safe()?/*安全性算法*/?
{?
int?ijkl=0;?
int?Work[MAXRESOURCE];?/*工作數組*/?
for(i=0;i Work[i]=AVAILABLE[i];?
for(i=0;i {?
FINISH[i]=false;?
}?
for(i=0;i {?
if(FINISH[i]==true)?
{?
continue;?
}?
else?
{?
for(j=0;j {?
if(NEED[i][j]>Work[j])?
{?
break;?
}?
}?
if(j==n)?
{?
FINISH[i]=true;?
for(k=0;k {?
Work[k]+=ALLOCATION[i][k];?
}?
p[l++]=i;?
i=-1;?
}?
else?
{?
continue;?
}?
}?
if(l==m)?
{?
cout<<“系統是安全的“<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????115712??2008-11-19?01:28??銀行家算法2\Debug\vc60.idb
?????文件?????110592??2008-11-19?01:27??銀行家算法2\Debug\vc60.pdb
?????文件?????548949??2008-11-19?01:27??銀行家算法2\Debug\銀行家算法.exe
?????文件?????790040??2008-11-19?01:27??銀行家算法2\Debug\銀行家算法.ilk
?????文件?????260282??2008-11-19?01:27??銀行家算法2\Debug\銀行家算法.obj
?????文件????1989136??2008-11-19?01:06??銀行家算法2\Debug\銀行家算法.pch
?????文件????1098752??2008-11-19?01:27??銀行家算法2\Debug\銀行家算法.pdb
?????文件??????10470??2008-10-22?16:47??銀行家算法2\算法.txt
?????文件???????3492??2008-11-19?01:20??銀行家算法2\銀行家算法.cpp
?????文件??????39936??2008-11-19?01:19??銀行家算法2\銀行家算法.doc
?????文件???????3451??2008-10-22?16:50??銀行家算法2\銀行家算法.dsp
?????文件????????545??2008-10-22?16:57??銀行家算法2\銀行家算法.dsw
?????文件??????41984??2008-11-19?01:33??銀行家算法2\銀行家算法.ncb
?????文件??????53760??2008-11-19?01:33??銀行家算法2\銀行家算法.opt
?????文件????????254??2008-11-19?01:28??銀行家算法2\銀行家算法.plg
?????目錄??????????0??2008-12-08?11:29??銀行家算法2\Debug
?????目錄??????????0??2008-12-08?11:29??銀行家算法2
-----------?---------??----------?-----??----
??????????????5067355????????????????????17
評論
共有 條評論