操作系统实验报告
深圳旅游必去十大景点
院别:XXXXXX
班级:XXXXXX
学号:XXXXXX
姓名:稻草人
实验题目:内存管理实验
一、实验目的
1、通过本次试验体会操作系统中内存的分配模式;
2、掌握内存分配的方法(FF,BF,WF);
3、学会进程的建立,当一个进程被终止时内存是如何处理被释放块,并当内存不满足进程申请时是如何使用内存紧凑;
4、掌握内存回收过程及实现方法;
5、学会进行内存的申请释放和管理;
二、实验内容
附源代码:
/*宏定义*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#define PROCESS_NAME_LEN 32 /*进程名称的最大长度*/
#define MIN_SLICE 10 /*最小碎片的大小*/
#define DEFAULT_MEM_SIZE 1024 /*默认内存的大小*/
#define DEFAULT_MEM_START 0 /*默认内存的起始位置*/
/* 内存分配算法 */
#define MA_FF 1
#define MA_BF 2
#define MA_WF 3
int mem_size=DEFAULT_MEM_SIZE; /*内存大小*/
int ma_algorithm = MA_FF; /*当前分配算法*/
int flag = 0; /*设置内存大小标志*/
static int pid = 0; /*初始pid*/
int algorithm;
/*描述每一个空闲块的数据结构*/
struct free_block_type{
int size;
int start_addr;
struct free_block_type *next;
};
/*指向内存中空闲块链表的首指针*/
struct free_block_type *free_block;
/*每个进程分配到的内存块的描述*/
struct allocated_block{
int pid;
int size;
int start_addr;
char process_name[PROCESS_NAME_LEN];
跨越障碍 struct allocated_block *next;
};
/*进程分配内存块链表的首指针*/
经学家struct allocated_block *allocated_block_head = NULL;
struct allocated_block *find_process(int id)
{
struct allocated_block *p;
p=allocated_block_head;
while(p!=NULL)
{
if (p->pid==id)
return p;
}
return NULL;
}
void swap(int *p,int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
return;
}
void do_exit()
{
exit(0);
}
/*初始化空闲块,默认为一块,可以指定大小及起始地址*/
struct free_block_type* init_free_block(int mem_size){
struct free_block_type *fb;
fb=(struct free_block_type *)malloc(sizeof(struct free_block_type));
if(fb==NULL){
printf("No mem\n");
return NULL;
}
fb->size = mem_size;
fb->start_addr = DEFAULT_MEM_START;
fb->next = NULL;
return fb;
}
/*显示菜单*/
display_menu(){
printf("\n");
printf("1 - Set memory size (default=%d)\n", DEFAULT_MEM_SIZE);
printf("2 - Select memory allocation algorithm\n");
printf("3 - New process \n");
printf("4 - Terminate a process \n");
printf("5 - Display memory usage \n");
printf("0 - Exit\n");
}
/*设置内存的大小*/
t_mem_size(){
int size;
高铁身份证没带怎么办 if(flag!=0){ //防止重复设置
printf("Cannot t memory size again\n");
return 0;
}
printf("Total memory size =");
scanf("%d", &size);
if(size>0) {
mem_size = size;
free_block->size = mem_size;
}
flag=1; return 1;
}
/*按FF算法重新整理内存空闲块链表*/
rearrange_FF(){
struct free_block_type *tmp, *work;
printf("Rearrange free blocks for FF \n");
tmp = free_block;
while(tmp!=NULL)
{ work = tmp->next;
while(work!=NULL){
if ( work->start_addr < tmp->start_addr)
{ /*地址递增*/
swap(&work->start_addr, &tmp->start_addr);
swap(&work->size, &tmp->size);
}
work=work->next;
}
tmp = tmp -> next;
}
}
/*按BF最佳适应算法重新整理内存空闲块链表*/
rearrange_BF(){
struct free_block_type *tmp, *work;
printf("Rearrange free blocks for BF \n");
tmp = free_block;
while(tmp!=NULL)
{ work = tmp->next;
while(work!=NULL){
if ( work->size > tmp->size) { /*地址递增*/
swap(&work->start_addr, &tmp->start_addr);
swap(&work->size, &tmp->size);
}
work=work->next;
}
tmp = tmp -> next;
}
}
/*按WF算法重新整理内存空闲块链表*/
rearrange_WF(){
struct free_block_type *tmp, *work;红楼梦主要人物
printf("Rearrange free blocks for WF \n");
tmp = free_block;
while(tmp!=NULL)
{ work = tmp->next;
while(work!=NULL){
if ( work->size < tmp->size) { /*地址递增*/
swap(&work->start_addr, &tmp->start_addr);
swap(&work->size, &tmp->size);
}
el
大学社会实践 work=work->next;
}
tmp = tmp -> next;
}电子驾照怎么领取
}
/*按指定的算法整理内存空闲块链表*/
rearrange(int algorithm){
维生素e涂脸正确用法
switch(algorithm){
ca MA_FF: rearrange_FF(); break;
ca MA_BF: rearrange_BF(); break;
ca MA_WF: rearrange_WF(); break;
}
}
/* 设置当前的分配算法 */
t_algorithm(){
printf("\t1 - First Fit\n");
printf("\t2 - Best Fit \n");