⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 死锁检测算法c++.txt

📁 操作系统的死锁检测算法
💻 TXT
字号:
/* 本程序包括:死锁检测算法


  VC++调试通过

 

#include<stdio.h>
#include<iostream.h>
#include<string.h>

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<MAXQUEUE;i++){
  occupy[i].process=-1;
  occupy[i].resource=-1;

  wait[i].process=-1;
  wait[i].resource=-1;
}
occupy_quantity=0;
wait_quantity=0;
}


//读数据文件
int readData()
{
FILE *fp;
char fname[20];
int i;

cout<<"请输入资源分配表文件的文件名:"<<endl;
strcpy(fname,"10trouble1.txt");
//cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
  cout<<"错误,文件打不开,请检查文件名:)"<<endl;
  return 0;
}
else{
  while(!feof(fp)){
   fscanf(fp,"%d %d",&occupy[occupy_quantity].resource,&occupy[occupy_quantity].process);
   occupy_quantity++;
  }
}

cout<<"请输入进程等待表文件的文件名:"<<endl;
strcpy(fname,"10trouble2.txt");
//cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
  cout<<"错误,文件打不开,请检查文件名:)"<<endl;
  return 0;
}
else{
  while(!feof(fp)){
   fscanf(fp,"%d %d",&wait[wait_quantity].process,&wait[wait_quantity].resource);
   wait_quantity++;
  }
}

//输出所读入的数据
cout<<endl<<endl<<"输出所读入的数据"<<endl;
cout<<"━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
cout<<"资源分配表"<<endl;
cout<<"资源编号  进程编号"<<endl;

for(i=0;i<occupy_quantity;i++){
  cout<<"  "<<occupy[i].resource<<"     "<<occupy[i].process<<endl;
}
cout<<"───────────────────────"<<endl;
cout<<"进程等待表"<<endl;
cout<<"进程编号  资源编号"<<endl;

for(i=0;i<wait_quantity;i++){
  cout<<"  "<<wait[i].resource<<"     "<<wait[i].process<<endl;
}
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<MAXQUEUE;i++){
  for(j=0;j<MAXQUEUE;j++){
   table[i][j]=0;
   table1[i][j]=0;
  }
}

//先找到进程最大编号
max_process=-1;
for(i=0;i<occupy_quantity;i++){
  if(occupy[i].process>max_process){
   max_process=occupy[i].process;
  }
}
for(i=0;i<wait_quantity;i++){
  if(wait[i].process>max_process){
   max_process=wait[i].process;
  }
}

for(i=0;i<wait_quantity;i++){
  for(j=0;j<occupy_quantity;j++){
   if(wait[i].resource==occupy[j].resource){
    table[wait[i].process][occupy[j].process]=1;
    table1[wait[i].process][occupy[j].process]=1;
   }
  }
}


cout<<"初始等待占用表:"<<endl;
for(i=0;i<max_process+1;i++){
  for(j=0;j<max_process+1;j++){
   cout<<table[i][j]<<"  ";
  }
  cout<<endl;
}

cout<<endl;

for(i=0;i<max_process+1;i++){
  for(j=0;j<max_process+1;j++){
   for(k=0;k<max_process+1;k++){
    table[i][j]=table[i][j]||(table[i][k]&&table[k][j]);
   }
  }
}
cout<<"检测后的等待占用表:"<<endl;

for(i=0;i<max_process+1;i++){
  for(j=0;j<max_process+1;j++){
   cout<<table[i][j]<<"  ";
  }
  cout<<endl;
}

flag=-1;
for(i=0;i<max_process+1;i++){
  if(table[i][i]==1){
   flag=i;
   break;
  }
}
cout<<endl<<endl<<"检测结果"<<endl;
cout<<"───────────────────"<<endl;
if(flag!=-1){
  cout<<"存在死锁"<<endl;
  cout<<"进程循环等待队列:";

  p=flag; //存在进程循环等待队列的那一进程
  //进程循环等待队列中的所有进程是table表中的这一行是1的进程,只是顺序要再确定
  t=1;
  while(t){
   cout<<p<<"  ";
   for(j=0;j<max_process+1;j++){
    if(table1[p][j]==1){
     if(table[j][flag]==1){
      p=j;
      break;
     }
    }
   }
   if(p==flag)t=0;
  }
  cout<<flag<<endl;
}
else{
  cout<<"不存在死锁"<<endl;
}
}
//显示版权信息函数
void version()
{
cout<<endl<<endl;

cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃       死 锁 检 测 算 法         ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃     (c)All Right Reserved  Neo       ┃"<<endl;
cout<<" ┃      sony006@163.com          ┃"<<endl;
cout<<" ┃     version 2004  build 1122      ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}

void main()
{
int flag;

version();

initial();

flag=readData();

if(flag)check();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -