【问题标题】:Use of mmap causes invalid argument error使用 mmap 会导致参数无效错误
【发布时间】:2014-02-03 05:21:57
【问题描述】:

我在 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));

我收到一个无效参数错误。

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

谢谢

【问题讨论】:

  • 你试过用MAP_PRIVATE代替MAP_SHARED吗?
  • 我试过了,错误依旧存在

标签: c


【解决方案1】:

我决定尝试在另一台机器上运行这段代码,它运行良好,这似乎是机器端的问题,而不是代码中的问题。至少现在我知道代码没有损坏:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多