本篇文章给大家谈谈linuxc语言实现查看cpu型号,以及linux系统如何查看cpu型号对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、linux如何查看cpu型号2、Linux下如何查看CPU信息, 包括位数和多核信息(转)3、Linux c 语言怎么方便的读取 cpu,磁盘信息4、Linux系统下如何用C/C++语言编写程序查看cpu_id的值
linux如何查看cpu型号
方法与Windows差不多,点击计算机,然后再对话框中选择属性属性就可以看到计算机的CPU类型。
Linux下如何查看CPU信息, 包括位数和多核信息(转)
(查看当前操作系统内核信息)
# cat /etc/issue | grep Linux
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)
(查看当前操作系统发行版信息)
# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
(看到有8个逻辑CPU, 也知道了CPU型号)
# cat /proc/cpuinfo | grep physical | uniq -c
4 physical id : 0
4 physical id : 1
(说明实际上是两颗4核的CPU)
Linux c 语言怎么方便的读取 cpu,磁盘信息
我附上我的代码给你参考。
CPU占用 需要查看/proc/stat 的信息
磁盘需要 使用statfs这个函数来确认文件所包含的信息。
我附上我的代码给你参考。
我的代码支持CPU使用率(占用率),内存占用率,及磁盘占用率。
#include stdio.h
#include stdlib.h
#include sys/time.h
#include unistd.h
#include sys/vfs.h
#include error.h
#define Gsize (1024.00*1024.00*1024.00)
#define Msize (1024.00*1024.00)
#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xef53
#endif
double time_so_far();
float get_cpu_rate();
float get_memory_rate();
float get_disk_rate();
int main(int argc,char *argv[])
{
get_cpu_rate();
get_memory_rate();
get_disk_rate();
return 0;
}
double time_so_far(){
struct timeval tp;
if(gettimeofday(tp,(struct timezone *)NULL) == -1)
perror(“gettimeofday”);
return ((double)(tp.tv_sec))+(((double)tp.tv_usec)*0.000001);
}
float get_cpu_rate(){
FILE *f1;
double ti,tf;
char c[10],d[10];
int t,i1,i2,i3,i4,i5,i6;
ti=time_so_far();
f1=fopen(“/proc/stat”,”r”);
fscanf(f1,”%s\t%d\t%d\t%d\n”,c,i1,i2,i3);
fclose(f1);
printf(“%s\t%d\t%d\t%d\n”,c,i1,i2,i3);
usleep(1000000);
tf=time_so_far();
f1=fopen(“/proc/stat”,”r”);
fscanf(f1,”%s\t%d\t%d\t%d\n”,c,i4,i5,i6);
fclose(f1);
printf(“%s\t%d\t%d\t%d\n”,c,i4,i5,i6);
t=(i4+i5+i6)-(i1+i2+i3);
printf(“%d\n”,t);
printf(“cpu usage: %.2f%%\n”,( t/((tf-ti)*100) )*100 );
}
float get_memory_rate(){
FILE *f1;
int itemp1,itemp2;
char c[10],d[10];
f1=fopen(“/proc/meminfo”,”r”);
fscanf(f1,”%s\t%d\t%s”,c,itemp1,d);
printf(“memory total is %d Kb\n”,itemp1);
printf(“memory total is %.2f Mb\n”,itemp1/1024.0);
fscanf(f1,”%s\t%d\t%s”,c,itemp2,d);
printf(“memory free is %d Kb\n”,itemp2);
printf(“memory free is %.2f Mb\n”,itemp2/1024.0);
fclose(f1);
printf(“men usage : %.2f%%\n”,((itemp1-itemp2)*100.0)/itemp1);
}
float get_disk_rate(){
struct statfs *fs;
long long blocks,bfree;
if(statfs(“/”,fs) != 0)
{
perror(“stafts”);
printf(“exit\n”);
exit(1);
}
blocks=fs-f_blocks;
bfree=fs-f_bfree;
//if(fs.f_type == EXT2_SUPER_MAGIC)
//{
printf(“Disk size of / is %.2f G\n”,blocks*fs-f_bsize/Gsize);
printf(“Free Disk size of / is %.2f G\n”,bfree*fs-f_bsize/Gsize);
printf(“Disk usage of / is %.2f%% \n”,bfree*100.0/blocks);
//}
}
Linux系统下如何用C/C++语言编写程序查看cpu_id的值
#include stdio.h
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
/* ecx is often an input as well as an output. */
asm volatile(“cpuid”
: “=a” (*eax),
“=b” (*ebx),
“=c” (*ecx),
“=d” (*edx)
: “0” (*eax), “2” (*ecx));
}
int main(int argc, char **argv)
{
unsigned eax, ebx, ecx, edx;
eax = 1; /* processor info and feature bits */ native_cpuid(eax, ebx, ecx, edx);
printf(“stepping %d\n”, eax 0xF);
printf(“model %d\n”, (eax 4) 0xF);
printf(“family %d\n”, (eax 8) 0xF);
printf(“processor type %d\n”, (eax 12) 0x3);
printf(“extended model %d\n”, (eax 16) 0xF);
printf(“extended family %d\n”, (eax 20) 0xFF);
}
linuxc语言实现查看cpu型号的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux系统如何查看cpu型号、linuxc语言实现查看cpu型号的信息别忘了在本站进行查找喔。