文档详情

可抢占调度算法(在VC下编译通过).doc

发布:2018-06-21约1.53万字共35页下载文档
文本预览下载声明
? 可抢占调度算法(在VC下编译通过) #include iostream.h #include string.h #include fstream.h #include math.h #include stdio.h #include stdlib.h const int FINISH=0;//完成状态 const int RUNNING=1;//运行状态 const int READY=2;//就绪状态 const int PCBNUM=3;//进程个数 typedef struct { long pid; char pname[10]; int pstate; int pneedtime; int ptime; int priority; }pcbs;//pcb结构 class pcbnode; //队列结点 class pcbnode { public: pcbs *pcb; pcbnode *link; pcbnode(); ~pcbnode(); int run();//运行操作 int runend();//运行结束? int insertnode(pcbnode *p,pcbnode *q);//在q后插入结点p int deletenode(pcbnode *p,pcbnode *q);//删除p结点,q为p的前驱 int addnode(pcbnode *p);//增加结点 }; pcbnode::pcbnode() { pcb=0; link=0; } pcbnode::~pcbnode() { if(link) delete link; if(pcb) pcb-pstate=FINISH; } int pcbnode::run() { pcb-pstate=RUNNING; ++(pcb-ptime); pcb-priority--;//优先级降低 return 0; } int pcbnode::runend() { return (pcb-pneedtime=pcb-ptime); } int pcbnode::addnode(pcbnode *p) { pcbnode *q; q=this; p-pcb-pstate=READY; while(q-link){ q=q-link; } q-link=p; return 0; } int pcbnode::insertnode(pcbnode *p,pcbnode *q) { p-link=q-link; q-link=p; return 0; } int pcbnode::deletenode(pcbnode *p,pcbnode *q) { q-link=p-link; p-link=0; return 0; } int randInt( int seed) //随机函数:产生不大于seed的正整数 { int r; r=rand(); while(rseed||r=0 ) r=rand(); return r; } void newpcb(pcbs *pcb, int order) //随机生成进程 { char buf[10]; pcb-pid=order; strcpy(pcb-pname, proc); _itoa(order,buf,10); strcat(pcb-pname,buf); pcb-pneedtime=randInt(5);//进程需要时间 pcb-ptime=0; pcb-priority=randInt(10);//优先度 } void pprint(pcbs *pcb, int count) { //打印进程状态 ofstream ofs(result.txt, ios::out|ios::app); coutid\tname\tstat\tneed\truntime\tpriendl; ofsid\tname\tstat\tneed\truntime\tpriendl; for(int i=0;icount;i++){ coutpcb[i].pid\t pcb[i].pname\t; ofs pcb[i].pid\t pcb[i].pname\t; switch(pcb[i].pstate) { case RUNNING: coutRU; ofsRU; break; case READY: coutRE; ofsRE; break; case FINISH: coutFI; ofsFI; break; } cout\tpcb[i].pneedtime; ofs\tpcb[i].pneedtime; cout\tpcb[i].ptime; ofs\tpcb[i].ptime; cout\tpcb[i].pr
显示全部
相似文档