一对多或者多对多进行进程通信。
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
addr 地址,填NULL
length 长度 要申请的映射区的长度
prot 权限
PROT_READ 可读
PROT_WRITE 可写
flags 标志位
MAP_SHARED 共享的 -- 对映射区的修改会影响源文件
MAP_PRIVATE 私有的
fd 文件描述符 需要打开一个文件
offset 指定一个偏移位置 ,从该位置开始映射
返回值
成功 返回映射区的首地址
失败 返回 MAP_FAILED ((void *) -1)
int munmap(void *addr, size_t length);
addr 映射区的首地址
length 映射区的长度
返回值
成功 返回0
失败 返回 -1
如果文件偏移量随便填个数会怎么样?
如果文件描述符先关闭,对mmap映射有没有影响?
如果更改mem变量的地址,释放的时候munmap,传入mem还能成功吗?
如果对mem越界操作会怎么样?
open的时候,可以新创建一个文件来创建映射区吗?
open文件选择O_WRONLY,可以吗?
当选择MAP_SHARED的时候,open文件选择O_RDONLY,prot可以选择PROT_READ|PROT_WRITE吗?
int truncate(const char *path, off_t length);
path 要拓展的文件
length 要拓展的长度
//mmapsend.c
#include
#include
#include
#include
#include
#include
#include
int main()
{
int fd = open("./a.txt",O_RDWR);
void *str = mmap(NULL,1024,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
// 写入数据
strcpy(str,"hello world");
return 0;
}
//mmaprcv.c
#include
#include
#include
#include
#include
#include
int main()
{
int fd = open("./a.txt",O_RDWR|O_CREAT,0777);
void *str = mmap(NULL,1024,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
printf("%s\n",(char*)str);
return 0;
}
#include
#include
#include
#include
#include
#include
int main()
{
int fd = open("./a.txt",O_RDWR);
if(fd<0)
{
fd = open("./a.txt",O_RDWR|O_CREAT,0666);
truncate("./a.txt",1024);//文件大小拓展
}
void *str = mmap(NULL,1024,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
printf("%s\n",(char*)str);
return 0;
}