主题:求救!!操作系统算法问题 ,哪位哥哥帮小妹解答下
模拟页式虚拟存储管理中缺页中断理想模型淘汰算法 我这有3种别的算法的 就是没理想模型淘汰算法的 哥哥们帮忙想下好吗!!!!!!!!!
#include<stdio.h>
#include<string.h>
#include<iostream.h>
const int block_time=10; //定义时间片的长度为10秒
const int MAXPCB=100; //定义最大进程数
//定义进程结构体
typedef struct node{
char name[20];
int status;
int time;
int privilege;
int finished;
int wait_time;
}pcb;
pcb pcbs[MAXPCB];//应改为双链结构
int quantity;
//初始化函数
void initial(){
int i;
for(i=0;i<MAXPCB;i++){
strcpy(pcbs[i].name,"");
pcbs[i].status=0;
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantity=0;
}
//读数据函数
int readData(){
FILE *fp;
char fname[20];
int i;
cout<<"请输入进程流文件名:";
cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
cout<<"错误,文件打不开,请检查文件名"<<endl;
}
else{
while(!feof(fp)){
fscanf(fp,"%s %d %d %d",pcbs[quantity].name,&pcbs[quantity].status,&pcbs[quantity].time,&pcbs[quantity].privilege);
quantity++;
}
//输出所读入的数据
cout<<"输出所读入的数据"<<endl;
cout<<"进程名 进程状态 所需时间 优先数"<<endl;
for(i=0;i<quantity;i++){
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].status<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}
return(1);
}
return(0);
}
//先进先出算法
void FIFO()
{
int i,j;
int total;
//输出FIFO算法执行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO算法执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;
for(i=0;i<quantity;i++){
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for(j=i+1;j<quantity;j++){
pcbs[j].wait_time+=pcbs[i].time;
}
}
total=0;
for(i=0;i<quantity;i++){
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantity<<endl;
}
#include <iostream.h>
#define Bsize 3
#define Psize 20
struct pageInfor
{
int content;//页面号
int timer;//被访问标记
};
class PRA
{
public:
PRA(void);
int findSpace(void);//查找是否有空闲内存
int findExist(int curpage);//查找内存中是否有该页面
int findReplace(void);//查找应予置换的页面
void display(void);//显示
void FIFO(void);//FIFO算法
void LRU(void);//LRU算法
void Optimal(void);//OPTIMAL算法
void BlockClear(void);//BLOCK恢复
pageInfor * block;//物理块
pageInfor * page;//页面号串
private:
};
PRA::PRA(void)
{
int QString[20]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
block = new pageInfor[Bsize];
for(int i=0; i<Bsize; i++)
{
block[i].content = -1;
block[i].timer = 0;
}
page = new pageInfor[Psize];
for(i=0; i<Psize; i++)
{
page[i].content = QString[i];
page[i].timer = 0;
}
}
int PRA::findSpace(void)
{
for(int i=0; i<Bsize; i++)
if(block[i].content == -1)
return i;//找到空闲内存,返回BLOCK中位置
return -1;
}
int PRA::findExist(int curpage)
{
for(int i=0; i<Bsize; i++)
if(block[i].content == page[curpage].content)
return i;//找到内存中有该页面,返回BLOCK中位置
return -1;
}
int PRA::findReplace(void)
{
int pos = 0;
for(int i=0; i<Bsize; i++)
if(block[i].timer >= block[pos].timer)
pos = i;//找到应予置换页面,返回BLOCK中位置
return pos;
}
void PRA::display(void)
{
for(int i=0; i<Bsize; i++)
if(block[i].content != -1)
cout<<block[i].content<<" ";
cout<<endl;
}
void PRA::Optimal(void)
{
int exist,space,position ;
for(int i=0; i<Psize; i++)
{
exist = findExist(i);
if(exist != -1)
{ cout<<"不缺页"<<endl; }
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
for(int k=0; k<Bsize; k++)
for(int j=i; j<Psize; j++)
{
if(block[k].content != page[j].content)
{ block[k].timer = 1000; }//将来不会用,设置TIMER为一个很大数
else
{
block[k].timer = j;
break;
}
}
position = findReplace();
block[position] = page[i];
display();
}
}
}
}
void PRA::LRU(void)
{
int exist,space,position ;
for(int i=0; i<Psize; i++)
{
exist = findExist(i);
if(exist != -1)
{
cout<<"不缺页"<<endl;
block[exist].timer = -1;//恢复存在的并刚访问过的BLOCK中页面TIMER为-1
}
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
position = findReplace();
block[position] = page[i];
display();
}
}
for(int j=0; j<Bsize; j++)
block[j].timer++;
}
}
void PRA::FIFO(void)
{
int exist,space,position ;
for(int i=0; i<Psize; i++)
{
exist = findExist(i);
if(exist != -1)
{cout<<"不缺页"<<endl;}
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
position = findReplace();
block[position] = page[i];
display();
}
}
for(int j=0; j<Bsize; j++)
block[j].timer++;//BLOCK中所有页面TIMER++
}
}
void PRA::BlockClear(void)
{
for(int i=0; i<Bsize; i++)
{
block[i].content = -1;
block[i].timer = 0;
}
}
#include<stdio.h>
#include<string.h>
#include<iostream.h>
const int block_time=10; //定义时间片的长度为10秒
const int MAXPCB=100; //定义最大进程数
//定义进程结构体
typedef struct node{
char name[20];
int status;
int time;
int privilege;
int finished;
int wait_time;
}pcb;
pcb pcbs[MAXPCB];//应改为双链结构
int quantity;
//初始化函数
void initial(){
int i;
for(i=0;i<MAXPCB;i++){
strcpy(pcbs[i].name,"");
pcbs[i].status=0;
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantity=0;
}
//读数据函数
int readData(){
FILE *fp;
char fname[20];
int i;
cout<<"请输入进程流文件名:";
cin>>fname;
if((fp=fopen(fname,"r"))==NULL){
cout<<"错误,文件打不开,请检查文件名"<<endl;
}
else{
while(!feof(fp)){
fscanf(fp,"%s %d %d %d",pcbs[quantity].name,&pcbs[quantity].status,&pcbs[quantity].time,&pcbs[quantity].privilege);
quantity++;
}
//输出所读入的数据
cout<<"输出所读入的数据"<<endl;
cout<<"进程名 进程状态 所需时间 优先数"<<endl;
for(i=0;i<quantity;i++){
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].status<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}
return(1);
}
return(0);
}
//先进先出算法
void FIFO()
{
int i,j;
int total;
//输出FIFO算法执行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO算法执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;
for(i=0;i<quantity;i++){
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for(j=i+1;j<quantity;j++){
pcbs[j].wait_time+=pcbs[i].time;
}
}
total=0;
for(i=0;i<quantity;i++){
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantity<<endl;
}
#include <iostream.h>
#define Bsize 3
#define Psize 20
struct pageInfor
{
int content;//页面号
int timer;//被访问标记
};
class PRA
{
public:
PRA(void);
int findSpace(void);//查找是否有空闲内存
int findExist(int curpage);//查找内存中是否有该页面
int findReplace(void);//查找应予置换的页面
void display(void);//显示
void FIFO(void);//FIFO算法
void LRU(void);//LRU算法
void Optimal(void);//OPTIMAL算法
void BlockClear(void);//BLOCK恢复
pageInfor * block;//物理块
pageInfor * page;//页面号串
private:
};
PRA::PRA(void)
{
int QString[20]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
block = new pageInfor[Bsize];
for(int i=0; i<Bsize; i++)
{
block[i].content = -1;
block[i].timer = 0;
}
page = new pageInfor[Psize];
for(i=0; i<Psize; i++)
{
page[i].content = QString[i];
page[i].timer = 0;
}
}
int PRA::findSpace(void)
{
for(int i=0; i<Bsize; i++)
if(block[i].content == -1)
return i;//找到空闲内存,返回BLOCK中位置
return -1;
}
int PRA::findExist(int curpage)
{
for(int i=0; i<Bsize; i++)
if(block[i].content == page[curpage].content)
return i;//找到内存中有该页面,返回BLOCK中位置
return -1;
}
int PRA::findReplace(void)
{
int pos = 0;
for(int i=0; i<Bsize; i++)
if(block[i].timer >= block[pos].timer)
pos = i;//找到应予置换页面,返回BLOCK中位置
return pos;
}
void PRA::display(void)
{
for(int i=0; i<Bsize; i++)
if(block[i].content != -1)
cout<<block[i].content<<" ";
cout<<endl;
}
void PRA::Optimal(void)
{
int exist,space,position ;
for(int i=0; i<Psize; i++)
{
exist = findExist(i);
if(exist != -1)
{ cout<<"不缺页"<<endl; }
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
for(int k=0; k<Bsize; k++)
for(int j=i; j<Psize; j++)
{
if(block[k].content != page[j].content)
{ block[k].timer = 1000; }//将来不会用,设置TIMER为一个很大数
else
{
block[k].timer = j;
break;
}
}
position = findReplace();
block[position] = page[i];
display();
}
}
}
}
void PRA::LRU(void)
{
int exist,space,position ;
for(int i=0; i<Psize; i++)
{
exist = findExist(i);
if(exist != -1)
{
cout<<"不缺页"<<endl;
block[exist].timer = -1;//恢复存在的并刚访问过的BLOCK中页面TIMER为-1
}
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
position = findReplace();
block[position] = page[i];
display();
}
}
for(int j=0; j<Bsize; j++)
block[j].timer++;
}
}
void PRA::FIFO(void)
{
int exist,space,position ;
for(int i=0; i<Psize; i++)
{
exist = findExist(i);
if(exist != -1)
{cout<<"不缺页"<<endl;}
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
position = findReplace();
block[position] = page[i];
display();
}
}
for(int j=0; j<Bsize; j++)
block[j].timer++;//BLOCK中所有页面TIMER++
}
}
void PRA::BlockClear(void)
{
for(int i=0; i<Bsize; i++)
{
block[i].content = -1;
block[i].timer = 0;
}
}