Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 14.9 Summary 531


system call and extra copying differs from the page fault overhead, then one approach
will perform better than the other.
On Linux 3.2.0, as far as elapsed time is concerned, the two versions of the program
show a large difference in clock time: the version usingreadandwritecompletes four
times faster than the version usingmmapandmemcpy.However, on Solaris 10, the
version withmmapandmemcpyis faster than the version withreadandwrite.Ifthe
CPU times arealmost the same, then why would the clock times differ? One possibility
is that we might have to wait longer for I/O to complete in one version. This wait time
is not counted as CPU processing time. Another possibility is that some system
processing might not be counted against our program — the processing done by system
daemons to write pages to disk, for example. As we need to allocate pages for reading
and writing, these system daemons will help make pages available. If the page writes
arerandom instead of sequential, then it will take longer to write them out to disk, so
we will need to wait longer beforethe pages become available for us to reuse.

Depending on the system, memory-mapped I/O can be moreefficient when
copying one regular file to another.Thereare limitations. Wecan’t use this technique to
copy between certain devices (such as a network device or a terminal device), and we
have to be careful if the size of the underlying file could change after we map it.
Nevertheless, some applications can benefit from memory-mapped I/O, as it can often
simplify the algorithms, since we manipulate memory instead of reading and writing a
file. One example is the manipulation of a frame buffer device that references a
bitmapped display.
Krieger,Stumm, and Unrau[ 1992 ] describe an alternative to the standardI/O
library (Chapter 5) that uses memory-mapped I/O.
We return to memory-mapped I/O in Section 15.9, showing an example of how it
can be used to provide shared memory between related processes.

14.9 Summary


In this chapter,we’ve described numerous advanced I/O functions, many of which are
used in the examples in later chapters:

•Nonblocking I/O—issuing an I/O operation without letting it block
•Recordlocking (which we’ll look at in moredetail through an example, the
database library in Chapter 20)
•I/O multiplexing—theselectandpollfunctions (we’ll use these in many of
the later examples)
•Asynchronous I/O
•Thereadvandwritevfunctions (also used in many of the later examples)
•Memory-mapped I/O (mmap)
Free download pdf