如何使用 C 程序和 malloc 找出处理器的页面大小?
是否可以使用 C 程序和 malloc 找出处理器的页面大小? 而不是通过使用 sysconf() 调用?
is it possible to find out the page size of a processor using C program and malloc?
and not by using sysconf() call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你可以#include一些linux内核头文件,你可以在On cygwin(也许还有windows的ddk)中找到宏PAGE_SIZE,
我在里面
有“肮脏的技巧”(非常非常肮脏!)来计算运行时的页面大小。所有这些都是不可移植的并且完全依赖于系统。
让我们玩得开心吧!
许多系统上的一个可能的技巧是创建一个只有 1 个字节长的共享内存对象。系统调用通常将大小向上舍入为系统页大小。漂白!
另一种情况是,如果您在类 Unix 系统上运行并且敢于拦截 SIGSEGV 信号,您可以尝试通过查找有效区域来探索内存,并查看它们被舍入到 2 的哪个幂。嗯...
那么,为什么 sysctl 不适合你呢?
问候
If you can #include some linux kernel header, you can find the macro PAGE_SIZE in
On cygwin (and perhaps windows' ddk) i's in
There are "dirty tricks" (very, very dirty!) to calculat the page size at runtime. All unportable and totally system-dependent.
Let's have some fun!
A possible trick on may systems is to create a shared memory object only 1 byte long. The system call usually rounds the size up to the system page size. Bleach!
Another one, if you are running on unix-like systems and you dare to intercept the SIGSEGV signal, you could try to explore the memory by looking for valid areas and see which power of 2 they are rounded. Uhm...
So, why sysctl is not good for you?
Regards
标准 C 没有“页面大小”的概念,因此,根据您的标签,没有。可能有一个特定于实现的方法 - 您是否特别厌恶使用 sysconf?我假设您提到它是因为它是实际获取所需信息的一种方式,在这种情况下,为什么不使用它呢?
Standard C has no concept of a "page size" so, based on your tags, no. There may be an implementation-specific method - do you have a particular aversion to using
sysconf
? I'm assuming you mention it because it's a way to actually get the information you need, in which case, why not use it?Malloc 不太可能告诉您您想知道的事情。虽然它通常具有最小大小和将分配的对齐注意事项,但它们不一定与页面大小相同(或以任何定义的方式相关)。
你愿意到别处去寻找吗?你运行什么环境?
Malloc isn't likely to tell you what you want to know. While it often has minimum sizes and alignment considerations of what it will allocate, they aren't necessarily the same as (or related to in any defined way) the page size.
Are you willing to look elsewhere? What environment are you running?
(这不是真正的答案,但我无法将其添加到评论部分,因此在这里回复:这只是我调查的一些结果)
(正如我之前在最初的问题中所说,我的兴趣纯粹是学术性的,我只需要几个计算页面大小并验证它们是否都同意相同值的方法)
在不使用 sysconf 的情况下找出系统/微处理器的页面大小只是为了好玩!这是我到目前为止尝试过的:
- 使用 sysconf(_SC_PAGE_SIZE) 获取实际页面大小:我在这台计算机中得到了 0x1000,它是 4K(其 intel 386 处理器,加载了 32 位 Linux。我的下一步将是验证用其他几种方法(如果我能可靠地找到这些值)
- 然后我
在循环中使用偏移计数器分配了 1 字节内存,我写入了超过 1 字节分配的内存,直到 0x19a8 超出了 1 页大小。并且刚好低于 2 页大小!
-SEGV 发生在 1page 到 2page 大小之间,这是可重复的 -
这个方法无法可靠地找到页面大小。
所以
有没有什么技巧(除了使用内核中可用的直接信息之外)来找出页面大小?我什至准备在 malloc 上妥协(意味着使用其他一些技巧)。
(its not really the answer, but I am unable to add it in comments section, so replying here: its just some results from my investigation)
(as i stated earlier in my original question, my interest was purely academic and I just needed several methods to calculate page size and verify that all of them agree to the same value)
to find out page size of system/microprocessor without using sysconf is just for fun! This is what I tried so far:
-get the actual page size using sysconf(_SC_PAGE_SIZE): I got in this computer 0x1000 which is 4K (its intel 386 processor with 32-bit linux loaded in it. My next steps will be to verify this value with several other ways (if I can find those reliably).
-then I allocated 1 byte memory
-in a loop, using offset counter, I wrote beyond 1 byte allocated memory. it worked fine till 0x19a8 which is beyond 1 page size and just below 2 page size!
-SEGV happens somewhere between 1page to 2page size, towards the end. This is repeatable.
-so this method couldn't find the page size reliably.
That's why I asked this question.
Is there a trick (other than using direct information available in kernel) to find out the page size? I am even ready to compromise on malloc (means using some other trick).