貌似我又变的懒了,好多天没有写日记了,虽然这几天天天加班,但是好像没有什么长进嘛。一个未来的同学请我写个东西,我想先在这mark下吧,呵呵,code没有经过严格的测试,不过我是按照操作系统教材里的步骤写的,希望没有什么大问题。下面是code
#include <iostream.h>
#include <fstream.h>
#define ResourcesTypes 3 //资源类型
#define ProcessesCount 5 //进程数目
#define TRUE 1
#define FALSE 0
int nAvailable[ResourcesTypes];//当前可用资源
int nResources[ResourcesTypes];//资源总个数,nResources[i]代表i类资源有多少个
int nPMaxNead[ProcessesCount][ResourcesTypes]; //该类进程最大需求
int nAllocation[ProcessesCount][ResourcesTypes];//已分配
int nNeed[ProcessesCount][ResourcesTypes];//需求
int nReq[ProcessesCount][ResourcesTypes];//当前请求
int nSequence[ProcessesCount]; //安全序列
int nWork[ProcessesCount]={0};//可提供给用户的临时工作向量
int nFinished=0;
bool Finish[ProcessesCount];
int initialize()
{
//......初始化数据,当然我没有全部写出来
ifstream fin("C://test.txt");
int i,j;
cout<<"请输入Available[]"<<endl;
for (i=0;i<ResourcesTypes;i++)
{
fin>>nAvailable[i];
}
cout<<"请输入Allocation[][]"<<endl;
for (i=0;i<ProcessesCount;i++)
{for(j=0;j<ResourcesTypes;j++)
fin>>nAllocation[i][j];
}
cout<<"请输入nNeed[][]"<<endl;
for (i=0;i<ProcessesCount;i++)
for(j=0;j<ResourcesTypes;j++)
fin>>nNeed[i][j];
fin.close();
return 0;
}
void testOut()
{
int i,j;
cout<<endl;
cout<<"请输出Available[]"<<endl;
for (i=0;i<ResourcesTypes;i++)
{
cout<<nAvailable[i]<<" ";
}
cout<<endl;
cout<<"请输出Allocation[][]"<<endl;
for (i=0;i<ProcessesCount;i++)
{for(j=0;j<ResourcesTypes;j++)
cout<<nAllocation[i][j]<<" ";
cout<<endl;
}
cout<<"请输出nNeed[][]"<<endl;
for (i=0;i<ProcessesCount;i++)
{ for(j=0;j<ResourcesTypes;j++)
cout<<nNeed[i][j]<<" ";
cout<<endl;
}
}
int bankerAlgorithm()
{
initialize();//初始化
//如果这时候有Request的话就给Request[][]赋值
block Request
{
}
/*判断
(1) 如果Request i[j]≤Need[i,j],便转向步骤(2);否则认为出错,因为它所需要的资源数已超过它所宣布的最大值。
(2) 如果Requesti[j]≤Available[j],便转向步骤(3);否则,表示尚无足够资源,Pi须等待。
(3) 系统试探着把资源分配给进程P i,并修改下面数据结构中的数值:
Available[j]:= Available[j]-Request i[j];
Allocation[i,j]:= Allocation[i,j]+Request i[j];
Need[i,j]:= Need[i,j]-Request i[j];
(4) 系统执行安全性算法,检查此次资源分配后系统是否处于安全状态。若安全,才正式将资源分配给进程Pi,以完
成本次分配;否则,将本次的试探分配作废,恢复原来的资源分配状态,让进程Pi等待。
*/
block Judge
{
}
//安全检测
if(!bsecurity)
rollback;
return 0;
}
bool bsecurity()
{
bool bEnd = true,bTag;
int i,j,nLength=0;
nFinished=0;
for (i=0;i<ResourcesTypes;i++)
{
nWork[i] = nAvailable[i];
}
for (i=0;i<ProcessesCount;i++)
{
Finish[i] = false;
int nTotalNeed=0;
for (j=0;j<ResourcesTypes;j++)
{
nTotalNeed += nNeed[i][j];
}
if(nTotalNeed == 0)
{
Finish[i] = true;
nFinished++;
}
}
while(nFinished<ProcessesCount)
{
for (i=0;i<ProcessesCount;i++)
if (!Finish[i]){
bTag = true;
for (j=0;j<ResourcesTypes;j++)
{
if(nNeed[i][j]<=nWork[j])
bTag = bTag && true;
else
bTag =bTag && false;
}
if(bTag)
{
for (j=0;j<ResourcesTypes;j++)
nWork[j] += nAllocation[i][j];
Finish[i] = true;
nSequence[nLength++] = i;
}
}
nFinished++;
}
for (i=0;i<ProcessesCount;i++)
{
bEnd =bEnd && Finish[i];
}
return bEnd;
}
int main()
{
return 0;
}