资源描述:
《进程调度算法 操作系统》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、进程调度::优先数、时间片轮转、先来先服务代码:#include#include#includetypedefstructnode{charname[10];intprio;intround;intcputime;intneedtime;intcount;charstate;structnode*next;}PCB;PCB*finish,*ready,*run,*r;intN;voidfirstin(){run=ready;run->state='R';rea
2、dy=ready->next;}voidprt1(chara){if(a=='P'
3、
4、a=='p')printf("进程号cpu时间所需时间优先数状态");elseif(a=='r'
5、
6、a=='R')printf("进程号cpu时间所需时间记数时间片状态");elseif(a=='f'
7、
8、a=='F')printf("进程号所需时间状态");}voidprt2(chara,PCB*q){if(a=='P'
9、
10、a=='p')printf("%-10s%-10d%-10d%-10d%c",q->name,
11、q->cputime,q->needtime,q->prio,q->state);elseif(a=='r'
12、
13、a=='R')printf("%-10s%-10d%-10d%-10d%-10d%c",q->name,q->cputime,q->needtime,q->count,q->round,q->state);elseif(a=='f'
14、
15、a=='F')printf("%-10s%-10d%c",q->name,q->needtime,q->state);}voidprt(charalgo){PCB*p
16、;prt1(algo);if(run!=NULL&&!(algo=='r'
17、
18、algo=='R'))//这个对RR不适应!!prt2(algo,run);p=ready;while(p!=NULL){prt2(algo,p);p=p->next;}p=finish;while(p!=NULL){prt2(algo,p);p=p->next;}getchar();}voidinsert1(PCB*q){PCB*p1,*s,*r;intb;s=q;p1=ready;r=p1;b=1;while((p1!=NULL)&&b
19、){if(p1->prio>=s->prio){r=p1;p1=p1->next;}elseb=0;}if(r!=p1){r->next=s;s->next=p1;}else{s->next=p1;ready=s;}}voidcreat1(charalg){PCB*p;inti,time;charna[10];ready=NULL;finish=NULL;run=NULL;printf("输入进程号和运行时间:");for(i=1;i<=N;i++){p=(PCB*)malloc(sizeof(PCB));sca
20、nf("%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;ready=p;}}//clrscr();if(alg=='p'
21、
22、alg=='P')printf("优先数算法输出信息:");elseprintf("先来先服务算法输出信息:");printf("*****
23、************************************");prt(alg);run=ready;ready=ready->next;run->state='R';}voidcreat2(charalg){PCB*p;inti,time,round;charna[10];ready=NULL;finish=NULL;run=NULL;printf("请输入时间片:");scanf("%d",&round);printf("输入进程号和运行时间:");for(i=1;i<=N;i++){p=(
24、PCB*)malloc(sizeof(PCB));scanf("%s",na);scanf("%d",&time);strcpy(p->name,na);p->cputime=0;p->needtime=time;p->state='W';p->count=0;p->round=round;p->next=NULL;if(i==1)/*按顺序插