16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

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.

Buffer Cache<br />

"getblk" to allocate a buffer for a disk block, <strong>the</strong> kernel:<br />

(1) finds <strong>the</strong> block on its hash queue, and its buffer is free.<br />

(2) cannot find <strong>the</strong> block on <strong>the</strong> hash queue, it allocates a buffer from <strong>the</strong> free list.<br />

(3) same as (2), but finds a buffer on free list marked "delayed-write", must write to disk and allocate ano<strong>the</strong>r.<br />

(4) same as (2), but free list is empty<br />

(5) finds <strong>the</strong> block on its hash queue, but its buffer is busy.<br />

In (1) <strong>the</strong> kernel marks <strong>the</strong> buffer busy and<br />

removes it from <strong>the</strong> free list. If o<strong>the</strong>r<br />

processes attempt to access <strong>the</strong> block, <strong>the</strong>y<br />

sleep until it is released.<br />

Figure 66. First Scenario in Finding a Buffer:<br />

Buffer on Hash Queue<br />

algorithm brelse<br />

input: locked buffer<br />

output: none<br />

{<br />

wakeup all procs: event, waiting for any buffer to become free;<br />

wakeup all procs: event, waiting for this buffer to become free;<br />

raise processor execution level to block interrupts;<br />

if (buffer contents valid and buffer not old)<br />

enqueue buffer at end of free list<br />

else<br />

enqueue buffer at beginning of free list lower processor<br />

execution level to allow interrupts;<br />

unlock(buffer);<br />

}<br />

"brelse" to release buffer when<br />

kernel is finished using it.<br />

Figure 3.6. Algorithm for Releasing a<br />

Buffer<br />

It wakes up processes that had fallen asleep because <strong>the</strong> buffer was busy, and processes that fallen asleep because<br />

no buffers remained on <strong>the</strong> free list.<br />

182

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

Saved successfully!

Ooh no, something went wrong!