📄 safecheck.cpp
字号:
// SafeCheck.cpp: implementation of the SafeCheck class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "OSDemo.h"
#include "SafeCheck.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SafeChecker::SafeChecker()
{
}
void SafeChecker::SetMatrix(int *alloc,int *need,int *available,int procnum,int resnum)
{
this->m_alloc=alloc;
this->m_need=need;
this->m_available=available;
this->m_procnum=procnum;
this->m_resnum=resnum;
this->buff="";
this->m_list=new int[procnum];
for(int i=0;i<procnum;i++)
{
m_list[i]=i;
}
DoCheck(m_procnum);
if(buff=="")
{
buff+="不存在安全序列!";
}
delete[] m_list;
}
void SafeChecker::DoCheck(int n)
{
int h;
if(n==0)
{
if(IsSafe())
{
CString buf;
for(h=0;h < m_procnum;h++)
{
buf.Format("[进程%d]=>",m_list[h]+1);
buff+=buf;
}
buff+="Finish!\r\n";
}
}
else{
for(h=0;h<n;h++)
{
DoCheck(n-1);
if(n%2!=0)
Swap(m_list[0],m_list[n-1]);
else
Swap(m_list[h],m_list[n-1]);
}
}
}
int SafeChecker::IsSafe()
{
int *work=new int[this->m_resnum];
for(int i=0;i<m_resnum;i++)
{work[i]=m_available[i];}
for(i=0;i<m_procnum;i++)
{
for(int j=0;j<m_resnum;j++)
{
if(m_need[m_list[i]*m_resnum+j]>work[j])
return 0;
}
for(j=0;j<m_resnum;j++)
{
work[j]+=m_alloc[m_list[i]*m_resnum+j];
}
}
return 1;
}
void SafeChecker::Swap(int &a, int &b)
{
int t;
t=a;
a=b;
b=t;
}
CString SafeChecker::GetResult()
{
return buff;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -