提交 e3eec925 创建 作者: haixia song's avatar haixia song

init

上级
/Debug
/Release
*.bak
\ No newline at end of file
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="PageRA" SubjectID="1580a23c-aa04-4cf3-8cca-488dc577df8d" TemplatePath="console(c)\Project" IncrementalUpload="0">
<Configurations>
<Configuration Name="Debug">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1"/>
<Tool Name="NASMAssemblerTool"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
<Configuration Name="Release">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG"/>
<Tool Name="NASMAssemblerTool"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
<File RelativePath=".\console.h">
</File>
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\console.c">
</File>
</Filter>
</Files>
</OSLProject>
添加文件
#include "console.h"
int main()
{
int *BlockofMemory; //内存物理块
const int BlockCount = 5;
int PageNumofRef[] = {7,0,1,2,0,3,0,4,2,3,0,1,1,7,0,1,0,3}; //页面号引用串
int PageNumRefCount = sizeof(PageNumofRef) / sizeof(PageNumofRef[0]);
BlockofMemory = (int*)malloc(BlockCount * sizeof(int));
if(BlockofMemory == (int*)NULL)
{
printf("内存分配出错\n");
exit(1);
}
ResetBlockofMemory(BlockofMemory, BlockCount);
Opt(BlockofMemory, PageNumofRef, BlockCount, PageNumRefCount);
ResetBlockofMemory(BlockofMemory,BlockCount);
Fifo(BlockofMemory, PageNumofRef, BlockCount, PageNumRefCount);
ResetBlockofMemory(BlockofMemory,BlockCount);
Lru(BlockofMemory, PageNumofRef, BlockCount, PageNumRefCount);
free(BlockofMemory);
return 0;
}
//输出内存块页面序号
int OutputBlockofMemory(int *BlockofMemory, int BlockCount, int ReplacePage, int PageNum)
{
int i;
printf("访问页面 %d 后,", PageNum);
printf("内存中的页面号为:\t");
for(i = 0; i < BlockCount; i++)
{
if(BlockofMemory[i] < 0)
printf("# ");
else
printf("%d ", BlockofMemory[i]);
}
if(ReplacePage != -1)
printf("\t淘汰页面号为:%d", ReplacePage);
printf("\n");
return -1;
}
//输出页面引用串号
void OutputPageNumofRef(int* PageNumofRef, int PageNumRefCount)
{
int i = 0;
printf("页面引用串为:\t");
for(i = 0; i < PageNumRefCount; i++)
printf("%d ", PageNumofRef[i]);
printf("\n");
}
//内存块页面号清零
void ResetBlockofMemory(int *BlockofMemory, int BlockCount)
{
int i;
for(i = 0; i < BlockCount; i++)
BlockofMemory[i] = -1;
}
//判断页是否在内存中,如果页在内存中,返回1,否则返回0;
int PageInBlockofMemory(int PageNum, int *BlockofMemory, int BlockCount)
{
int i;
for(i = 0; i < BlockCount; i++)
if(PageNum == BlockofMemory[i])
return 1;
return 0;
}
//下次访问次序
//参数j: 页面在内存块中的位置
//参数i: 页面号在页面号引用串中的位置
int DistanceOpt(int *BlockofMemory, int *PageNumofRef, int j, int i, int PageNumRefCount)
{
int k;
for(k = i + 1; k < PageNumRefCount; k++)
if(BlockofMemory[j] == PageNumofRef[k])
return k;
return PageNumRefCount;
}
//最佳页面置换算法
void Opt(int *BlockofMemory, int *PageNumofRef, int BlockCount, int PageNumRefCount)
{
int i, j, k;
int MaxIndex1, MaxIndex2;
int MissCount = 0;
int ReplacePage;
int EmptyBlockCount = BlockCount;
printf("**********最佳页面置换算法:**********\n");
//输出页面引用串号
OutputPageNumofRef(PageNumofRef, PageNumRefCount);
for(i = 0; i < PageNumRefCount; i++)
{
if(!PageInBlockofMemory(PageNumofRef[i], BlockofMemory, BlockCount)) //页不在内存中
{
MissCount++;
if(EmptyBlockCount > 0)
{
BlockofMemory[BlockCount - EmptyBlockCount] = PageNumofRef[i];
OutputBlockofMemory(BlockofMemory, BlockCount, -1, PageNumofRef[i]);
EmptyBlockCount--;
}
else
{
MaxIndex1 = MaxIndex2 = 0;
//求出未来最长时间不被访问的页
for(j = 0; j < BlockCount; j++)
{
MaxIndex2 = DistanceOpt(BlockofMemory, PageNumofRef, j, i, PageNumRefCount);
if(MaxIndex1 < MaxIndex2)
{
MaxIndex1 = MaxIndex2;
k = j;
}
}
ReplacePage = BlockofMemory[k];
BlockofMemory[k] = PageNumofRef[i];
OutputBlockofMemory(BlockofMemory, BlockCount, ReplacePage, PageNumofRef[i]);
}
}
else
{
OutputBlockofMemory(BlockofMemory,BlockCount, -1, PageNumofRef[i]);
}
}
printf("缺页次数为: %d\n", MissCount);
printf("OPT缺页率为: %.3f\n", (float)MissCount / PageNumRefCount);
}
//先进先出页面置换算法
void Fifo(int *BlockofMemory,int *PageNumofRef,int BlockCount,int PageNumRefCount)
{
int i;
int ReplacePage;
int ReplaceIndex = 0;
int MissCount = 0;
int EmptyBlockCount = BlockCount;
printf("**********先进先出页面置换算法:**********\n");
//输出页面引用串号
OutputPageNumofRef(PageNumofRef,PageNumRefCount);
for(i = 0; i < PageNumRefCount; i++)
{
if(!PageInBlockofMemory(PageNumofRef[i], BlockofMemory, BlockCount)) //页不在内存中
{
MissCount++;
if(EmptyBlockCount > 0)
{
BlockofMemory[BlockCount - EmptyBlockCount] = PageNumofRef[i];
OutputBlockofMemory(BlockofMemory, BlockCount, -1, PageNumofRef[i]);
EmptyBlockCount--;
}
else
{
ReplacePage = BlockofMemory[ReplaceIndex];
BlockofMemory[ReplaceIndex] = PageNumofRef[i];
ReplaceIndex = (ReplaceIndex + 1) % BlockCount;
OutputBlockofMemory(BlockofMemory, BlockCount, ReplacePage, PageNumofRef[i]);
}
}
else
OutputBlockofMemory(BlockofMemory, BlockCount, -1, PageNumofRef[i]);
}
printf("缺页次数为:%d\n", MissCount);
printf("FIFO缺页率为:%.3f\n", (float)MissCount / PageNumRefCount);
}
//最近最久未使用页面置换算法
void Lru(int *BlockofMemory, int *PageNumofRef, int BlockCount, int PageNumRefCount)
{
int i;
int MissCount = 0;
printf("************最近最久未使用页面置换算法:************\n");
//输出页面引用串号
OutputPageNumofRef(PageNumofRef, PageNumRefCount);
for(i = 0; i < PageNumRefCount; i++)
{
if(!PageInBlockofMemory(PageNumofRef[i], BlockofMemory, BlockCount)) //页不在内存中
{
MissCount++;
//
// 在此添加代码,实现 LRU 页面置换算法
//
}
else
OutputBlockofMemory(BlockofMemory, BlockCount, -1, PageNumofRef[i]);
}
printf("缺页次数为:%d\n", MissCount);
printf("LRU缺页率为:%.3f\n", (float)MissCount / PageNumRefCount);
}
#ifndef CONSOLE_H_
#define CONSOLE_H_
#include <stdio.h>
#include <stdlib.h>
int OutputBlockofMemory(int *BlockofMemory, int BlockCount, int ReplacePage, int PageNum);
void OutputPageNumofRef(int* PageNumofRef, int PageNumRefCount);
void ResetBlockofMemory(int *BlockofMemory, int BlockCount);
int PageInBlockofMemory(int PageNum, int *BlockofMemory, int BlockCount);
int DistanceOpt(int *BlockofMemory, int *PageNumofRef, int j, int i, int PageNumRefCount);
void Opt(int *BlockofMemory, int *PageNumofRef, int BlockCount, int PageNumRefCount);
void Fifo(int *BlockofMemory,int *PageNumofRef,int BlockCount,int PageNumRefCount);
void Lru(int *BlockofMemory, int *PageNumofRef, int BlockCount, int PageNumRefCount);
#endif /* CONSOLE_H_ */
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论