计算机操作系统的五种调度

代码变量名都已经很直白,这里就不打注释解释了。

FCFS 先来先服务

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


#define FORECASTNUMOFPROCESS 100


struct Proces
{
int name;
int arrivalTime;
int runTime;
}proces[FORECASTNUMOFPROCESS];


int cmpArrivalTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->arrivalTime)>(bb->arrivalTime))?1:-1);
}


int main()
{
int numOfProces;
int i,j;
int time;
printf("请输入进程数:\n");
scanf("%d",&numOfProces);
for(i = 0;i < numOfProces;i++)
{
printf("请输入第%c进程的到达时间和运行时间:\n",'A' + i);
proces[i].name = i;
scanf("%d%d",&proces[i].arrivalTime,&proces[i].runTime);
}
qsort(proces, numOfProces, sizeof(proces[0]), cmpArrivalTime);
time = proces[0].arrivalTime;
for(i = 0;i < numOfProces;i++)
{
if(time < proces[i].arrivalTime)
{
time = proces[i].arrivalTime;
}
printf("%d--%d\n",time,time + proces[i].runTime);
time += proces[i].runTime;
printf("%c\n",'A' + proces[i].name);
}
return 0;
}

SJF 短作业优先

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


#define FORECASTNUMOFPROCESS 100




int numOfProces;


struct Proces
{
int name;
int arrivalTime;
int runTime;
int restTime;
int isFinish;
}proces[FORECASTNUMOFPROCESS];


int isAllFinish()
{
int i;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].isFinish == 0)
{
return 0;
}
}
return 1;
}


int cmpArrivalTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->arrivalTime)>(bb->arrivalTime))?1:-1);
}


int cmpRunTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->runTime)>(bb->runTime))?1:-1);
}


int cmpRestTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->restTime)>(bb->restTime))?1:-1);
}


int searchMinStartTime()
{
int i;
int min;
min = proces[0].arrivalTime;
for(i = 1;i < numOfProces;i++)
{
if(min > proces[i].arrivalTime)
{
min = proces[i].arrivalTime;
}
}
return min;
}


void notPreemptiveMeasure(int time)
{
Proces tempOfProces[FORECASTNUMOFPROCESS];
Proces littleTimeProces[FORECASTNUMOFPROCESS];
int indexOfTempOfProces;
int indexOfLittleTimepOfProces;
int shortProcesTime;
int i;
if(time < searchMinStartTime())
  time =  searchMinStartTime();
for(;!isAllFinish();)
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpRunTime);
indexOfLittleTimepOfProces = 0;
shortProcesTime = tempOfProces[0].runTime;
for(i = 0;i < indexOfTempOfProces;i++)
{
if(tempOfProces[i].runTime == shortProcesTime)
{
littleTimeProces[indexOfLittleTimepOfProces++] = tempOfProces[i];
}
}
qsort(littleTimeProces, indexOfLittleTimepOfProces, sizeof(littleTimeProces[0]), cmpArrivalTime);
for(i = 0;i < numOfProces;i++)
{
if(proces[i].name == littleTimeProces[0].name)
{
proces[i].isFinish = 1;
printf("%d--%d\n",time,time + proces[i].runTime);
printf("%c\n",'A' + proces[i].name);
time += proces[i].runTime;
break;
}
}
}

}


void preemptiveRunMeasure(int time)
{
Proces tempOfProces[FORECASTNUMOFPROCESS];
int indexOfTempOfProces;
int i;
int nextTime;
int flag = -1;
if(time < searchMinStartTime())
  time = nextTime  = searchMinStartTime();
else
  nextTime = time;
for(;!isAllFinish();)
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpRunTime);
if(flag != -1 && flag != tempOfProces[0].name)
{
printf("%d--%d\n",nextTime,time );
nextTime = time;
printf("%c\n",'A' + proces[flag].name);
flag = tempOfProces[0].name;
}
for(i = 0;i < numOfProces;i++)
{
if(proces[i].name == tempOfProces[0].name)
{
proces[i].restTime--;
flag = tempOfProces[0].name;
if(proces[i].restTime == 0)
{
proces[i].isFinish = 1;
}
break;
}
}
time++;
}
printf("%d--%d\n",time - (proces[flag].runTime - proces[flag].restTime),time );
printf("%c\n",'A' + proces[flag].name);
}


