[英]invalid argument in mmap
#include <stdio.h> /* fprintf */
#include <stdlib.h> /* exit */
#include <string.h> /* memset */
#include <sys/mman.h> /* mmap */
#include <sys/types.h> /* pthread types */
#include <sys/stat.h> /* fchmod */
#include <pthread.h> /* thread primitives */
#include <fcntl.h> /* open */
#include <unistd.h> /* ftruncate */
#include <errno.h> /* errno */
#define LIB_ADDR 0xaabbccdd /* memorable random address */
#define UNMAP_FILE "unmapfile"
#define PAGE_SIZE 1024
#define DIE(msg) \
printf("-----\nDIE:%s\n-----\n", msg)
int
main(int argc, char **argv)
{
int err;
int unmap_fd;
unmap_fd = open(UNMAP_FILE, O_RDWR | O_CREAT);
if (unmap_fd < 0) DIE("open of unmap file failed");
err = ftruncate(unmap_fd, PAGE_SIZE);
if (err) DIE("ftruncate unmap file to page size failed");
err = mmap((void *)LIB_ADDR + PAGE_SIZE, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, unmap_fd, 0);
if (err = MAP_FAILED) DIE("mmap of to-be-unmapped page failed");
return 0;
}
What I am doing is quite simple. 我在做什么很简单。 I just want to mmap
a file onto a fixed address, but I get the error mmap of to-be-unmapped page failed
. 我只是想mmap
文件到一个固定的地址,但我得到了错误mmap of to-be-unmapped page failed
。 I have checked everything but still have no idea. 我检查了所有内容,但仍然不知道。
This 这个
if (err = MAP_FAILED) DIE("mmap of to-be-unmapped page failed");
should be 应该
if (err == MAP_FAILED) DIE("mmap of to-be-unmapped page failed");
//Notice the ==
You are just assigning here and it makes the condition true. 您只是在这里分配,它使条件成立。
mmap(2)
returns a void*
. mmap(2)
返回void*
。 So type of err
is also wrong. 因此, err
类型也是错误的。 Declare a new void pointer and use it: 声明一个新的void指针并使用它:
void *mptr;
....
....
mptr = mmap((void *)LIB_ADDR + PAGE_SIZE, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, unmap_fd, 0);
if (mptr == MAP_FAILED) DIE("mmap of to-be-unmapped page failed");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.