进程代码1.docx
《进程代码1.docx》由会员分享,可在线阅读,更多相关《进程代码1.docx(10页珍藏版)》请在冰豆网上搜索。
![进程代码1.docx](https://file1.bdocx.com/fileroot1/2022-11/17/873d5b71-d6c4-45cf-b75e-8771c50470c5/873d5b71-d6c4-45cf-b75e-8771c50470c51.gif)
进程代码1
#include
#include
#include
#include
#include
constint MAXCOMMANDLEN=50;
/////////////////////////////////////////////////////////////////////////////////////
//
// PROCESS
//
/////////////////////////////////////////////////////////////////////////////////////
classProcess //进程类
{
friendclassCPU;
protected:
staticintinit_ID; //随机进程ID
intID; //进程ID
charrunText[MAXCOMMANDLEN]; //进程指令数组
intIP; //进程指令指针,保存进程指令执行到的具体位置
boolISuseSource; //此进程是否使用资源,ture:
使用中 false:
未使用
boolISblocked; //此进程是否被阻塞 ture:
阻塞 false:
未阻塞
intunitTime; //进程单位被cpu执行时间,默认1
intblockTime; //进程被阻塞时间
public:
staticvoidRandID(); //随机生成进程ID
Process();
intgetID();
intgetIP();
voidsetIP(int);
voidRuned(); //进程被cpu执行
intgetUnittime(); //得到进程单位执行时间
intgetBlcoktime(); //得到进程阻塞时间
voidsetBlocktime(int); //设置进程阻塞时间
voidsetUnittime(int); //设置进程单位执行时间
chargetResult(int); //得到进程执行结果
char*getRuntext(); //得到进程执行的指令
voidsetBlockstate(bool); //设置阻塞状态
boolgetBlockstate();
boolgetISusesource(); //得到资源的状态 使用 未使用
voidsetISusesource(bool); //设置资源的使用状态
};
intProcess:
:
init_ID;
voidProcess:
:
RandID()
{
srand((unsigned)time(NULL));
init_ID=rand();
}
Process:
:
Process()
{
ID=init_ID++;
intcommandLen;
IP=0;
cout<<"PleaseinputthetextwhichprocessrunedbyCPU[#command#]:
>\\";
cin>>runText;
if((commandLen=strlen(runText))>MAXCOMMANDLEN)
exit(0);
runText[commandLen]='#'; //指令结束标志'#'
runText[commandLen+1]='\0';
ISuseSource=false;
ISblocked=false;
unitTime=1;
blockTime=0;
}
voidProcess:
:
Runed()
{
cout<}
intProcess:
:
getID()
{
returnID;
}
intProcess:
:
getIP()
{
returnIP;
}
voidProcess:
:
setIP(intip)
{
IP=ip;
}
boolProcess:
:
getISusesource()
{
returnISuseSource;
}
voidProcess:
:
setISusesource(bools)
{
ISuseSource=s;
}
char*Process:
:
getRuntext()
{
returnrunText;
}
intProcess:
:
getUnittime()
{
returnunitTime;
}
intProcess:
:
getBlcoktime()
{
returnblockTime;
}
voidProcess:
:
setBlocktime(intBT)
{
blockTime=BT;
}
voidProcess:
:
setUnittime(intUT)
{
unitTime=UT;
}
voidProcess:
:
setBlockstate(boolstate)
{
ISblocked=state;
}
boolProcess:
:
getBlockstate()
{
returnISblocked;
}
charProcess:
:
getResult(intk)
{
returnrunText[k];
}
/////////////////////////////////////////////////////////////////////////////////////
//
// SOURCE
//
/////////////////////////////////////////////////////////////////////////////////////
classSource //资源类
{
protected:
intID; //资源ID
boolstate; //资源状态true:
未被进程占有 false:
已被占有
intpro_ID; //使用资源的进程id
Process*pro; //使用资源的进程指针
inttime; //进程使用资源的时间
public:
Source(int);
boolgetState(); //得到进程状态
voidsetState(bool); //设置进程状态
voidsetTime(int); //设置进程使用资源的时间
voidsetPro(Process*); //设置使用该资源的进程
intgetID(); //得到资源id
intgetPorID(); //得到使用资源的进程id
voidsetProID(int); //设置使用资源的进程id
voidrunned(); //资源被cpu调用
};
Source:
:
Source(intid)
{
ID=id;
pro=NULL;
state=true;
}
voidSource:
:
setProID(intid)
{
pro_ID=id;
}
voidSource:
:
setTime(intt)
{
time=t;
}
voidSource:
:
setState(bools)
{
state=s;
}
boolSource:
:
getState()
{
returnstate;
}
voidSource:
:
setPro(Process*p)
{
pro=p;
}
voidSource:
:
runned()
{
if(time>0)
{
cout<<"(Source:
"< time--;
}
if(time<=0) //进程使用完资源释放资源,使用资源的时间到
{
pro->setISusesource(false);
intip=pro->getIP();
pro->setIP(++ip);
Source:
:
setState(true);
cout<getID()<<"relasethesource!
"< pro=NULL;
}
}
/////////////////////////////////////////////////////////////////////////////////////
//
// CPU
//
/////////////////////////////////////////////////////////////////////////////////////
typedefstructBlock //阻塞队列结构
{
Process*p_BlockProcess; //被阻塞的进程队列
intindex; //被阻塞的进程在就绪队列中的索引(位置)
}Block;
classCPU
{
protected:
Process*p_Process; //进程队列
Process**pp_Process; //进程就绪队列
Block *blockQueue ; //进程阻塞队列
Source *p_Source; //资源指针
intnumOfprocess; //进程数量
intnumOfblock; //被阻塞的进程数
intPC; //程序计数器
intallTime; //cpu运行的总时间
public:
CPU(int);
voidRun(); //cpu运行进程
bool_IC(Process&); //虚拟IC,进行进程指令翻译
voiduseSource(Process&); //进程申请资源
voidblockProcess(Process&); //阻塞进程
voidreleaseBlockPro(); //释放阻塞进程
intgetAlltime(); //得到进程运行的总时间
voiddisplayPro(); //显示进程的基本信息,id,指令,运行时间等
voidblockTimeADD(); //阻塞时间加1
};
CPU:
:
CPU(intnum)
{
p_Source=newSource(3