2022年2022年进程调度算法代码 .pdf
进程调度算法代码/ process.cpp : Defines the entry point for the console application. / #include stdafx.h #include stdio.h #include stdlib.h #include iostream #define NULL 0 #define false 0 #define true 1 bool _state=0; struct PCB int ID; int priority; int CPUtime; int ALLtime; int State; PCB* next; ; void init();/*产生 idle进程,输入用户进程数目,调用insert()*/ void print(PCB *pcb);/*输出进程属性信息*/ void print_init(PCB *pcb);/*输出所有PCB的初始值 */ void insert();/*生成进程属性信息,插入进程就绪队列*/ void Run_priority(PCB *pcb);/*运行进程,随机阻塞进程、产生新进程,插入就绪队列,唤醒阻塞进程 */ void block(PCB *pcb);/*调用 destroy()将进程插入阻塞队列*/ void wakeup();/*唤醒进程,插入就绪队列*/ void proc_priority();/*优先权调度算法模拟*/ /void Run_loop(PCB *pcb); void proc_loop();/*轮转法调度算法模拟*/ void update(PCB *pcb);/*更新进程信息*/ void pushback_queue(PCB *queue,PCB *item);/*将 item 插入到队列的尾部*/ void insert_queue(PCB *queue,PCB *item);/*将 item 插入到队列中,使得插入后,队列中按照优先级从高到低有序*/ void sort_queue(PCB *&queue);/*对 queue 中的结点进行排序,按照优先级从大到小*/ PCB *ready_queue,*block_queue,*idleprocess;/*就绪队列,阻塞队列及闲逛进程指针变量*/ int main(int argc, char* argv) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - int i=0; while(1) cout*PROCESS*/; cout(n Please select a num in(1,2,0) ); cout(n 1-priority ); cout(n 2-loop ); cout(n 0- exitn); couti; while(i) if(i=1) cout(n This is a example for priority processing: n ); init(); proc_priority(); else if (i=2) cout(n This is a example for round robin processing: n ); init(); proc_loop(); else coutPlease select a num in(1,2,0)n; couti; return 0; /输出所有PCB的初始值void print_init(PCB *pcb) PCB* temp=pcb-next ; cout(nID priority CPUtime ALLtime State); while(temp!=NULL) coutnID priority 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - CPUtime ALLtime; if(temp-State=0) coutState =1) cout( running); else coutnext; /输出进程属性信息void print(PCB *pcb) PCB *temp; temp=pcb; if(pcb-ID=0) cout(nThe idle peocess id running!); else coutnID priority CPUtime ALLtime; if(temp-State=0) coutState =1) cout( running); else coutnext; while(p!=0&p-priority=item-priority) q=p; p=p-next; if(p=0) item-next =0; q-next=item; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - else item-next =p; q-next =item; /将 item 插入到阻塞队列的尾部void pushback_queue(PCB *queue,PCB *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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 10 页 - - - - - - - - - newp-priority=rand()%3+1; newp-ALLtime=rand()%3+1; newp-next =NULL; pushback_queue(ready_queue,newp); /print(newp); /cout readyn); /生成 n 个进程属性信息,插入进程就绪队列,显示进程信息void insert(int n) for(int i=0;inext=0; ready_queue=new PCB; ready_queue-next=0; int i=0,pcb_number=-1;/闲逛进程放入就绪队列idleprocess=NULL; idleprocess=(PCB *)malloc(sizeof(PCB); idleprocess-ID=0; idleprocess-State=0; idleprocess-CPUtime=0; idleprocess-priority=0; idleprocess-ALLtime=0; idleprocess-next=NULL; idleprocess-next=ready_queue-next;/闲逛进程放入就绪队列ready_queue-next=idleprocess; / 也可以假定初始时系统中只有一个idle进程/ 输入初始进程的个数 while(pcb_number0) coutpcb_number; cout(nID priority CPUtime ALLtime Staten); for(;ipcb_number;i+) insert(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - cout* 就绪队列初始化成功endl; :print_init(ready_queue); coutState=2; pcb-CPUtime-=2; if(pcb-CPUtimeCPUtime+=2; coutnThe process noID is blocked!; print(pcb); cout blockedn); pcb-next=block_queue-next; block_queue-next =pcb; /更新进程信息, 就绪队列中进程的优先级均增加1 void update(PCB *pcb) PCB *temp=pcb-next; while(temp&temp-next ) temp-priority+; temp=temp-next; /唤醒进程,插入就绪队列void wakeup() if(block_queue-next=0)/*此时没有阻塞的进程,无所谓的唤醒*/ return ; PCB *q,*p; while(true) q=block_queue; p=q-next; while(p&rand()%20!=1) q=p; p=p-next; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 10 页 - - - - - - - - - if(p!=0) q-next =p-next ; break; p-State=0; coutendl; print(p); cout readyID=0) insert_queue(ready_queue,pcb); print(pcb); cout runningn; else pcb-State=1; pcb-CPUtime+=4; pcb-priority=pcb-priority -3;/*每运行一个时间片,其优先数减3*/ if(pcb-priority priority=1; print(pcb); printf(变迁 1: ready - runningn); if(rand()%3=1)/*PCB不是闲逛进程,满足条件侧阻塞此进程*/ if(pcb-CPUtime-2ALLtime) block(pcb); else/*已执行完毕,应该销毁进程*/ coutn; coutThe process noIDDestroyCPUtime=pcb-ALLtime)/*释放 */ coutn; coutThe process no ID DestroryID=0) insert_queue(ready_queue,pcb); print(pcb); cout runningn; else pcb-State=1; pcb-CPUtime=pcb-ALLtime; print(pcb); printf(变迁 1: ready - runningn); if(rand()%3=1)/*PCB不是闲逛进程,满足条件侧阻塞此进程*/ _state=1; block(pcb); else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 10 页 - - - - - - - - - coutn; coutThe process no ID Destroryendl; delete pcb; if(rand()%5=1) insert(3); if(rand()%7=1) wakeup(); /优先权调度算法模拟void proc_priority() sort_queue(ready_queue); PCB *temp=0,*running=0; int times=0; cout* 调度前 :; :print_init(ready_queue); for(;timesnext; ready_queue-next =running-next ; coutendl; cout* 调度开始 endl; Run_priority(running); coutn*本次调度结束。endl; /轮转调度算法模拟void proc_loop() PCB *temp=0,*running=0; int times=10; coutnext; ready_queue-next =running-next ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 10 页 - - - - - - - - - cout0) times=times-running-ALLtime;/每次运行一个进程减去ALLtime; if(times=0) Run_loop(running); else if(_state) / 如果运行时被阻塞,则运行2 个时间单位 times=times+2; _state=0; cout5487584574389574 fgfgfgfgf gfgfg38954378954375894378954375; else pushback_queue(block_queue,running);/时间不过, 则阻塞该进程, 放到阻塞队列 else if(times=0) coutn*本次调度时间片到!; break; coutn*本次调度结束。endl; 本程序在visual stidio 2008 professio名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 10 页 - - - - - - - - -