void preemptiveRestMeasure(int time)
{
Proces tempOfProces[FORECASTNUMOFPROCESS];
int indexOfTempOfProces;
int i;
int nextTime;
int flag = -1;
if(time < searchMinStartTime())
  time = nextTime  = searchMinStartTime();
else
  nextTime = time;
for(;!isAllFinish();)
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpRestTime);
if(flag != -1 && flag != tempOfProces[0].name)
{
printf("%d--%d\n",nextTime,time );
nextTime = time;
printf("%c\n",'A' + proces[flag].name);
flag = tempOfProces[0].name;
}
for(i = 0;i < numOfProces;i++)
{
if(proces[i].name == tempOfProces[0].name)
{
proces[i].restTime--;
flag = tempOfProces[0].name;
if(proces[i].restTime == 0)
{
proces[i].isFinish = 1;
}
break;
}
}
time++;
}
printf("%d--%d\n",time - (proces[flag].runTime - proces[flag].restTime),time );
printf("%c\n",'A' + proces[flag].name);
}


int main()
{
int i,j;
int flagIsPreemptive;
int flagOfRunOrRest;
int startTime;
printf("请输入起始时间:");
scanf("%d",&startTime);
printf("请输入进程数:");
scanf("%d",&numOfProces);
for(i = 0;i < numOfProces;i++)
{
printf("请输入%c进程的到达时间和运行时间:\n",'A' + i);
proces[i].name = i;
scanf("%d %d",&proces[i].arrivalTime,&proces[i].runTime);
proces[i].restTime = proces[i].runTime;
}
printf("是否抢占?0否,1是\n");
scanf("%d",&flagIsPreemptive);
if(flagIsPreemptive)
{
printf("抢占类型是运行时间还是剩余时间?0运行时间,1剩余时间\n");
scanf("%d",&flagOfRunOrRest);
if(flagOfRunOrRest)
{
preemptiveRestMeasure(startTime);
}
else
{
preemptiveRunMeasure(startTime);
}
}
else
{
notPreemptiveMeasure(startTime);
}
return 0;
}

优先级调度

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


#define FORECASTNUMOFPROCESS 100


int numOfProces;


struct Proces
{
int name;
int arrivalTime;
int runTime;
int priority;
int isFinish;
int restTime;
}proces[FORECASTNUMOFPROCESS];


int isAllFinish()
{
int i;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].isFinish == 0)
{
return 0;
}
}
return 1;
}


int cmpPriorityTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->priority)<(bb->priority))?1:-1);
}


int cmpRunTimeTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->runTime)>(bb->runTime))?1:-1);
}


int cmpArrivalTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->arrivalTime)>(bb->arrivalTime))?1:-1);
}


int searchMinStartTime()
{
int i;
int min;
min = proces[0].arrivalTime;
for(i = 1;i < numOfProces;i++)
{
if(min > proces[i].arrivalTime)
{
min = proces[i].arrivalTime;
}
}
return min;
}


void PSAnotPreemptiveMeasure(int time)
{
int i;
int mostHigh;
int indexOfTempOfProces;
int indexOfHighProces;
Proces tempOfProces[FORECASTNUMOFPROCESS];
Proces highPriorityProces[FORECASTNUMOFPROCESS];
if(time < searchMinStartTime())
{
time = searchMinStartTime();
}
while(!isAllFinish())
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpPriorityTime);
mostHigh = tempOfProces[0].priority;
indexOfHighProces = 0;
for(i = 0;i < indexOfTempOfProces;i++)
{
if(tempOfProces[i].priority == mostHigh)
{
highPriorityProces[indexOfHighProces++] = tempOfProces[i];
}
}
qsort(highPriorityProces, indexOfHighProces, sizeof(highPriorityProces[0]), cmpArrivalTime);
proces[highPriorityProces[0].name].isFinish = 1;
printf("%d--%d\n",time,time + highPriorityProces[0].runTime);
printf("%c\n",'A' + highPriorityProces[0].name);
time += highPriorityProces[0].runTime;
}
}


void PSAPreemptiveMeasure(int time)
{
Proces tempOfProces[FORECASTNUMOFPROCESS];
Proces highPriorityProces[FORECASTNUMOFPROCESS];
int indexOfTempOfProces;
int indexOfHighProces;
int mostHigh;
int nextTime;
int i;
int flag = -1;
if(time < searchMinStartTime())
{
time = nextTime = searchMinStartTime();
}
else
  nextTime = time;
for(;!isAllFinish();)
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
   qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpPriorityTime);
   mostHigh = tempOfProces[0].priority;
   indexOfHighProces = 0;
   for(i = 0;i < indexOfTempOfProces;i++)
   {
   if(tempOfProces[i].priority == mostHigh)
   {
   highPriorityProces[indexOfHighProces++] = tempOfProces[i];
   }
   }
   qsort(highPriorityProces, indexOfHighProces, sizeof(highPriorityProces[0]), cmpRunTimeTime);
   if(flag != -1 && flag != highPriorityProces[0].name)
   {
   printf("%d--%d\n",nextTime,time );
   nextTime = time;
   printf("%c\n",'A' + proces[flag].name);
   flag = tempOfProces[0].name;
   }
   for(i = 0;i < numOfProces;i++)
   {
   if(proces[i].name == highPriorityProces[0].name)
   {
    proces[i].restTime--;
    flag = proces[i].name;
    if(proces[i].restTime == 0)
    {
    proces[i].isFinish = 1;
    }
    break;
   }
    }
