gpt4 book ai didi

c++ - 如何从/dev/shm 获取有关可用内存的信息

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:29 29 4
gpt4 key购买 nike

我需要一种 C 或 C++ 的方法来从 /dev/shm 获取可用的空闲内存。请注意,不幸的是,在我的 Linux 上的 ARM 架构中,ipcs 报告了一个错误的最大值。可用内存信息,但是 df -h 正确地为我提供了来自 tmpfs 的当前可用内存。

问题是我试图通过 boost::interprocess::shared_memory_object::truncate 分配共享内存,但是当内存不可用时这个函数不会抛出。这个问题显然不在 boost::interprocess 中,而是来自底层 ftruncate(),当没有可用内存时,它不会返回适当的错误(https://svn.boost.org/trac/boost/ticket/4374 ), 所以 boost 不能抛出任何东西。

最佳答案

试试statvfs glibc函数,或者statfs系统调用

#include <sys/statvfs.h>
int statvfs(const char *path, struct statvfs *buf);

#include <sys/vfs.h> /* or <sys/statfs.h> */
int statfs(const char *path, struct statfs *buf);

// in both structures you can get the free memory
// by the following formula.
free_Bytes = s->f_bsize * s->f_bfree

关于c++ - 如何从/dev/shm 获取有关可用内存的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299206/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com