满意答案
chiara330
2013.10.15
采纳率:54% 等级:12
已帮助:24879人
#include
#include
#include
const int MAXQUEUE=100; //定义表的最大行数
typedef struct node{
int resource;
int process;
}cell;
cell occupy[MAXQUEUE];
int occupy_quantity;
cell wait[MAXQUEUE];
int wait_quantity;
//初始化函数
void initial()
{
int i;
for(i=0;i
occupy.process=-1;
occupy.resource=-1;
wait.process=-1;
wait.resource=-1;
}
occupy_quantity=0;
wait_quantity=0;
}
//读数据文件
int readData()
{
FILE *fp;
char fname[20];
int i;
cout<
strcpy(fname,"10trouble1.txt");
//cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
cout<
return 0;
}
else{
while(!feof(fp)){
fscanf(fp,"%d %d",&occupy[occupy_quantity].resource,&occupy[occupy_quantity].process);
occupy_quantity++;
}
}
cout<
strcpy(fname,"10trouble2.txt");
//cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
cout<
return 0;
}
else{
while(!feof(fp)){
fscanf(fp,"%d %d",&wait[wait_quantity].process,&wait[wait_quantity].resource);
wait_quantity++;
}
}
//输出所读入的数据
cout<
cout<
cout<
cout<
for(i=0;i
cout<
}
cout<
cout<
cout<
for(i=0;i
cout<
}
return 1;
}
//检测
void check()
{
int table[MAXQUEUE][MAXQUEUE];
int table1[MAXQUEUE][MAXQUEUE];
int i,j,k;
int flag,t,p;
int max_process;
//初始化表格
for(i=0;i
for(j=0;j
table[j]=0;
table1[j]=0;
}
}
//先找到进程最大编号
max_process=-1;
for(i=0;i
if(occupy.process>max_process){
max_process=occupy.process;
}
}
for(i=0;i
if(wait.process>max_process){
max_process=wait.process;
}
}
for(i=0;i
for(j=0;j
if(wait.resource==occupy[j].resource){
table[wait.process][occupy[j].process]=1;
table1[wait.process][occupy[j].process]=1;
}
}
}
cout<
for(i=0;i
for(j=0;j
cout<
}
cout<
}
cout<
for(i=0;i
for(j=0;j
for(k=0;k
table[j]=table[j]||(table[k]&&table[k][j]);
}
}
}
cout<
for(i=0;i
for(j=0;j
cout<
}
cout<
}
flag=-1;
for(i=0;i
if(table==1){
flag=i;
break;
}
}
cout<
cout<
if(flag!=-1){
cout<
cout<
p=flag; //存在进程循环等待队列的那一进程
//进程循环等待队列中的所有进程是table表中的这一行是1的进程,只是顺序要再确定
t=1;
while(t){
cout<
for(j=0;j
if(table1[p][j]==1){
if(table[j][flag]==1){
p=j;
break;
}
}
}
if(p==flag)t=0;
}
cout<
}
else{
cout<
}
}
00分享举报