内存映射
mmap() 系统调用在调用进程的虚拟地址空间中提供映射,将文件或设备映射到内存。它分为两种类型:
文件映射或文件支持映射 - 此映射将进程虚拟内存的区域映射到文件。这意味着对这些内存区域进行读写操作会导致文件被读写。这是默认的映射类型。
匿名映射 - 此映射将进程虚拟内存的区域映射到内存,但不依赖于任何文件。内容初始化为零。这种映射类似于动态内存分配 (malloc()),并在某些 malloc() 实现中用于某些分配。
一个进程的内存映射可以与其他进程的映射共享。这可以通过两种方式完成:
当两个进程映射文件的同一区域时,它们共享相同的物理内存页。
如果创建子进程,它将继承父进程的映射,这些映射引用与父进程相同的物理内存页。在子进程中对数据进行任何更改后,将为子进程创建不同的页面。
当两个或多个进程共享相同的页面时,每个进程都可以看到其他进程对页面内容所做的更改,具体取决于映射类型。映射类型可以是私有的或共享的:
私有映射 (MAP_PRIVATE) - 对此映射内容的修改对其他进程不可见,并且映射不会传递到底层文件。
共享映射 (MAP_SHARED) - 对此映射内容的修改对其他进程可见,并且映射传递到底层文件。
#include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
上述系统调用成功时返回映射的起始地址,出错时返回 MAP_FAILED。
虚拟地址 addr 可以是用户指定的,也可以由内核生成(当 addr 传递为 NULL 时)。length 字段指示映射的大小(以字节为单位)。prot 字段指示内存保护值,例如 PROT_NONE、PROT_READ、PROT_WRITE、PROT_EXEC,分别表示不允许访问、读取、写入或执行的区域。此值可以是单个值 (PROT_NONE),也可以与任何三个标志(最后三个)进行 OR 运算。flags 字段指示映射类型,即 MAP_PRIVATE 或 MAP_SHARED。fd 字段指示标识要映射的文件的文件描述符,offset 字段表示文件的起始点,如果需要映射整个文件,则 offset 应为零。
#include <sys/mman.h> int munmap(void *addr, size_t length);
上述系统调用成功时返回 0,出错时返回 -1。
系统调用 munmap 执行已映射内存区域的取消映射。addr 字段指示映射的起始地址,length 字段指示要取消映射的映射的大小(以字节为单位)。通常,映射和取消映射将针对整个映射区域。如果必须不同,则应缩小或分成两部分。如果 addr 没有任何映射,则此调用不会产生任何影响,并且调用返回 0(成功)。
让我们考虑一个例子:
步骤 1 - 将如下所示的字母数字字符写入文件:
| 0 | 1 | 2 | … | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | … | 59 | 60 | 61 |
| A | B | C | … | Z | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | b | c | … | x | y | z |
步骤 2 - 使用 mmap() 系统调用将文件内容映射到内存。这将返回映射到内存后的起始地址。
步骤 3 - 使用数组表示法访问文件内容(也可以使用指针表示法访问),因为它不会读取昂贵的 read() 系统调用。使用内存映射,避免用户空间、内核空间缓冲区和缓冲区缓存之间多次复制。
步骤 4 - 重复读取文件内容,直到用户输入“-1”(表示访问结束)。
步骤 5 - 执行清理活动,即取消映射已映射内存区域 (munmap())、关闭文件和删除文件。
/* Filename: mmap_test.c */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
void write_mmap_sample_data();
int main() {
struct stat mmapstat;
char *data;
int minbyteindex;
int maxbyteindex;
int offset;
int fd;
int unmapstatus;
write_mmap_sample_data();
if (stat("MMAP_DATA.txt", &mmapstat) == -1) {
perror("stat failure");
return 1;
}
if ((fd = open("MMAP_DATA.txt", O_RDONLY)) == -1) {
perror("open failure");
return 1;
}
data = mmap((caddr_t)0, mmapstat.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (data == (caddr_t)(-1)) {
perror("mmap failure");
return 1;
}
minbyteindex = 0;
maxbyteindex = mmapstat.st_size - 1;
do {
printf("Enter -1 to quit or ");
printf("enter a number between %d and %d: ", minbyteindex, maxbyteindex);
scanf("%d",&offset);
if ( (offset >= 0) && (offset <= maxbyteindex) )
printf("Received char at %d is %c\n", offset, data[offset]);
else if (offset != -1)
printf("Received invalid index %d\n", offset);
} while (offset != -1);
unmapstatus = munmap(data, mmapstat.st_size);
if (unmapstatus == -1) {
perror("munmap failure");
return 1;
}
close(fd);
system("rm -f MMAP_DATA.txt");
return 0;
}
void write_mmap_sample_data() {
int fd;
char ch;
struct stat textfilestat;
fd = open("MMAP_DATA.txt", O_CREAT|O_TRUNC|O_WRONLY, 0666);
if (fd == -1) {
perror("File open error ");
return;
}
// Write A to Z
ch = 'A';
while (ch <= 'Z') {
write(fd, &ch, sizeof(ch));
ch++;
}
// Write 0 to 9
ch = '0';
while (ch <= '9') {
write(fd, &ch, sizeof(ch));
ch++;
}
// Write a to z
ch = 'a';
while (ch <= 'z') {
write(fd, &ch, sizeof(ch));
ch++;
}
close(fd);
return;
}
输出
Enter -1 to quit or enter a number between 0 and 61: 3 Received char at 3 is D Enter -1 to quit or enter a number between 0 and 61: 28 Received char at 28 is 2 Enter -1 to quit or enter a number between 0 and 61: 38 Received char at 38 is c Enter -1 to quit or enter a number between 0 and 61: 59 Received char at 59 is x Enter -1 to quit or enter a number between 0 and 61: 65 Received invalid index 65 Enter -1 to quit or enter a number between 0 and 61: -99 Received invalid index -99 Enter -1 to quit or enter a number between 0 and 61: -1