操作系统实验四_第1页
操作系统实验四_第2页
操作系统实验四_第3页
操作系统实验四_第4页
操作系统实验四_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上学号E 专业计算机科学与技术 姓名施飞宇实验日期2018/11/05 教师签字 成绩实验报告【实验名称】 实验四 主存空间的分配与回收(一)【实验目的】理解在连续分区动态的存储管理方式下,如何实现主存空间的分配与回收。【实验原理】1. 首次适应算法实现主存空间的分配与回收;2. 循环首次适应算法实现主存空间的分配与回收;【实验内容】1. 采用可变式分区管理,使用首次适应算法实现主存空间的分配与回收数据结构:typedef struct pcbchar pnameN;/进程名|分区号 int begin;/起始地址 int end;/结束地址 int length;/进

2、程长度|分区大小char state;/进程状态|分区状态 struct pcb *next;/链表指针PCB;分区和进程同样的数据结构,其内容用链表进行存储。state=y表示已分配内存,state=n表示未进行内存分配。算法流程图:实验代码:/*2018/11/15施飞宇 笃行南楼a202*/#define N 20#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;typedef struct pcbchar pnameN;/进程名|分区号 int be

3、gin;/起始地址 int end;/结束地址 int length;/长度|分区大小char state;/进程状态|分区状态 struct pcb *next;/链表指针PCB;PCB headinput;/进程链表PCB headarea;/分区链表char number='a'void input_process();/建立进程函数void create_area();/创建分区大小void assign(PCB *Q,PCB *p);/分配内存void colect(PCB *Q,PCB *p);/回收内存void runprocess();/运行进程函数void p

4、rintp(PCB *p);/输出进程函数void printa(PCB *Q);/输出分区函数void input_process() cout<<"*创建进程*"<<endl;PCB *p1,*p2;cout<<"输入进程数目"<<endl;int num;cin>>num;int i=0;p1=&headinput;p2=p1;for(i=0;i<num;i+)cout<<"输入第"<<i+1<<"进程名,进程

5、大小"<<endl; scanf("%s",p1->pname);cin>>p1->length;p1->begin=p1->end=0;p1->state='n'/n代表未被分配内存p1->next=new PCB;p2=p1;p1=p1->next;delete p1;p1=NULL;p2->next=NULL;void create_area()/创建分区大小PCB *Q1;cout<<"*创建分区*"<<endl;Q1=&am

6、p;headarea;cin>>Q1->pname;Q1->begin=0;Q1->length=20;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB;Q1=Q1->next;cin>>Q1->pname;Q1->begin=20;Q1->length=30;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new P

7、CB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=50;Q1->length=40;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=90;Q1->length=50;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->

8、next=new PCB; Q1=Q1->next; cin>>Q1->pname;Q1->begin=140;Q1->length=60;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=NULL; Q1=Q1->next;void assign(PCB *Q,PCB *p)/分配内存 PCB *Q1=Q; PCB *Q2; while(Q1) if(Q1->length>=p->length&&Q1->state

9、!='y') p->begin=Q1->begin; p->end=p->begin+p->length; p->state='y' Q1->state='y' if(Q1->length!=p->length) Q2=new PCB; Q2->next=Q1->next; Q1->next=Q2; Q2->begin=p->end; Q2->end=Q1->end; Q1->end=Q2->begin; Q2->state='

10、;n' Q2->length=Q2->end-Q2->begin; Q1->length=p->length; Q2->pname0=char(+number); Q2->pname1='0' return; else Q1=Q1->next; cout<<p->pname<<"未被成功分配内存"<<endl;void colect(PCB *Q,PCB *p)/回收内存 PCB *Q1,*Q2,*Q3; Q1=Q;Q2=Q; while(Q1) if(Q1-&

11、gt;begin=p->begin&&Q1->state='y') Q1->state='n' Q1->end=p->end; Q1->length=Q1->end-Q1->begin; if(Q1->next->state='n'&&Q1->next->pname0!='P') Q3=Q1->next; Q1->next=Q3->next; Q1->end=Q3->end; Q1->leng

12、th=Q1->end-Q1->begin; delete Q3; if(Q1->pname0!='P'&&Q2->state='n') Q2->end=Q1->end; Q2->length=Q2->end-Q2->begin; Q2->next=Q1->next; delete Q1; return; Q2=Q1; Q1=Q1->next; void printp(PCB *p)/输出进程函数 cout<<"进程名 "<<&quo

13、t;起始地址 "<<"结束地址 "<<"进程大小"<<" "<<"分配状态(y|n)"<<endl; while(p) cout<<p->pname<<" "<<p->begin<<"k "<<p->end<<"k "<<p->length<<"k "

14、;<<p->state<<endl; p=p->next; void printa(PCB *Q)/输出分区函数 cout<<"分区名 "<<"起始地址 "<<"结束地址 "<<"分区大小"<<" "<<"分配状态(y|n)"<<endl; while(Q) cout<<Q->pname<<" "<&l

15、t;Q->begin<<"k "<<Q->end<<"k "<<Q->length<<"k "<<Q->state<<endl; Q=Q->next; int main() input_process();printp(&headinput); create_area();printa(&headarea); PCB *p1,*Q1; p1=&headinput; Q1=&headarea;

