mmap父子进程间通信
1. 测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>int var = 100;int main(void)
{int *p;pid_t pid;int fd;fd = open("temp", O_RDWR | O_CREAT | O_TRUNC, 0644);if(fd < 0) {perror("open error");exit(1);}unlink("temp"); //删除临时文件目录项,使之具备被释放条件ftruncate(fd, 4);p = (int *) mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);if(p == MAP_FAILED) {perror("mmap error");exit(1);}close(fd); //映射区建立完毕,即可关闭文件 pid = fork(); //创建子进程 if(pid == 0) {*p = 2000;var = 1000;printf("child, *P = %d, var = %d\n", *p, var);} else {sleep(1);printf("parent, *p = %d, var = %d\n", *p, var);wait(NULL);int ret = munmap(p, 4); //释放映射区 if(ret == -1) {perror("munmap error");exit(1);}}return 0;
}
输出结果:
2. 测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>int var = 100;int main(void)
{int *p;pid_t pid;int fd;fd = open("temp", O_RDWR | O_CREAT | O_TRUNC, 0644);if(fd < 0) {perror("open error");exit(1);}unlink("temp"); //删除临时文件目录项,使之具备被释放条件ftruncate(fd, 4);p = (int *) mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);if(p == MAP_FAILED) {perror("mmap error");exit(1);}close(fd); //映射区建立完毕,即可关闭文件 pid = fork(); //创建子进程 if(pid == 0) {*p = 2000;var = 1000;printf("child, *P = %d, var = %d\n", *p, var);} else {sleep(1);printf("parent, *p = %d, var = %d\n", *p, var);wait(NULL);int ret = munmap(p, 4); //释放映射区 if(ret == -1) {perror("munmap error");exit(1);}}return 0;
}
输出结果: