当前位置:文档之家› 进程调度算法论文优先级调度~

进程调度算法论文优先级调度~

题目操作系统课程设计实验一:进程调度算法1.实验目的通过优先权法和轮转算法的模拟加深对进程概念和进程调度过程的理解,掌握进程状态之间的切换,同时掌握进程调度算法的实现方法和技巧。

2.实验内容1)用C语言或C++语言来实现对n个进程采用优先权算法以及轮转算法的进程调度。

2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:(1)进程标识ID,其中0为闲逛进程,用户进程标识数为1,2,3…。

(2)进程优先级Priority,闲逛进程(idle)的优先级为0,用户进程的优先级大于0,且随机产生,标识数越大,优先级越高。

(3)进程占用CPU时间CPUtime,进程每运行一次,累计值等于4.(4)进程总共需要运行时间Alltime,利用随机函数产生。

(5)进程状态,0-就绪态;1-运行态;2-阻塞态。

(6)队列指针next,用来将多个进程控制块PCB链接为队列。

3)优先数改变的原则(1)进程在就绪队列中每呆一个时间片,优先数增加1。

(2)进程每运行一个时间片,优先数减3.4)在调度前,系统中拥有的进程数PCB_number由键盘输入,经初始化后,所有的进程控制块PCB链接成就绪队列。

