计算机系统结构课程实验2Word下载.docx
《计算机系统结构课程实验2Word下载.docx》由会员分享,可在线阅读,更多相关《计算机系统结构课程实验2Word下载.docx(20页珍藏版)》请在冰豆网上搜索。
即各个算法的具体实现。
最后将数据传送给前端输出。
(3)将主存看成是一个对象,然后建一个实体类MainStore。
在这个类中有主存的大小、页大小、页数、组数、页地址、计数器、计时器等属性。
页地址用于存放输入的页地址流,计数器用于计算命中率,当命中一次,计数器加1。
计时器用于LRU算法。
当命中时,计时器清零。
每一个循环结束,有地址的块计时器加1;
替换时,找到计时器最大的块地址,然后把它替换出去。
(4)一样将要输出的数据封装成一个类,然后进行输出。
六、实验代码
1、前端代码:
Index.jsp代码(用于输入数据):
在本页面中用到jqurey插件的js、css等代码也不粘贴出来了。
<
%@pagelanguage="
java"
contentType="
text/html;
charset=UTF-8"
pageEncoding="
UTF-8"
%>
%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"
:
//"
+request.getServerName()+"
"
+request.getServerPort()+path;
!
DOCTYPEhtmlPUBLIC"
-//W3C//DTDHTML4.01Transitional//EN"
"
http:
//www.w3.org/TR/html4/loose.dtd"
>
html>
head>
<
metahttp-equiv="
Content-Type"
content="
title>
体系结构实验二<
/title>
metaname="
author"
DeathGhost"
/>
linkrel="
stylesheet"
type="
text/css"
href="
css/style.css"
tppabs="
style>
body{height:
100%;
background:
#16a085;
overflow:
hidden;
}
canvas{z-index:
-1;
position:
absolute;
/style>
scriptsrc="
js/jquery.js"
/script>
js/verificationNumbers.js"
js/Particleground.js"
script>
$(document).ready(function(){
//粒子背景特效
$('
body'
).particleground({
dotColor:
'
#5cbdaa'
lineColor:
});
#FIFO'
).click(function(){
varmainSize=$("
#mainSize"
).val();
varpageSize=$("
#pageSize"
vargroupCount=$("
#groupCount"
varaddress=$("
#address"
form1.action="
%=basePath%>
/mainStore.jsp?
_m=FIFO"
;
$("
#form1"
).submit();
})
#LRU'
varMMC=$("
#MMC"
form1.action="
_m=LRU"
/head>
body>
dlclass="
admin_login"
<
dt>
<
strong>
存贮层次模拟器2<
/strong>
em>
Memoryhierarchysimulator<
/em>
/dt>
formid="
form1"
action="
method="
post"
ddclass="
m_icon"
inputtype="
text"
id="
mainSize"
name="
placeholder="
主存大小"
class="
login_txtbx"
/>
/dd>
c_icon"
pageSize"
页大小"
g_icon"
groupCount"
组数"
a_icon"
address"
地址流"
dd>
<
button"
FIFO"
value="
submit_btn"
LRU"
p>
来源:
ahref="
target="
_blank"
徐益<
/a>
/p>
/form>
/dl>
/body>
/html>
Main.jsp代码(用于输出数据):
%@pageimport="
com.xu.entity.OutPut"
java.util.List"
com.xu.entity.MainStore"
%@pagelanguage="
Inserttitlehere<
$(document).ready(function(){
lineColor:
tableborder="
1px"
bordercolor="
#89cff0"
width="
30%"
align="
center"
height="
40%"
valigh="
middle"
style="
color:
#89cff0;
margin-top:
230px;
List<
OutPut>
dataList=(List<
)request.getAttribute("
dataList"
);
floathit=(Float)request.getAttribute("
hit"
%>
tr>
th>
时间流<
/th>
页地址流<
thcolspan=<
%=dataList.get(0).getAddressStream().size()%>
主存里的地址流<
状态<
/tr>
for(inti=0;
i<
dataList.size();
i++){
OutPutoutput=dataList.get(i);
tdalign="
valign="
%=i%>
/td>
%=output.getPageAdress()%>
for(intj=0;
j<
output.getAddressStream().size();
j++){
%>
<
%=output.getAddressStream().get(j)%>
}
%=output.getState()%>
%
}
/table>
h2align="
命中率为:
%=hit%>
/h2>
2、后端代码:
主存实体类代码:
packagecom.xu.entity;
importjava.util.ArrayList;
importjava.util.List;
publicclassMainStore{
privateintmainSize;
//主存大小
privateintpageSize;
//页大小
privateintgroupCount;
//组数
privateintpageCount;
//页数
privateintcount;
//计数器
privateList<
Integer>
pageAddress=newArrayList<
();
//页地址
pageTime=newArrayList<
//计时器
publicintgetMainSize(){
returnmainSize;
}
publicvoidsetMainSize(intmainSize){
this.mainSize=mainSize;
publicintgetPageSize(){
returnpageSize;
publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
publicintgetGroupCount(){
returngroupCount;
publicvoidsetGroupCount(intgroupCount){
this.groupCount=groupCount;
publicintgetCount(){
returncount;
publicvoidsetCount(intcount){
this.count=count;
publicList<
getPageAddress(){
returnpageAddress;
publicvoidsetPageAddress(List<
pageAddress){
this.pageAddress=pageAddress;
getPageTime(){
returnpageTime;
publicvoidsetPageTime(List<
pageTime){
this.pageTime=pageTime;
publicintgetPageCount(){
returnpageCount;
publicvoidsetPageCount(){
this.pageCount=mainSize/pageSize;
}
Myservlt类代码:
packagecom.xu.web;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.xu.entity.MainStore;
importcom.xu.entity.OutPut;
@WebServlet("
/mainStore.jsp"
)
publicclassMyServltextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
Stringmothed=request.getParameter("
_m"
if("
.equals(mothed)){
fifo(request,response);
}elseif("
lru(request,response);
}
/**
*LRU算法
*@paramrequest
*@paramresponse
*/
privatevoidlru(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
intmainSize=Integer.parseInt(request.getParameter("
));
intpageSize=Integer.parseInt(request.getParameter("
intgroupCount=Integer.parseInt(request.getParameter("
Stringaddress=request.getParameter("
Stringstr[]=address.split("
int[]ad=newint[str.length];
for(inti=0;
str.length;
ad[i]=Integer.parseInt(str[i]);
MainStoremainStore=newMainStore();
mainStore.setMainSize(mainSize);
mainStore.setPageSize(pageSize);
mainStore.setGroupCount(groupCount);
mainStore.setPageCount();
intgroupSize=mainStore.getPageCount()/groupCount;
//组大小
mainStore.getPageCount();
mainStore.getPageTime().add(0);
mainStore.getPageAddress().add(-1);
List<
dataList=newArrayList<
ad.length;
OutPutoutPut=newOutPut();
outPut.setPageAdress(ad[i]);
intgroupNumber=funGroupNumber(ad[i],mainStore,groupSize);
booleanflag=false;
for(intj=groupNumber*groupSize;
(groupNumber+1)*groupSize;
j++){
if(mainStore.getPageAddress().get(j)==ad[i]){
System.out.println("
命中"
+ad[i]);
outPut.setState("
mainStore.setCount(mainStore.getCount()+1);
mainStore.getPageTime().set(j,0);
flag=true;
break;
}
if(!
flag){
for(intn=groupNumber*groupSize;
n<
n++){
if(mainStore.getPageAddress().get(n)==-1){
System.out.println("
调进"
mainStore.getPageAddress().set(n,ad[i]);
outPut.setState("
flag=true;
break;
}
if(!
intindex=funMaxTime(groupNumber,groupSize,mainSto