printf("作业名:
%s",processes[i].name);
printf("A类:
%d/%d",processes[i].a,processes[i].need_a);
printf("B类:
%d/%d",processes[i].b,processes[i].need_b);
printf("C类:
%d/%d",processes[i].c,processes[i].need_c);
printf("\n");
}
}
else{
printf("当前没有作业\n");
}
}
//显示版权信息函数
voidversion()
{
printf("\n");
printf("┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃银行家算法┃\n");
printf("┠───────────────────────┨\n");
printf("┃(c)AllRightReservedYT┃\n");
printf("┃移通学院┃\n");
printf("┃version2010buildsonghua┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\n");
}
voidmain()
{
intchioce=0;
intflag=1;
initial();
version();
while(flag){
printf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
printf("1.新加作业2.为作业申请资源3.撤消作业\n");
printf("4.查看资源情况0.退出系统\n");
printf("请选择:
");
scanf("%d",&chioce);
switch(chioce){
case1:
add();break;
case2:
bid();break;
case3:
finished();break;
case4:
view();break;
case0:
flag=0;break;
default:
printf("选择错误\n");
}
}
}
实验结果:
实验二:
时间片轮转法
基本思想:
将CPU的处理时间划分成一个个时间片,就绪队列中的诸进程轮流运行一个时间片,当时间片结束时,就强迫运行进程让出CPU,该进程进入就绪队列,等待下一次调度,同时,进程调度又去选择就绪队列中的一个进程,分配给它一个时间片,以投入运行。
在轮转法中,时间片长度的选择非常重要,将直接影响系统开销和响应时间。
如果时间片长度很小,则调度程序剥夺处理机的次数频繁,加重系统开销;反之,如果时间片长度选择过长,比方说一个时间片就能保证就绪队列中所有进程都执行完毕,则轮转法就退化成先进先出算法。
代码如下:
#include
#include
#include
typedefstructnode
{
charname[10];/*进程标识符*/
intprio;/*进程优先数*/
intround;/*进程时间轮转时间片*/
intcputime;/*进程占用CPU时间*/
intneedtime;/*进程到完成还要的时间*/
intcount;/*计数器*/
charstate;/*进程的状态*/
structnode*next;/*链指针*/
}PCB;
PCB*finish,*ready,*tail,*run;//队列指针
intN,t;//进程数,时间片的大小
voidfirstin()
{
run=ready;//就绪队列头指针赋值给运行头指针
run->state='R';//进程状态变为运行态
ready=ready->next;//就绪队列头指针后移到下一进程
}
voidprt1(chara)//输出标题函数
{
if(toupper(a)=='P')//优先级法
printf("进程名占用CPU时间到完成还要的时间轮转时间片状态\n");
}
voidprt2(chara,PCB*q)//进程PCB输出
{
if(toupper(a)=='P')//优先级法的输出
printf("%4s%8d%12d%14d%8c\n",q->name,q->cputime,q->needtime,q->round,q->state);
}
voidprt(charalgo)//输出函数
{
PCB*p;
prt1(algo);//输出标题
if(run!
=NULL)//如果运行指针不空
prt2(algo,run);//输出当前正在运行的PCB
p=ready;//输出就绪队列PCB
while(p!
=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish;//输出完成队列的PCB
while(p!
=NULL)
{
prt2(algo,p);
p=p->next;
}
getchar();//按住任意键继续
}
voidinsert(PCB*q)//时间片轮转的插入算法
{
PCB*p1,*s,*r;
s=q;//待插入的PCB指针
p1=ready;//就绪队列头指针
r=p1;//*r做pl的前驱指针
while(p1!
=NULL)
if(p1->round<=s->round)
{
r=p1;
p1=p1->next;
}
if(r!
=p1)
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1;//否则插入在就绪队列的头
ready=s;
}
}
voidcreate(charalg)//时间片轮转法创建链表进程PCB
{
PCB*p;
inti,time;
charna[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("输入进程名及其需要运行的时间(中间以空格隔开):
\n");
for(i=1;i<=N;i++)
{
p=newPCB;
scanf("%s%d",&na,&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='W';//进程的状态
p->round=0;
if(ready!
=NULL)
insert(p);
else
{
p->next=ready;
ready=p;
}
}
printf("*************时间片轮转法进程调度过程*************\n");
prt(alg);
run=ready;
ready=ready->next;
run->state='R';
}
voidtimeslicecycle(charalg)//时间片轮转法
{
while(run!
=NULL)
{
run->cputime=run->cputime+t;//处理时间加t
run->needtime=run->needtime-t;//完成需要时间减t
run->round=run->round+t;//运行完将其变为完成态,插入完成队列
if(run->needtime<=0)//当进程完成时
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!
=NULL)//就绪队列不空,将第一个进程投入进行
firstin();
}
else
{
run->state='W';//将进程插入到就绪队列中等待轮转
insert(run);//将就绪队列的第一个进程投入运行
firstin();
}
prt(alg);
}
}
voidmain()//主函数
{
charalgo='P';//算法标记
printf("输入进程的个数:
");
scanf("%d",&N);//输入进程数
printf("定义时间片大小:
");
scanf("%d",&t);//输入时间片大小
create(algo);//创建进程
timeslicecycle(algo);//时间片轮转法调度
}//main()
实验结果:
实验三:
非抢占式优先级调度算法
基本思想:
该算法的基本思想是进程优先级高者优先调度,是一种常用的进程调度算法。
该算法的关键是如何确定优先数。
通常确定优先数的方法有两种,即静态法和动态法。
(1)静态优先权是在创建进程时确定的,其运行特征是优先数确定之后在整个进行运行期间不再改变。
确定静态优先权的依据有进程的类型、进程所使用的资源、进程的估计运行时间等因素。
进程所申请的资源越多,估计的运行时间越长,进程的优先权越低。
进程类型不同,优先权也不同,如系统进程的优先权高于用户进程的优先权。
(2)动态优先级是指在创建进程时,其运行特征是根据系统资源的使用情况和进程的当前特点确定一个优先权,在进程运行过程中再根据情况的变化调整优先权。
动态优先权一般根据进程占有CPU时间的长短、进程等待CPU时间的长短等因素确定。
占有处理机的时间越长,则优先权越低;等待时间越长,则优先权越高。
①静态优先级调度算法实现较为简单,但不能反映系统以及进程在运行过程中发生的各种变化。
而动态优先级法可以满足这个方面的需要。
②动态优先级调度算法的性能一般介于时间片轮转算法和先来先服务算法之间。
代码如下:
#include
#include
#define MAX 5 //进程数/*非抢占式优先数算法*/
struct pro1
{
int num; //进程名
int arriveTime; //到达时间
int burst; //运行时间;
int weight; //优先数
struct pro1 *next;
};//函数声明
struct pro1* creatList();
void insert(struct pro1 *head,struct pro1 *s);
struct pro1* searchByAT(struct pro1 *head,int AT);
void run(struct pro1 *head);
void del(struct pro1* p);
int getCount(struct pro1 *head,int time);
struct pro1* creatList() //创建链表,按照进程的到达时间排列
{
struct pro1* head=(struct pro1*)malloc(sizeof(struct pro1));
head->next=NULL;
struct pro1 *s;
int i;
for(i=0;i{
s=(struct pro1*)malloc(sizeof(struct pro1));
printf("请输入进程名:
\n");
scanf("%d",&(s->num));
printf("请输入到达时间:
\n");
scanf("%d",&(s->arriveTime));
printf("请输入运行时间:
\n");
scanf("%d",&(s->burst));
printf("请输入优先数:
\n");