操作系统实验 内存管理文档格式.docx

上传人:b****2 文档编号:3043327 上传时间:2023-05-01 格式:DOCX 页数:14 大小:202.12KB
下载 相关 举报
操作系统实验 内存管理文档格式.docx_第1页
第1页 / 共14页
操作系统实验 内存管理文档格式.docx_第2页
第2页 / 共14页
操作系统实验 内存管理文档格式.docx_第3页
第3页 / 共14页
操作系统实验 内存管理文档格式.docx_第4页
第4页 / 共14页
操作系统实验 内存管理文档格式.docx_第5页
第5页 / 共14页
操作系统实验 内存管理文档格式.docx_第6页
第6页 / 共14页
操作系统实验 内存管理文档格式.docx_第7页
第7页 / 共14页
操作系统实验 内存管理文档格式.docx_第8页
第8页 / 共14页
操作系统实验 内存管理文档格式.docx_第9页
第9页 / 共14页
操作系统实验 内存管理文档格式.docx_第10页
第10页 / 共14页
操作系统实验 内存管理文档格式.docx_第11页
第11页 / 共14页
操作系统实验 内存管理文档格式.docx_第12页
第12页 / 共14页
操作系统实验 内存管理文档格式.docx_第13页
第13页 / 共14页
操作系统实验 内存管理文档格式.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

操作系统实验 内存管理文档格式.docx

《操作系统实验 内存管理文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统实验 内存管理文档格式.docx(14页珍藏版)》请在冰点文库上搜索。

操作系统实验 内存管理文档格式.docx

主要代码:

publicclassProcess{

privateStringname;

privateintsize;

privateintbeginPx;

privateintendPx;

publicintgetBeginPx(){

returnbeginPx;

}

publicvoidsetBeginPx(intbeginPx){

this.beginPx=beginPx;

publicintgetEndPx(){

returnendPx;

publicvoidsetEndPx(intendPx){

this.endPx=endPx;

publicStringgetName(){

returnname;

publicvoidsetName(Stringname){

this.name=name;

publicintgetSize(){

returnsize;

publicvoidsetSize(intsize){

this.size=size;

}

2.根据用户输入而分配内存的大小,若输入的大小大于目前可分配内存的大小则拒绝分配操作,否则增加一个新进程入链表中,并在已分配表中增加进程的名字,更新剩余内存大小。

分配内存流程图:

绘画paint()方法的主要代码:

publicvoidpaint(Graphicsg){

super.paint(g);

//刷新页面

Colorc=g.getColor();

g.setColor(Color.WHITE);

g.fillRect(20,400,450,50);

//若已有分配内存

if(myList.size()!

=0){

g.setColor(Color.BLACK);

//从已分配的内存中循环获取像素起点位置和像素的大小

for(inti=0;

i<

myList.size();

i++){

g.fillRect(myList.get(i).getBeginPx(),400,myList.get(i).getEndPx()-myList.get(i).getBeginPx(),50);

g.setColor(c);

}

}

显示已分配进程名字的showListName()方法主要代码:

publicvoidshowListName(ArrayList<

Process>

myList){

ProcessnewProcess=null;

showList.removeAll();

for(inti=0;

newProcess=myList.get(i);

showList.add(newProcess.getName());

}

分配内存的监视器主要代码:

publicclassAssignMemoryListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

//当输入分配内存大小不完整时提示

if((mbField.getText().equals("

"

))||(kbField.getText().equals("

))||(byteField.getText().equals("

))){

JOptionPane.showMessageDialog(null,"

请完整输入所需分配内存的大小"

"

警告对话框"

JOptionPane.WARNING_MESSAGE);

else{

//当内存不足时

if(Integer.parseInt(mbField.getText())*1024*1024+Integer.parseInt(kbField.getText())*1024+Integer.parseInt(byteField.getText())>

(m*1024*1024+k*1024+b)){

JOptionPane.showMessageDialog(null,"

所剩内存不足"

else{

Stringname=JOptionPane.showInputDialog(null,"

输入进程名字"

输入对话框"

JOptionPane.PLAIN_MESSAGE);

//获得分配内存名字

ProcessnewProcess=newProcess();

newProcess.setName(name);

newProcess.setSize(Integer.parseInt(mbField.getText())*1024*1024+Integer.parseInt(kbField.getText())*1024+Integer.parseInt(byteField.getText()));

size=newProcess.getSize();

fsize=size;

//把size转成float类型计算,小数计算必须要化成整形

px=(int)(450/p*fsize);

//获得像素的长度

newProcess.setBeginPx(index);

newProcess.setEndPx(index+px);

index=index+px;

myList.add(newProcess);

//把进程添加到链表中

//绘图

repaint();

showListName(myList);

//显示所用进程的名字

//三个输入框清空

mbField.setText("

);

kbField.setText("

byteField.setText("

//更改剩余内存值

intleftSize=(m*1024*1024+k*1024+b)-size;

m=leftSize/(1024*1024);

k=leftSize/1024%1024;

b=leftSize%1024;

notice2.setText("

可分配最大内存:

+m+"

MB"

+k+"

KB"

+b+"

Byte"

输入要分配的内存大小:

图1分配内存

输入名字:

图2输入名字

分配两个内存:

图3分配两个内存

3.用户选择已分配的内存名字而回收相应的内存模块:

回收流程图:

回收内存主要代码:

publicclassCollectMemoryListenerimplementsActionListener{

publicvoidactionPerformed(ActionEventarg0){

intnum=showList.getSelectedIndex();

//获得被选中的序号

//更新剩余内存值

intreleaseSize=myList.get(num).getSize();

m=m+releaseSize/(1024*1024);

k=k+releaseSize/1024%1024;

b=b+releaseSize%1024;

notice2.setText("

//从链表中删去该进程

myList.remove(num);

showListName(myList);

repaint();

回收第一个内存后,绘图更新,剩余内存更新:

图4回收第一个已分配的内存

重新整合内存主要代码:

publicclassRefreshMemoryListenerimplementsActionListener{

index=20;

size=myList.get(i).getSize();

//把size转成float类型计算

myList.get(i).setBeginPx(index);

myList.get(i).setEndPx(index+px);

repaint();

图5重新整合内存

四、实验总结

通过本次实验,加深了对内存管理的理解。

并掌握了单链表应用的重要方法。

运用java的GUI为客户端整体布局,并且运用paint()方法来表示内存分配的状态。

一个已分配的内存表示一个对象,所有对象都存放在链表中,当回收内存时则把改对象在链表中移除。

每次绘图都是根据链表中存在的对象而绘画。

由于对象中存放有起点的像素和终点的像素,因而实现起来简单。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2