- 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通過文件映射的內存位于哪塊內存區???