2013-08-18 31 views
1

我需要一种使用C或C++的方法来从/dev/shm获取可用内存。请注意,在我的Linux上的ARM架构上,不幸的是,ipcs报告错误的最大值。可用内存信息,但df -h正确地给我从tmpfs当前可用内存。如何从/ dev/shm获得有关可用内存的信息

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

回答

3

尝试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  
2

posix_fallocate()在文件系统将或者分配盘区背衬,或如果有足够的空间(ENOSPC)失败。

#include <fcntl.h> 

int posix_fallocate(int fd, off_t offset, off_t len); 

的posix_fallocate()函数应确保offset处开始的常规文件数据的存储需要,继续len个字节是文件系统的存储介质上的分配。如果posix_fallocate()成功返回,则由于文件系统存储介质上没有可用空间,后续写入指定的文件数据不会失败。

这听起来像您可能想要的功能。

+0

我会尽力让你知道。谢谢。 – Martin

相关问题