#mmap是将文件映射到内存中进行通信
##典型映射代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
void sys_err(const char *str){
perror(str);
exit(1);
}
int main(int argc,char *argv[]){
char *p=NULL;
int fd;
fd=open("testmap",O_RDWR|O_CREAT|O_TRUNC,0644);
if(fd==-1)
sys_err("open error");
//两行代码等价于ftruncate()函数
/*
lseek(fd,10,SEEK_END);
write(fd,"\0",1);
*/
ftruncate(fd,20);
int len=lseek(fd,0,SEEK_END);
p=mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(p==MAP_FAILED)
sys_err("mmap error");
strcpy(p,"hello mmap");
printf("----%s\n",p);
//释放映射区
int ret=munmap(p,len);
if(ret==-1)
sys_err("munmap error");
return 0;
}
##父子进程间mmap通信代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
void sys_err(const char *str){
perror(str);
exit(1);
}
int var=1000;
int main(int argc,char *argv[]){
int *p;
pid_t pid;
int fd;
fd=open("temp",O_RDWR|O_CREAT|O_TRUNC,0644);
if(fd<0){
sys_err("open error");
exit(1);
}
ftruncate(fd,4);
p=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(p==MAP_FAILED){
sys_err("mmap error");
exit(1);
}
close(fd);
pid=fork();
if(pid==0){
*p=2000;
var=100;
printf("child,*p=%d,var=%d\n",*p,var);
}
else{
sleep(1);
printf("parent,*p=%d,var=%d\n",*p,var);
wait(NULL);
}
int ret=munmap(p,4);
if(ret==-1){
sys_err("munmap error");
exit(1);
}
}
##血缘关系写端代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
struct student{
int id;
char name[256];
int age;
};
void sys_err(const char *str){
perror(str);
exit(1);
}
int main(int argc,char *argv[]){
struct student stu={1,"xiaoming",18};
struct student *p;
int fd;
fd=open("temp",O_RDWR|O_CREAT|O_TRUNC,0644);
if(fd<0){
sys_err("open error");
exit(1);
}
ftruncate(fd,sizeof(stu));
p=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(p==MAP_FAILED){
sys_err("mmap error");
exit(1);
}
close(fd);
while(1){
memcpy(p,&stu,sizeof(stu));
stu.id++;
sleep(1);
}
int ret=munmap(p,sizeof(stu));
if(ret==-1){
sys_err("munmap error");
exit(1);
}
}
##无血缘关系读端代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
struct student{
int id;
char name[256];
int age;
};
void sys_err(const char *str){
perror(str);
exit(1);
}
int main(int argc,char *argv[]){
struct student stu={1,"xiaoming",18};
struct student *p;
int fd;
fd=open("temp",O_RDWR|O_CREAT|O_TRUNC,0644);
if(fd<0){
sys_err("open error");
exit(1);
}
ftruncate(fd,sizeof(stu));
p=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(p==MAP_FAILED){
sys_err("mmap error");
exit(1);
}
close(fd);
while(1){
printf("id=%d,name=%s,age=%d\n",p->id,p->name,p->age);
sleep(1);
}
int ret=munmap(p,sizeof(stu));
if(ret==-1){
sys_err("munmap error");
exit(1);
}
}
##注意事项
1、mmap通信和fifo的队列读写不同,可以多次读取;
2、创建映射区的过程中,隐含着一次对映射文件的读操作;
3、当MAP_SAHRED时,要求:映射区的权限应《=文件打开的权限(出于对映射区的保护)。而MAP_PRIVATE则无所谓,因为mmap中的权限是对内存的限制;
4、映射区的释放与文件关闭无关,只要映射建立成功,文件可以立即关闭;
5、当映射文件大小为0时,不能创建映射区。所以:用于映射的文件必须要有实际大小。mmap使用时常常会出现总线错误,通常是由于共享文件存储空间大小引起的。
6、mmap传入的地址一定是mmap的返回地址,坚决杜绝指针++操作;
7、文件偏移量必须为4K的整数倍;
8、mmap创建映射区出错的概率非常高,一定要检查返回值,确保映射区建立成功再进行后续操作