POSIX physical disk size using C / C ++

I am working on a high-performance I / O program, and I am trying to find a better way to determine the physical (rather than logical) size of the bytes of the deviceโ€™s disk blocks using C ++. My research so far has led me to the following piece of code:

#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;
}

The man pages say the following about st_blksize:

The st_blksize field gives the "preferred" block size for efficient file system I / O. (Writing to a file in smaller fragments can result in inefficient read-modify-rewrite.)

but does not mention that st_blksizeis the size of a logical or physical disk.

, st_blksize, , POSIX OS .

+5
1

, , , .

POSIX , ioctl, .

linux ioctl(fd, BLKPBSZGET, &block_size)

Solaris dkio , .

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).

FreeBSD iotcl(fd, DIOCGSECTORSIZE, &block_size).

+5

All Articles