模拟进程调度及银行家算法的实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//main.cpp
#include "Banker.h"
#include "Process.h"
#include
#include


int main ()
{
int choice;
cout<<"请输入选择:(1.进程调度 2.银行家算法.0退出)"<cin>>choice;


PCB pcb;
Queue Qready;

int timeclips;
int process;
int i;
int runtime;
int runedtime = 0;
int killtime = 0;


int Max[][3] = {5,5,9,5,3,6,4,0,11,4,2,5,4,2,4};
int Allocation[][3] = {2,1,2,4,0,2,4,0,5,2,0,4,3,1,4};
int Need[5][3];
int Available[] = {2,3,3};
int k,j;
int Request[3];


if (1 == choice)
{
srand ((int)time(NULL));

InitQueue (Qready);

cout<<"请输入进程数:";
cin>>process;
cout<<"请输入时间片:";
cin>>timeclips;



for (i = 0; i < process; i++)
{
runtime = int (10.0 * rand() / RAND_MAX ) + 1;
= i;
pcb.runtime = runtime;
pcb.runedtime = 0;
pcb.killtime = 0;
PushNode (Qready,pcb);
cout<<"process"<<<<" "<<"runtime="<cin>>choice;

while (choice)
{
cout<<"请输入进程号:";
cin>>k;
cout<<"请输入申请资源数:";
cin>>Request[0]>>Request[1]>>Request[2];
k = k - 1;
Banker (Need,Request,Available,Allocation,k);
ShowArray (Max,Allocation,Available);
cout<<"请输入选择:(1.继续分配资源 0.退出))"<cin>>choice;
}
}
return 0;
}


//bank.cpp
#include "Banker.h"



int ArraySub(int Array1[][3], int Array2[][3], int Array3[][3])
{

int i,j;
for (i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
Array3[i][j] = Array1[i][j] - Array2[i][j];
return 0;
}


bool CompareArray(int Request[], int Need[])
{
int i;
for (i = 0; i < 3; i++)
if (Request[i] > Need[i])
return false;

return true;
}

int SubStract (int Available1[],int Available2[])
{
int i;
for (i = 0; i < 3; i++)
Available1[i] = Available1[i] - Available2[i];
return 0;

}


int Plus(int Available1[],int Available2[])
{
int i;
for (i = 0; i < 3; i++)
Available1[i] = Available1[i] + Available2[i];
return 0;

}



int Copy (int Available1[],int Available2[])
{
int i;
for (i = 0; i < 3; i++)
Available1[i] = Available2[i];
return 0;

}



int CopyArray(int Array1[][3], int Array2[][3])
{
int i,j;
for (i = 0; i < 5; i++)
for (j = 0; j < 3; j++)
Array1[i][j] = Array2[i][j];
return 0;

}



bool SearchProcess (int Need[][3],int Available[],bool bState[] ,int &k)
{
int i;
for (i = 0; i < 5; i++)
if (CompareArray(Need[i],Available) && !bState[i])
{
k = i;
return true;
}
return false;

}

bool Security(int Need[][3]

, int Available[],int Allocation[][3],int Safety[5])
{
bool bState[] = {false,false,false,false,false};

int i = 0,j = 0;
int k;
int AvailableSource[3];
int NeedSource[5][3];
int AllocationSource[5][3];

Copy (AvailableSource,Available);
CopyArray (NeedSource,Need);
CopyArray (AllocationSource,Allocation);

while (SearchProcess (Need,Available,bState,k))
{
Safety[i++] = k;
bState[k] = true;
Plus (Available,Allocation[k]);

}

Copy (Available, AvailableSource);
CopyArray (Need, NeedSource);
CopyArray (Allocation, AllocationSource);
for (j = 0; j < 5; j++)
if (!bState[j])
return false;

return true;

}



bool Banker (int Need[][3], int Request[],int Available[], int Allocation[][3], int k)
{

int Safety[5];

if (!CompareArray(Request,Need[k]))
{
cout<<"资源不能分配"<return false;
}

if (!CompareArray(Request,Available))
{
cout<<"资源不能分配"<return false;
}

int AvailableSource[3];
int NeedSource[5][3];
int AllocationSource[5][3];


Copy (AvailableSource,Available);
CopyArray (NeedSource,Need);
CopyArray (AllocationSource,Allocation);

SubStract (Available,Request);
SubStract (Need[k],Request);
Plus (Allocation[k],Request);



if (Security (Need,Available,Allocation,Safety))
{
cout<<"资源申请成功,存在如下安全序列:"<for (int i = 0; i < 5; i++)
cout<<"p"<
}
else
{
cout<<"资源申请失败!!!!"<Copy (Available, AvailableSource);
CopyArray (Need, NeedSource);
CopyArray (Allocation, AllocationSource);
}

return 0;


}


void ShowArray(int Max[][3],int Allocation[][3],int Available[])
{
int i,j;
cout<<"The Max resource is:"<for (i = 0; i < 5; i++)
{
cout<for (j = 0; j < 3; j++)
cout<}
cout<cout<<"The Allocation resource is:"<for (i = 0; i < 5; i++)
{
cout<for (j = 0; j < 3; j++)
cout<}
cout<cout<<"The available resource is:";
for (i = 0; i < 3; i++)
cout<cout<
}

//process.cpp
#include "Process.h"


void InitQueue(Queue &Q)
{
Q.head =new Lnode;
Q.head ->next=NULL;
Q.rear =Q.head;
}


void PushNode(Queue &Q, PCB pcb)
{
Lnode *p=new Lnode;
p ->pcb=pcb;
p ->next=NULL;

Q.head ->next=p;
Q.head =p;
}


PCB PopNode(Queue &Q)
{
Lnode *p=new Lnode;
p=Q.rear ->next;
Q.rear =Q.rear ->next;
return p->pcb ;
}

bool IsNull(Queue &Q)
{
if(NULL==(Q.rear ->next))
return false;
else
return true;
}




void ShowProcess (PCB pcb)
{
cout<<"process"<<<<" "<<"runtime="<

相关文档
最新文档