
一、目的:
自行编制模拟程序,通过形象化的状态显示,深入理解进程的概念、进程之间的状态转换及其所带来的PCB内容 、组织的变化,理解进程与其PCB间的一一对应关系。
二、 内容及要求:
1)设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。
2)编写、调试程序。进程的数目、进程的状态模型(三状态、五状态、七状态或其它)以及PCB的组织形式可自行选择(本实验采用5状态)。
3)合理设计与进程PCB相对应的数据结构。PCB的内容要涵盖进程的基本信息、控制信息、资源需求及现场信息。
4)设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、组织结构的变化。
5)代码书写要规范,要适当地加入注释。
6) 认真进行预习,完成预习报告。
7) 实验完成后,要认真总结,完成实验报告。
三、程序流程图:
四、使用的数据结构及说明:
在本实验中,主要用到的数据结构是PCB的结构,其中进PCB的数据结构如下:
structPCB
{
int time;//所需时间片
char Pid;//进程ID
intPpri;//进程优先级
};
其中time是进程所需的时间片,Pid是进程的ID号,每产生一个进程ID+1,Ppri是进程优先级,数字越大优先级越高。
五、运行结果及说明:
运行结果的截图:
创建进程:
进程开始运行
每个时间片用完的进程调度
进程阻塞
进程异常终止
进程从阻塞态到就绪态
上面的运行结果是程序执行的每一步进程调度的显示,从进程的创建到进程的执行,结束,每一步进程调度都有显示。
七、程序使用说明:
1)输入字符’P’创建进程,’T’当前时间片用完,’D’当前运行进程异常结束,’W’当前运行进程阻塞,’O’阻塞进程得到足够资源,编程就绪态,’R’当前运行进程为空时,就绪态进程进入运行态。
3)程序将显示每一步进程的执行状态。
八、程序源代码及其文字说明:
#include #include #include #include using namespace std; struct pro { int time;//所需时间片 char Pid;//进程ID intPpri;//进程优先级 }; vector vector pro Run;//running进程 char p='a'; boolgreaterpro(const pro& s1,const pro s2) { return s1.Ppri > s2.Ppri; } void Occurs()//阻塞态->就绪态 { if(Ready.size()<=5) { if(!Blocked.empty()) { Ready.push_back(Blocked.front()); Blocked.erase(Blocked.begin());// sort((Ready.begin()),Ready.end(),greaterpro);//ready队列排序 } } else cout<<"ready queue is full."< void Create()//新建一个进程 { if(Ready.size()<=5) { pro process; cout<<"please input time:"; cin>>process.time; cout<<"please input priority:"; cin>>process.Ppri; process.Pid=p; Ready.push_back(process); sort((Ready.begin()),Ready.end(),greaterpro); p++; } else cout<<"ready queue is full."< void Dispatch()//运行态空时往运行态添加一个进程 { pro temp; if(Run.Pid==NULL)//running状态为空 { if(!Ready.empty()&&(*Ready.begin()).Ppri>Run.Ppri) { Run=Ready.front(); Ready.erase(Ready.begin()); } } else if(Ready.front().Ppri>Run.Ppri)//running状态不为空,而且ready队列有process优先级高于running状态process优先级 { temp=Run; Run=Ready.front(); Ready.erase(Ready.begin()); Ready.push_back(temp); sort((Ready.begin()),Ready.end(),greaterpro); } else cout<<"there is a process is runing"< void Realse()//进程结束 { if(!Ready.empty()) { Run=Ready.front(); Ready.erase(Ready.begin()); } else { Run.Pid=NULL; Run.time=-1; Run.Ppri=-1; cout<<"there is no process in the cache."< } void Timeout()//时间片用完 { if(!Run.Pid==NULL) { Run.time--; if(Run.time==0)//进程结束 { Realse(); } else//进程未结束,进入ready状态 { Ready.push_back(Run); sort((Ready.begin()),Ready.end(),greaterpro); Run=Ready.front(); Ready.erase(Ready.begin()); } } else//running状态为空 Dispatch(); } void Wait()//阻塞 { if(Run.Pid!=NULL) { Blocked.push_back(Run); Run.Pid=NULL; Run.Ppri=-1; Run.time=-1; Dispatch(); } } void Output() { vector cout<<"running:|"< for(Iter = Blocked.begin();Iter!=Blocked.end();Iter++ ) cout<<(*Iter).Pid<<" "<<(*Iter).time<<" "<<(*Iter).Ppri<<"|"; cout< for(Iter = Ready.begin();Iter!=Ready.end();Iter++ ) cout<<(*Iter).Pid<<" "<<(*Iter).time<<" "<<(*Iter).Ppri<<"|"; cout< int main() { char in=NULL; while(in!='e') { cout<<"press 'P' mean crea a process"< switch (in) { case 'T':Timeout();Output();break; case 'D':Dispatch();Output();break; case 'W':Wait();Output();break; case 'P':Create();Output();break; case 'O':Occurs();Output();break; case 'R':Realse();Output();break; break; } } return 0; }
