用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ 死锁检测算法

2013-03-11 作者: 小蜜锋举报

[c++]代码库

#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();
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...