用c++STL实现进程管理

项目要求:

设计一个允许n个进程并发运行的进程管理模拟系统。该系统包括有简单的进程控制,其进程调度采用时间片轮转算法。每个进程用一个PCB表示,其内容根据具体情况设置。各进程之间有一定的同步关系(可选)。系统在运行过程中应能显示或打印各进程的状态及有关参数的变化情况,以便观察诸进程的运行过程及系统的管理过程。

 

时间片轮转法(Round-Robin,RR)主要用于分时系统中的进程调度。为了实现轮转调度,系统把所有就绪进程按先入先出的原则排成一个队列。新来的进程加到就绪队列末尾。每当执行进程调度时,进程调度程序总是选出就绪队列的队首进程,让它在CPU上运行一个时间片的时间。时间片是一个小的时间单位,通常为10~100ms数量级。当进程用完分给它的时间片后,系统的计时器发出时钟中断,调度程序便停止该进程的运行,把它放入就绪队列的末尾;然后,把CPU分给就绪队列的队首进程,同样也让它运行一个时间片,如此往复。

 

我们使用queue和priority_queue分别模拟就绪队列run和阻塞队列wait,每次CPU处理的即为就绪队列run的队头。

 

pcb进程块包含name-进程名;runtime-进程剩余时间;runedtime-占用CPU的总时间;killtime-单次占用CPU的时间;waitpoint-每次减时间片,当不大于0时将进程加入阻塞队列;waittime-等待时长;

 

项目代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 struct PCB{
  5     int name;
  6     int runtime;
  7     int runedtime;
  8     int state;
  9     int killtime;
 10     int waitpoint;
 11     int waittime;
 12 }; 
 13 
 14 struct cmp{
 15     bool operator()(PCB a,PCB b){
 16         return a.waittime>b.waittime;
 17     }
 18 };
 19 
 20 int n=3,timeslice=5;
 21 
 22 queue<PCB> run;
 23 
 24 priority_queue<PCB,vector<PCB>,cmp> wait;
 25 
 26 void Init(){
 27     srand((unsigned)time(NULL));
 28     for(int i=0;i<n;i++){
 29         
 30         PCB temp;
 31         temp.name=i;
 32         temp.runtime=rand()%20+1;
 33         temp.runedtime=0;
 34         temp.killtime=0;
 35         temp.waitpoint=rand()%20+1;
 36         temp.waittime=rand()%20+1;
 37         //cout<<temp.runtime<<" "<<temp.waitpoint<<" "<<temp.waittime<<endl;
 38         run.push(temp);
 39         cout<<"name "<<temp.name<<","<<"runtime="<<temp.runtime<<","<<"runedtime="
 40                     <<temp.runedtime<<","<<"killtime="<<temp.killtime<<","<<"waitpoint="<<
 41                     temp.waitpoint<<","<<"waittime="<<temp.waittime<<endl;
 42     }
 43     cout<<"以上为各进程初始参数"<<endl; 
 44     cout<<endl;
 45 }
 46 
 47 void Run(){
 48     while(run.size()>0||wait.size()>0){
 49         if(run.size()){
 50             PCB temp=run.front();
 51             run.pop();
 52             temp.waitpoint-=timeslice;
 53             if(temp.waitpoint>0){//不阻塞 
 54                 temp.runtime-=timeslice;
 55                 if(temp.runtime<=0){
 56                     temp.killtime=temp.runtime+timeslice;
 57                     temp.runedtime=temp.runedtime+temp.killtime;
 58                     temp.runtime=0;
 59                     
 60                     cout<<"name "<<temp.name<<","<<"runtime="<<temp.runtime<<","<<"runedtime="
 61                     <<temp.runedtime<<","<<"killtime="<<temp.killtime<<","<<"waitpoint="<<
 62                     temp.waitpoint<<","<<"waittime="<<temp.waittime<<endl<<"进程"<<temp.name<<"已结束"<<endl; 
 63                 }
 64                 else{
 65                     temp.runedtime+=timeslice;
 66                     temp.killtime=timeslice;
 67                     run.push(temp);
 68                     
 69                     cout<<"name "<<temp.name<<","<<"runtime="<<temp.runtime<<","<<"runedtime="
 70                     <<temp.runedtime<<","<<"killtime="<<temp.killtime<<","<<"waitpoint="<<
 71                     temp.waitpoint<<","<<"waittime="<<temp.waittime<<endl; 
 72                 }
 73             }
 74             else{
 75                 cout<<"进程"<<temp.name<<"产生阻塞"<<endl; 
 76                 temp.waitpoint=100;
 77                 wait.push(temp);
 78             }
 79         }
 80         while(wait.size()){
 81             PCB temp;
 82             temp=wait.top();
 83             wait.pop();
 84             temp.waittime-=timeslice;
 85             if(temp.waittime>0){
 86                 wait.push(temp);
 87                 break;
 88             }
 89             else{
 90                 cout<<"进程"<<temp.name<<"即将被唤醒"<<endl; 
 91                 run.push(temp);
 92             }
 93         } 
 94         cout<<endl;
 95     }
 96     cout<<endl<<endl<<endl<<"***********END***********"<<endl;
 97     cout<<"****Designed by Kiven****"<<endl;
 98 }
 99 
100 int main(){
101     ios::sync_with_stdio(false);
102     Init();
103     Run();
104     return 0;
105 }

 

posted @ 2017-12-26 17:01  Kiven#5197  阅读(2146)  评论(0编辑  收藏  举报