使用mmap会导致无效的参数错误

时间:2014-02-03 05:21:57

标签: c

我在使用c中的mmap,我遇到了一个非常奇怪的错误。当我运行以下代码块(which is sample code from this website

/* The file descriptor. */
int fd;
/* Information about the file. */
struct stat s;
int status;
size_t size;
/* The file name to open. */
const char * file_name = "myfile.txt";
/* The memory-mapped thing itself. */
const void * mapped;
int i;

/* Open the file for reading. */
fd = open ("myfile.txt", O_RDONLY);
check (fd < 0, "open %s failed: %s", file_name, strerror (errno));

/* Get the size of the file. */
status = fstat (fd, & s);
check (status < 0, "stat %s failed: %s", file_name, strerror (errno));
size = s.st_size;

/* Memory-map the file. */
mapped = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0);
check (mapped == MAP_FAILED, "mmap %s failed: %s",
       file_name, strerror (errno));

我遇到了无效的参数错误。

我的研究让我得出结论,这是一个抵消问题,但我完全迷失了我能做些什么来解决它。任何建议都将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

我决定尝试在另一台机器上运行此代码并且工作正常,这似乎是机器方面的问题,而不是代码中的问题。至少现在我知道代码没有被破坏:)