页面调度算法(FIFO,LRU,OPT)

页面调度算法(FIFO,LRU,OPT)2006-11-22 19:29
#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;

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, t = 0,flag = 0,x;

InitQueue( Q );

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; }

if( flag == 0 ) { DeQueue( Q,x );
cout << x << " was taken out "
<< page << " was taken in";
EnQueue( Q,page );
}
cout << endl;
VisitQueue( Q );
cout << ":";
}
flag = 0;
}
cout << "缺页中断数为:" << endl;
cout << "t= " << t << endl;
}

void LRU( int *a,int n,int m ) {
LinkQueue Q;int page, t = 0,flag = 0,x,e;
InitQueue( Q );
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; }
if( flag == 0 ) { DeQueue( Q,x );
cout << x << " was taken out " << page << " was taken in";
EnQueue( Q,page );
}
cout << endl;
VisitQueue( Q );
cout << ":";
}
flag = 0;
}
cout << "缺页中断数为:" << endl;
cout << "t= " << t << endl;
}


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 w = 0,flag = 0;
int *t =new int[ n + 1 ];

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--;fl

ag = 1;break; }
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 << a[ max( t,n ) ] << " " << "was taken out"
<< " " << a[ i ] << " was taken in ";

a[ max( t,n ) ] = a[ i ];

}

cout << endl;
for( int s = 1;s <= n;s++ )
cout << a[ s ] << " ";
cout << ":";

}
for( int r = 1;r <= n;r++ ) t[ r ] = 0;
flag = 0;

}
cout << "缺页中断数为:" << endl;
cout << "w = " << w << endl;
delete [] a;
}

main()
{
int m,n;
cout << "输入页面数:" << endl;
cin >> m;
int *a = new int[ m + 1 ];
cout << "输入驻留集大小:" << endl;
cin >> n;

cout << "input the pages" << endl;
for( int i = 1;i <= m;i++ )
cin >> a[ i ];
cout << endl;
cout << "The result of FIFO:" << endl;
FIFO( a,n,m );
cout << endl;
cout << "The result of LRU:" << endl;
LRU( a,n,m );
cout << endl;
cout << "The result of OPT:" << endl;
OPT( a,n,m );
cout << endl;
return 0;
delete [] a;
}



相关文档
最新文档