#include <stdio.h>
int curr[5] = { 25,40,45,25 }, maxclaim[5] = { 70,60,60,60 };
int maxres[1], running[4], avl[4];
int i, j, exec, r = 1, p = 4, k = 1, alloc = 0, count = 0, safe = 0;
int main()
{
for (i = 0; i < p; i++)
{
running[i] = 1;
count++;
}
maxres[0] = 150;
printf("\n P1 P2 P3 P4");
printf("\n最大的需求:");
for (i = 0; i < p; i++)
{
printf(" %d ", maxclaim[i]);
}
printf("\n分配的资源:");
for (i = 0; i < p; i++)
{
printf(" %d ", curr[i]);
}
printf("\n");
for (i = 0; i < p; i++)
{
alloc += curr[i];
}
for (i = 0; i < r; i++)
{
avl[i] = maxres[i] - alloc;
}
while (count != 0)
{
safe = 0;
for (i = 0; i < p; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < r; j++)
{
if (maxclaim[i] - curr[i] > avl[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf("\n进程%d正在执行", i + 1);
running[i] = 0;
count--;
safe = 1;
for (j = 0; j < r; j++)
{
avl[j] += curr[i];
}
break;
}
}
}
if (!safe) {
printf("\n进程处于不安全状态\n");
break;
}
else {
printf("\n进程处于安全状态");
printf("\n可用资源:");
for (i = 0; i < r; i++) {
printf(" %d", avl[i]);
}
printf("\n");
}
}
getchar();
}
操作系统 | 银行家算法
最新推荐文章于 2024-07-17 23:40:39 发布