time++;
}
printf("%d--%d\n",time - (proces[flag].runTime - proces[flag].restTime),time );
printf("%c\n",'A' + proces[flag].name);
}


void PSADynamicMeasure(int time)
{
int changeValue;
printf("请输入随每秒时间变化优先级的改变量:\n");
scanf("%d",&changeValue);
Proces tempOfProces[FORECASTNUMOFPROCESS];
Proces highPriorityProces[FORECASTNUMOFPROCESS];
int indexOfTempOfProces;
int indexOfHighProces;
int mostHigh;
int nextTime;
int i;
int flag = -1;
qsort(proces, numOfProces, sizeof(proces[0]), cmpArrivalTime);
if(time < proces[0].arrivalTime)
  time = nextTime = proces[0].arrivalTime;
else
  nextTime = time;
for(;!isAllFinish();)
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
   qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpPriorityTime);
   mostHigh = tempOfProces[0].priority;
   indexOfHighProces = 0;
   for(i = 0;i < indexOfTempOfProces;i++)
   {
   if(tempOfProces[i].priority == mostHigh)
   {
   highPriorityProces[indexOfHighProces++] = tempOfProces[i];
   }
   }
   qsort(highPriorityProces, indexOfHighProces, sizeof(highPriorityProces[0]), cmpRunTimeTime);
   if(flag != -1 && flag != highPriorityProces[0].name)
   {
   printf("%d--%d\n",nextTime,time );
   nextTime = time;
   printf("%c\n",'A' + proces[flag].name);
   flag = tempOfProces[0].name;
   }
   for(i = 0;i < numOfProces;i++)
   {
   if(proces[i].name == highPriorityProces[0].name)
   {
    proces[i].restTime--;
    proces[i].priority += changeValue; 
    flag = proces[i].name;
    if(proces[i].restTime == 0)
    {
    proces[i].isFinish = 1;
    }
    break;
   }
    }
time++;
}
printf("%d--%d\n",time - (proces[flag].runTime - proces[flag].restTime),time );
printf("%c\n",'A' + proces[flag].name);
}


int main()
{
int i,j;
int IsPreemptive;
int IsStaticOrDynamic;
int time;
printf("请输入起始时间:\n");
scanf("%d",&time);
printf("请输入进程数:\n");
scanf("%d",&numOfProces);
for(i = 0;i < numOfProces;i++)
{
printf("请输入第%c进程的到达时间,运行时间和优先级:\n",'A' + i);
proces[i].name = i;
scanf("%d%d%d",&proces[i].arrivalTime,&proces[i].runTime,&proces[i].priority);
proces[i].restTime = proces[i].runTime;
    }
    printf("请输入优先级的类型?0是静态的,1是动态的\n");
    scanf("%d",&IsStaticOrDynamic);
    if(IsStaticOrDynamic)
    {
    PSADynamicMeasure(time);
    }
    else
    {
    printf("请输入是否抢占?0是非抢占,1是抢占\n");
    scanf("%d",&IsPreemptive);
    if(IsPreemptive)
    {
    PSAPreemptiveMeasure(time);
    }
    else
    {
    PSAnotPreemptiveMeasure(time);
    }
    }
return 0;
}

高响应比调度

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


#define FORECASTNUMOFPROCESS 100


int numOfProces;


struct Proces
{
int name;
int arrivalTime;
int runTime;
int waitTime;
double priority;
int isFinish;
}proces[FORECASTNUMOFPROCESS];


int isAllFinish()
{
int i;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].isFinish == 0)
{
return 0;
}
}
return 1;
}


int cmpPriorityTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->priority)<(bb->priority))?1:-1);
}


int cmpRunTimeTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->runTime)>(bb->runTime))?1:-1);
}


int cmpArrivalTime(const void *a,const void *b)
{
     struct Proces *aa=(Proces *)a;
     struct Proces *bb=(Proces *)b;
     return(((aa->arrivalTime)>(bb->arrivalTime))?1:-1);
}


