回 帖 发 新 帖 刷新版面

主题:求救!!操作系统算法问题 ,哪位哥哥帮小妹解答下

模拟页式虚拟存储管理中缺页中断理想模型淘汰算法    我这有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;
}
}

回复列表 (共1个回复)

沙发

void main(void)
{
cout<<"|----------页 面 置 换 算 法----------|"<<endl;
cout<<"|---power by zhanjiantao(028054115)---|"<<endl;
cout<<"|-------------------------------------|"<<endl;
cout<<"页面号引用串:7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1"<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<"选择<1>应用Optimal算法"<<endl;
cout<<"选择<2>应用FIFO算法"<<endl;
cout<<"选择<3>应用LRU算法"<<endl;
cout<<"选择<0>退出"<<endl;
int select;
PRA test;

while(select)
{
  cin>>select;
  switch(select)
  {
   case 0:
    break;
   case 1:
    cout<<"Optimal算法结果如下:"<<endl;
    test.Optimal();
    test.BlockClear();
    cout<<"----------------------"<<endl;
    break;
   case 2:
    cout<<"FIFO算法结果如下:"<<endl;
    test.FIFO();
    test.BlockClear();
    cout<<"----------------------"<<endl;
    break;
   case 3:
    cout<<"LRU算法结果如下:"<<endl;
    test.LRU();
    test.BlockClear();
    cout<<"----------------------"<<endl;
    break;
   default:
    cout<<"请输入正确功能号"<<endl;
    break;
  }
  
}
}
/*功能描述:输入可用内存页面数和一个作业访问逻辑页号的序列
*给出FIFO、LRU、OPT算法的缺页中断率
*本程序中设初始页框里的页号与实际页号都不同
*在VC6.0里测试通过
*/
#include<iostream.h>
#define  M  40
int N;
struct Pro
{
    int num,time;
};
int Input(int m,Pro p[M])
{   
    cout<<"请输入实际页数:";
    do
    {
        cin>>m;
        if(m>M)cout<<"数目太多,请重试"<<endl;
        else break;
    }while(1);
    cout<<endl<<"请输入各页面号"<<endl;
       for(int i=0;i<m;i++)
    {
        cin>>p[i].num;
        p[i].time=0;
    }
    return m;
}

void print(Pro *page1)//打印当前的页面
{
    Pro *page=new Pro[N];
    page=page1;
    for(int i=0;i<N;i++)cout<<page[i].num<<"  ";
    cout<<endl;
}


int  Search(int e,Pro *page1  )
{
    Pro *page=new Pro[N];
    page=page1;
    for(int i=0;i<N;i++)if(e==page[i].num)return i;
    return -1;
}

int Max(Pro *page1)
{
    Pro *page=new Pro[N];
    page=page1;
    int e=page[0].time,i=0;
    while(i<N)//找出离现在时间最长的页面
    {
        if(e<page[i].time)e=page[i].time;
        i++;
    }
    for( i=0;i<N;i++)if(e==page[i].time)return i;
    
    return -1;
}

int Compfu(Pro *page1,int i,int t,Pro p[M])
{
    Pro *page=new Pro[N];
    page=page1;
    
    int count=0;
    for(int j=i;j<M;j++)
    {
        if(page[t].num==p[j].num )break;
        else count++;
    }
    return count;
    
}

int main()
{
    
    cout<<"可用内存页面数"<<endl;
    cin>>N;    
    Pro p[M];
    Pro *page=new Pro[N];
    char c;
    int m=0,t=0;
    float n=0;
    m=Input(m,p);    
    
    do{
        
        for(int i=0;i<N;i++)//初试化页面基本情况
        {
            page[i].num=0;
            page[i].time=2-i;
        }
        /*int j=0,count=1;
        page[0].num=p[0].num;
        int i=1,k=1;
        while(i<N)
        {
        int flag=1;
        for(j=0;j<i;j++)
        if(p[k].num==page[i].num)
        {n++;flag=0;break;}
        if(flag==1){page[i].num=p[k].num;i++;}
        count++;k++;
    }*/
        i=0;        
        cout<<"f:FIFO页面置换"<<endl;
        cout<<"l:LRU页面置换"<<endl;
        cout<<"o:OPT页面置换"<<endl;
        cout<<"按其它键结束"<<endl;
        cin>>c;
        
        if(c=='f')//FIFO页面置换
        {
            n=0;
            cout<<"页面置换情况:   "<<endl;
            while(i<m)
            {
                if(Search(p[i].num,page)>=0)i++;//找到相同的页面
                else
                {  
                    if(t==N)t=0;
                    else
                    {
                        n++;//
                        page[t].num=p[i].num;
                        print(page);
                        t++;
                    }
                }
            }
            cout<<"缺页次数:"<<n<<"    缺页率:"<<n/m<<endl;      
            
        }
        if(c=='l')//LRU页面置换
        {    n=0;
        cout<<"页面置换情况:   "<<endl;
        while(i<m)
        {    
            int k;
            k=t=Search(p[i].num,page);
            if(t>=0)
                
                page[t].time=0;
            
            
            else
            {  
                n++;
                t=Max(page);
                
                page[t].num=p[i].num;
                page[t].time=0;
            }
            if(t==0){page[t+1].time++;page[t+2].time++;}
            if(t==1){page[2].time++;page[0].time++;}
            if(t==2){page[1].time++;page[0].time++;}
            if(k==-1)   print(page);                
            i++;
        }
        cout<<"缺页次数:"<<n<<"    缺页率:"<<n/m<<endl;
        }
        if(c=='o')//OPT页面置换
        {
            n=0;
            while(i<m)
            {
                if(Search(p[i].num,page)>=0)i++;
                else
                {
                    int temp=0,cn;
                    for(t=0;t<N;t++)
                    {
                        if(temp<Compfu(page,i,t,p))
                        {
                            temp=Compfu(page,i,t,p);
                            cn=t;
                        }
                    }
                    page[cn]=p[i];
                    n++;
                    print(page);
                    i++;
                }
            }
            cout<<"缺页次数:"<<n<<"    缺页率:"<<n/m<<endl;
        }
        
    }while(c=='f'||c=='l'||c=='o');
    
    
    
    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册