#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<windows.h>
void Init(int **arr,int row, int col) //赋值
{
//int** p= (int **)malloc(sizeof(int *)*row);//申请一组一维指针空间。
for (int i = 0; i < row; i++)
arr[i] = (int *)malloc(sizeof(int)* col); //对于每个一维指针,申请一行数据的空间。
for (int i = 0; i < col; ++i)
printf(" %c", 65 + i);
printf("\n");
for (int i = 0; i < row; i++)
{
printf("P%d ", i);
for (int j = 0; j < col; j++)
{
scanf("%d", &arr[i][j]);
}
}
}
void Print_double(int **arr, int row, int col) //打印矩阵
{
for (int i = 0; i < col; ++i)
{
printf(" %c", 65 + i);
}
printf("\n");
for (int i = 0; i < row; i++)
{
printf("P%d", i);
for (int j = 0; j < col; j++)//这个循环可以输出一行元素。
printf(" %d ", arr[i][j]);//输出i行j列的元素。
printf("\n");//每行输入结束加换行符。
}
}
void Print_sing(int* arr, int num) //打印单表
{
for (int i = 0; i < num; ++i)
{
printf(" %d ", arr[i]);
}
}
int ID(int* arr, int procsee) //1 成功 -1失败 //输出序列号
{
int count = 0;
int sum = 0;
int ret = -1;
for (int i = 0; i < procsee; ++i)
{
sum += i;
}
for (int k = 0; k < procsee; ++k)
{
++count;
}
if (count == sum)
{
ret = 1;
printf("安全序列号为:\n");
for (int i = 0; i < procsee; ++i)
{
if (i == procsee - 1)
printf("P%d", arr[i]);
else
printf(" P%d ->", arr[i]);
}
printf("\n");
return ret;
}
return ret;
}
//Allocation //Need //Work ==Avaliable
void Safe(int **arr1, int **arr2,int* arr3,int *safe,int process,int resource) //安全序列
{
int true[10] = { 0 };
int Work[10] = { 0 };
for (int i = 0; i < resource; ++i)
{
Work[i] = arr3[i];
}
int safe_num = 0;
int i = 0;
int n = 0;
while (safe_num != process)
{
if (true[i] == 0)
{
int finish = 0;
for (int j = 0; j < resource; ++j)
{
if (arr2[i][j] <= Work[j])
{
++finish;
}
}
if (finish == resource)
{
for (int k = 0; k < resource; ++k)
{
Work[k] += arr1[i][k];
}
true[i] = 1;
safe[n] = i;
++safe_num;
i = -1;
++n;
}
}
++i;
}
ID(safe, process);
}
//AL //NE //AV
void Dis(int** arr1, int** arr2, int* arr3, int* safe, int process, int resource) //分配
{
int p1 = process;
int p2 = resource;
int Request[10] = { 0 };
int num = 0;
int r2 = 0, r3 = 0;
printf("请输入想要分配的进程序号(0 -- %d):\n", process - 1);
scanf("%d", &num);
printf("请输入请求的资源数目: \n"); //获得Request
for (int i = 0; i < resource; ++i)
{
scanf("%d", &Request[i]);
}
printf("\n");
for (int i = 0; i < resource; ++i) // Avaliable = Avaliable - Request Allocation = Allocation + Request
{ // Need = Need -Request
arr1[num][i] += Request[i];
arr2[num][i] -= Request[i];
arr3[i] -= Request[i];
if (arr1[num][i] < 0 || arr2[num][i] < 0 || arr3[i] < 0)
r3 = 1;
}
if (r3 == 0)
{
for (int i = 0; i < process; ++i) // 把Avaliable 和 Need 进行比较
{
int r1 = 0;
for (int j = 0; j < resource; ++j)
{
if (arr3[j] >= arr2[i][j]) //若Avaliable >= Need ++r1
{
++r1;
}
}
if (r1 == resource) //若r1 == resource 则说明有一个Need 能够被全部满足 则++r2
{
++r2;
}
}
if (r2 != 0)
{
printf("已占用的资源矩阵为:\n");
Print_double(arr1, process, resource);
printf("需要的资源矩阵为:\n");
Print_double(arr2, process, resource);
printf("\n");
printf("可利用的资源为:\n");
Print_sing(arr3, resource);
printf("\n");
Safe(arr1, arr2, arr3, safe, p1, p2);
}
else
{
printf("可利用资源小于系统所需资源,分配失败,展示原本的资源\n");
for (int i = 0; i < resource; ++i)
{
arr1[num][i] -= Request[i];
arr2[num][i] += Request[i];
arr3[i] += Request[i];
}
printf("已占用的资源矩阵为:\n");
Print_double(arr1, process, resource);
printf("需要的资源矩阵为:\n");
Print_double(arr2, process, resource);
printf("\n");
printf("可利用的资源为:\n");
Print_sing(arr3, resource);
}
}
else
printf("可利用资源小于系统所需资源,分配失败\n");
for (int i = 0; i < resource; ++i)
{
arr1[num][i] -= Request[i];
arr2[num][i] += Request[i];
arr3[i] += Request[i];
}
}
void menu()
{
printf("*************************1. 输入数据 ************************\n");
printf("*************************2. 展示数据 ************************\n");
printf("*************************3. 安全序列 ************************\n");
printf("*************************4. 资源分配 ************************\n");
printf("*************************5. 退出 *************************\n");
}
int main()
{
int process = 0;
int resource = 0;
int safe[10] = { 0 };
int input;
int Work[10] = { 0 };
int Avaliable[10] = { 0 };
int** Allocation = (int**)malloc(sizeof(int *)* process);
int** Need = (int**)malloc(sizeof(int *)* process);
while (1)
{
menu();
printf("请输入你的选项:\n");
scanf("%d", &input);
switch (input)
{
case 1: printf("请输入进程数量和资源数量:");
scanf("%d%d", &process, &resource);
if (process <= 0 || resource <= 0)
printf("输入错误,请重新输入!\n");
printf("请输入已占用的资源数组\n");
Init(Allocation, process, resource);
printf("请输入需要的资源数组\n");
Init(Need, process, resource);
printf("请输入可利用资源数目: \n");
for (int i = 0; i < resource; ++i)
{
scanf("%d", &Avaliable[i]);
}
for (int i = 0; i < resource; ++i)
{
Work[i] = Avaliable[i];
}
break;
case 2: printf("已占用的资源为:\n");
Print_double(Allocation, process, resource);
printf("\n");
printf("系统所需的资源为:\n");
Print_double(Need, process, resource);
printf("\n");
printf("你输入的可利用资源数目为:\n");
for (int i = 0; i < resource; ++i)
{
printf("%d ", Avaliable[i]);
}
printf("\n");
break;
case 3:Safe(Allocation, Need, Avaliable, safe, process, resource);
break;
case 4: Dis(Allocation, Need, Avaliable, safe, process, resource);
break;
case 5: exit(0);
default:
break;
}
}
return 0;
}
结果如下:
来源:CSDN
作者:丶独醒
链接:https://blog.csdn.net/m1059247324/article/details/103848199