mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-08 22:36:48 +00:00
- 添加文件映射相关接口,目前已简单实现fat文件系统的私有映射和共享映射 - 添加msync系统调用(由于当前未实现脏页自动回写,需要手动调用msync进行同步) - 简单实现PageCache(暂时使用HashMap进行文件页号与页的映射) - 添加新的PageFlags标志结构,原PageFlags改名为EntryFlags - 参考linux使用protection_map映射表进行页面标志的获取 - 添加页面回收机制 - 添加页面回收内核线程 - 缺页中断使用的锁修改为irq_save; 添加脏页回写机制 - 修复do_cow_page死锁问题 - 访问非法地址时发送信号终止进程 - 修复重复插入反向vma表的错误 - 添加test_filemap文件映射测试程序
62 lines
1.3 KiB
C
62 lines
1.3 KiB
C
#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;
|
|
}
|