Home > OS >  About using statfs calculate the capacity of the doubt
About using statfs calculate the capacity of the doubt

Time:09-20

Hi:
I want to get the capacity of the disk with C language, I use the statfs function,
 
Statfs ("/dev/sda1, & amp; DiskInfo);
TotalBlocks=diskInfo. F_bsize;
TotalSize=totalBlocks * diskInfo. F_blocks;
MbTotalsize=totalSize> 20;
FreeDisk=diskInfo. F_bfree * totalBlocks;
MbFreedisk=freeDisk> 20;
Printf ("/dev/sda total=% luMB, free=% luMB \ n ", mbTotalsize, mbFreedisk);

Print out the results as follows:/dev/sda1 total=3961 MB, free=3961 MB
If I change the path to/dev/sda, the same numerical,
If I change to the/boot, print the results are as follows: the/boot total=99657 MB, free=90006 MB, calculated should be 97.32 G and 87.9 G
And I use fdisk to see, it is

Disk/dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512=512 bytes
Sector size (logical/physical) : 512 bytes/512 bytes
I/O size (minimum/optimal) : 512 bytes/512 bytes
DOS Disklabel type:
Disk identifier: 0 x44ab91f2

And a few sda is
Device Boot Sectors Start End Size Id Type
/dev/sda1 * 2048, 207620095, 207618048, 99 g 83 Linux
/dev/sda2 207622142 209713151 2091010 1021 m 5 Extended
/dev/sda5 207622144, 209713151, 2091008, 1021 m 82 Linux swap/Solaris

By contrast, fdisk view to/dev/sda1 size and I test it out/boot partition size closest to, but still there is a difference of 2 g, I print the f_bsize and f_blocks, the result is the bsize is 4096, blocks is 1014209,
Code should be no problem, the problem come out to my understanding of the partition size,
Who can help me, in a nutshell,
1. Why is my path is set to/dev/sda (1), the size is 3961 m, rather than the fdisk printed 99 g?
2. Why the/boot is 97.32 G, not 99 G? The/boot 97.32 G come from?
3. Why/dev/sda is 100 g, rather than the sda1, sda2, sda5 sum? Is not this a few partition of sda?
  • Related