操作系统时间片轮转算法与优先级调度算法.docx

上传人:b****5 文档编号:4044713 上传时间:2022-11-27 格式:DOCX 页数:9 大小:16.43KB
下载 相关 举报
操作系统时间片轮转算法与优先级调度算法.docx_第1页
第1页 / 共9页
操作系统时间片轮转算法与优先级调度算法.docx_第2页
第2页 / 共9页
操作系统时间片轮转算法与优先级调度算法.docx_第3页
第3页 / 共9页
操作系统时间片轮转算法与优先级调度算法.docx_第4页
第4页 / 共9页
操作系统时间片轮转算法与优先级调度算法.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

操作系统时间片轮转算法与优先级调度算法.docx

《操作系统时间片轮转算法与优先级调度算法.docx》由会员分享,可在线阅读,更多相关《操作系统时间片轮转算法与优先级调度算法.docx(9页珍藏版)》请在冰豆网上搜索。

操作系统时间片轮转算法与优先级调度算法.docx

操作系统时间片轮转算法与优先级调度算法

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

typedefstructnode

{

charname[10];/*进程标识符*/

intprio;/*进程优先数*/

intround;/*进程时间轮转时间片*/

intcputime;/*进程占用CPU时间*/

intneedtime;/*进程到完成还要的时间*/

intcount;/*计数器*/

charstate;/*进程的状态*/

structnode*next;/*链指针*/

}PCB;

PCB*finish,*ready,*tail,*run;/*队列指针*/

intN;/*进程数*/

/*将就绪队列中的第一个进程投入运行*/

firstin()

{

run=ready;/*就绪队列头指针赋值给运行头指针*/

run->state='R';/*进程状态变为运行态*/

ready=ready->next;/*就绪对列头指针后移到下一进程*/

}

/*标题输出函数*/

voidprt1(chara)

{

if(toupper(a)=='P')/*优先数法*/

printf("namecputimeneedtimeprioritystate\n");

else

printf("namecputimeneedtimecountroundstate\n");

}

/*进程PCB输出*/

voidprt2(chara,PCB*q)

