2013-08-18 51 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

相關問題