红联Linux门户
Linux帮助

内存映射问题

发布时间:2009-09-21 18:01:03来源:红联作者:kgduu
#include
#include
#include
#include
#include
#include
#include


int main()
{
int fd;
char *buf;
int i;
struct stat statbuf;

if (stat("test.txt", &statbuf) == -1) {
perror("fail to get stat");
exit(1);
}

fd = open("test.txt", O_WRONLY);
if (fd == -1) {
perror("fail to open");
exit(1);
}

buf = (char *)mmap(NULL, statbuf.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (buf == MAP_FAILED) {
perror("fail to mmap");
exit(1);
}

strcpy(buf, "China beijing");

if (munmap(buf, statbuf.st_size) == -1) {
perror("fail to munmap");
exit(1);
}

close(fd);

return 0;
}

运行后提示为:fail to mmap: Permission denied
是什么原因?
文章评论

共有 1 条评论

  1. wangpu719 于 2009-10-07 17:38:33发表:

    注意你打开文件时的权限;
    fd = open(filepath, O_RDWR);