mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
feat(mm): 简单实现fat文件系统的文件映射 (#840)
- 添加文件映射相关接口,目前已简单实现fat文件系统的私有映射和共享映射 - 添加msync系统调用(由于当前未实现脏页自动回写,需要手动调用msync进行同步) - 简单实现PageCache(暂时使用HashMap进行文件页号与页的映射) - 添加新的PageFlags标志结构,原PageFlags改名为EntryFlags - 参考linux使用protection_map映射表进行页面标志的获取 - 添加页面回收机制 - 添加页面回收内核线程 - 缺页中断使用的锁修改为irq_save; 添加脏页回写机制 - 修复do_cow_page死锁问题 - 访问非法地址时发送信号终止进程 - 修复重复插入反向vma表的错误 - 添加test_filemap文件映射测试程序
This commit is contained in:
1
user/apps/test_filemap/.gitignore
vendored
Normal file
1
user/apps/test_filemap/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
test_filemap
|
20
user/apps/test_filemap/Makefile
Normal file
20
user/apps/test_filemap/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
ifeq ($(ARCH), x86_64)
|
||||
CROSS_COMPILE=x86_64-linux-musl-
|
||||
else ifeq ($(ARCH), riscv64)
|
||||
CROSS_COMPILE=riscv64-linux-musl-
|
||||
endif
|
||||
|
||||
CC=$(CROSS_COMPILE)gcc
|
||||
|
||||
.PHONY: all
|
||||
all: main.c
|
||||
$(CC) -static -o test_filemap main.c
|
||||
|
||||
.PHONY: install clean
|
||||
install: all
|
||||
mv test_filemap $(DADK_CURRENT_BUILD_DIR)/test_filemap
|
||||
|
||||
clean:
|
||||
rm test_filemap *.o
|
||||
|
||||
fmt:
|
61
user/apps/test_filemap/main.c
Normal file
61
user/apps/test_filemap/main.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// 打开文件
|
||||
int fd = open("example.txt", O_RDWR | O_CREAT | O_TRUNC, 0777);
|
||||
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
write(fd, "HelloWorld!", 11);
|
||||
char buf[12];
|
||||
buf[11] = '\0';
|
||||
close(fd);
|
||||
|
||||
fd = open("example.txt", O_RDWR);
|
||||
read(fd, buf, 11);
|
||||
printf("File content: %s\n", buf);
|
||||
|
||||
// 将文件映射到内存
|
||||
void *map = mmap(NULL, 11, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (map == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
close(fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("mmap address: %p\n", map);
|
||||
|
||||
// 关闭文件描述符
|
||||
// close(fd);
|
||||
|
||||
// 访问和修改文件内容
|
||||
char *fileContent = (char *)map;
|
||||
printf("change 'H' to 'G'\n");
|
||||
fileContent[0] = 'G'; // 修改第一个字符为 'G'
|
||||
printf("mmap content: %s\n", fileContent);
|
||||
|
||||
// 解除映射
|
||||
printf("unmap\n");
|
||||
if (munmap(map, 11) == -1)
|
||||
{
|
||||
perror("munmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fd = open("example.txt", O_RDWR);
|
||||
read(fd, buf, 11);
|
||||
printf("File content: %s\n", buf);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user