📄 os.cpp
字号:
}
int getstate(){
return state;
}
};
class schedule{
public:
PCB process[10];//假定系统最多只有100个进程
int Psize; //系统当前拥有的进程数
int ep; //指向一个空进程模块
mainmemory M; //主存
equip a[3]; //三台外设
pqueque F[3]; //多级反馈队列
int count; //用以记录二次时间
int OSMEM[10]; //模拟OS占用主存
int time; //系统运行时间
public:
schedule(){
for(int i=0;i<=9;i++) //操作系统占用了10个页
OSMEM[i]=M.allocate();
//初始进程
Psize=ep=1;
int temp[10];
for(int j=0;j<=9;j++)
temp[j]=M.allocate();
process[0].refresh(ep,temp,10,100);
F[0].enqueque(&process[0]);//放入调度队列中
count=1;
//初始化外设
for(j=0;j<=2;j++)
a[j].equip1(j);
time=0;
}
bool step(){
PCB* temp=new PCB();
if(F[0].getlength()!=0)
{F[0].dequeque(temp);
execute(temp);
back(temp,1);
return true;
}
else if(F[1].getlength()!=0)
{ F[1].dequeque(temp);
execute(temp);
back(temp,2);
return true;
}
else if(F[2].getlength()!=0)
{ F[2].dequeque(temp);
execute(temp);
back(temp,3);
return true;
}
else{
cout<<"系统中已经无用户进程"<<endl;
return false;
}
}
bool execute(PCB* P){
P->state=3; //running
instruction IR=P->A[P->PC]; //取指令
P->PC++;
int t=IR.getopcode();//指令译码
switch(t){
case 0:
{Psize++;
ep++;
int temp[10];
for(int i=0;i<=9;i++)
temp[i]=M.allocate();
process[ep-1].refresh(ep,temp,10,IR.getoprand());
F[0].enqueque(&process[ep-1]);
return true;}
case 1:
{ P->page(IR.getoprand());
return true;}
case 2:
{int t=IR.getoprand();
int test=a[t].request1(P);//对设备
if(test==1)
{P->own[P->ownp++]=t;
P->ownsize++;
}
if(test==0){
P->request=t;
P->state=2;}
if(test==-1)
cout<<"申请失败"<<endl;
return true;
}
case 3:
{int t=IR.getoprand();
PCB* temp=new PCB();
if(a[t].own->ID==P->ID && a[t].getstate()==1)
{
a[t].deallocate(temp);//对设备
if(temp->state==1)
F[0].enqueque(temp);//插入的两句
int j=0;
while(P->own[j]!=t)
j++;
P->own[j]=-1;
P->ownsize--;
return true;
}
else
{cout<<"错误:进程请求释放一个并没有占有的设备"<<endl;
return false;
}
}
case 4:
{PCB* t=new PCB();
int temp[100],size;
P->free1(temp,size);//释放内存空间
for(int i=0;i<=size-1;i++)
M.deallocate(temp[i]);
for(int j=0;j<=2;j++)
if(a[j].own->ID==P->ID && a[j].getstate()==1)
{a[j].deallocate(t);
if(t->state==1)
F[0].enqueque(t);
}
Psize--;
P->ID=-1;
P->state=-1;
return true;
}
}
}
bool back(PCB* P,int s){
if(P->state==3)
{ P->state=1;
switch(s){
case 1:
{F[1].enqueque(P);
return true;
}
case 2:
{ if(count==1)
{
F[1].front(P);
count--;
}
else
{
F[2].enqueque(P);
count++;
}
return true;
}
case 3:
{F[2].front(P);
return true;
}
}
}
}
pagefaultcheck(){ //控制缺页率 (0.2<=pagefault rate<=0.5)
int temp=0;
if(time%10 ==0)
for(int i=0;i<=9;i++)
if(process[i].ID!=-1)
{if(process[i].frate()<0.2)
{temp=process[i].decpt();
M.deallocate(temp);
}
if(process[i].frate()>0.5)
{temp=M.allocate();
process[i].addpt(temp);
}
}
}
menu(){
cout<<"************************************"<<endl;
cout<<"0---系统中的进程情况"<<endl;
cout<<"1---主存分布情况"<<endl;
cout<<"2---设备情况"<<endl;
cout<<"3---就绪队列"<<endl;
cout<<"4---等待队列"<<endl;
cout<<"5---下一步"<<endl;
cout<<"*************************************"<<endl;
cout<<"请输入您的选择:";
int i;
cin>>i;
switch(i)
{case 0: {for(int i=0;i<=9;i++)
if(process[i].ID!=-1)
process[i].output();
break;}
case 1: M.output();
break;
case 2: {for(int i=0;i<=2;i++)
a[i].output();
break;
}
case 3: {for(int i=0;i<=2;i++)
{cout<<"第"<<i<<"队:"<<endl;
F[i].output();}
break;
}
case 4: {for(int i=0;i<=2;i++)
{cout<<"第"<<i<<"号设备的等待队列:"<<endl;
a[i].request.output();}
break;
}
case 5:
{ time++;
for(int i=0;i<=9;i++)
if(process[i].ID!=-1)
process[i].timer();
step();
pagefaultcheck();
break;
}
}
menu();
}
};
/*main(){
equip a(1);
//a.output();
PCB a1,b,c,*p=new PCB();
pqueque t;
int temp[10],temp2[10],k=0;
for(int i=0;i<=9;i++)
temp[i]=100-i;
int size=8;
a1.refresh(5,temp,size,100);
b.refresh(6,temp,3,10);
c.refresh(7,temp,5,20);
a.request1(&a1);a.request1(&a1);
a.output();
a.output();
a.output();
a.request1(&c);a.deallocate(p);
a.output();
p->output();}
/*main(){
mainmemory M;
M.output();
for(int i=0 ;i<=10;i++)
cout<<M.allocate()<<endl;
M.output();
M.deallocate(2);
M.deallocate(9);
M.output();
}*/
/*
int main(int argc, char* argv[])
{PCB a,b,c,*p=new PCB();
pqueque t;
int temp[10],temp2[10],k=0;
for(int i=0;i<=9;i++)
temp[i]=100-i;
int size=8;
a.refresh(5,temp,size,100);
b.refresh(6,temp,3,10);
c.refresh(7,temp,5,20);
t.front(&c);
t.enqueque(&a);
t.enqueque(&b);
t.output();
t.dequeque(p);t.output();
p->output();
}
/* a.timer();
a.output();
a.free1(temp2,k);
a.page(10);
a.output();
cout<<k<<endl;
for( i=0;i<=k-1;i++)
cout<<temp2[i]<<endl;
/*instruction a;
//pagetable b(temp,size);
//b.output();
//b.page(5);
//b.output();
//b.free1(temp2,k);
// cout<<b.getsize();
// a.output();
/* PCB t;
t.output();
mainmemory aw;
aw.output();
cout<<aw.allocate();
aw.output();
aw.deallocate(0);
aw.output();
return 0;
}*/
main(){
schedule a;
a.process[0].A[0].setopcode(0);
a.process[0].A[0].setoprand(2);
a.process[0].A[1].setopcode(0);
a.process[0].A[1].setoprand(2);
/*while(true)
{cout<<"................................"<<endl;
a.step();
for(int i=0;i<=9;i++)
if(a.process[i].ID!=-1)
a.process[i].output();
getchar();
}*/
a.menu();
}
/* a.process[0].output();
a.a[1].output();
a.a[2].output();
a.F[1].enqueque(&a.process[0]);
a.M.output();
//a.process[0].output();getlength();
//PCB* temp=new PCB();
// if(a.F[0].getlength()!=0)
// a.F[0].dequeque(temp);
//a.F[0].output();//F[0].getlength();
//temp->output();
a.step();
a.step();
a.M.output();
a.a[1].output();
a.a[2].output();
// a.execute(temp);
// back(temp,1);
// return true;
// }*/
/* for(int i=0;i<=9;i++)
if(a.process[i].ID!=-1)
a.process[i].output();
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -