红联Linux门户
Linux帮助

freebsd下计算cpu、memory、hd等相信的代码

发布时间:2007-02-10 00:43:40来源:红联作者:System
/*
* FreeBSD CPU Information 0.1
* ---------------------------
* Simple program to display the total RAM, and CPU information.
* Compile: cc -o cpuinfo cpuinfo.c
* ---------------------------
*/
#include
#include
#include
#include
#include
#include
extern int errno;
int main(void)
{
int len, numcpu, cpuspeed, totalmem, usermem;
char cpuarch[64], cpumodel[64];
printf("FreeBSD CPU Information\n");
printf("Version 0.1\n");
len = sizeof(cpuarch);
if (sysctlbyname("hw.machine_arch", &cpuarch, &len, NULL, NULL) == -1)
{
perror("sysctlbyname()");
return -1;
}
len = sizeof(cpumodel);
if (sysctlbyname("hw.model", &cpumodel, &len, NULL, NULL) == -1) {
perror("sysctlbyname()");
return -1;
}
len = sizeof(cpuspeed);
if (sysctlbyname("machdep.tsc_freq", &cpuspeed, &len, NULL, NULL) ==
-1) {
perror("sysctlbyname()");
return -1;
}
len = sizeof(numcpu);
if (sysctlbyname("hw.ncpu", &numcpu, &len, NULL, NULL) == -1) {
perror("sysctlbyname()");
return -1;
}
len = sizeof(totalmem);
if (sysctlbyname("hw.physmem", &totalmem, &len, NULL, NULL) == -1) {
perror("sysctlbyname()");
return -1;
}
len = sizeof(usermem);
if (sysctlbyname("hw.usermem", &usermem, &len, NULL, NULL) == -1) {
perror("sysctlbyname()");
return -1;
}
cpuspeed = cpuspeed / 1000000;
totalmem = (totalmem - 1000000) / 1000000;
usermem = (usermem - 1000000) / 1000000;
printf("Architecture:\t%s\n", cpuarch);
printf("Number of CPUs:\t%d\n", numcpu);
printf("CPU Model:\t%s\n", cpumodel);
printf("CPU Speed:\t%dMHz\n", cpuspeed);
printf("Total Memory:\t%dMB\n", totalmem);
printf("User Memory:\t%dMB\n", usermem);
printf("\n");
return 0;
}
文章评论

共有 0 条评论