A warning about read(2)/write(2)

Stephen Carroll sbc at sp7040.UUCP
Sat Jun 18 06:33:28 AEST 1988


In article <122 at quintus.UUCP>, ok at quintus writes:
> 
> stuff deleted....
> 
> THAT WAS A BAD IDEA.
> 
> If you want to make your program's I/O more efficient, simply replacing
> stdio calls by low-level UNIX calls is not a good idea.  If you use
> stdio, that package will do I/O a buffer at a time, so you only get a
> system call (and a disc access) when a buffer is filled or emptied.
> 
> The package I am talking about was reading and writing 56 byte chunks,
> so it was doing a system call and a disc access for every 56 bytes.
> (And since 56 does not divide 8192 evenly, some of these transfers
> could cost two disc accesses.)
> 

WRONG!!!! You could read and write 1 byte at a time, but this does not create
a disk access for each system call.  It is true that read/write causes a
system call on each invocation, as compared to stdio, but Unix DOES NOT read
and write the disk on each of those system calls.  The kernel will buffer at
least on block of data at a time, and more likely two blocks with sequential
reads and disk read-aheads.  It is true that the greater number of system calls
and context switches will tend to slow the program down, but they will not cause
more disk accesses.  The only case where this would be true would be on a loaded
system where kernel buffers are at a premium and have to be flushed very often.

Of course if you were accessing the disk via the raw interface (character 
special device), this is then true.  But of course the stdio library would not
work in this case, would it!


MAIL:	Steve B. Carroll			PHONE: (801) 594 5623
	Unisys Corporation, MS B2F12		FAX:   (801) 594 5660/4750/5174
	322 North 2200 West
	Salt Lake City, UT  84116-2979		TELEX: (310) 3789522 UNIVAC SLC
UUCP:   ...!{ihnp4 | hpda | sun }!sp7040!sbc



More information about the Comp.unix.questions mailing list