ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Bytes in a Physical I/O Block
I'm converting some utilities from Solaris to Linux and have come across one of those "little differences" (gotchas!) between System V and Linux...
I can't seem to find (in spite of a lot of looking, searching and fooling around) the equivalent of STD_BLK, the number of bytes in a physical I/O block. I pretty much know it's going to be either 512 or 1024, but would like to know the token in some system header file (if there is one); it's used something like this
Code:
...
/*
* keep copying until empty; nread should equal
* STD_BLK except on the last read
*/
while ((nread = read (ifdesc, buf, STD_BLK)) > 0)
(void) write (ofdesc, buf, nread);
...
1. There is no global "BLOCK_SIZE" compile-time constant because the block size obviously can (and probably will!) vary depending on where your file happens to be located (for example, /dev/hdb1 might have a different block size from your CD drive, which will have a different blocksize than a remote network share).
2. The whole *point* of "file streams" is to abstract AWAY implementation details like "physical block size" (and location!).
3. As far as real-world performance considerations, trying to map your program's user-space "read's()" to physical blocks will probably be futile. It's unlikely you'll get *any* performance gains - and you might wind up over-complicating your code.
ANYWAY:
The answer to your question is "(ioctl, FIGETBSZ, &blocksize)"
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
STD_BLK is part of the SVR4 interface definition (SVID) so this constant defined in limit.h is shared between several Unix variants based on System V. As far as I know, it doesn't serve any useful purpose outside being there to comply with the standard. Your utility that relies upon it would probably equally works but faster with a larger block size.
Last edited by jlliagre; 08-23-2009 at 08:16 AM.
Reason: typo
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Original Poster
Rep:
Oh, gosh, didn't mean to start a firestorm, all I was looking for was a more-or-less-standard token (like BUFSIZ and other handy defined token values that make life and portability a little easier). I know that STD_BLK is a SVR4-defined token particular to individual platform implementations and I was kind of hoping that there was something similar in Linux and that I just hadn't been able to find it; only looking for portability, not blazing performance (I/O with read() and its close relatives is, I think, just about as fast as his little feet will go in any event). A call to stat() returns st_blksize, the blocksize for file system I/O in the status structure, and that would most likely be dynamic enough (and cover all the bases for every type of file media) to "emulate" STD_BLK. As it turns out, the blocksize on this machine is 4096 as reported by a call to stat(), not my WAG of 512 or 1024.
Thanks for the interesting discussion; as usual, I learn more than I knew.
Y; 4k is pretty std(ish), but you might see 8k (or even larger) on DB dedicated disks.
I certainly wouldn't rely on it being a specific size if I could avoid it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.