实验目的
1、理解操作系统存储管理原理
操作系统的发展使得系统完成了大部分的内存管理工作。对于程序员而言,这些内存管理的过程完全透明不可见。因此,程序员开发时从不关心系统如何为自己分配内存,而且永远认为系统可以分配给程序所需要的内存。在程序开发时,程序员真正需要做的就是:申请内存、使用内存、释放内存,其他一概无需过问。
2、研读Linux 内存管理所用到的文件include/linux/mm.h,主要包括两个数据结构:mem_map、free_area
3、在Linux 下,用malloc()函数实现cat或copy命令。
实验一:
1、定义学号的.c文件
vi B17070306.c
2、编写.c文件
代码如下;
#include <stdio.h>
#include <stdlib.h> //exit函数,实验指导上少了这一个头文件
#include <string.h>
#include <malloc.h>
int main(void)
{
char * str;
if ((str=(char*)malloc(10))==NULL)
{
printf("not enough memory to allocate buffer\\n");
exit(1);
}
strcpy(str,"hello");
printf("string is %s\\n",str);
free(str);
return 0;
}
3、编译运行
gcc B17070306.c
./a.out
4、过程与结果及结果分析
实验一运行结果:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLzAzMxETMzATM1ITMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
结果分析:
函数通过void *malloc(size_t size) 分配指定大小size个字节的内存空间,成功时返回分配内存的指针(即所分配内存的地址)。该内存区域没有清空。
然后通过strcpy(str,"hello");将hello赋值给该内存空间,然后输出并通过void free(void * addr);函数释放由malloc()分配的内存,addr是要释放内存空间的起始地址,并且addr必须是被以前malloc( )调用返回的。
实验二:
1、创建.c文件
vi sanyanjie.c
2、编写.c文件
最初代码:
#include<stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
main()
{
int fd,len;
void *tp;
struct stat ps;
fd=open(“/home/jf03/try”,0);
fstat(fd,&ps);
len=ps.st_size;
tp=malloc(len);
read(fd,tp,len);
printf(“%s\\n”,tp);
close(fd);
}
报错:
原因是return-type:函数有无返回值以及返回值类型不匹配;这是因为c标准里规定main返回值必须为int。具体可参看这篇
故修改为
#include<stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int fd,len;
void *tp;
struct stat ps;
fd=open(“/home/jf03/try”,0);
fstat(fd,&ps);
len=ps.st_size;
tp=malloc(len);
read(fd,tp,len);
printf(“%s\\n”,tp);
close(fd);
return 0;
}
然后有报错
原因是使用open函数使用错误,仔细查看open函数发现其相关性质,具体参考这篇
1. 头文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2. 定义函数:
int open(const char * pathname, int flags);
int open(const char * pathname, int flags, mode_t mode);
参数pathname指向欲打开的文件路径字符串
故再次修改头文件,及其open函数的参数,根据题意,修改为上一文件的目录。
#include<stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/types.h>
#include <fcntl.h>
int main()
{
int fd,len;
void *tp;
struct stat ps;
fd = open("B17070306.c", 0);
根据题意,在打开文件后,通过fstat()获得文件长度,然后通过malloc()系统调用申请响应大小的内存空间,通过read()将文件内容完全读入该内存空间,并显示出来。知道函数有问题,输出应该是长度即len,故将 printf("%s\\n",tp);修改为printf("the length of the file: %d\n", len);
故最终正确的代码是
#include<stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/types.h>
#include <fcntl.h>
int main()
{
int fd,len;
void *tp;
struct stat ps;
fd = open("B17070306.c", 0);
fstat(fd,&ps);
len=ps.st_size;
tp=malloc(len);
read(fd,tp,len);
printf("the length of the file: %d\n", len);
close(fd);
return 0;
}
3、编译运行
gcc sanyanjie.c
./a.out
4、结果与过程
结果分析:
该函数通过fstat()获得文件长度,然后通过malloc()系统调用申请响应大小的内存空间,通过read()将文件内容完全读入该内存空间,并显示出来。
参考文献:
https://www.cnblogs.com/DswCnblog/p/5825872.html
https://www.cnblogs.com/eastnapoleon/p/3309917.html
https://blog.csdn.net/jh0703/article/details/47795397
https://blog.csdn.net/qq_30592303/article/details/102594905