LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2009, 09:25 AM   #1
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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);
...
Thanks.
 
Old 08-22-2009, 05:02 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Please bear in mind a few points:

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

'Hope that helps .. PSM
 
Old 08-22-2009, 05:51 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 08-22-2009, 06:43 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Neither of the first two Linux boxes/GCC environments I looked at happened to have any "STD_BLK" constant defined.

And, of course, it would be pretty useless even if they did...
 
Old 08-22-2009, 10:01 PM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Original Poster
Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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.

Last edited by tronayne; 08-23-2009 at 08:54 AM.
 
Old 08-24-2009, 01:49 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by paulsm4 View Post
Neither of the first two Linux boxes/GCC environments I looked at happened to have any "STD_BLK" constant defined.
Indeed, Gnu/Linux doesn't pretend nor need to be SVR4 compliant.
Quote:
And, of course, it would be pretty useless even if they did...
Yes, as useless as it is with Solaris
 
Old 08-24-2009, 01:52 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to map a physical disk block to a file squarecmb Linux - General 2 04-12-2007 09:06 PM
st 0: Block limits 1 -16777215 bytes -- what this error means? sathyguy Linux - Enterprise 1 02-10-2007 08:27 AM
WinXP on one physical drive and SUSE 10 on another physical drive flanker12k Linux - Newbie 4 05-13-2006 08:36 AM
TX bytes vs. httpd bytes ovrload Linux - Networking 3 10-12-2005 04:19 PM
1 block = ? bytes captainstorm Linux - Newbie 2 07-08-2003 10:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration