Performance-friendly I/O interfaces
For applications with high input/output performance requirements
(including network I/O), it is worthwhile to look at operating system
support for efficient I/O routines.
As an example, here is simple pseudo-code that reads the contents of
an open file
in and writes them to an open socket
out - this code
could be part of a file server. A straightforward way of coding this
read()/write() system calls to copy the bytes through a
Unfortunately, this common programming paradigm results in high memory traffic and inefficient use of a system's caches. Also, if a small buffer is used, the number of system operations and, in particular, of user/kernel context switches will be quite high.
On systems that support memory mapping of files using
mmap() , the following is more efficient if the source is an actual file:
An even more efficient
- and also more concise - variant is the
sendfile() call, which directly copies the bits from the file to the network.
Note that an operating system could optimize this internally up to the point where data blocks are copied directly from the disk controller to the network controller without any involvement of the CPU.
For more complex situations, the
sendfilev() interface can be used to send data from multiple files and memory buffers to construct complex protocol units with a single call.
– Main.SimonLeinen - 26 Jun 2005
One thing to note, the above usage of write and sendfile is simplified, these system-calls can stop in the middle and return the number of bytes written, for real-world usage you should have a loop around them to continue sending the rest of the file and handle signal errors.
The first loop should be written as:
OpenSolaris, 2008. This document contains, in section 4.5 ("zero-copy interface") a description of how
sendfilevis implemented in current Solaris, as well as suggestions on generalizing the internal kernel interfaces that support it.
– Main.BaruchEven - 05 Jan 2006
-- Main.SimonLeinen - 12 Sep 2008