开发者

Show memory and cpu in C

how can I 开发者_StackOverflowsee my CPU mhz and Free mem (like free) in OpenBSD with a C code?


Using BSD sysctl(3) to get the live CPU frequency and posix sysconf(3) for free.

#include <stdio.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <unistd.h>

main (argc, argv)
        char **argv;
{
        size_t sz;
        auto psize, cpuspeed, getMhz[] = {CTL_HW, HW_CPUSPEED};
        sz = sizeof(cpuspeed);
        sysctl(getMhz, 2, &cpuspeed, &sz, NULL, 0);
        printf("CPU: %d MHz Free: %ld MB\n", cpuspeed, (sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE))>>20
);
        return 0;
}


For free, use the source. CPU clock speed is trickier. dmesg | grep CPU | grep Hz will do it in a shell. You may want to try parsing your system log.


Look at the popen function. It creates a pipe, forks a given shell command, and returns a file descriptor you can read from.

man popen
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