使用C/C ++的POSIX上的物理磁盘块大小 [英] Physical disk block size on POSIX using C/C++

查看:147
本文介绍了使用C/C ++的POSIX上的物理磁盘块大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发高性能I/O程序,并且我试图找到确定物理(而不是逻辑)字节的最佳方法使用C ++的设备磁盘块的大小.到目前为止,我的研究使我想到了以下代码片段:

I'm working on a high performance I/O program and I'm trying to find the best way to determine the physical (and not the logical) byte size of a device's disk blocks with C++. My research so far has led me to the following code snippet:

#include <iostream>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char ** argv)
{
// file information including block size of the device
struct stat info;
// device to get block size from
char * device = "/mnt/hdb1";

if (stat(device, &info))
{
printf("stat() error");
strerror(errno);
exit(1);
}
printf("Prefered block size for '%s' is %i byte\n", device, info.st_blksize);
return 0;
}

手册页中关于st_blksize的内容如下:

The man pages says the following about st_blksize:

st_blksize字段提供首选"块大小,以提高效率 文件系统I/O. (以较小的块写入文件可能会导致 低效的读取-修改-重写.)

The st_blksize field gives the "preferred" blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)

,但没有提及st_blksize是逻辑磁盘块大小还是物理磁盘块大小.

, but it does not mention if st_blksize is the logical or the physical disk block size.

因此,st_blksize physical 磁盘块大小,如果是,则这是检测物理磁盘块大小的最POSIX OS可移植方式.

So, is st_blksize the physical disk block size, and if so, then is this the most POSIX OS portable way of detecting the physical disk block size.

推荐答案

我写了一个答案,虽然希望不能对块设备正确工作.

I wrote an answer, that while hopeful did not work correctly for block devices.

没有用于获取设备基本物理块大小的POSIX机制,您将不得不求助于ioctl,这取决于平台.

There is no POSIX mechanism for obtaining the fundamental physical block size of a device, you will have to resort to ioctl, which is platform dependent.

对于linux,有 ioctl(fd, BLKPBSZGET, &block_size)

For linux there's ioctl(fd, BLKPBSZGET, &block_size)

对于Solaris,有 dkio 界面,该界面可让您获取物理块大小.

For Solaris there's the dkio interface, which allows you to get the physical block size.

dk_minfo_ext media_info;
if (-1 != ioctl(fd, DKIOMEDIAINFOEXT, &media_info))
    block_size = media_info.dki_pbsize;

对于Mac OS X,它是 ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size) .

For Mac OS X, it's ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size).

对于FreeBSD,它应该为 iotcl(fd, DIOCGSECTORSIZE, &block_size) .

For FreeBSD, it should be iotcl(fd, DIOCGSECTORSIZE, &block_size).

这篇关于使用C/C ++的POSIX上的物理磁盘块大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