缺页中断模拟系统操作系统课程设计

缺页中断模拟系统操作系统课程设计
#include< iostream.h >
#include"stdlib.h"

typedef int QElemType;
#define ok 1
#define overflow 0
#define error 0

typedef struct Qnode {
QElemType data;
struct Qnode *next;
}Qnode,*Queueptr;
typedef struct {
Queueptr front;
Queueptr rear;
}LinkQueue;

void menu(int *a,int n,int m);

InitQueue( LinkQueue &Q ) {
Q.front = Q.rear = ( Queueptr )malloc( sizeof( Qnode ));
if( !Q.front ) exit ( overflow );
Q.front->next =NULL;
return ok;
}

EnQueue( LinkQueue &Q,QElemType e ) {
Queueptr p;
p = ( Queueptr )malloc( sizeof( Qnode ));
if( !p ) exit( overflow );
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return ok;
}

DeQueue( LinkQueue &Q,QElemType &e ) {
Queueptr p;
if( Q.front == Q.rear ) return error;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if( Q.rear == p ) Q.rear = Q.front;
free( p );
return ok;
}

void VisitQueue( LinkQueue Q ) {
     Queueptr p;
p = Q.front->next;
while( p ) { cout << p->data << " ";p = p->next; }
}

CompareQueue( LinkQueue Q,int page ) {
     Queueptr p;
p = Q.front->next;
while( p ) {
   if( p->data == page ) return ok;
       p = p->next;
}
return error;
}


void FIFO( int *a,int n,int m ) {
LinkQueue Q;
int page, flag = 0,x;
float t = 0.0;
float y=0.0;

InitQueue( Q );
 cout<<"内存中的页面号为:";
for( int i = 1;i <= m;i++ )
{
   page = a[ i ];
   t++;
   if( t <= n ) { EnQueue( Q,page );
                
                  cout << a[ i ] << " "; }
   else {
        for( int j = 1;j <= n;j++ )
          if( CompareQueue( Q,page ) ) { t--;flag = 1;break; }
    cout<<endl;

     if( flag == 0 ) { DeQueue( Q,x );
  cout<<endl;
 cout << page << "号页面进入内存"<<endl;
                       cout << x <<"号页面被淘汰"<<endl;
        
           EnQueue( Q,page );
                      }
      cout<<"内存中的页面号为:";
      VisitQueue( Q );
      cout << endl;
   }
   flag = 0;
}
y=(t/m)*100;
cout<<endl;
cout<<"缺页次数为:"<< t << endl;
cout<<"缺页率为:"<<y<<"%"<<endl;
menu(a,n,m);
}
  
void LRU( int *a,int n,int m ) {
LinkQueue Q;int page,flag = 0,x,e;
float t = 0.0;
float y=0.0;
InitQueue( Q );
 cout<<"内存中的页面号为:" ;
for( int i = 1;i <= m;i++ )
{
   page = a[ i ];t++;
   if( t <= n ) { EnQueue( Q,page );
    
cout << a[ i ] << " "; }
   else { for( int j = 1;j <= n;j++ )
          if( CompareQueue( Q,page ) ) { t--;DeQueue(Q,e);EnQueue(Q,e);flag = 1;break; }
    cout<<endl;
       if( flag == 0 ) { DeQueue( Q,x );
    cout<<endl;
cout<< page << "号页面进入内存"<<endl;
                      cout << x << "号页面被淘汰" <<endl;

          EnQueue( Q,page );
       }
 cout<<"内存中的页面号为:";
      VisitQueue( Q );
       cout <<endl;
   }
   flag = 0;
}
y=(t/m)*100;
cout<<endl;
cout << "缺页次数为:"<< t << endl;
cout<<"缺页率为:"<<y<<"%"<<endl;
menu(a,n,m);
}


int max( int *t,int n ){
int max =t[ 1 ],s = 1;
for( int i = 1;i <= n;i++ )
   if( t[ i ] > max ) {max = t[ i ];s = i;}
   return s;
}

void OPT( int a[ 21 ],int n,int m ) {

int flag = 0;
float w = 0.0;
int *t =new int[ n + 1 ];
float y=0.0;
 cout<<"内存中的页面号为:" ;
for( int i = 1;i <= m;i++ )
{
   w++;
   if( w <= n )   cout << a[ i ] << " " ;
 
   else{
    for( int q = 1;q <= n;q++ )
     if( a[ i ] == a[ q ] ) { w--;flag = 1;break; }
  cout<<endl;
    if( flag == 0 )
    {
     for( int j = 1;j <= n;j++ )
      for( int k = i;k <= m;k++ )
      {
       if( a[ j ] !=   a[ k ] ) t[ j ]++;
          else break;
      }
       cout<<endl;
       cout<< a[ i ] << "号页面进入内存"<<endl;
      cout << a[ max( t,n ) ] <<"号页面被淘汰" <<endl;
      a[ max( t,n ) ] = a[ i ];
  
    }

 cout<<"内存中的页面号为:" ;
       for( int s = 1;s <= n;s++ )
      cout << a[ s ] << " ";
    cout <<endl;
  
   }
     for( int r = 1;r <= n;r++ ) t[ r ] = 0;
     flag = 0;

}
y=(w/m)*100;
cout<<endl;
     cout << "缺页次数为:" << w << endl;
cout<<"缺页率为"<<y<<"%"<<endl;
     delete [] a;
  menu(a,n,m);
}

void menu(int *a ,int n,int m){
int d;
cout<<"*******************************"<<endl;
cout<<"       请选择页面调度算法"<<endl;
cout<<"           1.FIFO算法"<<endl;
cout<<"           2.LRU 算法"<<endl;
cout<<"           3.OPT 算法"<<endl;
cout<<"           4.退出界面"<<endl;
cout<<"*******************************"<<endl;
cout<<endl;  www.751com.cn
cout<<"请输入你的选择(1-4):"<<endl;
cin>>d;
if(d<1||d>4){
cout<<"你的选择有误,请重新选择(1-4):"<<endl;
cin>>d;}
switch(d){
case 1:
cout << "调用FIFO算法的输出结果为:" << endl;
cout<<endl;
FIFO( a,n,m );
cout << endl;
break;
case 2:
cout << "调用LRU算法的输出结果为:" << endl;
cout<<endl;
LRU( a,n,m );
cout << endl;
break;
case 3:
cout << "调用OPT算法的输出结果为:" << endl;
cout<<endl;
OPT( a,n,m );
cout << endl;
break;
case 4:break;main()
{
int m,n;
cout << "输入待调入页面数:" << endl;
cin >> m;
int *a = new int[ m + 1 ];
     cout << "输入可使用的物理块数:" << endl;
cin >> n;
 
cout << "输入要调入的页面号码:" << endl;
for( int i = 1;i <= m;i++ )
   cin >> a[ i ];
cout << endl;
menu(a,n,m);
return 0;
     delete [] a;}844

[1] [2] [3] 下一页

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有