12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

290 Chap. 8 File Processing <strong>and</strong> External Sortingtakes maximum advantage of this micro-parallelism is double buffering. Imaginethat a file is being processed sequentially. While the first sec<strong>to</strong>r is being read, theCPU cannot process that information <strong>and</strong> so must wait or find something else <strong>to</strong> doin the meantime. Once the first sec<strong>to</strong>r is read, the CPU can start processing whilethe disk drive (in parallel) begins reading the second sec<strong>to</strong>r. If the time required forthe CPU <strong>to</strong> process a sec<strong>to</strong>r is approximately the same as the time required by thedisk controller <strong>to</strong> read a sec<strong>to</strong>r, it might be possible <strong>to</strong> keep the CPU continuouslyfed with data from the file. The same concept can also be applied <strong>to</strong> output, writingone sec<strong>to</strong>r <strong>to</strong> disk while the CPU is writing <strong>to</strong> a second output buffer in memory.Thus, in computers that support double buffering, it pays <strong>to</strong> have at least two inputbuffers <strong>and</strong> two output buffers available.Caching information in memory is such a good idea that it is usually extended<strong>to</strong> multiple buffers. The operating system or an application program might s<strong>to</strong>remany buffers of information taken from some backing s<strong>to</strong>rage such as a disk file.This process of using buffers as an intermediary between a user <strong>and</strong> a disk file iscalled buffering the file. The information s<strong>to</strong>red in a buffer is often called a page,<strong>and</strong> the collection of buffers is called a buffer pool. The goal of the buffer poolis <strong>to</strong> increase the amount of information s<strong>to</strong>red in memory in hopes of increasingthe likelihood that new information requests can be satisfied from the buffer poolrather than requiring new information <strong>to</strong> be read from disk.As long as there is an unused buffer available in the buffer pool, new informationcan be read in from disk on dem<strong>and</strong>. When an application continues <strong>to</strong> readnew information from disk, eventually all of the buffers in the buffer pool will becomefull. Once this happens, some decision must be made about what informationin the buffer pool will be sacrificed <strong>to</strong> make room for newly requested information.When replacing information contained in the buffer pool, the goal is <strong>to</strong> select abuffer that has “unnecessary” information, that is, that information least likely <strong>to</strong> berequested again. Because the buffer pool cannot know for certain what the patternof future requests will look like, a decision based on some heuristic, or best guess,must be used. There are several approaches <strong>to</strong> making this decision.One heuristic is “first-in, first-out” (FIFO). This scheme simply orders thebuffers in a queue. The buffer at the front of the queue is used next <strong>to</strong> s<strong>to</strong>re newinformation <strong>and</strong> then placed at the end of the queue. In this way, the buffer <strong>to</strong> bereplaced is the one that has held its information the longest, in hopes that this informationis no longer needed. This is a reasonable assumption when processingmoves along the file at some steady pace in roughly sequential order. However,many programs work with certain key pieces of information over <strong>and</strong> over again,<strong>and</strong> the importance of information has little <strong>to</strong> do with how long ago the informa-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!