📄 轮转法调度.cpp
字号:
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>//时间库
//////////////////////////////////////////////////////////
long getsystemtime();//声明获取系统当前时间函数
enum status{R,E,W,U};
class pcb{
public:
char name[3];//进程名
long time;//进程执行单位数
enum status ps;//进程状态
int secondworkon;//进程的纯执行时间
long begintime;//进程生成时间
long endtime;//进程完成时间
pcb(){}
pcb(char pname[2],long ptime,enum status pstatus)
{
name[0]=pname[0];
name[1]=pname[1];
name[2]=pname[2];
time=ptime;
ps=pstatus;
secondworkon=0;
begintime=getsystemtime();
endtime=begintime;
}
};
//pcb节点
class node{
public:
pcb nodepcb;
node *next;
node(pcb& npcb){nodepcb=npcb;next=0;}
node(){}
};
//就绪队列
class readyq{
public:
node *rhead;
node *rbase;
int rlength;
readyq(){rhead=rbase=0;rlength=0;}
void routq(){//出就绪队列
rhead=rhead->next;
rlength--;
if(!rlength)rbase=rhead;
}
void rinq(node& rpcb){//入就绪队列
if(rbase==0)rbase=rhead=&rpcb;
else{
rbase->next=&rpcb;
rbase=&rpcb;
}
rpcb.next=0;
rlength++;
}
};
//等待队列
class waitq{
public:
node *whead;
node *wbase;
int wlength;
waitq(){whead=wbase=0;wlength=0;}
node& woutq(){//出等待队列
node* node00=new node;
whead->nodepcb.ps=R;
(*node00)=whead->nodepcb;
whead=whead->next;
wlength--;
if(!wlength)wbase=whead;
return (*node00);
}
void winq(node& wpcb){//入等待队列
if(wbase==0)wbase=whead=&wpcb;
else{
wbase->next=&wpcb;
wbase=&wpcb;
}
wpcb.next=0;
wlength++;
}
};
//完成队列
class finishq{
public:
node *fhead;
node *fbase;
int flength;
finishq(){fhead=fbase=0;flength=0;}
void finq(node& fpcb){//入队列
if(fbase==0)fbase=fhead=&fpcb;
else{
fbase->next=&fpcb;
fbase=&fpcb;
}
fpcb.next=0;
flength++;
}
};
//获取系统当前时间
long getsystemtime(){
char systemtime[9];
_tzset();
_strtime(systemtime);
systemtime[8]='\0';
long secondnum=((int)(systemtime[0])*10+(int)systemtime[1])*3600+((int)(systemtime[3])*10+(int)(systemtime[4]))*60+(int)(systemtime[6])*10+(int)(systemtime[7]);
return secondnum;
}
//时间片
long timer(long t){
for(long i=0;i<t;i++){
int sum=0;
sum++;
sum--;
if(i>550000000)break;
}
return(t-i);
}
//执行进程
void run(node& runnode,readyq& ready,finishq& finish){
long timea1=getsystemtime();//开始执行时间
runnode.nodepcb.ps=E;
cout<<endl<<endl<<"进程"<<ready.rhead->nodepcb.name<<"执行中..."<<endl<<endl;
ready.routq();
runnode.nodepcb.time=timer(runnode.nodepcb.time);
long timea2=getsystemtime();//执行后时间(时间片到、完成)
if(runnode.nodepcb.time){
cout<<"进程"<<runnode.nodepcb.name<<"时间片用完!"<<endl<<endl;
runnode.nodepcb.ps=R;
ready.rinq(runnode);
}
else {
cout<<"进程"<<runnode.nodepcb.name<<"完成!"<<endl<<endl;
runnode.nodepcb.endtime=getsystemtime();
runnode.nodepcb.ps=U;
finish.finq(runnode);
}
runnode.nodepcb.secondworkon=runnode.nodepcb.secondworkon+(timea2-timea1);
}
int random(int number){//产生随机数的函数
return (int)(number/(float)RAND_MAX * rand());
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/////////////主函数
void main(void){
cout<<" ★★★★★★★★操作系统之处理机调度模拟系统★★★★★★★★"<<endl<<endl;
cout<<" 班级:02103410 学号:05 姓名:张东彪"<<endl<<endl;
//1.产生随机进程数
int Np;
srand((unsigned)time(NULL));
Np=random(5)+2;
cout<<endl<<"随机产生的进程数为: "<<Np<<"个!"<<endl;
//2.生成进程的随机调入时间
//能力有限,无法完成!
//3.处理机的调度——进程调度
readyq readyqueue;
waitq waitqueue;
finishq finishqueue;
pcb pjc1("p1",1800000000,W),pjc2("p2",1400000000,W),pjc3("p3",300000000,W),
pjc4("p4",1900000000,W),pjc5("p5",800000000,W),pjc6("p6",2000000000,W),
pjc7("p7",1700000000,W);
node node1(pjc1),node2(pjc2),node3(pjc3),node4(pjc4),node5(pjc5),node6(pjc6),
node7(pjc7);
waitqueue.winq(node1);
waitqueue.winq(node2);
waitqueue.winq(node3);
waitqueue.winq(node4);
waitqueue.winq(node5);
waitqueue.winq(node6);
waitqueue.winq(node7);
for(int abc=0;abc<Np;abc++)readyqueue.rinq(waitqueue.woutq());
//////////////////////////////////////////////////////////////////////////
cout<<endl<<endl<<"系统执行的第一个进程是:"<<readyqueue.rhead->nodepcb.name<<endl;
while(readyqueue.rlength){
node& runnode=(*(readyqueue.rhead));
run(runnode,readyqueue,finishqueue);
if(readyqueue.rlength){
cout<<"将要执行进程的先后顺序为:";
for(node* an=(readyqueue.rhead);an!=0;an=an->next)cout<<an->nodepcb.name<<" ";
cout<<endl<<endl;}
}
cout<<Np<<"个进程均已执行完毕,系统退出!"<<endl;
////////////////////////////////////////////////////////////////////////////////
//计算平均周转时间和平均加权周转时间
float Ti=0,Tii=0;
for(node* bc=finishqueue.fhead;bc!=0;bc=bc->next)Ti=Ti+bc->nodepcb.endtime-bc->nodepcb.begintime;
Ti=Ti/Np;
for(bc=finishqueue.fhead;bc!=0;bc=bc->next)Tii=Tii+(bc->nodepcb.endtime-bc->nodepcb.begintime)/bc->nodepcb.secondworkon;
Tii=Tii/Np;
cout<<endl<<endl<<" 平均周转时间为:"<<Ti<<"秒;"<<endl;
cout<<endl<<" 平均加权周转时间为:"<<Tii<<"秒。"<<endl;
}//The end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -