当前位置: 技术问答>linux和unix
怎样用程序实现监测内存啊?
来源: 互联网 发布时间:2015-10-27
本文导语: 希望监测内存,当内存消耗大于20%时,重启计算机。 请问用shell怎样实现啊。谢谢大家的帮忙 | void mem(unsigned int *Total, unsigned int * Free ) { unsigned int mem_total; unsigned int mem_use...
希望监测内存,当内存消耗大于20%时,重启计算机。
请问用shell怎样实现啊。谢谢大家的帮忙
请问用shell怎样实现啊。谢谢大家的帮忙
|
void mem(unsigned int *Total, unsigned int * Free )
{
unsigned int mem_total;
unsigned int mem_used;
unsigned int mem_free;
unsigned int mem_shared;
unsigned int mem_buffered;
unsigned int mem_cached;
char mem[21];
char text[201];
FILE *fp;
fp = fopen("/proc/meminfo", "r");
while (fgets(text, 200, fp))
{
if (strstr(text, "Mem:"))
{
sscanf(text, "%s %u %u %u %u %u %u", mem, &mem_total, &mem_used, &mem_free, &mem_shared, &mem_buffered, &mem_cached);
}
}
fclose(fp);
// mem_total = get_phys_pages();
// mem_free = get_avphys_pages();
mem_total = mem_total / 1048576;
mem_used = mem_used / 1048576;
mem_free = mem_free / 1048576;
mem_shared = mem_shared / 1048576;
mem_buffered = mem_buffered / 1048576;
mem_cached = mem_cached / 1048576;
*Total = mem_total;
*Free = mem_free;
}
{
unsigned int mem_total;
unsigned int mem_used;
unsigned int mem_free;
unsigned int mem_shared;
unsigned int mem_buffered;
unsigned int mem_cached;
char mem[21];
char text[201];
FILE *fp;
fp = fopen("/proc/meminfo", "r");
while (fgets(text, 200, fp))
{
if (strstr(text, "Mem:"))
{
sscanf(text, "%s %u %u %u %u %u %u", mem, &mem_total, &mem_used, &mem_free, &mem_shared, &mem_buffered, &mem_cached);
}
}
fclose(fp);
// mem_total = get_phys_pages();
// mem_free = get_avphys_pages();
mem_total = mem_total / 1048576;
mem_used = mem_used / 1048576;
mem_free = mem_free / 1048576;
mem_shared = mem_shared / 1048576;
mem_buffered = mem_buffered / 1048576;
mem_cached = mem_cached / 1048576;
*Total = mem_total;
*Free = mem_free;
}