#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(void)
{
pid_t pid;
char *p=NULL;
int fd=open("mytest.txt",O_CREAT|O_RDWR,0644);
if(fd<0)
{
perror("open error\n");
exit(1);
}
int len=ftruncate(fd,4);
if(len==-1)
{
perror("open error\n");
exit(1);
}
p=mmap(NULL,4,PROT_READ |PROT_WRITE,MAP_SHARED,fd,0 );
if(p == MAP_FAILED)
{
error("mmap error\n");
exit(1);
}
strcpy(p,"abc");
munmap(p,4);
close(fd);
}
open的时候可以O_CREAT 一个新的文件来创建映射区吗?会出错
如何p++了munmap能否成功 ?
首地址发生变化了 不允许
如果open的时候O_RDONLY mmap的时候PROT_READ |PROT_WRITE会怎么样?
不允许 权限不足
创建映射区的权限要小于等于打开文件的权限
如果文件偏移量为1000会怎么样?
文件位置偏移1000个字节在映射
偏移参数必须为4k整数倍
如果不检测mmap的返回值会怎么样?
mmap什么情况下失败 空间为0 偏移量参数错误
文件描述符先关闭对mmap有没有影响 没有影响 操作文件的句柄