内存映射
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),也可以与三个标志(最后 3 个)中的任何一个进行 ORd。字段 flags 指示映射类型,可以是 MAP_PRIVATE 或 MAP_SHARED。字段"fd"指示要映射的文件的文件描述符,字段"offset"表示文件的起始点,如果需要映射整个文件,offset 应该为零。
#include <sys/mman.h> int munmap(void *addr, size_t length);
上述系统调用成功时返回 0,错误时返回 -1。
系统调用 munmap 执行已内存映射区域的取消映射。字段 addr 表示映射的起始地址,长度表示要取消映射的映射的字节大小。通常,映射和取消映射是针对整个映射区域的。如果必须有所不同,则应缩小或分成两部分。如果 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
", offset, data[offset]);
else if (offset != -1)
printf("Received invalid index %d
", 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

