ºìÁªLinuxÃÅ»§
Linux°ïÖú

Ëã·¨--²Ù×÷ϵͳ--3ÖÖÒ³ÃæÖû»Ëã·¨

·¢²¼Ê±¼ä:2005-08-30 10:56:03À´Ô´:ºìÁª×÷Õß:frog
Ëã·¨Á¬ÔØ£¨7£©--²Ù×÷ϵͳ֮3ÖÖÒ³ÃæÖû»Ëã·¨
×÷Õߣº À´×Ô£º ÔĶÁ´ÎÊý£º 58033 [´ó ÖРС]


1.ÎÊÌâÃèÊö¼°Éè¼Æ˼Ï룺ÔÚ½ø³ÌÔËÐйý³ÌÖУ¬ÈôÆäËùÒª·ÃÎʵÄÒ³Ãæ²»ÔÚÄÚ´æÐè°ÑËüÃǵ÷ÈëÄڴ棬µ«ÄÚ´æÒÑÎÞ¿ÕÏпռäʱ£¬ÎªÁ˱£Ö¤¸Ã½ø³ÌÄÜÕý³£ÔËÐУ¬ÏµÍ³±ØÐë´ÓÄÚ´æÖе÷³öÒ»Ò³³ÌÐò»òÊý¾Ý£¬ËÍ´ÅÅ̵ĶԻ»ÇøÖС£µ«Ó¦½«ÄĸöÒ³Ãæµ÷³ö£¬ËùÒÔÐèÒª¸ù¾ÝÒ»¶¨µÄËã·¨À´È·¶¨¡£ÒÔÏ·ֱðÊÇÈý¸öËã·¨µÄÉè¼Æ˼Ïë¡£
OPTIMAL£º×î¼ÑÖû»Ëã·¨¡£ÆäËùÑ¡ÔñµÄ±»ÌÔÌ­Ò³Ã棬½«ÊÇÒÔºóÓÀ²»Ê¹Óõģ¬»òÊÇÔÚ×£¨Î´À´£©Ê±¼äÄÚ²»ÔÙ±»·ÃÎʵÄÒ³Ãæ¡£
FIFO£ºÏȽøÏȳöÖû»Ëã·¨¡£¸ÃËã·¨×ÜÊÇÌÔÌ­×îÏȽøÈëÄÚ´æµÄÒ³Ã棬¼ÈÑ¡ÔñÔÚÄÚ´æÖÐפÁôʱ¼ä×î¾ÃµÄÒ³ÃæÓèÒÔÌÔÌ­¡£
LRU£º×î½ü×î¾ÃδʹÓÃÖû»Ëã·¨¡£¸ÃËã·¨¸³Óèÿ¸öÒ³ÃæÒ»¸ö·ÃÎÊ×ֶΣ¬ÓÃÀ´¼Ç¼һ¸öÒ³Ãæ×ÔÉϴα»·ÃÎÊÒÔÀ´Ëù¾­ÀúµÄʱ¼äT£¬µ±ÐëÌÔÌ­Ò»¸öÒ³Ãæʱ£¬Ñ¡ÔñÏÖÓÐÒ³ÃæÖÐÆäTÖµ×î´óµÄ¸øÓèÌÔÌ­¡£


#include

#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 {
block.content = -1;
block[i].timer = 0;
}

page = new pageInfor[Psize];
for(i=0; i {
page[i].content = QString[i];
page[i].timer = 0;
}
}

int PRA::findSpace(void)
{
for(int i=0; i if(block[i].content == -1)
return i;//ÕÒµ½¿ÕÏÐÄڴ棬·µ»ØBLOCKÖÐλÖÃ
return -1;
}

int PRA::findExist(int curpage)
{

for(int i=0; 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 if(block[i].timer >= block[pos].timer)
pos = i;//ÕÒµ½Ó¦ÓèÖû»Ò³Ã棬·µ»ØBLOCKÖÐλÖÃ
return pos;
}

void PRA::display(void)
{


for(int i=0; i if(block[i].content != -1)
cout< cout<}


void PRA::Optimal(void)
{
int exist,space,position ;

for(int i=0; i {
exist = findExist(i);
if(exist != -1)
{ cout<<"²»È±Ò³"< else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
for(int k=0; k for(int j=i; 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 {
exist = findExist(i);
if(exist != -1)
{
cout<<"²»È±Ò³"< block[exist].timer = -1;//»Ö¸´´æÔڵIJ¢¸Õ·ÃÎʹýµÄ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 block[j].timer++;
}
}

void PRA::FIFO(void)
{

int exist,space,position ;

for(int i=0; i {
exist = findExist(i);
if(exist != -1)
{cout<<"²»È±Ò³"<
else
{
space = findSpace();
if(space != -1)
{
block[space] = page[i];
display();
}
else
{
position = findReplace();
block[position] = page[i];
display();
}
}
for(int j=0; j block[j].timer++;//BLOCKÖÐËùÓÐÒ³ÃæTIMER++
}
}

void PRA::BlockClear(void)
{
for(int i=0; i {
block[i].content = -1;
block[i].timer = 0;
}
}


void main(void)
{
cout<<"|----------Ò³ Ãæ Öà »» Ëã ·¨----------|"< cout<<"|---power by zhanjiantao(028054115)---|"< cout<<"|-------------------------------------|"< cout<<"Ò³ÃæºÅÒýÓô®:7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1"< cout<<"----------------------------------------------------"< cout<<"Ñ¡Ôñ<1>Ó¦ÓÃOptimalËã·¨"< cout<<"Ñ¡Ôñ<2>Ó¦ÓÃFIFOËã·¨"< cout<<"Ñ¡Ôñ<3>Ó¦ÓÃLRUËã·¨"< cout<<"Ñ¡Ôñ<0>Í˳ö"< int select;
PRA test;


while(select)
{
cin>>select;
switch(select)
{
case 0:
break;
case 1:
cout<<"OptimalËã·¨½á¹ûÈçÏÂ:"< test.Optimal();
test.BlockClear();
cout<<"----------------------"< break;
case 2:
cout<<"FIFOËã·¨½á¹ûÈçÏÂ:"< test.FIFO();
test.BlockClear();
cout<<"----------------------"< break;
case 3:
cout<<"LRUËã·¨½á¹ûÈçÏÂ:"< test.LRU();
test.BlockClear();
cout<<"----------------------"< break;
default:
cout<<"ÇëÊäÈëÕýÈ·¹¦ÄܺÅ"< break;
}

}

}
·¢²¼Ê±¼ä£º2005-4-15

[[i] Last edited by frog on 2005-10-23 at 16:17
]
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. °®µÄ±ßÔµ ÓÚ 2005-09-01 00:34:14·¢±í:

    Ö§³Ö

  2. ming ÓÚ 2005-08-31 00:31:06·¢±í:

    Ö§³Ö