当前位置: 技术问答>linux和unix
借这里的技术含量,小弟提问如下,希望高手留言
来源: 互联网 发布时间:2015-09-04
本文导语: 怎样在程序中获取主板上的信息,比如CPU的ID号和主频呀,PCI插槽有几个呀等等! 当然能获取的越详细就越好! 请各位高手留言 | 以下我所写的一个程序,你只需简单的修改一下就可以得到运...
怎样在程序中获取主板上的信息,比如CPU的ID号和主频呀,PCI插槽有几个呀等等!
当然能获取的越详细就越好!
请各位高手留言
当然能获取的越详细就越好!
请各位高手留言
|
以下我所写的一个程序,你只需简单的修改一下就可以得到运行的结果.
FILE* fp;
char buffer[256];
size_t bytes_read;
char* match ;
float clock_speed;
int processor_count , cache_size;
char tmp[10][128] ;
char vendor[128] , model_name[128] ;
// Read the entire contents of /proc/cpuinfo into the buffer.
fp = fopen ("/proc/cpuinfo", "r");
bytes_read = fread (buffer, 1, sizeof (buffer), fp);
fclose (fp);
// Bail if read failed or if buffer isn’t big enough.
if (bytes_read == 0 || bytes_read == sizeof (buffer))
{
fprintf(stderr,"buffer isn’t big enough!n");
}
// NUL-terminate the text.
buffer[bytes_read] = '';
// Locate the line that starts with "processor".
match = strstr (buffer, "processor");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line processor!n");
}
else
{
// Parse the line to extract the processor.
sscanf (match, "processor : %d", &processor_count);
sprintf(tmp[0], "%d#",processor_count+1);
}
// Locate the line that starts with "vendor_id".
match = strstr (buffer, "vendor_id");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line vendor id!n");
}
else
{
// Parse the line to extract the vendor_id.
sscanf (match, "vendor_id : %s", vendor);
sprintf(tmp[1], "%s#", vendor);
}
// Locate the line that starts with "model_name".
match = strstr (buffer, "model name");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line model name!n");
}
else
{
// Parse the line to extract the model_name.
sscanf (match, "model name : %s", model_name);
sprintf(tmp[2], "%s#", model_name);
}
// Locate the line that starts with "cpu MHz".
match = strstr (buffer, "cpu MHz");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line cpu MHz!n");
}
else
{
// Parse the line to extract the clock speed.
sscanf (match, "cpu MHz : %f", &clock_speed);
sprintf(tmp[3], "%5.2fMHz#",clock_speed);
}
// Locate the line that starts with "cache size".
match = strstr (buffer, "cache size");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line cache size!n");
}
else
{
// Parse the line to extract the cache size.
sscanf (match, "cache size : %d", &cache_size);
sprintf(tmp[4], "%dKB#",cache_size);
}
FILE* fp;
char buffer[256];
size_t bytes_read;
char* match ;
float clock_speed;
int processor_count , cache_size;
char tmp[10][128] ;
char vendor[128] , model_name[128] ;
// Read the entire contents of /proc/cpuinfo into the buffer.
fp = fopen ("/proc/cpuinfo", "r");
bytes_read = fread (buffer, 1, sizeof (buffer), fp);
fclose (fp);
// Bail if read failed or if buffer isn’t big enough.
if (bytes_read == 0 || bytes_read == sizeof (buffer))
{
fprintf(stderr,"buffer isn’t big enough!n");
}
// NUL-terminate the text.
buffer[bytes_read] = '';
// Locate the line that starts with "processor".
match = strstr (buffer, "processor");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line processor!n");
}
else
{
// Parse the line to extract the processor.
sscanf (match, "processor : %d", &processor_count);
sprintf(tmp[0], "%d#",processor_count+1);
}
// Locate the line that starts with "vendor_id".
match = strstr (buffer, "vendor_id");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line vendor id!n");
}
else
{
// Parse the line to extract the vendor_id.
sscanf (match, "vendor_id : %s", vendor);
sprintf(tmp[1], "%s#", vendor);
}
// Locate the line that starts with "model_name".
match = strstr (buffer, "model name");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line model name!n");
}
else
{
// Parse the line to extract the model_name.
sscanf (match, "model name : %s", model_name);
sprintf(tmp[2], "%s#", model_name);
}
// Locate the line that starts with "cpu MHz".
match = strstr (buffer, "cpu MHz");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line cpu MHz!n");
}
else
{
// Parse the line to extract the clock speed.
sscanf (match, "cpu MHz : %f", &clock_speed);
sprintf(tmp[3], "%5.2fMHz#",clock_speed);
}
// Locate the line that starts with "cache size".
match = strstr (buffer, "cache size");
if (match == NULL)
{
fprintf(stderr,"Don't locate the line cache size!n");
}
else
{
// Parse the line to extract the cache size.
sscanf (match, "cache size : %d", &cache_size);
sprintf(tmp[4], "%dKB#",cache_size);
}