📄 2.c
字号:
/*实习二 主存空间的分配和回收
一、实习内容
主存储器空间的分配和回收。
二、实习目的
通过本实习帮助理解在不同的存储管理方式下应怎样贮存空间的分配和回收。
三、实习题目
本实习有两题,可任选一题。
第一题:在可变分区管理方式下采用首次适应算法实现主存分配和回收。
[提示]:
(1) 可变分区方式是按作业需要的主存空间大小来分割分区的。当要装入一个作业时,根据作业需要的主存
容量查看是否有足够的空闲空间,若有,则按需分配,否则,作业无法装入。假定内存大小为128K,
初始状态见右图。空闲区说明表格式为:
起址--指出空闲区的启始地址;
长度--一个连续空闲区的长度;
状态--有两种状态,一种是"未分配"状态,另一种是"空表目"状态。
(2) 采用首次适应算法分配。运行时,输入一系列分配请求和回收请求。
*/
#include <stdio.h>
#include <malloc.h>
struct block{
int start; //起址
int len; //长度
int state; //状态:0为未分配,1为已分配
struct block *next;
};
struct block *create(){
struct block *head,*temp,*present;
temp = (struct block *)malloc(sizeof(struct block));
temp->start = 5;
temp->len = 5;
temp->state = 1;
head = present = temp;
temp = (struct block *)malloc(sizeof(struct block));
temp->start = 10;
temp->len = 4;
temp->state = 1;
present->next = temp;
present = temp;
temp = (struct block *)malloc(sizeof(struct block));
temp->start = 14;
temp->len = 12;
temp->state = 0;
present->next = temp;
present = temp;
temp = (struct block *)malloc(sizeof(struct block));
temp->start = 26;
temp->len = 6;
temp->state = 1;
present->next = temp;
present = temp;
temp = (struct block *)malloc(sizeof(struct block));
temp->start = 32;
temp->len = 96;
temp->state = 0;
temp->next = NULL;
present->next = temp;
return head;
}
void main( void ){
int choice; //用户选择
int cap; //要求分配或者收回的空间大小
struct block *head,*present,*temp;
head = create();
present = head;
while(1){
printf("=========分配和收回请求===========\n");
printf("\t1.分配;\n");
printf("\t2.收回;\n");
printf("\t0.退出。\n");
printf("==================================\n");
printf("Your choice is:");
scanf("%d",&choice);
if(choice==0)
break;
while(choice!=0){
switch(choice){
case 1:
printf("\nPlease input the capability you want to assign:");
scanf("%d",&cap);
while(present!=NULL){
if((present->len>cap)&&(present->state==0)){
temp = (struct block *)malloc(sizeof(struct block));
temp->len = present->len - cap;
temp->state = 0;
temp->start = present->start+cap;
present->len = cap;
present->state = 1;
temp->next = present->next;
present->next = temp;
break;
}
else
present = present->next;
}
if(present->next==NULL)
printf("\nThere are no sufficient capablity for you require!\nPlease try to assign a litter one.\n");
choice = 0;
break;
case 2:
printf("\nPlease input the capablity you want to release:");
scanf("%d",&cap);
temp = head;
present = temp->next;
//判断第一个是否满足条件
if((temp->len==cap)&&(temp->state==1)){
if(temp->next->state==0){
temp->len += cap;
temp->next = temp->next->next;
}
else
temp->state = 0;
choice = 0;
break;
}
while((present!=NULL)&&(present->state==1)){
if(present->len==cap){ //找到满足条件的
if(temp->state==0){ //上面一个为未分配
temp->next = present->next;
temp->len += present->len;
}//if
else{
temp = present->next;
if(temp->state==0){ //下面一个未分配
present->next = temp->next;
present->len += temp->len;
}//if
else
present->state = 0;
}//else
choice = 0;
break;
}//if
else{
temp = temp->next;
present = temp->next;
}//else
}//while
choice = 0;
break;
default:
printf("Input Error!Please try again!\n");
choice = 0;
break;
}
}
}//while(1)
printf("\nThe resent stuation of the Memory portion is:\n");
printf("**************************************************\n");
printf("\tFROM\tTO\tCAPABLITY\tSTATE\n");
while(head!=NULL){
printf("\t%d\t%d\t%d\t\t%s\n",head->start,head->start+head->len,head->len,(head->state==0)?"free":"busy");
head = head->next;
}
}
/*第二题:在分页式管理方式下采用位示图来表示主存分配情况,实现主存分配和回收。
[提示]:
(1) 假定系统的主存被分成大小相等的64个块,用0/1对应空闲/占用。初始位示图如右图。
(2) 当要装入一个作业时,根据作业对主存的需求量,先查空闲块数是否能满足作业要求,若能满足,
则查位示图,修改位示图和空闲块数。位置与块号的对应关系为:
块号=j*8+i,其中i表示位,j表示字节。
根据分配的块号建立页表。页表包括两项:页号和块号。
(3) 回收时,修改位示图和空闲块数。
(4) 要求程序能显示位示图和空闲块数的变化。
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -