Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
メモリ情報を調べる
# Makefile
sysinfo: sysinfo.c
g++ -o sysinfo sysinfo.c
clean:
rm -f sysinfo
// sysinfo.c
// メモリ情報を調べる
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/sysinfo.h>
int main(int argc, char *argv[])
{
int ret = 0;
struct sysinfo info = {0};
ret = sysinfo(&info);
if (ret != 0) {
printf("Error: sysinfo() %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("uptime: %ld\n", info.uptime);
printf("1 minute load averages: %lu\n", info.loads[0]);
printf("5 minute load averages: %lu\n", info.loads[1]);
printf("15 minute load averages: %lu\n", info.loads[2]);
printf("totalram: %lu\n", info.totalram);
printf("freeram: %lu\n", info.freeram);
printf("sharedram: %lu\n", info.sharedram);
printf("bufferram: %lu\n", info.bufferram);
printf("totalswap: %lu\n", info.totalswap);
printf("freeswap: %lu\n", info.freeswap);
printf("procs: %d\n", info.procs);
printf("totalhigh: %lu\n", info.totalhigh);
printf("freehigh: %lu\n", info.freehigh);
printf("mem_unit: %d\n", info.mem_unit);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment