内存共享映射的实现通信

xiaoxiao2025-06-10  5

//内存共享映射 #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> int main(void) { int fd, len; int *p; fd = open("hello", O_RDWR);//将fd与hello文件连接 if(fd<0) { perror("open"); exit(1); } len = lseek(fd, 0, SEEK_END);//读取hello文件的大小 p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);//建立映射 if(p==MAP_FAILED) { perror("mmap"); exit(1); } close(fd); P[0]=0x30313233;//修改内存中数据 munmap(p, len);//释放映射内存,关闭映射 return 0; }
转载请注明原文地址: https://www.6miu.com/read-5031603.html

最新回复(0)