www.pudn.com > os.rar > Priority.cpp
#include#include"QueueNode.h" #include "LinkQueue.h" using namespace std; QueueNode *FindHighprocess(LinkQueue& Q,PCBNode * ProcessTable,int& currenttime); void RunPriority(int& currenttime,LinkQueue& Q,PCBNode * ProcessTable,QueueNode *highprocess); void FindReadyQueue(int currenttime,LinkQueue& Q,PCBNode * ProcessTable); bool Isallfinish(LinkQueue Q,PCBNode * ProcessTable);//判断是否所有进程都finish void ShowCurQueue(int currenttime,LinkQueue Q,PCBNode * ProcessTable); void dynamicPriority(LinkQueue& Q,PCBNode * ProcessTable) //优先级数越高,优先级越高 { int currenttime=0; cout<<"当前时间: "< GetnextQNode();//找到当前就绪队列中优先级最高的 highprocess=FindHighprocess(Q,ProcessTable,currenttime); bool allfinish=Isallfinish(Q,ProcessTable); //运行 while(allfinish==false) { if(highprocess!=NULL) { RunPriority(currenttime,Q,ProcessTable,highprocess); FindReadyQueue(currenttime,Q,ProcessTable);//在此却认就绪队列 if(ProcessTable[highprocess->GetID()].GetremainTime()==0)//该进程完成 { ProcessTable[highprocess->GetID()].Setstatus(FINISH);//进程完成标志 cout< GetID()<<"完成!"< GetnextQNode(); QueueNode *p=Q.head->GetnextQNode(); while(p!=NULL) //找到当前就绪队列中优先级最高的(优先级最高,没有完成的,已到达的) { if(ProcessTable[highprocess->GetID()].GetpriorityNum() <= ProcessTable[p->GetID()].GetpriorityNum() && ProcessTable[p->GetID()].Getstatus()!=FINISH && p->Getisarrive()==true) { highprocess=p; flag=true;//在就绪队列中找到了highprocess } p=p->GetnextQNode(); } if(flag==true && highprocess!=NULL) { cout< GetID()<<" 其优先级为: "< GetID()].GetpriorityNum()< GetID()<<"正在运行"< GetID()].SetremainTime( ProcessTable[highprocess->GetID()].GetremainTime()-1 ); //该进程剩下需要的运行时间-1 int prioritynum=ProcessTable[highprocess->GetID()].GetpriorityNum();//运行了的进程优先级降低1级 ProcessTable[highprocess->GetID()].SetpriorityNum(prioritynum-1); QueueNode *other=Q.head->GetnextQNode(); //其他等待进程优先级加1级 while(other!=NULL ) { if(other!=highprocess && other->Getisarrive()==true && ProcessTable[other->GetID()].Getstatus()!=FINISH) { if(ProcessTable[other->GetID()].GetarriveTime()!=currenttime) ProcessTable[other->GetID()].SetpriorityNum( ProcessTable[other->GetID()].GetpriorityNum()+1 ); } other=other->GetnextQNode(); } cout< GetnextQNode(); while(other!=NULL) { if(other->Getisarrive()==true && ProcessTable[other->GetID()].Getstatus()!=FINISH) cout<<"进程"< GetID()<<"的优先级: "< GetID()].GetpriorityNum()< GetnextQNode(); } } void FindReadyQueue(int currenttime,LinkQueue& Q,PCBNode * ProcessTable)//哪些进程已经到达,就绪 { QueueNode *p=Q.head->GetnextQNode(); while(p!=NULL) { if(currenttime >= ProcessTable[p->GetID()].GetarriveTime()) { if(p->Getisarrive()==false) { cout<<"当前时间"< GetID()<<"进入就绪队列!"< Setisarrive(true); ProcessTable[p->GetID()].SetarriveTime(currenttime);//设置到达时间 ShowCurQueue(currenttime,Q,ProcessTable); } p->Setisarrive(true); } p=p->GetnextQNode(); } } bool Isallfinish(LinkQueue Q,PCBNode * ProcessTable)//判断是否所有进程都finish { bool flag=true; QueueNode *node=Q.head->GetnextQNode(); while(node!=NULL) { if(ProcessTable[node->GetID()].Getstatus()!=FINISH) { flag=false;//没有全部完成 } node=node->GetnextQNode(); } return flag;//全部完成 } void ShowCurQueue(int currenttime,LinkQueue Q,PCBNode * ProcessTable) { cout<<"当前就绪队列中进程号: "; //输出就绪队列 QueueNode *p=Q.head->GetnextQNode(); while(p!=NULL) { if(ProcessTable[p->GetID()].Getstatus()!=FINISH && p->Getisarrive()==true) cout< GetID()<<" "; p=p->GetnextQNode(); } }