目录
按时间片轮转法实现处理器调度的程序
enum STATE {E,R,F};
class PCB{
public:
string name;
PCB* next;
int time;
int finatime;///越大越优先
STATE sta;
PCB(string name,int time){
this->name=name;
this->next=NULL;
this->time=time;
this->finatime=0;
this->sta=E;
}
};
class runqueue{
public:
PCB* statrPCB;
PCB* finaPCB;
int sum;
runqueue(){
this->sum=0;
}
void add(PCB target){
this->sum++;
if(this->sum==1){
statrPCB=⌖
finaPCB=⌖
}
else{
finaPCB->next=⌖
finaPCB=⌖
target.next=statrPCB;
}
}
void runclock(){
PCB* rep=statrPCB;
int finasum=0;
while(1){
if(rep->sta!=F){
rep->finatime+=1;
cout<<"进程"<<rep->name<<"拿到时间片并执行"<<",已经完成时间为"<<rep->finatime<<",总需要时间为"<<rep->time<<"\n";
if(rep->finatime==rep->time){
rep->sta=F;
finasum++;
}
}
if(finasum==this->sum) break;
rep=rep->next;
}
}
};
按优先数调度算法实现处理器调度的程序
enum STATE {E,R,F};
class PCB{
public:
string name;
PCB* next;
int time;
int pri;///瓒婂ぇ瓒婁紭鍏?
STATE sta;
PCB(string name,int time,int pri){
this->name=name;
this->next=NULL;
this->time=time;
this->pri=pri;
this->sta=E;
}
friend bool operator < (const PCB &a,const PCB &b)
{
return a.pri < b.pri;
}
};
signed main()
{
PCB A("1",1,5);
PCB B("2",2,4);
PCB C("3",6,6);
priority_queue<PCB> pq;
pq.push(A);
pq.push(B);
pq.push(C);
while(pq.size()){
auto t=pq.top();
pq.pop();
cout<<"进程"<<t.name<<"执行"<<",剩余完成时间为"<<t.time<<"\n";
t.time-=1;
t.pri-=1;
if(t.time==0){
t.sta=F;
}
else{
pq.push(t);
}
}
}
银行家算法
class banker{
public:
int max[5][5]={
{7,5,3},
{3,2,2},
{9,0,2},
{2,2,2},
{4,3,3},
};
int allo[5][5]={
{0,1,0},
{2,0,0},
{3,0,2},
{2,1,1},
{0,0,2},
};
bool chec;
int need[5][5]={0};
int avl[5]={3,3,2};
banker(){
calneed();
}
void calneed(){
for(int i=0;i<=4;++i){
for(int j=0;j<=2;++j){
need[i][j]=max[i][j]-allo[i][j];
}
}
}
void dfs(vector<int> &res,bool fl[],int nowav[]){
if(res.size()==5){
this->chec=1;
cout<<"目前系统处于安全,安全序列为:";
for(auto t:res) cout<<t<<" ";
cout<<endl;
return ;
}
for(int i=0;i<=4;++i){
if(fl[i]){
continue;
}
bool can=1;
for(int j=0;j<=2;++j){
if(nowav[j]<need[i][j]){
can=0;
break;
}
}
if(can){
res.push_back(i);
for(int j=0;j<=2;++j){
nowav[j]+=allo[i][j];
}
fl[i]=1;
dfs(res,fl,nowav);
fl[i]=0;
res.pop_back();
for(int j=0;j<=2;++j){
nowav[j]-=allo[i][j];
}
}
}
}
bool check(){
bool fl[6]={0};
int nowav[5];
for(int i=0;i<=2;++i) nowav[i]=avl[i];
vector<int> res;
chec=0;
dfs(res,fl,nowav);
if(!chec){
cout<<"目前系统不安全,不能分配资源\n";
}
return chec;
}
void ask(int index,int vec[]){
for(int j=0;j<=2;++j){
if(need[index][j]<vec[j]){
cout<<"需求大于总需求\n";
return ;
}
if(avl[j]<vec[j]){
cout<<"系统无法供应此需求\n";
return ;
}
}
for(int j=0;j<=2;++j){
allo[index][j]+=vec[j];
avl[j]-=vec[j];
}
calneed();
if(!check()){
for(int j=0;j<=2;++j){
allo[index][j]-=vec[j];
avl[j]+=vec[j];
}
calneed();
}
}
};
模拟实现动态分区存储管理
class DYMEM{
int sum;
set<pair<int,int> >st;
map<string,int> start;
map<int,string> Name;
public:
DYMEM(int sum=0){
this->sum=sum;
st.insert({0,0});
st.insert({sum,sum});
}
void show(){
for(auto i=st.begin();i!=st.end();i++){
auto j=i;
j++;
if(i==st.begin()){
if(j->first-i->second>0){
cout<<"空闲区域 "<<i->second<<" "<<j->first<<endl;
}
}
else if(j!=st.end()){
cout<<Name[i->first]<<" "<<i->first<<" "<<i->second<<endl;
if(j->first-i->second>0){
cout<<"空闲区域 "<<i->second<<" "<<j->first<<endl;
}
}
}
}
void allo(string name,int large){
for(auto i=st.begin();i!=st.end();i++){
auto j=i;
j++;
if(j==st.end()) break;
if((j->first)-(i->second)>=large){
start[name]=i->second;
Name[i->second]=name;
st.insert({i->second,i->second+large});
return;
}
}
cout<<"内存不足,不能分配\n";
}
void erase(string name){
for(auto i=st.begin();i!=st.end();i++){
if(start[name]==i->first && i!=st.begin()){
//cout<<i->first<<" "<<i->second<<endl;
st.erase(*i);
return ;
}
}
}
};
分页管理下存储情况模拟
class Fenye{
public:
bool fl[10][10];
int sum;
map<string,vector<int>> mp;
Fenye(){
this->sum=64;
for(int i=0;i<=7;++i){
for(int j=0;j<=7;++j){
fl[i][j]=0;
}
}
}
void allo(string name,int need){
if(need>this->sum){
cout<<"内存不足,无法分配\n";
return ;
}
int now=0;
vector<int> tar;
for(int i=0;i<=7;++i){
for(int j=0;j<=7;++j){
if(!fl[i][j]){
fl[i][j]=1;
tar.push_back(i*8+j);
now++;
if(now==need){
mp[name]=tar;
sum-=need;
return;
}
}
}
}
}
void huishou(string name){
sum+=mp[name].size();
for(auto t:mp[name]){
this->fl[t/8][t%8]=0;
}
}
void show(){
cout<<"\n当前位置图\n";
for(int i=0;i<=7;++i){
for(int j=0;j<=7;++j){
cout<<fl[i][j]<<" ";
}
cout<<'\n';
}
cout<<"\n";
}
void look(string name){
cout<<"作业"<<name<<"的页表为\n";
for(auto t:mp[name]){
cout<<t<<" ";
}
}
};
页面置换算法模拟(opt,fifo,lru,lfu)
int randab(int a,int b){
return (rand()%(b-a+1))+a;
}
class zhihuan{
public:
int cmd[321];//指令
zhihuan(){
this->cmd[0]=randab(0,318);
cout<<cmd[0]<<endl;
for(int i=1;i<=319;++i){
if(i%4==1){
this->cmd[i]=this->cmd[i-1]+1;
}
else if(i%4==2){
this->cmd[i]=randab(0,this->cmd[i-1]-1);
}
else if(i%4==3){
this->cmd[i]=this->cmd[i-1]+1;
}
else if(i%4==0){
this->cmd[i]=randab(this->cmd[i-1],318);
}
cout<<cmd[i]<<endl;
}
cout<<endl;
}
void opt(){
int op[35][330];
for(int i=0;i<=31;++i){
op[i][319]=999;
}
for(int i=318;i>=0;--i){
int now=cmd[i]/10;
for(int j=0;j<=31;++j){
op[j][i]=op[j][i+1];
}
op[now][i]=i;
}
//处理优先级
int neicun[5];
neicun[1]=-1;
neicun[2]=-1;
neicun[3]=-1;
neicun[4]=-1;
int queye=0;
for(int i=0;i<=319;++i){
for(int j=1;j<=4;++j){
cout<<neicun[j]<<" ";
}
cout<<endl;
int fl=0;
int now=cmd[i]/10;
if(neicun[1]==now || neicun[2]==now || neicun[3]==now || neicun[4]==now) continue;
else{
for(int j=1;j<=4;++j){
if(neicun[j]==-1){
neicun[j]=now;
queye++;
fl=1;
break;
}
}
if(fl) continue;
//cout<<op[neicun[1]][i]<<"*"<<op[neicun[2]][i]<<"*"<<op[neicun[3]][i]<<"*"<<op[neicun[4]][i]<<endl;
int mx=max(max(op[neicun[1]][i],op[neicun[2]][i]),max(op[neicun[3]][i],op[neicun[4]][i]));
for(int j=1;j<=4;++j){
if(op[neicun[j]][i]==mx){
neicun[j]=now;
queye++;
fl=1;
break;
}
}
}
}
cout<<"缺页数为 "<<queye<<endl;
}
void fifo(){
queue<int> q;
int neicun[5];
neicun[1]=-1;
neicun[2]=-1;
neicun[3]=-1;
neicun[4]=-1;
int queye=0;
for(int i=0;i<=319;++i){
for(int j=1;j<=4;++j){
cout<<neicun[j]<<" ";
}
cout<<endl;
int fl=0;
int now=cmd[i]/10;
cout<<"now"<<now<<endl;
if(neicun[1]==now || neicun[2]==now || neicun[3]==now || neicun[4]==now) continue;
else{
for(int j=1;j<=4;++j){
if(neicun[j]==-1){
neicun[j]=now;
q.push(now);
queye++;
fl=1;
break;
}
}
if(fl) continue;
auto t=q.front();
for(int j=1;j<=4;++j){
if(neicun[j]==t){
neicun[j]=now;
q.push(now);
q.pop();
queye++;
}
}
}
}
cout<<"缺页数为 "<<queye<<endl;
}
void lru(){
stack<int> st;
int neicun[5];
neicun[1]=-1;
neicun[2]=-1;
neicun[3]=-1;
neicun[4]=-1;
int queye=0;
for(int i=0;i<=319;++i){
for(int j=1;j<=4;++j){
cout<<neicun[j]<<" ";
}
cout<<endl;
int fl=0;
int now=cmd[i]/10;
if(neicun[1]==now || neicun[2]==now || neicun[3]==now || neicun[4]==now) continue;
else{
for(int j=1;j<=4;++j){
if(neicun[j]==-1){
neicun[j]=now;
st.push(now);
queye++;
fl=1;
break;
}
}
if(fl) continue;
auto t1=st.top();
st.pop();
auto t2=st.top();
st.pop();
auto t3=st.top();
st.pop();
auto t4=st.top();
st.pop();
st.push(t3);
st.push(t2);
st.push(t1);
for(int j=1;j<=4;++j){
if(neicun[j]==t4){
neicun[j]=now;
st.push(now);
queye++;
}
}
}
}
cout<<"缺页数为 "<<queye<<endl;
}
void lfu(){
map<int,int> mp;
int neicun[5];
neicun[1]=-1;
neicun[2]=-1;
neicun[3]=-1;
neicun[4]=-1;
int queye=0;
for(int i=0;i<=319;++i){
for(int j=1;j<=4;++j){
cout<<neicun[j]<<" ";
}
cout<<endl;
int fl=0;
int now=cmd[i]/10;
if(neicun[1]==now || neicun[2]==now || neicun[3]==now || neicun[4]==now){
mp[now]++;
continue;
}
else{
for(int j=1;j<=4;++j){
if(neicun[j]==-1){
neicun[j]=now;
mp[now]=1;
queye++;
fl=1;
break;
}
}
if(fl) continue;
int mn=min(min(mp[neicun[1]],mp[neicun[2]]),min(mp[neicun[3]],mp[neicun[4]]));
for(int j=1;j<=4;++j){
if(mp[neicun[j]]==mn){
neicun[j]=now;
mp[neicun[j]]=0;
mp[now]=1;
queye++;
break;
}
}
}
}
cout<<"缺页数为 "<<queye<<endl;
}
};