一、代码
#include <sys/mman.h>
mmap()
1.1 文件myfile
需要事先准备好的文件内容:
1.2 代码
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char*argv[])
{
int fd = open("myfile", O_RDWR);
if (fd < 0)
{
perror("open file error");
exit(1);
}
int *p = mmap(NULL, 6, PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED)
{
perror("mmap error");
exit(1);
}
close(fd);
p[0] = 0x30313233;
munmap(p, 6);
return 0;
}
二、输出结果
执行完后,再查看文件myfile: