📄 gaixie.c
字号:
/* Note:Your choice is C IDE */
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#define M 5
#define N 3
#define FALSE 0
#define TRUE 1
struct a
{int MAX;
int ALLOCATION;
int NEED;
};
struct b
{int AVAILABLE;
int Request;
};
/*M个进程对N类资源最大资源需求量*/
/*int MAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
/*系统可用资源数*/
/*int AVAILABLE[N]={10,5,7};
/*M个进程对N类资源最大资源需求量*/
/*int ALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
/*M个进程已经得到N类资源的资源量 */
/*int NEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
/*M个进程还需要N类资源的资源量*/
/*int Request[N]={0,0,0};*/
void main()
{
int i=0,j=0;
char finish='Y';
void showdata();
void changdata(int);
void rstordata(int);
void showdata1();
int chkerr(int);
struct a c[100][100];
struct b x[100];
showdata();
while(finish=='Y'||finish=='y')
{
i=-1;
while(i<0||i>=M)
{
printf("input the course number 0 to ");/*请输入需申请资源的进程号(从0到M-1) */
printf("%d",M-1);
printf(",if not return input:");/*否则重输入!*/
scanf("%d",&i);
if(i<0||i>=M)printf("the course number not in, repeat input:\n");/*输入的进程号不存在,重新输入*/
}
printf("the course");/*输入的进程*/
printf("%d",i);
printf(" the requst resources number:\n");/*申请的资源数*/
for (j=0;j<N;j++)
{
printf("resources");/*资源*/
printf("%d",j);
printf(":\n");
scanf("%d",&x[j].Request);
if(x[j].Request>c[i][j].NEED)
{
printf("the course");/*进程*/
printf("%d:",i);
printf("the requst resources number biger the course need number:");/*申请的资源数大于进程 */
printf("%d\n",i);
/*printf("still need:");/*("还需要"); */
/*printf("%d\n",j); */
printf("the requst resources is unreasonable,repeat select\n");/*类资源的资源量!申请不合理,出错!请重新选择*/
finish='N';
break;
}
else
{
if(x[j].Request>x[j].AVAILABLE)
{
printf("the course");/*进程*/
printf("%d:",i);
printf("the requst resources number biger the Available");/*申请的资源数大于系统可用*/
printf("%d",j);
printf("the requst resources is unreasonable,repeat select:\n");/*类资源的资源量!申请不合理,出错!请重新选择*/
finish='N';
break;
}
}
}
if(finish=='Y'||finish=='y')
{
changdata(i);
if(chkerr(i))
{
rstordata(i);
showdata1();
}
else
showdata1();
}
else
showdata1();
}
}
void showdata()
{
int i,j;
struct a c[100][100];
struct b x[100];
printf("the Available nubmer:\n");/*系统可用的资源数为*/
for (j=0;j<N;j++)
{
printf(" resources");/*资源*/
printf("%d",j);
printf(":");
scanf("%d",&x[j].AVAILABLE);
}
printf("\n");
printf("the course need resources:\n");/*各进程还需要的资源量*/
for (i=0;i<M;i++)
{
printf(" course");/*进程 */
printf("%d",i);
printf(":");
for (j=0;j<N;j++){
printf(" resources");/*资源*/
printf("%d",j);
printf(":");
scanf("%d",&c[i][j].NEED);
}
printf("\n");
}
printf("the course already have the Allocation:\n");/*各进程已经得到的资源量*/
for (i=0;i<M;i++)
{
printf(" course:");/*进程*/
printf("%d",i);
for (j=0;j<N;j++){
printf(" resources");/*资源*/
printf("%d",j);
printf(":");
scanf("%d",&c[i][j].ALLOCATION);
}
printf("\n");
}
}
void changdata(int k)
{
int j;
struct a c[100][100];
struct b x[100];
for (j=0;j<N;j++)
{
x[j].AVAILABLE=x[j].AVAILABLE-x[j].Request;
c[k][j].ALLOCATION=c[k][j].ALLOCATION+x[j].Request;
c[k][j].NEED=c[k][j].NEED-x[j].Request;
}
};
void rstordata(int k)
{
int j;
struct a c[100][100];
struct b x[100];
for (j=0;j<N;j++)
{
x[j].AVAILABLE=x[j].AVAILABLE+x[j].Request;
c[k][j].ALLOCATION=c[k][j].ALLOCATION-x[j].Request;
c[k][j].NEED=c[k][j].NEED+x[j].Request;
}
};
int chkerr(int s)
{
int WORK,FINISH[M],temp[M];
int i,j,k=0;
struct a c[100][100];
struct b x[100];
for(i=0;i<M;i++)FINISH[i]=FALSE;
for(j=0;j<N;j++)
{
WORK=x[j].AVAILABLE;
i=s;
while(i<M)
{
if (FINISH[i]==FALSE&&c[i][j].NEED<=WORK)
{
WORK=WORK+c[i][j].ALLOCATION;
FINISH[i]=TRUE;
temp[k]=i;
k++;
i=0;
}
else
{
i++;
}
}
for(i=0;i<M;i++)
if(FINISH[i]==FALSE)
{
printf("\n");
printf("system not safe,this time requst resources is not success:\n");/*系统不安全! 本次资源申请不成功*/
printf("\n");
return 1;
}
}
printf("\n");
printf("system is safe,this time requst resources is success:\n");/*经安全性检查,系统安全,本次分配成功.*/
printf("\n");
printf("the safe number: ");/* 本次安全序列*/
for(i=0;i<M;i++){
printf("course");/*进程*/
printf("%d",temp[i]);
printf("->");
}
printf("\n");
return 0;
}
void showdata1()
{
int i,j;
struct a c[100][100];
struct b x[100];
printf("the Available nubmer:\n");/*系统可用的资源数为*/
for (j=0;j<N;j++)
{
printf(" resources");/*资源*/
printf("%d",j);
printf(":");
printf("%d",&x[j].AVAILABLE);
}
printf("\n");
printf("the course need resources:\n");/*各进程还需要的资源量*/
for (i=0;i<M;i++)
{
printf(" course");/*进程 */
printf("%d",i);
printf(":");
for (j=0;j<N;j++){
printf(" resources");/*资源*/
printf("%d",j);
printf(":");
printf("%d",&c[i][j].NEED);
}
printf("\n");
}
printf("the course already have the Allocation:\n");/*各进程已经得到的资源量*/
for (i=0;i<M;i++)
{
printf(" course:");/*进程*/
printf("%d",i);
for (j=0;j<N;j++){
printf(" resources");/*资源*/
printf("%d",j);
printf(":");
printf("%d",&c[i][j].ALLOCATION);
}
printf("\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -