Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
页
页面置换算法
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
项目模板
页面置换算法
提交
e3eec925
提交
e3eec925
7月 19, 2019
创建
作者:
haixia song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
272 行增加
和
0 行删除
+272
-0
.gitignore
.gitignore
+4
-0
PageRA.oslproj
PageRA.oslproj
+33
-0
PageRA.puo
PageRA.puo
+0
-0
console.c
console.c
+218
-0
console.h
console.h
+17
-0
没有找到文件。
.gitignore
0 → 100644
浏览文件 @
e3eec925
/Debug
/Release
*.bak
\ No newline at end of file
PageRA.oslproj
0 → 100644
浏览文件 @
e3eec925
<?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>
PageRA.puo
0 → 100644
浏览文件 @
e3eec925
添加文件
console.c
0 → 100644
浏览文件 @
e3eec925
#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
);
}
console.h
0 → 100644
浏览文件 @
e3eec925
#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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论