#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = shm_open("tset.txt", O_CREAT|O_RDWR, 0777);//需要连接 -lrt
if(fd==0)
{
perror("open error");
}
ftruncate(fd, 1024);//修改文件长度
mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);//设置共享映射
write(fd, "hello", 6);
char buf[1024];
read(fd, buf, 6);
printf("%s\n",buf);
return 0;
}
mmap/shm_open 映射进程间共享文件
最新推荐文章于 2025-02-26 18:21:21 发布