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

13. BUFFER CACHE<br />

The Buffer Cache<br />

When a process wants to access data from a file, <strong>the</strong> kernel brings <strong>the</strong> data into main memory where <strong>the</strong> process<br />

can examine it, alters it and requests <strong>the</strong> data to be saved in <strong>the</strong> filesystem.<br />

The kernel attempts to minimize <strong>the</strong> frequency of disk access by keeping a pool of internal data buffers, called <strong>the</strong><br />

buffer cache, which contains <strong>the</strong> data in recently used disk blocks.<br />

When reading data, if data is already in cache (pre-cache), <strong>the</strong> kernel does not have to read from disk. O<strong>the</strong>rwise<br />

<strong>the</strong> kernel reads <strong>the</strong> data from disk and caches it.<br />

When writing data, data is written to cache to minimize disk writes (delay-write).<br />

Buffer Headers<br />

A buffer consists of two parts:<br />

- <strong>the</strong> memory array that contains data from <strong>the</strong> disk and<br />

- <strong>the</strong> buffer header that identifies <strong>the</strong> buffer.<br />

The buffer is an in-memory copy of <strong>the</strong> disk block. A disk block can never map into more than one buffer at a time.<br />

The buffer header contains:<br />

- device number - logical filesystem number<br />

- block number - from <strong>the</strong> disk<br />

- status<br />

- locked/busy<br />

- valid data<br />

- delayed-write<br />

- currently read/write buffer to disk<br />

- waiting for buffer to free<br />

- pointer to data array for <strong>the</strong> buffer<br />

- pointer to next buffer on hash queue<br />

- pointer to previous buffer on hash queue<br />

- pointer to next buffer on free list<br />

- pointer to previous buffer on free list<br />

Structure of <strong>the</strong> Buffer Pool<br />

The kernel caches data according to least recently used: i.e. it cannot use <strong>the</strong> buffer until all o<strong>the</strong>r buffers have been<br />

used more recently.<br />

The free list is a circular list<br />

of buffers linked both ways,<br />

which uses a dummy buffer<br />

header.<br />

Figure 63. Free List of Buffers<br />

180

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

Saved successfully!

Ooh no, something went wrong!