1. 求取total memory size
static int mem_size_mb(void)
{
FILE* f;
char buf[4096];
long int memsize = -1;
f = fopen("/proc/meminfo", "r");
if (f == NULL)
return -1;
while (fgets(buf, sizeof(buf), f) != NULL) {
long int value;
if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
memsize = value / 1024;
break;
}
}
fclose(f);
return memsize;
}
2. 求取cpu個數
static int cpu_count(void)
{
FILE* f;
char buf[4096];
int count = 0;
f = fopen("/proc/stat", "r");
if (f == NULL)
return -1;
while (fgets(buf, sizeof(buf), f) != NULL) {
if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3]))
count++;
}
fclose(f);
if (count == 0)
return -1;
return count;
}
3. running process 個數
static int running_processes(void)
{
FILE* f;
char buf[4096];
int running = -1;
f = fopen("/proc/stat", "r");
if (f == NULL)
return -1;
while (fgets(buf, sizeof(buf), f) != NULL) {
int value;
if (sscanf(buf, "procs_running %u", &value) == 1) {
running = value;
break;
}
}
fclose(f);
return running;
}
沒有留言:
張貼留言