#include <stdio.h> #include <stdlib.h>

#define MAX_PROCESSES 10 // 最大进程数 #define MAX_RESOURCES 10 // 最大资源数

int available[MAX_RESOURCES]; // 可利用资源向量 int max[MAX_PROCESSES][MAX_RESOURCES]; // 最大需求矩阵 int allocation[MAX_PROCESSES][MAX_RESOURCES]; // 分配矩阵 int need[MAX_PROCESSES][MAX_RESOURCES]; // 需求矩阵 int work[MAX_RESOURCES]; // 工作向量 int finish[MAX_PROCESSES]; // 完成向量 int n_processes, n_resources; // 进程数、资源数

// 初始化 void initialize() { printf("请输入进程数和资源数:"); scanf("%d %d", &n_processes, &n_resources);

// 输入可利用资源向量
printf("请输入可利用资源向量:");
for (int i = 0; i < n_resources; ++i) {
    scanf("%d", &available[i]);
}

// 输入最大需求矩阵
printf("请输入最大需求矩阵:\n");
for (int i = 0; i < n_processes; ++i) {
    printf("进程 %d:", i);
    for (int j = 0; j < n_resources; ++j) {
        scanf("%d", &max[i][j]);
    }
}

// 输入分配矩阵
printf("请输入分配矩阵:\n");
for (int i = 0; i < n_processes; ++i) {
    printf("进程 %d:", i);
    for (int j = 0; j < n_resources; ++j) {
        scanf("%d", &allocation[i][j]);
    }
}

// 计算需求矩阵
for (int i = 0; i < n_processes; ++i) {
    for (int j = 0; j < n_resources; ++j) {
        need[i][j] = max[i][j] - allocation[i][j];
    }
}

// 初始化工作向量和完成向量
for (int i = 0; i < n_resources; ++i) {
    work[i] = available[i];
}
for (int i = 0; i < n_processes; ++i) {
    finish[i] = 0;
}

}

// 显示资源分配表 void display() { printf("资源分配表:\n"); printf("进程编号 | 最大需求 | 已分配资源 | 需求资源 |\n"); for (int i = 0; i < n_processes; ++i) { printf("%8d |", i); for (int j = 0; j < n_resources; ++j) { printf("%6d", max[i][j]); } printf(" | "); for (int j = 0; j < n_resources; ++j) { printf("%6d", allocation[i][j]); } printf(" | "); for (int j = 0; j < n_resources; ++j) { printf("%6d", need[i][j]); } printf(" |\n"); } }

// 判断是否安全 int is_safe() { // 初始化工作向量和完成向量 for (int i = 0; i < n_resources; ++i) { work[i] = available[i]; } for (int i = 0; i < n_processes; ++i) { finish[i] = 0; }

// 循环直到所有进程都完成或者无法继续分配资源
int count = 0;
while (count < n_processes) {
    int found = 0;
    for (int i = 0; i < n_processes; ++i) {
        if (!finish[i]) {
            int j;
            for (j = 0; j < n_resources; ++j) {
                if (need[i][j] > work[j]) {
                    break;
                }
            }
            if (j == n_resources) {
                for (int k = 0; k < n_resources; ++k) {
                    work[k] += allocation[i][k];
                }
                finish[i] = 1;
                found = 1;
                ++count;
            }
        }
    }
    if (!found) {
        return 0;   // 无法继续分配资源,不安全
    }
}
return 1;   // 所有进程都完成,安全

}

// 动态申请资源 void request_resources() { int process_id; printf("请输入请求资源的进程编号:"); scanf("%d", &process_id);

// 输入请求向量
int request[MAX_RESOURCES];
printf("请输入请求资源向量:");
for (int i = 0; i < n_resources; ++i) {
    scanf("%d", &request[i]);
}

// 判断请求是否合法
for (int i = 0; i < n_resources; ++i) {
    if (request[i] > need[process_id][i]) {
        printf("请求超过了进程的最大需求!\n");
        return;
    }
    if (request[i] > available[i]) {
        printf("没有足够的资源可用!\n");
        return;
    }
}

// 尝试分配资源
for (int i = 0; i < n_resources; ++i) {
    available[i] -= request[i];
    allocation[process_id][i] += request[i];
    need[process_id][i] -= request[i];
}

// 判断是否安全
if (is_safe()) {
    printf("分配成功!\n");
} else {
    printf("分配失败,恢复原状!\n");
    for (int i = 0; i < n_resources; ++i) {
        available[i] += request[i];
        allocation[process_id][i] -= request[i];
        need[process_id][i] += request[i];
    }
}

}

int main() { initialize(); display();

// 动态申请资源
while (1) {
    char choice;
    printf("是否继续申请资源?(y/n)");
    scanf(" %c", &choice);
    if (choice == 'n' || choice == 'N') {
        break;
    }
    request_resources();
    display();
}

return 0;

}

给这个程序一组测试值用来做实验内容:以下是一组测试值:

进程数和资源数:5 3 可利用资源向量:3 3 2 最大需求矩阵: 进程 0:7 5 3 进程 1:3 2 2 进程 2:9 0 2 进程 3:2 2 2 进程 4:4 3 3 分配矩阵: 进程 0:0 1 0 进程 1:2 0 0 进程 2:3 0 2 进程 3:2 1 1 进程 4:0 0 2

输入完毕后,可以选择是否继续申请资源,可以多次测试。

银行家算法 C 语言实现:资源分配安全检测

原文地址: https://www.cveoy.top/t/topic/nZQI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录