sor*_*h-r 9 c++ linux hardware hard-drive
我需要在Win和*nix机器上获得硬盘规格.我<hdreg.h>在Linux上使用过这样的:
static struct hd_driveid hd;
int device;
if ((device = open("/dev/sda", O_RDONLY | O_NONBLOCK)) < 0)
{
cerr << "ERROR: Cannot open device /dev/sda \n";
exit(1);
}
if (!ioctl(device, HDIO_GET_IDENTITY, &hd))
{
cout << hd.model << endl;
cout << hd.serial_no << endl;
cout << hd.heads << endl;
}
Run Code Online (Sandbox Code Playgroud)
我需要hd_driveid告诉我一些关于磁盘的更多信息.我想知道:
我的问题是:
列表中几乎所有内容都与"硬盘规格"无关:
hd_driveid.sector_bytes(通常为512,但一些现代驱动器使用4096).我不知道最大的"块大小",这是文件系统的属性.我也不确定为什么这很有用.各行业的总规模是hd_driveid.lba_capacity_2.此外,可以通过类似的方式获得以字节为单位的大小
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <unistd.h>
...
off_t size_in_bytes = lseek(device, 0, SEEK_END);
if (size_in_bytes == (off_t)-1) { ... error, error code in ERRNO ... }
Run Code Online (Sandbox Code Playgroud)
请注意,在这两种情况下,它可能比C×H×S计算的大小大几兆字节.
如果您告诉我们您为什么要这些信息,这可能有所帮助......
//-------------------------------------------------
// Without Boost LIB usage
//-------------------------------------------------
#include <sys/statvfs.h>
#include <sys/sysinfo.h>
//-------------------------------------------------
stringstream strStream;
unsigned long hdd_size;
unsigned long hdd_free;
ostringstream strConvert;
//---
struct sysinfo info;
sysinfo( &info );
//---
struct statvfs fsinfo;
statvfs("/", &fsinfo);
//---
//---
unsigned num_cpu = std::thread::hardware_concurrency();
//---
ifstream cpu_freq("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq");
strStream << cpu_freq.rdbuf();
std::string cpufrequency = strStream.str();
//---
strStream.str("");
ifstream cpu_temp("/sys/class/thermal/thermal_zone0/temp");
strStream << cpu_temp.rdbuf();
strConvert<< fixed << setprecision(2) << std::stof(strStream.str());
std::string cputemp = strConvert.str();
//---
std::string mem_size = to_string( (size_t)info.totalram * (size_t)info.mem_unit );
//---
hdd_size = fsinfo.f_frsize * fsinfo.f_blocks;
hdd_free = fsinfo.f_bsize * fsinfo.f_bfree;
//---
std::cout << "CPU core number ==" << num_cpu << endl;
std::cout << "CPU core speed ==" << cpufrequency << endl;
std::cout << "CPU temperature (C) ==" << cputemp << endl;
//---
std::cout << "Memory size ==" << mem_size << endl;
//---
std::cout << "Disk, filesystem size ==" << hdd_size << endl;
std::cout << "Disk free space ==" << hdd_free << endl;
//---
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11230 次 |
| 最近记录: |