16、 cout<<"*内存分配*"<<endl; while(p1) assign(Q1,p1); printa(&headarea); p1=p1->next; p1=&headinput; cout<<"*内存回收*"<<endl; while(p1) Q1=&headarea; colect(Q1,p1); printa(&headarea); p1=p1->next; return 0;测试数据:进程名 起始地址 结束地址 进程大小 分配状态(y|n)A 0k

17、 0k 2k nB 0k 0k 3k nC 0k 0k 4k nD 0k 0k 13k nE 0k 0k 23k nF 0k 0k 45k n分区名 起始地址 结束地址 分区大小 分配状态(y|n)P0 0k 20k 20k nP1 20k 50k 30k nP2 50k 90k 40k nP3 90k 140k 50k nP4 140k 200k 60k n运行结果:2. 采用可变式分区管理,使用循环首次适应算法实现主存空间的分配与回收数据结构:typedef struct pcbchar pnameN;/进程名|分区号 int begin;/起始地址 int end;/结束地址 int l

18、ength;/进程长度|分区大小char state;/进程状态|分区状态 struct pcb *next;/链表指针PCB;分区和进程同样的数据结构,其内容用链表进行存储。state=y表示已分配内存,state=n表示未进行内存分配。算法流程图:程序代码:/*2018/11/15施飞宇 笃行南楼a202*/#define N 20#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;typedef struct pcbchar pnameN;/进程名|分区

19、号 int begin;/起始地址 int end;/结束地址 int length;/长度|分区大小char state;/进程状态|分区状态 struct pcb *next;/链表指针PCB;PCB headinput;/进程链表PCB headarea;/分区链表char number='a'void input_process();/建立进程函数void create_area();/创建分区大小PCB * assign(PCB *Q,PCB *p);/分配内存void colect(PCB *Q,PCB *p);/回收内存void runprocess();/运行进

20、程函数void printp(PCB *p);/输出进程函数void printa(PCB *Q);/输出分区函数void input_process() cout<<"*创建进程*"<<endl;PCB *p1,*p2;cout<<"输入进程数目"<<endl;int num;cin>>num;int i=0;p1=&headinput;p2=p1;for(i=0;i<num;i+)cout<<"输入第"<<i+1<<&qu

21、ot;进程名,进程大小"<<endl; scanf("%s",p1->pname);cin>>p1->length;p1->begin=p1->end=0;p1->state='n'/n代表未被分配内存p1->next=new PCB;p2=p1;p1=p1->next;delete p1;p1=NULL;p2->next=NULL;void create_area()/创建分区大小PCB *Q1;cout<<"*创建分区*"<<en

22、dl;Q1=&headarea;cin>>Q1->pname;Q1->begin=0;Q1->length=20;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB;Q1=Q1->next;cin>>Q1->pname;Q1->begin=20;Q1->length=30;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->n

23、ext=new PCB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=50;Q1->length=40;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=new PCB; Q1=Q1->next;cin>>Q1->pname;Q1->begin=90;Q1->length=50;Q1->end=Q1->begin+Q1->length;Q1->state='n

24、9;Q1->next=new PCB; Q1=Q1->next; cin>>Q1->pname;Q1->begin=140;Q1->length=60;Q1->end=Q1->begin+Q1->length;Q1->state='n'Q1->next=NULL; Q1=Q1->next;PCB* assign(PCB *Q,PCB *p)/分配内存 PCB *Q1=Q; PCB *Q2; while(Q1) if(Q1->length>=p->length&&Q1-

25、>state!='y') p->begin=Q1->begin; p->end=p->begin+p->length; p->state='y' Q1->state='y' if(Q1->length!=p->length) Q2=new PCB; Q2->next=Q1->next; Q1->next=Q2; Q2->begin=p->end; Q2->end=Q1->end; Q1->end=Q2->begin; Q2->s

26、tate='n' Q2->length=Q2->end-Q2->begin; Q1->length=p->length; Q2->pname0=char(+number); Q2->pname1='0' return Q1; else Q1=Q1->next; cout<<p->pname<<"未被成功分配内存"<<endl;void colect(PCB *Q,PCB *p)/回收内存 PCB *Q1,*Q2,*Q3; Q1=Q;Q2=Q; while

27、(Q1) if(Q1->begin=p->begin&&Q1->state='y') Q1->state='n' Q1->end=p->end; Q1->length=Q1->end-Q1->begin; if(Q1->next->state='n'&&Q1->next->pname0!='P') Q3=Q1->next; Q1->next=Q3->next; Q1->end=Q3->end;

28、 Q1->length=Q1->end-Q1->begin; delete Q3; if(Q1->pname0!='P'&&Q2->state='n') Q2->end=Q1->end; Q2->length=Q2->end-Q2->begin; Q2->next=Q1->next; delete Q1; return; Q2=Q1; Q1=Q1->next; void printp(PCB *p)/输出进程函数 cout<<"进程名 "

29、<<"起始地址 "<<"结束地址 "<<"进程大小"<<" "<<"分配状态(y|n)"<<endl; while(p) cout<<p->pname<<" "<<p->begin<<"k "<<p->end<<"k "<<p->length<<"k "<<p->state<<endl; p=p->next; void printa(PCB *Q)/输出分区函数 cout<<"分区名 "<<"起始地址 "<<"结束地址 "<<"分区大小"<<&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论