void measurePriority(int index)
{

proces[index].priority = (double)proces[index].waitTime / (double)proces[index].runTime + 1;


}


int searchMinStartTime()
{
int i;
int min;
min = proces[0].arrivalTime;
for(i = 1;i < numOfProces;i++)
{
if(min > proces[i].arrivalTime)
{
min = proces[i].arrivalTime;
}
}
return min;
}


void measure(int time)
{
int i;
int indexOfTempOfProces;
Proces tempOfProces[FORECASTNUMOFPROCESS];
if(time < searchMinStartTime())
{
time = searchMinStartTime();
}
while(!isAllFinish())
{
indexOfTempOfProces = 0;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime <= time && proces[i].isFinish == 0)
{
proces[i].waitTime = time;
measurePriority(i);
tempOfProces[indexOfTempOfProces++] = proces[i];
}
}
qsort(tempOfProces, indexOfTempOfProces, sizeof(tempOfProces[0]), cmpPriorityTime);
proces[tempOfProces[0].name].isFinish = 1;
printf("%d--%d\n",time,time + tempOfProces[0].runTime);
printf("%c\n",'A' + tempOfProces[0].name);
time += tempOfProces[0].runTime;
}
}


int main()
{
int time;
int i,j;
printf("请输入开始时间:\n");
scanf("%d",&time);
printf("请输入进程数:\n");
scanf("%d",&numOfProces);
for(i = 0;i < numOfProces;i++)
{
printf("请输入第%c进程的到达时间和运行时间:\n",'A' + i);
proces[i].name = i;
scanf("%d%d",&proces[i].arrivalTime,&proces[i].runTime);
    }
    measure(time);
return 0;
}

时间片轮转

时间片轮转里面的时间片长度默认是2,小编这里懒的改了,自己改一下就好了。

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


#define FORECASTNUMOFPROCESS 100


int numOfProces;


struct Proces
{
int name;
int arrivalTime;
int runTime;
int isFinish;
int restTime;
}proces[FORECASTNUMOFPROCESS];


int isAllFinish()
{
int i;
for(i = 0;i < numOfProces;i++)
{
if(proces[i].isFinish == 0)
{
return 0;
}
}
return 1;
}


void measure()
{
int time = 0;
int len;
int i;
int nextTime;
int flag = 0;
int indexOfSequence = 0;
int sequence[FORECASTNUMOFPROCESS];
int tempOfSequence[FORECASTNUMOFPROCESS];
while(!isAllFinish())
{
for(i = 0;i < numOfProces;i++)
{
if(proces[i].arrivalTime == time)
{
sequence[indexOfSequence++] = proces[i].name;
}
}
if(time % 2 == flag && time != 0 && time > nextTime)
{
for(i = 1;i < indexOfSequence;i++)
{
tempOfSequence[i - 1] = sequence[i];
}
if(proces[sequence[0]].restTime != 0)
{
printf("%d--%d\n",nextTime,time);
nextTime = time;
   printf("%c\n",'A' + proces[sequence[0]].name);
}
tempOfSequence[indexOfSequence - 1] = sequence[0];
memcpy(sequence,tempOfSequence,sizeof(int) * indexOfSequence);
}
if(indexOfSequence == 0)
{
time++;
nextTime = time;
continue;
}
proces[sequence[0]].restTime--;
if(proces[sequence[0]].restTime == 0)
{
if(time % 2 == 0)
{
flag = 1;
}
else
{
flag = 0;
}
printf("%d--%d\n",nextTime,time + 1);
printf("%c\n",'A' + proces[sequence[0]].name);
proces[sequence[0]].isFinish = 1;
for(i = 1;i < indexOfSequence;i++)
{
sequence[i - 1] = sequence[i];
}
indexOfSequence--;
nextTime = time + 1;
}
time++;
}
}


int main()
{
int i,j;
int IsPreemptive;
int IsStaticOrDynamic;
printf("请输入进程数:\n");
scanf("%d",&numOfProces);
for(i = 0;i < numOfProces;i++)
{
printf("请输入第%c进程的到达时间和运行时间:\n",'A' + i);
proces[i].name = i;
scanf("%d%d",&proces[i].arrivalTime,&proces[i].runTime);
proces[i].restTime = proces[i].runTime;
    }
    measure();
return 0;
}

银行家算法

#include<stdio.h>
#include<string.h>
#define FORECASTNUMOFPROCESS 100
#define FORECASTNUMOFRESOURCE 1000


