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 />

Process B will find <strong>the</strong> locked block on <strong>the</strong> hash queue. Process B marks <strong>the</strong> buffer "in demand" and <strong>the</strong> sleeps.<br />

Ano<strong>the</strong>r process C, may have been waiting for <strong>the</strong> same buffer, if C is scheduled before B, B must check <strong>the</strong> block<br />

is free.<br />

Process C may allocate <strong>the</strong> buffer to ano<strong>the</strong>r block, so when process B executes it must search for <strong>the</strong> block again.<br />

With contention for a locked buffer need to start search again.<br />

Figure 72. Race for a Locked Buffer.<br />

The kernel guarantees that all<br />

processes waiting for buffers will wake<br />

up, because it allocates buffers during<br />

<strong>the</strong> execution of system calls and frees<br />

<strong>the</strong>m before returning.<br />

Processes in user mode do not control <strong>the</strong> allocation of kernel buffers directly, so <strong>the</strong>y cannot purposely "hog"<br />

buffers.<br />

The kernel does not guarantee that a process get a buffer in <strong>the</strong> order that <strong>the</strong>y requested one.<br />

Reading and Writing Disk Blocks<br />

algorithm bread * block read */<br />

input: file system block number<br />

output: buffer containing data<br />

{<br />

get buffer for block (algorithm<br />

getblk);<br />

if (buffer data valid)<br />

return buffer;<br />

initiate disk read;<br />

sleep(event disk read complete);<br />

return (buffer);<br />

Figure 73. Reading a Disk Block Bach, "bread".<br />

If <strong>the</strong> disk block is not in cache, <strong>the</strong> kernel calls <strong>the</strong> disk driver to "schedule" a read request and goes to sleep<br />

awaiting <strong>the</strong> event that <strong>the</strong> I/O completes.<br />

185

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

Saved successfully!

Ooh no, something went wrong!