1. Chapter 3-Buffer Cache
1
TOPICS
UNIX system Architecture
Buffer Cache
Buffer Pool Structure
Retrieval of Buffer
Release Buffer
Reading and Writing Disk Blocks
Mrs.Sowmya Jyothi, Faculty, Mangalore
References:
The Design of the UNIX Operating System
by Maurice J. Bach
3. Buffer Cache
In Computer Science, a buffer is a region of a physical memory
storage used to temporarily hold data while it is being moved
from one place to another.
Typically, the data is stored in a buffer as it is retrieved from an
input device (such as a mouse) or just before it is sent to an
output device (such as speakers).
However, a buffer may be used when moving data between
processes within a computer.
Buffer cache contains the data in recently used disk blocks.
When a process wants to access data from a file, the kernel
brings the data into main memory, alters it and then request
to save in the file system.
To increase the response time and throughput, the kernel
minimizes the frequency of disk access by keeping a pool of
internal data buffer called buffer cache.
3
4. Buffer Cache
Buffer cache contains the data in recently used disk
blocks
When reading data from disk, the kernel attempts to
read from buffer cache.
If data is already in the buffer cache, the kernel does
not need to read from disk
If data is not in the buffer cache, the kernel reads the
data from disk and cache it
4
5. Buffer Headers
A buffer consists of two parts
◦a memory array
◦buffer header
disk block : buffer = 1 : 1
5
6. 6
Buffer Header
device num
block num
status
ptr to next buf on
hash queue
ptr to previous buf on
hash queue
ptr to next buf on
free list
ptr to previous buf on
free list
ptr to data area
7. Buffer Headers
1. Device num
logical file system number
2. Block num
block number of the data on disk
3. Status
1. The buffer is currently locked.
2. The buffer contains valid data.
3. delayed-write
4. The kernel is currently reading or writing the contents of the
disk.
5. A process is currently waiting for the buffer to become free.
4. Kernel identifies the buffer content by examining device num
and block num.
7
8. Structures of the buffer pool
Kernel caches data in the buffer pool according to
LRU algorithm
The kernel maintains a free list of buffer
◦doubly linked list
◦take a buffer from the head of the free list.
◦When returning a buffer, attaches the buffer to the
tail.
8
free list
head
buf 1 buf 2 buf n
Forward ptrs
Back ptrs
Figure 3.2 Free list of Buffers
9. Reading and Writing disk blocks
To read block ahead
1. The kernel checks if the block is in the cache or not.
2. If the block in not in the cache, it invokes the disk
driver to read the block.
3. The process goes to sleep state, awaiting the event that
the I/O is complete.
4. The disk controller interrupts the processor when the
I/O is complete
5. The disk interrupt handler awakens the sleeping
processes
6. The content of disk blocks are now in the buffer
7. When the process no longer need the buffer, it releases
it so that other processes can access it.
9
11. Reading and Writing disk blocks
To write a disk block
◦ The kernel informs the disk driver that it has a buffer
whose contents should be output.
◦ The disk driver schedules the block for I/O.
◦ If the write is synchronous, the calling process goes to
sleep awaiting I/O completion and releases the buffer
when awakens.
◦ If the write is asynchronous, the kernel starts the disk
write. The kernel release the buffer when the I/O
completes.
11
14. Advantages of the buffer cache
1. The use of buffers allows uniform disk access. It simplifies
system design.
2. The system places no data alignment restrictions on user
processes doing I/O. By Copying data from user buffers to
system buffers => the Kernel eliminates the need for special
alignment of user buffers, making user programs simpler and
more portable..
3. Use of the buffer cache can reduce the amount of disk traffic
thereby increasing overall system throughput and decreasing
response time.
4. Single image of disk blocks contained in the cache => helps
insure file system integrity
15. Disadvantages of the buffer cache
1. Since the kernel does not immediately write data to the
disk for a delayed write, the system is vulnerable to
crashes that leave disk data in an incorrect state.
2. Use of the buffer cache requires an extra data copy when
reading and writing to and from user processes. When
transmitting large amounts of data, the extra copy slows
down performance.
Delayed write => vulnerable to crashes that leave disk data
in incorrect state
An extra data copy when reading and writing to and from
user processes => slow down when transmitting large data
15