📄 timecycle.cpp
字号:
// TimeCycle.cpp: implementation of the TimeCycle class.
//
//////////////////////////////////////////////////////////////////////
#include "TimeCycle.h"
#include <conio.h>
#include<iomanip>
#define ESC 27
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
TimeCycle::TimeCycle()
{
w_running = -1;
}
TimeCycle::~TimeCycle()
{
}
/*****************************/
void TimeCycle::Create()
{
static int pid = 1;
Process *p=new Process(pid);
w_promap[pid]=p;
readyq.push_back(pid);
p=NULL;
++pid;
if(w_running==-1)
Dispatch();
}
void TimeCycle::Kill(int pid)//kill the process
{
if(pid>0){
INTPMAP::iterator it=w_promap.find(pid);
if(it==w_promap.end())
return;
if(w_running==pid){
w_running=-1;
Dispatch();
}
else if( it->second->state == "ready" ){
readyq.remove(pid);
}
else if(it->second->state == "blocked"){
blockedq.remove(pid);
}
delete it->second;
w_promap.erase(pid);
}
}
void TimeCycle::Block()//block the process
{
if(w_running<=0)
{
cout<<"error: No process is running!"<<endl;
return;
}
INTPMAP::iterator it=w_promap.find(w_running);
if(it!=w_promap.end()){
it->second->state="blocked";
blockedq.push_back(w_running);
w_running = -1;
Dispatch();
}
}
void TimeCycle::Wakeup()//wake up the process
{
if(blockedq.empty()){
cout<<"error:No process is blocked!"<<endl;
return;
}
int pid=blockedq.front();
blockedq.pop_front();
INTPMAP::iterator it=w_promap.find(pid);
if(it!=w_promap.end()){
it->second->state="ready";
readyq.push_back(pid);
if(w_running==-1)
Dispatch();
}
}
void TimeCycle::Suspended()//suspend the process
{
}
void TimeCycle::Active() //active process
{
}
void TimeCycle::TimeOut()
{
if(w_running==-1)
return;
if(w_promap.empty()){
cout<<"error:No process!"<<endl;
return;
}
INTPMAP::iterator it;
it=w_promap.find(w_running);
if(it!=w_promap.end()){
it->second->used_time += 5;
it->second->need_time -= 5;
if(it->second->need_time <=0 ){
Kill(w_running);
}
else{
it->second->state="ready";
readyq.push_back(w_running);
w_running=-1;
Dispatch();
}
}
it=NULL;
}
void TimeCycle::Dispatch()
{
if(readyq.empty()){
cout<<"error:No process is readying!"<<endl;
return;
}
if(w_running==-1){
INTPMAP::iterator it;
int pid=readyq.front();
readyq.pop_front();
it=w_promap.find(pid);
if(it!=w_promap.end()){
it->second->state="running";
w_running=pid;
}
it=NULL;
}
}
void TimeCycle::SysManualRun()
{
int pid;
char key;
while(1){
cout<<"\n\n\nInput key('h' or 'H' to show help command)\n——>";
key=getch();
switch(key){
case 'c' :
case 'C' :
cout<<"create a process"<<endl;
Create();
break;
case 'k' :
case 'K' :
cout<<"kill a process \nPlease input a process pid:"<<endl;
cin>>pid;
Kill(pid);
break;
case 'b' :
case 'B' :
cout<<"block the running process"<<endl;
Block();
break;
case 'w' :
case 'W' :
cout<<"wakeup a process"<<endl;
Wakeup();
break;
case 'h' :
case 'H' :
Help();
break;
case ESC : exit(1);
default :
TimeOut();
break;
}
ShowInfo();
}//end_while
}
void TimeCycle::ShowInfo()
{
INTPMAP::iterator it = NULL;
cout<<"|****************** show the process infomation ****************"<<endl;
cout<<setiosflags(ios_base::left)
<<setw(3)<<"Pid"
<<setw(15)<<"name"
<<setw(15)<<"used time"
<<setw(15)<<"need time"
<<setw(15)<<"state"
<<endl;
//the running process
cout<<"the running process"<<endl;
if(w_running==-1)
cout<<"No process is running!"<<endl;
else{
it = w_promap.find(w_running);
if(it!=w_promap.end()){
cout<<setiosflags(ios_base::left)
<<setw(3)<<w_running
<<setw(15)<<it->second->name
<<setw(15)<<it->second->used_time
<<setw(15)<<it->second->need_time
<<setw(15)<<it->second->state
<<endl;
}
}
list<int>::iterator pos;
//the ready process
cout<<"the front of ready process"<<endl;
if(!readyq.empty()){
pos = readyq.begin();
while(pos != readyq.end())
{
it=w_promap.find(*pos);
if(it!=w_promap.end()){
cout<<setiosflags(ios_base::left)
<<setw(3)<<it->first
<<setw(15)<<it->second->name
<<setw(15)<<it->second->used_time
<<setw(15)<<it->second->need_time
<<setw(15)<<it->second->state
<<endl;
}
++pos;
}
}
else{
cout<<"No process is ready!"<<endl;
}
//the blocked process
cout<<"the front of blocked process"<<endl;
if(!blockedq.empty()){
pos=blockedq.begin();
while(pos!=blockedq.end()){
it=w_promap.find(*pos);
if(it!=w_promap.end()){
cout<<setiosflags(ios_base::left)
<<setw(3)<<it->first
<<setw(15)<<it->second->name
<<setw(15)<<it->second->used_time
<<setw(15)<<it->second->need_time
<<setw(15)<<it->second->state
<<endl;
}
++pos;
}
}
else{
cout<<"No process is blocked!"<<endl;
}
}
void TimeCycle::Help()
{
cout<<" C——————Create a process\n"
<<" B——————Block a process\n"
<<" W——————Wakeup process\n\n"
<<" H——————help\n"
<<" ESC—————Out!\n"
<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -