进程调度的优先数法和简单轮转法
2015-11-30 20:52
351 查看
#include<iostream> #include<string> #include<time.h> using namespace std; int n; class PCB { public: int pri;//进程优先数 int runtime;//进程运行CPU时间 int pieceOftime;//轮转时间片 int needOftime;//还需要时间 int Counter; string procname;//进程名 string state;//进程状态 PCB * next; }; PCB * run = NULL; PCB * ready = NULL; PCB * finish = NULL; PCB * tial = ready; void Dtime(int t); void Prinft(int a) { if(a==1) { cout<<"进程名称"<<"\t"<<"优先数"<<"\t"<<"还需要时间"<<"\t"<<"已运行时间"<<"\t"<<"状态:"<<endl; } else cout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<<"\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl; } void Prinft(int b,PCB * p) { if(b==1) { cout<<p->procname<<"\t\t"<<p->pri<<"\t"<<p->needOftime<<"\t\t"<<p->runtime<<"\t\t"<<p->state<<endl; } else cout<<p->procname<<"\t\t"<<p->runtime<<"\t\t"<<p->needOftime<<"\t\t"<<p->Counter<<"\t"<<p->pieceOftime<<"\t"<<p->state<<endl; } void display(int c) { PCB *p; if(run!=NULL) Prinft(c,run); //Dtime(2); p=ready; while(p!=NULL) { Prinft(c,p); p=p->next; } //Dtime(2); p=finish; while(p!=NULL) { Prinft(c,p); p=p->next; } } void insert(PCB *p)//插入就绪队列按Pri大小 { PCB *S1,*S2; if(ready==NULL) { p->next = NULL; ready = p; } else { S1 = ready; S2 = S1; while(S1!=NULL) { if(S1->pri >= p->pri) { S2 = S1; S1 = S1->next; } else break; } if(S2->pri >= p->pri) { S2->next = p; p->next = S1; } else { p->next = ready; ready = p; } } } bool CTProcessOfPri() { PCB * Node; cout <<"输入创建进程的数目:"<<endl; cin >>n; for(int j = 0;j < n; j++) { Node = new PCB; if(Node==NULL) return false; else { cout <<"输入进程的名称,进程需CPU时间:"<<endl; cin >>Node->procname>>Node->needOftime; Node->runtime = 0; Node->state ="就绪"; Node->pri =Node->needOftime; cout <<"进程"<<Node->procname<<"创建完毕!"<<endl; } insert(Node); } return true; } void priority(int i) { run = ready; ready = ready->next; run->state = "运行"; Prinft(i); while(run!=NULL) { run->runtime=run->runtime+1; run->needOftime=run->needOftime-1; run->pri=run->pri-1; if(run->needOftime==0) { run->state = "完成"; run->next = finish; finish = run; run=NULL; if(ready!=NULL) { run = ready; run->state = "运行"; ready = ready->next; } } else if((ready!=NULL)&&(run->pri<ready->pri)) { run->state="就绪"; insert(run); run = ready; run->state = "运行"; ready = ready->next; } display(i); } } void queue(PCB *p) { if(ready==NULL) { p->next = NULL; ready = p; tial = p; } else { tial->next = p; tial = p; p->next = NULL; } } bool CTProcessOfRuntime() { PCB * Node; int m; cout <<"输入创建进程的数目:"<<endl; cin >>n; cout <<"输入时间片:"<<endl; cin >>m; for(int j = 0;j < n; j++) { Node = new PCB; if(Node==NULL) return false; else { cout <<"输入进程的名称,进程需CPU时间:"<<endl; cin >>Node->procname>>Node->needOftime; Node->runtime = 0; Node->state ="就绪"; Node->Counter = 0; Node->pieceOftime = m; cout <<"进程"<<Node->procname<<"创建完毕!"<<endl; } queue(Node); } return true; } void Runtime(int c) { run = ready; ready = ready->next; run->state = "运行"; Prinft(c); while(run!=NULL) { run->runtime=run->runtime+1; run->needOftime=run->needOftime-1; run->Counter = run->Counter + 1; if(run->needOftime==0) { run->state = "完成"; run->next = finish; finish = run; run = NULL; if(ready!=NULL) { run = ready; ready = ready->next; } } else if(run->Counter == run->pieceOftime) { run->Counter = 0; run->state = "就绪"; queue(run); run=NULL; if(ready!=NULL) { run = ready; run->state = "运行"; ready = ready->next; } } display(c); } } int main() { int i; cout <<"菜单:"<<endl; cout <<"输入0 退出"<<endl; cout <<"输入1 优先数调度算法"<<endl; cout <<"输入2 循环时间片轮转算法"<<endl; cin >>i; switch(i) { case 1: CTProcessOfPri(); priority(i); break; case 2: CTProcessOfRuntime(); Runtime(i); break; default: break; } return 0; } void Dtime(int t) { time_t current_time; time_t start_time; time(&start_time); do { time(& current_time); }while((current_time-start_time)<t); }
相关文章推荐
- 【项目经验】——文本框限制输入字符长度
- 转载AFNetworking源码解析(一)
- 笔记十一(canvas颜色应用基础)
- my first app——用lucene搭建简单的搜索引擎
- scala并发编程第二章习题
- Leetcode题解(三)
- 关于调整互联网、电话订票起售时间的公告
- 奇异值分解的几何解释及其应用
- 微信开发数据收发原理及消息数据格式
- iOS NSURLConnection GET和POST
- 指向指针的指针(2)
- 在中间件维护的时候应该注意tns的地方 -------运维日志7
- 自己实现的一个list双链表
- SpringMVC配置及初级入门11_30
- UI第十四天:UI⾼级可视化设计
- c++命令结束进程树
- Android IOS WebRTC 音视频开发总结(五三)-- 国内IM & RTC SDK列表
- [设计模式]01.单例模式
- ————————————————1004——————
- Java基础之面向过程