您好,欢迎访问三七文档
进程调度模拟程序:假设有10个进程需要在CPU上执行,分别用:2先进先出调度算法;2基于优先数的调度算法;2最短执行时间调度算法确定这10个进程在CPU上的执行过程。要求每次进程调度时在屏幕上显示:当前执行进程;就绪队列;等待队列要求:1)创建10个进程的PCB,每个PCB包括:进程名、进程状态、优先级(1~5)、需要在处理机上执行的时间(ms)、队列指针等;2)初始化10个PCB(产生随机数0或1,分别表示进程处于就绪态或等待态);3)根据调度算法选择一个就绪进程在CPU上执行;4)在进程执行过程中,产生随机数0或1,该随机数为1时,将等待队列中的第一个PCB加入就绪队列的对尾;5)产生一个随机数,表示执行进程能在处理机上执行的时间,该执行时间结束后根据调度算法进行进程调度;或者一个进程在CPU上执行完后需要根据调度算法进行进程调度;6)在进程调度时产生随机数0或1,当该随机数为0时,将执行进程加入就绪队列对尾;否则,将执行进程加入等待队列对尾;一直到就绪队列和等待队列为空,程序执行结束。给个参考的程序:#includestdio.h#includestdlib.h#includetime.h#definenull0FILE*f;intflag=1;/*保存现场部分*/typedefstructsce{intmidresult;inttimes;}scen;/*进程控制块*/typedefstructpcb_type{charname[20];intpid;int(*fp)();scenscene;intpriority;intstatus;/*4为未结束,5为结束*/structpcb_type*next;}pcb;/*按优先级入队列*/pcb*inqueue(pcb*head,pcb*apcb){pcb*p=head;while(p-next!=null){if(p-next-priorityapcb-priority)break;p=p-next;}/*while*/if(p-next==null)p-next=apcb;else{apcb-next=p-next;p-next=apcb;}returnhead;}/*inqueue*//*出队列*/pcb*outqueue(pcb*head){pcb*p=head-next;head-next=p-next;p-next=null;returnp;}/*outqueue*//*判断队列是否为空*/intcheckqueue(pcb*head){if(head-next==null)return0;elsereturn1;}floatRandom(){srand(time(0));returnrandom(1000)/1000;}/*进程一*/intprocess1(int*midresult,int*times){intallTheTimes=3;inttemp=*midresult;intn=0;inti=0;for(;iallTheTimes-(*times);i++){temp++;n++;if(Random()0.33)/*模拟时间片*/{*midresult=temp;*times=*times+n;return4;}}*midresult=temp;return5;}/*进程二*/intprocess2(int*midresult,int*times){intallTheTimes=4;inttemp=*midresult;intn=0;inti=0;for(;iallTheTimes-(*times);i++){temp++;n++;if(Random()0.33||Random()0.66){*midresult=temp;*times=*times+n;return4;}}*midresult=temp;return5;}/*进程三*/intprocess3(int*midresult,int*times){intallTheTimes=5;inttemp=*midresult;intn=0;inti=0;for(;iallTheTimes-(*times);i++){temp++;n++;if(Random()0.66){*midresult=temp;*times=*times+n;return4;}}*midresult=temp;return5;}/*调度器*/voidschedule(pcb*head1,pcb*head2){pcb*p,*r;pcb*temp_head;/*检验就绪队列是否为空*/if(!checkqueue(head1)){/*空则交换两个优先级队列*/temp_head=head1;head1=head2;head2=temp_head;/*如果仍为空,则退出*/if(!checkqueue(head1)){fprintf(f,Allprocessesarecomplete!);flag=0;return;}}/*以下为对队列的现状的显示*/r=head1;fprintf(f,\nhead1);while(r-next!=null){r=r-next;fprintf(f,[%s]\t,r-name);}fprintf(f,null\n);r=head2;fprintf(f,head2);while(r-next!=null){r=r-next;fprintf(f,[%s]\t,r-name);}fprintf(f,null\n);/*以上为对队列的现状的显示*//*不为空,从高优先级队列队头取一个PCB*/p=outqueue(head1);/*检查进程的状态,恢复现场*/if(p-status!=5){fprintf(f,%s,p-name);fprintf(f,isusingCPU\t);p-status=p-fp(&(p-scene.midresult),&(p-scene.times));}/*进程结束,打印结果*/if(p-status==5){fprintf(f,%s,p-name);fprintf(f,iscomplete,theresultis);fprintf(f,%d\n,p-scene.midresult);/*释放进程控制块*/free(p);/*结束本次调度*/return;}/*进程未结束,入队列*/fprintf(f,\n%s,p-name);fprintf(f,isinqueue2);inqueue(head2,p);/*以下为对队列的现状的显示*/r=head1;fprintf(f,\nhead1);while(r-next!=null){r=r-next;fprintf(f,[%s]\t,r-name);}fprintf(f,null\n);r=head2;fprintf(f,head2);while(r-next!=null){r=r-next;fprintf(f,[%s]\t,r-name);}fprintf(f,null\n);/*以上为对队列的现状的显示*/}/*schedule*//*初始化进程控制块*/voidinit_process(pcb*apcb){apcb-scene.midresult=0;apcb-scene.times=0;/*Setname*/printf(Pleasegiveanamefortheprocess:);fflush(stdin);gets(apcb-name);/*Setpriority*/printf(Pleasegive);printf(%s,apcb-name);printf(apriority:);fflush(stdin);scanf(%d,&apcb-priority);/*Setstatus*/apcb-status=4;apcb-next=null;}/*init_process*//*主函数*/voidmain(){pcb*r;pcb*head1=(pcb*)malloc(sizeof(pcb));pcb*head2=(pcb*)malloc(sizeof(pcb));pcb*p1=(pcb*)malloc(sizeof(pcb));pcb*p2=(pcb*)malloc(sizeof(pcb));pcb*p3=(pcb*)malloc(sizeof(pcb));f=fopen(a.txt,w);head1-next=null;head2-next=null;p1-fp=process1;p2-fp=process2;p3-fp=process3;init_process(p1);init_process(p2);init_process(p3);head1=inqueue(head1,p1);head1=inqueue(head1,p2);head1=inqueue(head1,p3);while(flag){/*以下为对队列的现状的显示(在schedule函数外)*/fprintf(f,/*outside*/);r=head1;fprintf(f,\nhead1);while(r-next!=null){r=r-next;fprintf(f,[%s]\t,r-name);}fprintf(f,null\n);r=head2;fprintf(f,head2);while(r-next!=null){r=r-next;fprintf(f,[%s]\t,r-name);}fprintf(f,null\n);fprintf(f,/*eof*/);/*以上为对队列的现状的显示(在schedule函数外)*/schedule(head1,head2);/*调度*/}fclose(f);getch();}/*main*/
本文标题:进程调度算法
链接地址:https://www.777doc.com/doc-5459679 .html