int availableOfResource[FORECASTNUMOFPROCESS];
int max[FORECASTNUMOFRESOURCE][FORECASTNUMOFPROCESS];
int allocation[FORECASTNUMOFRESOURCE][FORECASTNUMOFPROCESS];
int need[FORECASTNUMOFRESOURCE][FORECASTNUMOFPROCESS];
int finish[FORECASTNUMOFRESOURCE];
int sequence[FORECASTNUMOFRESOURCE];
int numOfProces;
int numOfResource;


int checkSecurity()
{
int i,j;
int flag;
int temoOfAvailableOfResource[FORECASTNUMOFPROCESS];
memcpy(temoOfAvailableOfResource,availableOfResource,sizeof(int) * numOfResource);
for(i = 0;i < numOfProces;i++)
{
if(finish[i] == 1)
{
continue;
}
flag = 1;
for(j = 0;j < numOfResource;j++)
{
if(need[i][j] > temoOfAvailableOfResource[j])
{
flag = 0;
break;
}
}
if(flag)
{
finish[i] = 1;
for(j = 0;j < numOfResource;j++)
{
temoOfAvailableOfResource[j] += allocation[i][j];
}
i = -1;
}
}
for(i = 0;i < numOfProces;i++)
{
if(finish[i] == 0)
{
return 0;
   }
}
return 1;
}


void measureSequence(int numOfRestProcess)
{
int i,j;
int flag;
if(numOfRestProcess == 0)
{
for(i = 0;i < numOfProces;i++)
{
printf("p%d ",sequence[i]);
}
printf("\n");
return;
}
for(i = 0;i < numOfProces;i++)
{
if(finish[i])
{
continue;
}
flag = 1;
for(j = 0;j < numOfResource;j++)
{
if(need[i][j] > availableOfResource[j])
{
   flag = 0;
   break;
}
}
if(flag)
{
finish[i] = 1;
for(j = 0;j < numOfResource;j++)
{
availableOfResource[j] += allocation[i][j];
}
sequence[numOfProces - numOfRestProcess] = i;
measureSequence(numOfRestProcess - 1);
finish[i] = 0;
for(j = 0;j < numOfResource;j++)
{
availableOfResource[j] -= allocation[i][j];
}
}
}
}


int main()
{
int i,j;
int count;
int requestNum;
char sign;
int flag;
int request[FORECASTNUMOFPROCESS];
printf("请输入有多少个进程和资源\n");
scanf("%d%d",&numOfProces,&numOfResource);
printf("请输入每类资源的个数\n");
for(i = 0 ;i < numOfResource;i++)//输入每类资源的个数 
{
printf("资源%c: ",'A' + i);
scanf("%d",&availableOfResource[i]);
}
printf("请输入每个进程对不同资源的需求\n");
for(i = 0;i < numOfProces;i++)
{
printf("进程P%d: \n",i);
for(j = 0 ;j < numOfResource;j++)
{
printf("需要资源%c: \n",'A' + j);
scanf("%d",&max[i][j]);//输入第i个进程对第j类资源的最大需求 
printf("已经分配: \n");
scanf("%d",&allocation[i][j]); //输入对第i个进程的第j类资源已经分配的数量
availableOfResource[j] -= allocation[i][j];
need[i][j] = max[i][j] - allocation[i][j];//计算第i个进程对第j类资源还需要多少 
}
}
if(checkSecurity())
{
memset(finish,0,sizeof(finish));
printf("输入请求次数: \n");
   scanf("%d",&count);
while(count--)
{
printf("输入请求资源的进程号: \n");
scanf("%d",&requestNum);
for(i = 0;i < numOfResource;i++)
{
printf("请求资源%c: \n",'A' + i);
scanf("%d",&request[i]);
availableOfResource[i] -= request[i];
need[requestNum][i] -= request[i];
allocation[requestNum][i] += request[i];
}
if(checkSecurity())
{
memset(finish,0,sizeof(finish));
printf("系统是安全的,所有安全序列如下:\n");
measureSequence(numOfProces);
}
else
{
printf("此时系统不安全!\n");
for(i = 0;i < numOfResource;i++)
   {
   availableOfResource[i] += request[i];
   need[requestNum][i] += request[i];
   allocation[requestNum][i] -= request[i];
   }
continue;
}
printf("是否在此基础上请求资源?(0否,1是)");
scanf("%d",&flag);
for(i = 0;i < numOfResource && flag == 0;i++)
{
availableOfResource[i] += request[i];
need[requestNum][i] += request[i];
allocation[requestNum][i] -= request[i];
}
} 
}
return 0;
}
点赞