16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Buffer Cache<br />

algorithm breada /* block read and read ahead */<br />

input: (1) file system block number for immediate read<br />

(2) file system block number for asynchronous read<br />

output: buffer containing data for immediate read<br />

{<br />

if (first block not in cache)<br />

{<br />

get buffer for first block (algorithm getblk);<br />

if (buffer data not valid)<br />

initiate disk read;<br />

}<br />

if (second block not in cache)<br />

{<br />

get buffer for second block (algorithm getblk);<br />

if (buffer data valid)<br />

release buffer (algorithm brelse);<br />

else<br />

initiate disk read;<br />

}<br />

if (first block was originally in cache)<br />

{<br />

read first block (algorithm bread);<br />

return buffer;<br />

}<br />

sleep(event first buffer contains valid data);<br />

return buffer;<br />

}<br />

Figure 74. Algorithm for Block<br />

"breada".<br />

Read Ahead<br />

If <strong>the</strong> second block is not in buffer cache, <strong>the</strong><br />

kernel instructs <strong>the</strong> disk driver to read it<br />

asynchronously.<br />

algorithm bwrite /* block write * /<br />

input: buffer<br />

output: none<br />

{<br />

initiate disk write;<br />

if (I/O synchronous)<br />

{<br />

sleep(event I/O complete);<br />

release buffer (algorithm brelse);<br />

}<br />

else<br />

if (buffer marked for delayed write)<br />

mark buffer to put at head of free list;<br />

}<br />

Figure 75. Writing a Disk Block Bach, "bwrite".<br />

If <strong>the</strong> write as asynchronous, <strong>the</strong> kernel starts <strong>the</strong> disk write but does not wait for <strong>the</strong> write to complete. The kernel<br />

will release <strong>the</strong> buffer when I/O completes.<br />

The kernel marks <strong>the</strong> buffer "delayed-write" and releases <strong>the</strong> buffer using "brelse". The kernel writes <strong>the</strong> block to<br />

disk before ano<strong>the</strong>r process can reallocate <strong>the</strong> buffer.<br />

Advantages and Disadvantages of Buffer Cache<br />

- use of buffers allows uniform disk access, (data is part of a file, an inode, or a super block) (simpler system<br />

design).<br />

- system places no data alignment restrictions on user processes doing I/O (because <strong>the</strong> kernel aligns data<br />

internally).<br />

- use of buffer cache can reduce <strong>the</strong> amount of disk traffic (increasing throughput and decreasing response time)<br />

("delayed write" avoids unnecessary disk writes) (amount of memory available for buffers).<br />

- buffer algorithms help insure file system integrity (serialize process access - preventing data corruption).<br />

- reduction of disk traffic (vulnerable to crashes that leave disk in an incorrect state).<br />

- use of buffer cache requires an extra data copy when reading and writing to and from user processes (for large<br />

amounts of data - slows down performance) (small amounts - improves performance - cache, delayed write).<br />

Summary<br />

The kernel uses least recently used replacement to keep blocks in buffer cache, assuming that blocks that were<br />

recently accessed are likely to be accessed again soon.<br />

The hash function and hash queues enable <strong>the</strong> kernel to find particular blocks quickly, and use of doubly linked<br />

lists makes it easy to remove buffers from <strong>the</strong> lists.<br />

The kernel identifies <strong>the</strong> block it needs by logical device and block number. "getblk" searches buffer cache for a<br />

block, if present and free, locks <strong>the</strong> buffer and returns it.<br />

If <strong>the</strong> buffer is locked, <strong>the</strong> requesting process sleeps until it becomes free.<br />

If <strong>the</strong> block is not in <strong>the</strong> cache, <strong>the</strong> kernel reassigns a free buffer to <strong>the</strong> block, locks it and returns.<br />

If kernel determines that is not necessary to copy data immediately to disk, it marks <strong>the</strong> buffer "delayed-write". A<br />

process is not sure when <strong>the</strong> data is physically on disk.<br />

186

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

Saved successfully!

Ooh no, something went wrong!