{

if(toupper(a)=='P')/*优先数法的输出*/

printf("%-10s%-10d%-10d%-10d%c\n",q->name,

q->cputime,q->needtime,q->prio,q->state);

else/*轮转法的输出*/

printf("%-10s%-10d%-10d%-10d%-10d%-c\n",q->name,

q->cputime,q->needtime,q->count,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;

}

p=ready;

printf("就绪队列:

");

while(p!

=NULL){

printf("%s\t",p->name);

p=p->next;

}

printf("\n");

p=finish;

printf("完成队列:

");

while(p!

=NULL){

printf("%s\t",p->name);

p=p->next;

}

printf("\n");

getch();/*压任意键继续*/

}

/*优先数的插入算法*/

insert1(PCB*q)

{

PCB*p1,*s,*r;

intb;

s=q;/*待插入的PCB指针*/

p1=ready;/*就绪队列头指针*/

r=p1;/*r做p1的前驱指针*/

b=1;

while((p1!

=NULL)&&b)/*根据优先数确定插入位置*/

if(p1->prio>=s->prio)

{

r=p1;

p1=p1->next;

}

else

b=0;

if(r!

=p1)/*如果条件成立说明插入在r与p1之间*/

{

r->next=s;

s->next=p1;

}

else

{

s->next=p1;/*否则插入在就绪队列的头*/

ready=s;

}

}

/*轮转法插入函数*/

insert2(PCB*p2)

{

tail->next=p2;/*将新的PCB插入在当前就绪队列的尾*/

tail=p2;

p2->next=NULL;

}

/*优先数创建初始PCB信息*/

voidcreate1(charalg)

{

PCB*p;

inti,time;

charna[10];

ready=NULL;/*就绪队列头指针*/

finish=NULL;/*完成队列头指针*/

run=NULL;/*运行队列指针*/

printf("Enternameandtimeofprocess\n");/*输入进程标识和所需时间创建PCB*/

for(i=1;i<=N;i++)

{

p=malloc(sizeof(PCB));

scanf("%s",na);

scanf("%d",&time);

strcpy(p->name,na);

p->cputime=0;

p->needtime=time;

p->state='W';

p->prio=50-time;

if(ready!

=NULL)/*就绪队列不空调用插入函数插入*/

insert1(p);

else

{

p->next=ready;/*创建就绪队列的第一个PCB*/

ready=p;

}

}

//clrscr();

system("CLS");

printf("outputofpriority:

\n");

printf("************************************************\n");

prt(alg);/*输出进程PCB信息*/

run=ready;/*将就绪队列的第一个进程投入运行*/

ready=ready->next;

run->state='R';

}

/*轮转法创建进程PCB*/

voidcreate2(charalg)

{

PCB*p;

inti,time;

charna[10];

ready=NULL;

finish=NULL;

run=NULL;

printf("Enternameandtimeofroundprocess\n");

for(i=1;i<=N;i++)

{

p=malloc(sizeof(PCB));

scanf("%s",na);

scanf("%d",&time);

strcpy(p->name,na);

p->cputime=0;

p->needtime=time;

p->count=0;/*计数器*/

p->state='W';

p->round=2;/*时间片*/

if(ready!

=NULL)

insert2(p);

else

{

p->next=ready;

ready=p;

tail=p;

}

}

//clrscr();

system("CLS");

printf("outputofround\n");

printf("************************************************\n");

prt(alg);/*输出进程PCB信息*/

run=ready;/*将就绪队列的第一个进程投入运行*/

ready=ready->next;

run->state='R';

}

/*优先数调度算法*/

priority(charalg)

{

while(run!

=NULL)/*当运行队列不空时,有进程正在运行*/

{

run->cputime=run->cputime+1;

run->needtime=run->needtime-1;

run->prio=run->prio-3;/*每运行一次优先数降低3个单位*/

if(run->needtime==0)/*如所需时间为0将其插入完成队列*/

{

run->next=finish;

finish=run;

run->state='F';/*置状态为完成态*/

run=NULL;/*运行队列头指针为空*/

if(ready!

=NULL)/*如就绪队列不空*/

firstin();/*将就绪对列的第一个进程投入运行*/

}

else/*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/

if((ready!

=NULL)&&(run->prioprio))

{

run->state='W';

insert1(run);

firstin();/*将就绪队列的第一个进程投入运行*/

}

prt(alg);/*输出进程PCB信息*/

}

}

/*时间片轮转法*/

roundrun(charalg)

{

while(run!

=NULL)

{

run->cputime=run->cputime+1;

run->needtime=run->needtime-1;

run->count=run->count+1;

if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/

{

run->next=finish;

finish=run;

run->state='F';

run=NULL;

if(ready!

=NULL)

firstin();/*就绪对列不空,将第一个进程投入运行*/

}

else

if(run->count==run->round)/*如果时间片到*/

{

run->count=0;/*计数器置0*/

if(ready!

=NULL)/*如就绪队列不空*/

{

run->state='W';/*将进程插入到就绪队列中等待轮转*/

insert2(run);

firstin();/*将就绪对列的第一个进程投入运行*/

}

}

prt(alg);/*输出进程信息*/

}

}

/*主函数*/

main()

{

charalgo;/*算法标记*/

//clrscr();

system("CLS");

printf("typethealgorithm:

P/R(priority/roundrobin)\n");

scanf("%c",&algo);/*输入字符确定算法*/

printf("Enterprocessnumber\n");

scanf("%d",&N);/*输入进程数*/

if(algo=='P'||algo=='p')

{

create1(algo);/*优先数法*/

priority(algo);

}

else

if(algo=='R'||algo=='r')

{

create2(algo);/*轮转法*/

roundrun(algo);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1