11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

274 Chap. 8 File Processing <strong>and</strong> External SortingThis example illustr<strong>at</strong>es why it is important to keep disk files from becomingfragmented, <strong>and</strong> why so-called “disk defragmenters” can speed upfile processing time. File fragment<strong>at</strong>ion happens most commonly when thedisk is nearly full <strong>and</strong> the file manager must search for free space whenevera file is cre<strong>at</strong>ed or changed.8.3 Buffers <strong>and</strong> Buffer PoolsGiven the specific<strong>at</strong>ions of the disk drive from Example 8.1, we find th<strong>at</strong> it takesabout 9.5+11.1×1.5 = 26.2 ms to read one track of d<strong>at</strong>a on average. It takes about9.5+11.1/2+(1/256)×11.1 = 15.1 ms on average to read a single sector of d<strong>at</strong>a.This is a good savings (slightly over half the time), but less than 1% of the d<strong>at</strong>a onthe track are read. If we want to read only a single byte, it would save us effectivelyno time over th<strong>at</strong> required to read an entire sector. For this reason, nearly all diskdrives autom<strong>at</strong>ically read or write an entire sector’s worth of inform<strong>at</strong>ion wheneverthe disk is accessed, even when only one byte of inform<strong>at</strong>ion is requested.Once a sector is read, its inform<strong>at</strong>ion is stored in main memory. This is knownas buffering or caching the inform<strong>at</strong>ion. If the next disk request is to th<strong>at</strong> samesector, then it is not necessary to read from disk again because the inform<strong>at</strong>ionis already stored in main memory. Buffering is an example of one method forminimizing disk accesses mentioned <strong>at</strong> the beginning of the chapter: Bring offadditional inform<strong>at</strong>ion from disk to s<strong>at</strong>isfy future requests. If inform<strong>at</strong>ion from fileswere accessed <strong>at</strong> r<strong>and</strong>om, then the chance th<strong>at</strong> two consecutive disk requests are tothe same sector would be low. However, in practice most disk requests are closeto the loc<strong>at</strong>ion (in the logical file <strong>at</strong> least) of the previous request. This means th<strong>at</strong>the probability of the next request “hitting the cache” is much higher than chancewould indic<strong>at</strong>e.This principle explains one reason why average access times for new disk drivesare lower than in the past. Not only is the hardware faster, but inform<strong>at</strong>ion is alsonow stored using better algorithms <strong>and</strong> larger caches th<strong>at</strong> minimize the number oftimes inform<strong>at</strong>ion needs to be fetched from disk. This same concept is also usedto store parts of programs in faster memory within the CPU, using the CPU cacheth<strong>at</strong> is prevalent in modern microprocessors.Sector-level buffering is normally provided by the oper<strong>at</strong>ing system <strong>and</strong> is oftenbuilt directly into the disk drive controller hardware. Most oper<strong>at</strong>ing systemsmaintain <strong>at</strong> least two buffers, one for input <strong>and</strong> one for output. Consider wh<strong>at</strong>would happen if there were only one buffer during a byte-by-byte copy oper<strong>at</strong>ion.The sector containing the first byte would be read into the I/O buffer. The outputoper<strong>at</strong>ion would need to destroy the contents of the single I/O buffer to write thisbyte. Then the buffer would need to be filled again from disk for the second byte,

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

Saved successfully!

Ooh no, something went wrong!