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

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

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

Sec. 8.3 Buffers <strong>and</strong> Buffer Pools 279Example 8.4 Assume each sector of the disk file (<strong>and</strong> thus each block inthe buffer pool) stores 1024 bytes. Assume th<strong>at</strong> the buffer pool is in thest<strong>at</strong>e shown in Figure 8.5. If the next request is to copy 40 bytes beginning<strong>at</strong> position 6000 of the file, these bytes should be placed into Sector 5(whose bytes go from position 5120 to position 6143). Because Sector 5is currently in the buffer pool, we simply copy the 40 bytes contained inspace to byte positions 880-919. The buffer containing Sector 5 is thenmoved to the buffer pool ahead of the buffer containing Sector 1.An altern<strong>at</strong>ive interface is to have the buffer pool provide to the user a directpointer to a buffer th<strong>at</strong> contains the requested inform<strong>at</strong>ion. Such an interface mightlook as follows:/** ADT for buffer pools using the buffer-passing style */public interface BufferPoolADT {/** Return pointer to the requested block */public byte[] getblock(int block);/** Set the dirty bit for the buffer holding "block" */public void dirtyblock(int block);// Tell the size of a bufferpublic int blocksize();};In this approach, the buffer pool user is made aware th<strong>at</strong> the storage space isdivided into blocks of a given size, where each block is the size of a buffer. The userrequests specific blocks from the buffer pool, with a pointer to the buffer holdingthe requested block being returned to the user. The user might then read from orwrite to this space. If the user writes to the space, the buffer pool must be informedof this fact. The reason is th<strong>at</strong>, when a given block is to be removed from the bufferpool, the contents of th<strong>at</strong> block must be written to the backing storage if it has beenmodified. If the block has not been modified, then it is unnecessary to write it out.Example 8.5 We wish to write 40 bytes beginning <strong>at</strong> logical position6000 in the file. Assume th<strong>at</strong> the buffer pool is in the st<strong>at</strong>e shown in Figure8.5. Using the second ADT, the client would need to know th<strong>at</strong> blocks(buffers) are of size 1024, <strong>and</strong> therefore would request access to Sector 5.A pointer to the buffer containing Sector 5 would be returned by the call togetblock. The client would then copy 40 bytes to positions 880-919 ofthe buffer, <strong>and</strong> call dirtyblock to warn the buffer pool th<strong>at</strong> the contentsof this block have been modified.

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

Saved successfully!

Ooh no, something went wrong!