计算并输出下述各种算法在不同内存容量下的命中率。
A. FIFO先进先出的算法
B. LRR最近最少使用算法
C. OPT最佳淘汰算法(先淘汰最不常用的页地址)
D. LFR最少访问页面算法
E. NUR最近最不经常使用算法
代码:
#include<stdio.h>
#include<iostream.h>
#include <stdlib.h>
#include<time.h>
struct aa{
int page;
int count;
aa* next;
};
void main()
{
time_t t;
srand(unsigned(time(&t)));
int i,n,j,ii,m,answer,ffalse,count,fangfa,temp1,min,nn,mm;
double sum;
aa *head,*tail,*temp,*table,*first,*ti;
/* nn=4;mm=1;
for(nn=4;nn>32;nn++)
{
for(mm=1;mm>5;mm++)
{*/
cin>>m;
//m=nn;
cout<<endl;
cout<<"fangfa: 1-FIFO;2-LRR;3-OPT;4-LFR;5-NUR"<<endl;
cout<<"Mothed:";
cin>>fangfa;
//fangfa=mm;
ffalse=0;
answer=0;
table=new(aa);
temp=table;
table->page=-1;
table->count=0;
head=table;
for(ii=2;ii<=m;ii++)
{
table=new(aa);
table->page=-1;
table->count=0;
temp->next=table;
temp=table;
if (ii==m){table->next=NULL;}
}
tail=table;
temp=head;
first=head;
count=0;
i=0;
while(i<320)
{
min=400;
if (count==0) {n=(rand()%320+1)%320; j=n/10;}
if (count==1) {n=rand()%(n+1);j=n/10;}
if(count==2) {j=((n+1)%320)/10;}
if(count==3) {j=((rand()%(320-n-2))+n+2)/10;}
table=head;
temp=head;
answer=0;
min=400;
if (fangfa==5)
{
while(table!=NULL)
{
if (table->page==j){answer=1;table->count=2;}
table=table->next;
}
if (answer!=1)
{
table=head;
while (table!=NULL)
{
if (table->count<min){temp=table;min=table->count;}
table=table->next;
}
if (temp->page!=-1) ++ffalse;
temp->page=j;
temp->count=1;
}
table=head;
if ((i%32)==0)
{
while(table!=NULL)
{
if (table->page!=-1) table->count=1;
// if (table->page==j){answer=1;++(table->count);}
table=table->next;
}
}
}
if ((fangfa==4)||(fangfa==3))
{
while(table!=NULL)
{
if (table->page==j){answer=1;++(table->count);}
table=table->next;
}
if(answer!=1)
{ table=head;
while (table!=NULL)
{
if (table->count<min){temp=table;min=table->count;}
table=table->next;
}
if (temp->page!=-1){
++ffalse;
temp->page=j;
table=head;
while(table){
table->count=1;
table=table->next;
}
}
else{
temp->page=j;
++(temp->count);
}
}
}
if (fangfa==2){
while((table!=NULL)&&(fangfa==2))
{
if (table->page==j){answer=1;temp=table;}
table=table->next;
}
if((fangfa==2)&&(answer==1))
{
//table=temp;
temp1=temp->page;
while (temp!=NULL)
{
temp->page=temp->next->page;
temp=temp->next;
}
tail->page=temp1;
}
if((answer!=1)&&(fangfa==2))
{
if (first->page!=-1)
ffalse=ffalse+1;
first->page=j;
temp=head;
while (temp!=NULL)
{
temp->page=temp->next->page;
temp=temp->next;
}
tail->page=j;
}
}
table=head;
while((table!=NULL)&&(fangfa==1))
{
if (table->page==j){answer=1;}
table=table->next;
}
if ((answer!=1)&&(fangfa==1))
{
if (first->page!=-1) ffalse=ffalse+1;
first->page=j;
if (first->next!=NULL) { first=first->next;}
else {first=head;}
}
++i;
++count;
if (count==4){count=0;}
}
sum=1.0-ffalse/320.0;
/*cout<<nn<<" ";
if (fangfa==1) cout<<"FIFO:"<<sum<<" ";
if (fangfa==2) cout<<" LRR:"<<sum<<" ";
if (fangfa==3) cout<<" OPT:"<<sum<<" ";
if (fangfa==4) cout<<" LFR:"<<sum<<" ";
if (fangfa==5) cout<<" NUR:"<<sum<<" ";
if(mm==5) cout<<endl;*/
cout<<sum<<endl;
for(ti=head;ti!=NULL;ti=ti->next)cout<<ti->page<<" ";
cout<<endl;
}
实验四 文件系统设计
1.目的和要求
本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。
2.实验内容
为DOS系统设计一个简单的二级文件系统。要求做到以下几点:
①可以实现下列几条命令
LOGIN 用户登陆
DIR 列文件目录
CREATE 创建文件
DELETE 删除文件
OPEN 打开文件
CLOSE 关闭文件
READ 读文件
WRITE 写文件
②列目录时要列出文件名,物理地址,保护码和文件长度。
③源文件可以进行读写保护。
3.实验环境
同前
4.实验提示
①首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。
②用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2…并以编号作为物理地址,在目录中进行登记。
5.实验运行结果
参考程序见下(本程序需要在c:下建一个名为osfile的目录及一个名为file的子目录):
#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "stdlib.h"
#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/
#define MAXCHILD 50 /*the largest child*/
#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/
typedef struct /*the structure of OSFILE*/
{int fpaddr; /*file physical address*/
int flength; /*file length*/
int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write(default);*/
char fname[MAXNAME]; /*file name*/
} OSFILE;
typedef struct /*the structure of OSUFD*/
{char ufdname[MAXNAME]; /*ufd name*/
OSFILE ufdfile[MAXCHILD]; /*ufd own file*/
}OSUFD;
typedef struct /*the structure of OSUFD'LOGIN*/
{char ufdname[MAXNAME]; /*ufd name*/
char ufdpword[8]; /*ufd password*/
} OSUFD_LOGIN;
typedef struct /*file open mode*/
{int ifopen; /*ifopen:0-close,1-open*/
int openmode; /*0-read only,1-write only,2-read and write,3-initial*/
}OSUFD_OPENMODE;
OSUFD *ufd[MAXCHILD];