码迷,mamicode.com
首页 > 其他好文 > 详细

存储器管理

时间:2018-07-08 10:36:18      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:else   独立   break   malloc   大小   设计   amp   str   输入   

实验目的:

 

本实验的目的是,使学生熟悉存储器管理系统的设计方法,加深对所学各种存储器管理方案的了解。

 

实验要求:

要求能独立地编写一个常用的存储分配算法,并能设计一个存储管理的模拟系统并调试运行。

对存储算法进行分析比较,评测其性能优劣,加深理解。

 

实验内容:

 

采用固定分区、最佳适应BF算法或首次适应算法,编写存储器管理模拟系统。

可用任何一种高级语言编写。

基本要求: (1)掌握动态内存分配过程alloc()和回收过程free();(2)在给定初始状态下,采用一种算法进行内存块的分配和回收,要求每次分配和回收后显示空闲内存分区的情况。

 

注:算法可任选、数据结构可任选。

 

#include<stdio.h>
#include<stdlib.h>
typedef struct freearea{
int address;
int size;
struct freearea *front;
struct freearea *next;
}*FreeLink,FreeNode;
typedef struct Mat{
int address;
int size;
struct Mat *front;
struct Mat *next;
}*MatLink,Matnode;
MatLink mat,Mathead,Matend;
FreeLink freelink,freehead,freeend;
int total;
int jobcount=0;


void insert();
void deletejob();
void shuchu1();
void shuchu2();
void initfreearea(){
freelink=(FreeLink)malloc(sizeof(FreeNode));

freelink->address=2000;
freelink->size=5000;
total=freelink->size;
freelink->front=NULL;
freelink->next=NULL;
freehead=freelink;
freeend=freelink;
}

void founction(){
printf("\n1.插入作业 2.删除作业 3.查看作业 4.查看空闲内存\n");
int n;
printf("请输入(1-4):\n");
scanf("%d",&n);
if(n<1||n>4){
printf("请重新输入(1-4):\n");
scanf("%d",&n);
}
switch(n){
case 1 :insert();break;
case 2 :deletejob();break;
case 3 :shuchu2();break;
case 4 :shuchu1();break;
}
}
void insert(){
int n;
MatLink p;
FreeLink fh,fn,h;

h=freehead;



printf("请输入分配内存的大小:\n");
scanf("%d",&n);
if(n>total){
printf("内存只剩下%d,请输入分配内存的大小:\n",total);
scanf("%d",&n);
}

if(jobcount==0){
Mathead=(MatLink)malloc(sizeof(Matnode));
while(h!=NULL){

if(freehead->size>=n){

Mathead->address=freehead->address;
printf("\n-----------%d\n",freehead->address);
freehead->address=freehead->address+n;
freehead->size=freehead->size-n;
if(freehead->size==0){
h=h->next;
freehead=h;
}
break;
}else if(freeend->size>=n&&freeend->front->size<n){
Mathead->address=freeend->address;
freeend->address=freeend->address+n;
freeend->size=freeend->address-n;
if(freeend->size==0){
freeend=freeend->front;
}
break;
}else{
while(h!=freeend){
if(h->size<=n&&h->next->size>n){

Mathead->address=h->address;
h->address=h->address+n;
h->size=h->size-n;
if(h->size==0){
fn=h->next;
fh=h->front;
fh->next=fn;
fn->front=fh;
}
break;
}else{
h=h->next;
}
}
}


}

Mathead->size=n;
Mathead->front=NULL;
Mathead->next=NULL;
Matend=Mathead;
jobcount++;
total=total-n;


}else{
p=(MatLink)malloc(sizeof(Matnode));
printf("\n-----------%d--%d\n",freehead->size,n);
while(h!=NULL){
if(freehead->size>=n){
printf("\n---1--------\n");
p->address=freehead->address;
freehead->address=freehead->address+n;
freehead->size=freehead->size-n;
if(freehead->size==0){
h=h->next;
freehead=h;
}
break;
}else if(freeend->size>=n&&freeend->front->size<n){
p->address=freeend->address;
freeend->address=freeend->address+n;
freeend->size=freeend->address-n;
if(freeend->size==0){
freeend=freeend->front;}
break;
}else{
while(h!=freeend){
if(h->size<=n&&h->next->size>n){

p->address=h->address;
h->address=h->address+n;
h->size=h->size-n;
if(h->size==0){
fn=h->next;
fh=h->front;
fh->next=fn;
fn->front=fh;
}
break;
}else{
h=h->next;
}
}
}


}
p->size=n;
p->front=Matend;
Matend->next=p;
p->next=NULL;
Matend=p;

jobcount++;


total=total-n;
}
shuchu1();
shuchu2();
}
void shuchu1(){
FreeLink p;
p=freehead;
while(p!=NULL)
{
printf("空闲内存首地址:%d\t空闲内存大小:%d\n",p->address,p->size);
p=p->next;
}

}
void shuchu2(){
MatLink p;
p=Mathead;
while(p!=NULL)
{
printf("内存首地址:%d\t内存大小:%d\n",p->address,p->size);
p=p->next;
}
}
void deletejob(){
int n,i;
MatLink p,q,t,z;
FreeLink h,f,y;
p=Mathead;
printf("请输入删除作业的编号(0-%d):\n",jobcount-1);
scanf("%d",&n);
if(n>jobcount||n<0){
printf("内存无此作业\n");
printf("请重新输入删除作业的编号(0-%d):\n",jobcount-1);
scanf("%d",&n);
}
if(jobcount==1){
t=Mathead;
Mathead=NULL;
}else{
if(n==0){
t=Mathead;
p=p->next;
p->front=NULL;

Mathead=p;
}else if(n==jobcount-1){
t=Matend;
q=Matend->front;
q->next=NULL;
Matend=p;
}else{
for(i=0;i<jobcount-1;i++){
if(i==n){

z=p->next;
q=p->front;
q->next=z;
z->front=q;

t=p;

break;
}else{
p=p->next;
}
}
}

}//*

jobcount-=1;
total=total+t->size;
h=freehead;
f=(FreeLink)malloc(sizeof(FreeNode));
f->address=t->address;
f->size=t->size;
printf("\n--1---%d %d\n",t->address,t->size);
if(f->address<freehead->address){
freehead->front=f;
f->next=freehead;
f->front=NULL;
freehead=f;
printf("\n---...--\n");

}else if(f->address>freeend->address){
freeend->next=f;
f->front=freeend;
f->next=NULL;
freeend=f;

}else{
while(h!=freeend){
if(f->address>h->address&&f->address<h->next->address){
y=h->next;
h->next=f;
f->front=h;
y->front=f;
f->next=y;

break;
}else{

h=h->next;

}

}
}

shuchu1();
shuchu2();
}
int main(){
initfreearea();
while(1){
founction();
}



return 0;
}

存储器管理

标签:else   独立   break   malloc   大小   设计   amp   str   输入   

原文地址:https://www.cnblogs.com/huifeidezhuzai/p/9278983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!