内存回收有四种情况:
实例:某系统内存容量为800K,下 面分别给出中的空闲分区表和分配分区表,系统采用动
态分区存储管理策略。现有按照如下顺序释放作业空间:
(1)释放作业:Task2[情况D];
(2)释放作业:Task4 [情况A];
(3)释放作业:Task6[情况B];
(4)释放作业:Task7[情况C];
(5)释 放作业:Task9 [不存在]
并打印每次回收分配空间后的空闲分区表和分配分区表。
分配分区表:
空闲分区表:
1.动态地任意顺序输入“分配分区表项”,按照地址递增方式建立“分配分区表”:
输入:
2. 动态地任意顺序输入“空闲分区表项"”,按照地址递增方式建立“空闲分区表”
输入:
3.共内存回收5次:
输入:
4.回收内存分配区,模拟第一次内存回收:TASK2 【情况D】
输入:
分配成功,输出:
回收内存分配区,模拟第二次内存回收: Task4 , 【情况A】。
输入:
分配成功,输出:
回收内存分配区,模拟第三次内存回收: Task6 [ 情况B]。
输入:
分配失败,输出:
回收内存分配区,模拟第四次内存回收: Task7 , 【情况C】。
输入:
分配成功,输出:
回收内存分配区,模拟第五次内存回收: Task9[ 不存在 ]。
输入:
分配成功,输出:
实现代码 :
#include <malloc.h> #include <stdio.h> #include <string.h> #define NULL 0 typedef struct table { int address; /*存储分区起始地址*/ int length; /*存储分区长度*/ int flag; /*存储分区标志,0 为空闲,1 为被作业占据*/ char name[10]; /*当 flag==1 时存储分区占用标志作业名,否则存储空 nil*/ struct table *next; } node; node *work; /*设置一个全局变量 work:定位需要释放的结点*/ char type; /*设置一个全局变量 type:标注回收类型*/ bool success; /*设置一个全局变量 success:标注回收结点是否在分配分区表中*/ void insert(node *head, node *p) /*按照“地址递增方式”将 p 结点插入链表相应位置*/ { node *q,*w; if(head->next->address>p->address) { p->next=head->next; head->next=p; return; } q=head->next->next; w=head->next; while(q) { if(q->address>p->address) break; w=q; q=q->next; } w->next=p; p->next=q; return; } node *creat() /*根据地址递增方式建立分配分区表(flag==1)或空闲分区表(flag==0)*/ { printf("address length flag(0 or 1):\n"); node *head,*p; head=NULL; head=(node *)malloc(sizeof(node)); head->next=NULL; int a,b,c; if(head->next==NULL) { p=(node *)malloc(sizeof(node)); scanf("%d%d%d",&a,&b,&c); if(a==0&&b==0) return head; p->address=a; p->length=b; p->flag=c; if(p->flag==1) { printf("\tinput job_name:"); scanf("%s",p->name); } else strcpy(p->name,"nil"); head->next=p; head->next->next=NULL; } while(scanf("%d%d%d",&a,&b,&c)!=EOF) { if(a==0&&b==0&&c==0) break; p=(node *)malloc(sizeof(node)); p->address=a; p->length=b; p->flag=c; if(p->flag==1) { printf("\tinput job_name:"); scanf("%s",p->name); } else strcpy(p->name,"nil"); insert(head,p); } return head; } node *found(node *distributedhead,char workn[10]) /*查找已分配表中要回收的分区位置*/ { success=false; node *p,*q,*w; p=distributedhead->next; q=distributedhead; w=(node *)malloc(sizeof(node)); w=NULL; while(p) { if(strcmp(p->name,workn)==0) break; q=p; p=p->next; } if(p) { success=true; w=p; w->flag=0; strcpy(w->name,"nil"); q->next=p->next; } return w; } void release(node *freehead,node *work) /*分四种情况完成空闲分区回收过程*/ { if(freehead->next->address>(work->address+work->length)) { printf("\nThe type of release is D!\n"); work->next=freehead->next; freehead->next=work; return; } else if(freehead->next->address==(work->address+work->length)) { printf("\nThe type of release is A!\n"); freehead->next->address=work->address; freehead->next->length+=work->length; return; } node *p; p=freehead->next; while(p) { if(p->next&&work->address==(p->address+p->length)&&(work->address+work->length)<p->next->address) { printf("\nThe type of release is A!\n"); p->length+=work->length; return; } if(p->next&&work->address==(p->address+p->length)&&(work->address+work->length)==p->next->address) { printf("\nThe type of release is C!\n"); p->length+=p->next->length+work->length; p->next=p->next->next; return; } if(p->next&&work->address>(p->address+p->length)&&(work->address+work->length)==p->next->address) { printf("\nThe type of release is B!\n"); p->next->address-=work->length; p->next->length+=work->length; return; } if(p->next&&work->address>(p->address+p->length)&&(work->address+work->length)<p->next->address) { printf("\nThe type of release is D!\n"); work->next=p->next; p->next=work; return; } p=p->next; } work->next=(node *)malloc(sizeof(node)); work->next=NULL; p=work; return; } void print (node *head) /*输出链表*/ { node *p; p=head->next; while(p) { printf("%d,%d,%d,%s\n",p->address,p->length,p->flag,p->name); p=p->next; } } int main() { int i,sum; struct table *dtable,*ftable; char workn[10]; printf("The distributed table is:\n"); dtable=creat(); /*dtable 输入已分配情况表*/ printf("The free table is:\n"); ftable=creat(); /*ftable 输入未分配情况表*/ /*以下模拟逐个内存回收过程*/ printf("Input the released work segment sum:"); scanf("%d",&sum); i=1; while(sum--) { printf("%d: input the released work segment name:",i++); scanf("%s",workn); work=(node *)malloc(sizeof(node)); work=found(dtable,workn); if(success) { printf("\n要回收的分区存在!\n"); release(ftable,work); printf("\ndistributed table is:\n"); print(dtable); printf("\nfree table is:\n"); print(ftable); } else { printf("\n要回收的分区不存在!\n"); } } return 0; }