- 实验目的
掌握Linux操作系统的内存分配与使用的编程接口;
掌握Linux操作系统中进程虚拟内存的映射;
通过本次实验体会Linux操作系统中内存的分配模式;
- 实验内容
学习Linux系统的内存动态申请、释放的函数;
学习Linux操作系统提供的进程地址映射的工具;
观察进程用户空间的虚存管理变化,包括堆区、文件映射区和栈区;
实验报告要求:
- 写出/proc/$pid/maps数据各字段含义;
- 运行代码,观察进程空间变化(/proc/$pid/maps)过程; 截图简要说明进程空间变化的内容,截图画出变量所在内存段;
- 编写程序,连续申请分配六个128MB空间(记为1~6号),然后释放第2、3、5号的128MB空间。然后再分配1024MB,记录该进程的虚存空间变化(/proc/$pid/maps),每次操作前后检查/proc/$pid/status文件中关于内存的情况,简要说明虚拟内存变化情况。推测此时再分配64M内存将出现在什么位置,实测后是否和你的预测一致?解释说明用户进程空间分配属于课本中的离散还是连续分配算法?首次适应还是最佳适应算法?
- 实验指导
- 虚存空间的管理
进程空间是一个虚存空间,被分割成两大区域——内核空间和用户空间。多个进程的虚存空间并存于系统中。
按分配内存空间的方式不同,一个程序所使用的内存区域可以分为静态内存与动态内存。在程序开始运行时有系统分配的内存称为静态内存,在程序运行过程中由用户自己申请分配的内存称为动态内存。
静态内存是由编译器来分配的。动态内存是由用户根据需要随时申请,使用完毕后手动释放。动态内存的使用存在着巨大的隐患,使用完毕后要释放所占用的内存空间,否则会导致内存泄漏。
- 进程映像

用户可以通过/proc/cpuinfo查看物理和虚拟地址空间大小。
栈:存放局部变量、函数参数,维护函数调用上下文。
堆:应用程序动态分配的内存区域,例如:malloc,calloc申请的内存来自堆。
bss段:未初始化全局变量,未初始化全局静态变量
data 数据段:已初始化全局变量、已初始化全局静态变量、局部静态变量。数据段属于静态内存分配(静态存储区),可读可写。
text 代码段:可执行代码、字符串常量,代码段通常属于只读
- 动态内存函数
- 申请内存
#include <stdlib.h>
void *malloc(size_t size);
函数malloc的参数size表示申请分配的内存空间的大小,以字节为单位。
调用成功时,函数malloc的返回值都为被分配的内存空间的指针;调用失败时,返回值为NULL。
-
- 释放内存
#include <stdlib.h>
void free(void *ptr);
此函数的作用是释放由函数malloc分配的动态内存。参数ptr是指向要释放的动态内存的指针。
注意:当动态内存被释放后,原来指向它的指针就会变为悬空指针。
代码示例:
mainProg.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "mainProg.h"
#define FRAME_SIZE (64*1024)
#define STRING_SIZE 100
int bss_var;
int global_data=1;
void StackLocation(int stack_var0, int stack_var1)
{
print_format();
printf("Stack Location\n");
printf("\tThe address of Stack_var0 : %p\n",&stack_var0);
printf("\tThe address of Stack_var1:%p\n",&stack_var1);
}
void DataSegmentLocation()
{
print_format();
printf("BSS Location:\n");
printf("\tAddress of bss_var:%p\n",&bss_var);
printf("Data Location:\n");
printf("\tAddress of global data(Data Segment):%p\n",&global_data);
static int static_data=4;
printf("\tNew end of static data(Data Segment):%p\n",&static_data);
getchar();
}
void TextLocation(int (*ptr)(int ,char**))
{
print_format();
printf("Code Location:\n");
printf("\tAddress of main (Code Segment):%p \n",ptr);
print_format();
}
void HeapLocation()
{
print_format();
printf("Heap Location:\n");
char *heap_var1=(char*)malloc(60*1024*1024);
char *heap_var2 = (char*)malloc(60*1024*1024);
printf("\tAddress of heap_var1 is %p\n",heap_var1);
printf("\tAddress of heap_var2 is %p\n",heap_var2);
printf("Please check /proc/PID/maps file to check the change,press Enter to continue!\n");
getchar();
free(heap_var1);
free(heap_var2);
printf("Please check /proc/PID/maps after Free heap to check the change,press Enter to continue!\n");
getchar();
}
void print_format()
{
printf("====================================================\n");
}
void file_map()
{
int fd;
void *start_addr;
struct stat sb;
char modify_info[STRING_SIZE]="Modify map-file content!";
char fileName[]="map-file.txt";
fd=open(fileName,O_RDWR|O_CREAT,0644);
write(fd,modify_info,STRING_SIZE);
fstat(fd,&sb);
start_addr = mmap(NULL,sb.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(start_addr==MAP_FAILED)
{
printf("mmap file failed\n");
return;
}
print_format();
printf("File Map Location!\n");
printf("\tMapped area stared by addr:%p:\n\tThe original info of file are as below:",start_addr);
fputs((char*)start_addr,stdout);
printf("\tPlease check /proc/PID/maps file to check the map \n");
getchar();
strcpy(start_addr,modify_info);
munmap(start_addr,sb.st_size);
close(fd);
print_format();
getchar(); //used to check maps, smaps
}
int main(int argc, char **argv)
{
printf("Process ID:%d\n",getpid());
printf("Below are address of types of process's mem\n");
char fileName[STRING_SIZE];
TextLocation(main);
StackLocation(2,3);
DataSegmentLocation();
HeapLocation();
file_map();
}
mainProg.h:
void print_format();
void HeapLocation();
void TextLocation(int (*ptr)(int ,char**));
void DataSegmentLocation();
void file_map();
void StackLocation(int stack_var0, int stack_var1) ;
970




被折叠的 条评论
为什么被折叠?