3.实验步骤a)画出程序流程图a)动态优先权的进程调度算法模拟流程b)轮转法进程调度算法模拟流程b)程序算法如下:#include "stdafx.h"#define NULL 0#include <stdio.h>#include <stdlib.h>#include <iostream>using namespace std;/*进程PCB结构*/struct PCB{int ID; //进程标识int priority; //优先级int CPUtime; // 进程占用CPU的时间int ALLtime; // 进程总共需要运行的时间int State; // 进程状态struct PCB *next; // 指向下一节点的指针};typedef struct PCB pcb;void init(); //产生idle进程,输入用户进程数目,//调用insert()void print(PCB *pcb); //输出进程属性信息void print_init(PCB *pcb);void insert(); //生成进程属性信息,插入进程就绪队列void run_priority(PCB *pcb); //运行进程,随机阻塞进程,产生新进程,插入就绪队列,唤醒阻塞进程void run_loop(PCB *pcb); //运行进程,随机阻塞进程,产生新进程,插入就绪队列,唤醒阻塞进程void block(PCB *pcb); //调用destroy(),将进程插入阻塞队列void wakeup_priority(); //唤醒进程,插入就绪队列void wakeup_loop(); //唤醒进程,插入就绪队列void proc_priority(); //优先权调度算法模拟void proc_loop(); //轮转法调度算法模拟void update(PCB *pcb);//更新进程信息PCB *ready_queue,*block_queue,*idleprocess; //就绪队列,阻塞队列及闲逛进程指针变量int main(int argc, char* argv[]){int i=0;while(1){cout<<("\n Please select a number (1,2,0)");cout<<("\n 1--priority");cout<<("\n 2--loop");cout<<("\n 0--exit\n");cin>>i;if(i==1){cout<<("\nThis is an example for priority processing:\n");init();::proc_priority();}else if(i==2){cout<<("\nThis is an example for round robin processing:\n");init();proc_loop();}else if(i==0)exit(1);}return 0;}//输出所有PCB的初始值,此函数用于测试程序void print_init(PCB *pcb){PCB *temp=pcb->next ;cout<<("\n ID priority CPUtime ALLtimeState\n");while(temp!=NULL){cout<<"\n"<<" "<<temp->ID<<" "<<temp->priority <<""<<temp->CPUtime <<" "<<temp->ALLtime;if(temp->State ==0)cout<<(" ready");else if(temp->State ==1)cout<<(" running");elsecout<<(" blocked");temp=temp->next ;}}//输出进程属性信息void print(PCB *pcb){PCB *temp;temp=pcb;if(pcb->ID ==0)cout<<("\n\t The idle process is running!");else{cout<<"\n"<<" "<<temp->ID<<" "<<temp->priority <<" "<<temp->CPUtime <<" "<<temp->ALLtime;if(temp->State ==0)cout<<(" ready");else if(temp->State ==1)cout<<(" running");elsecout<<(" blocked");}}void insert_queue(PCB *queue,PCB *item){//将item插入到队列中,使得插入后队列中按照优先级从高到低有序PCB *p,*q;q=queue;p=q->next ;while(p!=0 && p->priority >= item->priority ){q=p;p=p->next ;}if(p==0){//在队尾插入item->next =0;q->next =item;}else{//在q之后,p之前插入item->next =p;q->next =item;}}void pushback_queue(PCB *queue,PCB *item){//将item插入到队列尾部PCB *p,*q;q=queue;p=q->next ;while(p!=0){q=p;p=p->next ;}item->next =q->next ;q->next =item;}//对queue中的节点进行排序,按照优先级从大到小void sort_queue(PCB *&queue){PCB *temp=new PCB;temp->next =0;while(queue->next ){PCB *p;p=queue->next ;queue->next =p->next ;::insert_queue (temp,p);}queue->next =temp->next;delete temp;}//生成进程属性信息,插入进程就绪队列,显示进程信息void insert(){PCB *newp=0;static long id=0;newp=new PCB;id++;newp->ID =id;newp->State =0;newp->CPUtime =0;newp->priority =rand()%3+1;newp->ALLtime =rand()%3+1;newp->next =NULL;::pushback_queue (ready_queue,newp);//将新生成进程插入到就绪队列尾部print(newp);cout<<("\t建立: Creating-> ready\n");}//生成进程属性信息,插入进程就绪队列,显示进程信息void insert(int n){for(int i=1;i<n;i++){insert();}}//产生idle进程,输入用户进程数目,调用insert()void init(){//为每个队列设立头结点,便于插入删除操作block_queue=new PCB;block_queue->next =0;ready_queue=new PCB;ready_queue->next =0;//int i;int pcb_number=-1;idleprocess=NULL; //设置闲逛进程PCB各字段值idleprocess=(PCB*)malloc(sizeof(PCB));idleprocess->ID =0;idleprocess->State =0;idleprocess->CPUtime =0;idleprocess->priority =0;idleprocess->ALLtime =1;idleprocess->next =NULL;//闲逛进程放入就绪对列idleprocess->next =ready_queue->next ;ready_queue->next =idleprocess;//也可假定初始时系统中只有一个idle进程//输出初始时进程的个数/* while(pcb_number<0){cout<<("Input the number of the PCB to be started:");cin>>pcb_number;}cout<<("\n ID priority CPUtime ALLtime State\n");for(i=0;i<pcb_number;i++)insert();//insert(pcb_number);*/cout<<"就绪队列初始化成功!"<<endl;::print_init (ready_queue);cout<<endl;}//调用destory(),将进程插入阻塞队列void block(PCB *pcb){//将pcb插入到阻塞队列pcb->State =2; //将PCB所指进程的状态设置为阻塞pcb->CPUtime -=2; //设进程执行半个时间片单位后被阻塞// pcb->next =NULL;print(pcb);cout<<(" 变迁2:running->blocked\n");//将pcb插入到阻塞队列//插入方式参考唤醒条件//因为是随机唤醒一个进程,所以简单的把它放置在阻塞队列的头部pcb->next =block_queue->next ;block_queue->next =pcb;}void update(PCB *pcb){//就绪队列中进程的优先级均增加1PCB *temp=ready_queue->next ;while(temp && temp->next ){//就绪队列的最后一个是闲逛进程temp->priority ++;temp=temp->next ;}}void run_priority(PCB *pcb){//如果pcb是闲逛进程,则不作处理,再次放入就绪队列ready_queueif(pcb->ID ==0){::insert_queue (ready_queue,pcb);print(pcb);cout<<(" 变迁1:ready->running\n");}else{//如果不是闲逛进程,则进行如下处理pcb->State =1;//设置该进程的状态为“运行”pcb->CPUtime +=4;//累计该进程占用CPU的时间pcb->priority =pcb->priority - 3;//每运行一个时间片,其优先数减3if(pcb->priority <1)pcb->priority =1;print(pcb);cout<<(" 变迁1:ready->running\n");if(rand()%3==1)//PCB不是闲逛进程,满足条件则阻塞此进程{if(pcb->CPUtime - 2 < pcb->ALLtime )block(pcb);else{//已经执行完毕,应该销毁进程cout<<endl;cout<<"\t the process no "<<pcb->ID<<" is completed! 销毁:running->Destroy"<<endl;delete pcb;}}else{//否则看该进程是否执行完毕,如果执行完毕,则释放,否则再次放入就绪队列if(pcb->CPUtime >= pcb->ALLtime ){//释放cout<<endl;cout<<"\t the process no "<<pcb->ID<<" is completed! 销毁:running->Destroy"<<endl;delete pcb;}else{//再次放入就绪队列,按照优先级有序::insert_queue (ready_queue,pcb);}}}update(ready_queue);//更新就绪队列进程优先数if(rand()%5==1){insert(3);//创建3个新进程::sort_queue (ready_queue);}if(rand()%7==1)wakeup_priority(); //唤醒一个进程//返回当前进程是否被阻塞,1(已阻塞),0(未阻塞)}void run_loop(PCB *pcb){//如果pcb是闲逛进程,则不作处理,再次放入就绪队列ready_queueif(pcb->ID ==0){::pushback_queue (ready_queue,pcb);print(pcb);cout<<(" 变迁1:ready->running\n");}else{//如果不是闲逛进程,则进行如下处理pcb->State =1;//设置该进程的状态为“运行”pcb->CPUtime +=4;//累计该进程占用CPU的时间print(pcb);cout<<(" 变迁1:ready->running\n");if(rand()%3==1)//PCB不是闲逛进程,满足条件则阻塞此进程{if(pcb->CPUtime - 2 < pcb->ALLtime )block(pcb);else{//已经执行完毕,应该销毁进程cout<<endl;cout<<"\t the process no "<<pcb->ID<<" is completed! 销毁:running->Destroy"<<endl;delete pcb;}}else{//否则看该进程是否执行完毕,如果执行完毕,则释放,否则再次放入就绪队列if(pcb->CPUtime >= pcb->ALLtime ){//释放//释放cout<<endl;cout<<"\t the process no "<<pcb->ID<<" is completed! 销毁:running->Destroy"<<endl;delete pcb;}else{//再次放入就绪队列::pushback_queue (ready_queue,pcb);}}}if(rand()%5==1){insert(3);//创建3个新进程}if(rand()%7==1)wakeup_loop();//唤醒一个进程//返回当前进程是否被阻塞,1(已阻塞),0(未阻塞)}void wakeup_priority(){//随机唤醒一个阻塞进程//采用遍历阻塞队列的方法,每访问一个,就产生一个随机数//如果该随机数对20求余恰好是1,则当前节点被唤醒,就纳入就绪队列if(block_queue->next ==0)//此时没有被阻塞的进程,不需唤醒return;PCB *p,*q;//下面遍历阻塞队列q永远指向p的前驱while(true){q=block_queue;p=q->next ;while(p && rand()%20!=1){q=p;p=p->next ;}if(p!=0){//p就是要唤醒的进程q->next =p->next ;break;}}p->State =0;cout<<endl;::print (p);cout<<" 变迁3:blocked->ready"<<endl;::insert_queue (ready_queue,p)}void wakeup_loop(){//随机唤醒一个阻塞进程//采用遍历阻塞队列的方法,每访问一个,就产生一个随机数//如果该随机数对20求余恰好是1,则当前节点被唤醒,就纳入就绪队列if(block_queue->next ==0)//此时没有被阻塞的进程,不需唤醒return;PCB *p,*q;//下面遍历阻塞队列q永远指向p的前驱while(true){q=block_queue;p=q->next ;while(p && rand()%20!=1){q=p;p=p->next ;}if(p!=0){//p就是要唤醒的进程q->next =p->next ;break;}}p->State =0;cout<<endl;::print (p);cout<<" 变迁3:blocked->ready"<<endl;::pushback_queue (ready_queue,p);}//优先权优先调度算法void proc_priority(){::sort_queue (ready_queue);PCB *temp=0,*running=0;//running 的PCB指针int times;//block_queue=NULL;//for(times=0;times<300;times++)for(times=0;times<10;times++){//找到优先级最高的进程,并将之从队列中删除cout<<"本次调度前:"<<endl;::print_init (ready_queue);running=ready_queue->next ;//running 指向就绪队列的队首PCB ready_queue->next =running->next ;cout<<endl;cout<<"本次调度开始"<<endl;::run_priority (running);cout<<"\n本次调度结束."<<endl;}}//轮转法进程调用算法void proc_loop(){PCB *temp=0,*running=0;//running 的PCB指针int times;//block_queue=NULL;//for(times=0;times<300;times++)for(times=0;times<10;times++){//将running 从队列中删除cout<<"本次调度前:"<<endl;::print_init (ready_queue);running=ready_queue->next ;//running 指向就绪队列的队首PCBready_queue->next =running->next ;cout<<endl;cout<<"本次调度开始"<<endl;::run_loop (running);cout<<"\n本次调度结束."<<endl;}}c)写出程序并运行,运行界面如下:选择1,进行优先权调度,得到结果如下:选择2,进行时间片轮转调度,结果为:选择0,退出运行界面。

相关主题