- mmap 文件–内存映射
函数原型
#include <sys/mman.h>void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
参数介绍:
add 传 NULL
length 映射区的长度
protPROT_READ 可读PROT_WRITE可写
flagsMAP_SHARED 共享的,对内存的修改会影响源文件MAP_PRIVATE 私有的
fd 文件描述符,open一个文件
offset 偏移量返回值:成功:返回可用内存的首地址,失败:返回MAP_FAILED
释放映射区
int munmap(void *addr, size_t length);
参数:
addr 传mmap的返回值
length mmap创建的长度
返回值:成功返回0失败返回-1
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
这两个函数可以根据文件描述符,或者是文件名,修改文件的大小(字节)
- mmap函数注意事项
- 使用mmap进行父子进程之间的通信
代码示例:
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/wait.h>int main(int argc, char *argv[]) {if(argc !=2) {printf("./a.out filename1");return -1;}int fd = open(argv[1], O_RDWR|O_CREAT, 0666);ftruncate(fd, 4);int *mem = mmap(NULL, 4, PROT_READ|PROT_WRITE, MAP_SHARED, fd , 0);pid_t pid = fork();if(pid == 0) {// son*mem = 100;sleep(3);printf("son mem = %d\n", *mem);} else if(pid>0){//fathersleep(1);printf("father mem = %d\n", *mem);*mem = 2;wait(NULL);}munmap(mem, 4);close(fd);return 0;
}
- 使用mmap,让无血缘关系的进程之间进行通信代码案例:
写端:
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/wait.h>
typedef struct _student
{int sid;char sname[20];
}Student;
int main(int argc, char *argv[]) {if(argc!=2) {printf("./a.out filename\n");return -1;}int fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);int length = sizeof(Student);ftruncate(fd, length);Student *stu = mmap(NULL, length, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);int num = 0;while (1){stu->sid = num++;sprintf(stu->sname, "mynames_x%d", num); sleep(1);}munmap(stu, length);close(fd);return 0;
}
读端:
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/wait.h>
typedef struct _student
{int sid;char sname[20];
}Student;
int main(int argc, char *argv[]) {if(argc!=2) {printf("./a.out filename\n");return -1;}int fd = open(argv[1], O_RDONLY, 0666);int length = sizeof(Student);ftruncate(fd, length);Student *stu = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);int num = 0;while (1){printf("read sid= %d\n", stu->sid);printf("%s\n", stu->sname);sleep(1);}munmap(stu, length);close(fd);return 0;
}
- 利用mmap使用4个进程对一个文件进行拷贝
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>int main(int argc, char*argv[]) {if(argc!=3) {printf("./a.out filename_src filename_desc \n");return -1;}int srcfd = open(argv[1], O_RDWR);int desfd = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0666);struct stat sb;stat(argv[1], &sb);int len = sb.st_size;truncate(argv[2], len);char *memsrc = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, srcfd, 0);if(memsrc == MAP_FAILED) {printf("mmaperror1\n");return -1;}char *memdes = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, desfd, 0);if(memdes == MAP_FAILED) {printf("mmaperror2\n");return -1;}int i=0;int one_size = len / 4;int remainder_size = len % 4;pid_t pid;for(; i<5; ++i) {pid = fork();if(pid==0) {break;}}if (i<5) {if(i==4) {memcpy(memdes + i*one_size, memsrc + i*one_size, one_size + remainder_size);} else {memcpy(memdes + i*one_size, memsrc + i*one_size, one_size);}} else {wait(NULL);printf("cp file end\n");}int unmemsrc = munmap(memsrc, len);int unmemdes = munmap(memdes, len);printf("%d unmemsrc --------- %d\n", getpid(), unmemsrc);printf("%d unmemdes %d\n", getpid(), unmemdes);close(desfd);close(srcfd); return 0;
}
ps:疑惑的地方 上面这端代码最后释放内存的这一部分,相当于多个进程对同一块内存进行了重复释放吧? 是这样吗?
ps2:疑惑的地方 mmap通过文件映射的内存位于哪块内存区???