【问题标题】:How to read return value of mmap如何读取mmap的返回值
【发布时间】:2013-08-07 17:09:17
【问题描述】:

我是这个主题的新手,我已经映射了 3 页。我如何阅读每个内容?我知道我必须使用 PAGE_SHIFT,但我不知道如何使用。

unsigned int* address = mmap(...)

【问题讨论】:

  • unsigned int i = address[0] -- 有什么问题?..
  • 如何获取第二页的起始地址?
  • 这取决于您使用的架构。至于x86PAGE_SIZE是4096。所以页面包含PAGE_SIZE / sizeof(int)项目,page0&address[0]page1&address[1024]page2&address[2048]。跨度>

标签: linux-kernel mmap memory-address


【解决方案1】:

类似以下的...

#define PAGE_SIZE 4096

unsigned int * address = mmap(...)

unsigned int * page0 = &address[ 0 * PAGE_SIZE / sizeof(int) ];
unsigned int * page1 = &address[ 1 * PAGE_SIZE / sizeof(int) ];
unsigned int * page2 = &address[ 2 * PAGE_SIZE / sizeof(int) ];

【讨论】:

  • 谢谢你能解释一下吗?
  • PAGE_SIZE / sizeof(int) 为您提供每页 int 项的数量。所以每个页面的第一个(零索引)项目的地址类似于&address[ N * items_per_page],其中N 是页面索引。
猜你喜欢
  • 1970-01-01
  • 2019-10-03
  • 2012-12-18
  • 1970-01-01
  • 2011-04-27
  • 2017-07-04
  • 2010-09-07
  • 2020-11-01
  • 2021-08-11
相关资源
最近更新 更多