存储映射I/O(Memory-mapped I/O) 使一个磁盘文件与存储空间中的一个缓冲区相映射。于是从缓冲区中取数据,就相当于读文件中的相应字节。与此类似,将数据存入缓冲区,则相应的字节就自动写入文件。这样,就可在不使用read和write函数的情况下,使地址指针完成I/O操作。
使用这种方法,首先应该通知内核,将一个指定文件映射到存储区域中。这个映射工作可以通过mmap函数来实现。
void *mmap(void addr, size_t length, int prot, int flags, int fd, off_t offset); 创建共享内存映射
参数:
addr:指定映射区的首地址。通常传NULL,表示让系统自动分配
length:共享内存映射区的大小。(<= 文件的实际大小)
prot:共享内存映射区的读写属性。PROT_READ、PROT_WRITE、PROT_READ|PROT_WRITE
flags:标注共享内存的共享属性。MAP_SHARED、MAP_PRIVATE。shared意思是修改会反映到磁盘上, private表示修改不反映到磁盘上
fd: 用于创建共享内存映射区的那个文件的 文件描述符。
offset:默认0,表示映射文件全部。偏移位置。需是 4k 的整数倍。
返回值:成功:映射区的首地址。失败:MAP_FAILED (void(-1)), errno
int munmap(void *addr, size_t length); 释放映射区。
参数:
addr:mmap 的返回值
length:大小
注意事项:
1.创建映射区的过程中,隐含着一次对映射文件的读操作
2.当MAP_SHARED时,要求:映射区的权限应该<=文件打开的权限(出于对映射区的保护)。而MAP_PRIVATE则无所谓,因为mmap中的权限是对内存的限制
3.映射区的释放与文件关闭无关。只要映射建立成功,文件可以立即关闭
4.特别注意,当映射文件大小为0时,不能创建映射区。所以:用于映射的文件必须要有实际大小!!mmap使用时常常会出现总线错误,通常是由于共享文件存储空间大小引起的。如,400字节大小的文件,在简历映射区时,offset4096字节,则会报出总线错误
5.munmap传入的地址一定是mmap返回的地址。坚决杜绝指针++操作
6.文件偏移量必须为4K的整数倍
7.mmap创建映射区出错概率非常高,一定要检查返回值,确保映射区建立成功再进行后续操作。
//mmap.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int fd = open("mem.txt", O_RDWR | O_CREAT | O_TRUNC, 0644); //创建并截断文件
//int fd = open("mem.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); //Permission denied
ftruncate(fd, 20); //open的时候,可以创建一个文件来创建映射区,但前提是文件大小不能为0
//创建映射区
char *mem = mmap(NULL, 20, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
//offset必须是4k的整数倍
//char *mem = mmap(NULL, 5, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 4096);
//char *mem = mmap(NULL, 5, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mem == MAP_FAILED)
{
perror("mmap err!");
return -1;
}
strcpy(mem, "hello world");
//close(fd); /如果文件描述符先关闭,对mmap映射没有影响,因为映射通道已经通了
//mem++; //不能改变地址,否则无法释放
//释放
int ret = munmap(mem, 20);
if (ret < 0)
{
perror("munmap error!");
}
close(fd);
return 0;
}
//mmap_fork.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
//先创建映射区
int fd = open("mem.txt", O_RDWR);
int *mem = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
//int *mem = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mem == MAP_FAILED)
{
perror("mmap error!");
return -1;
}
//fork子进程
pid_t pid = fork();
//父进程和子进程交替修改数据
if (pid < 0)
{
perror("fork error!");
return -1;
}
else if (pid == 0)
{
*mem = 100;
printf("child, *mem = %d\n", *mem);
sleep(3);
printf("child, *mem = %d\n", *mem);
}
else if (pid > 0)
{
sleep(1);
printf("parent, *mem = %d\n", *mem);
*mem = 1001;
printf("parent, *mem = %d\n", *mem);
wait(NULL);
}
int ret = munmap(mem, 4);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
close(fd);
return 0;
}
//mmpa_anon.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int *mem = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (mem == MAP_FAILED)
{
perror("mmap error!");
return -1;
}
//fork子进程
pid_t pid = fork();
//父进程和子进程交替修改数据
if (pid < 0)
{
perror("fork error!");
return -1;
}
else if (pid == 0)
{
*mem = 100;
printf("child, *mem = %d\n", *mem);
sleep(3);
printf("child, *mem = %d\n", *mem);
}
else if (pid > 0)
{
sleep(1);
printf("parent, *mem = %d\n", *mem);
*mem = 1001;
printf("parent, *mem = %d\n", *mem);
wait(NULL);
}
int ret = munmap(mem, 4);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
return 0;
}
//mmap_read.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
typedef struct Student
{
int sid;
char sname[20];
}Student;
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
//1.open file
int fd = open(argv[1], O_RDWR, 0666);
int length = sizeof(Student);
//2.mmap
Student *stu = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (stu == MAP_FAILED)
{
perror("map error!");
return -1;
}
//3.修改内存数据, read
while (1)
{//每隔一秒读一次映射区的内容
printf("sid = %d, sname = %s\n", stu->sid, stu->sname);
sleep(1);
}
//4.释放映射区和关闭描述符
int ret = munmap(stu, length);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
close(fd);
return 0;
}
//mmap_write.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
typedef struct Student
{
int sid;
char sname[20];
}Student;
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
//1.open file
int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 0666);
int length = sizeof(Student);
ftruncate(fd, length);
//2.mmap
Student *stu = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (stu == MAP_FAILED)
{
perror("map error!");
return -1;
}
//3.修改内存数据, write
int num = 1;
while (1)
{//每隔一秒修改一次映射区的内容
stu->sid = num;
sprintf(stu->sname, "LiHua-%03d", num++);
sleep(1);
}
//4.释放映射区和关闭描述符
int ret = munmap(stu, length);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
close(fd);
return 0;
}