Chap 3. The Buffer Cache
Chap 3. The Buffer Cache
Chap 3. The Buffer Cache
Buffering Exam
#include <stdio.h> FILE* test; int I, data = 0; Int main() { test = fopen(test, w); for(i == 0; i < 2000; i++) fwrite(&data, sizeof(data), 1, test) return 0; }
Buffer Cache
The kernel attempts to minimize the frequency of disk access Buffer cache == pool of buffers Buffer cache contains the data in recently used disk blocks Higher-level kernel algorithms instruct the buffer cache modules to pre-cache data or to delay-write data to maximize caching effect
Buffer Header
Buffer consists of two parts:
Buffer header: identifies the buffer Memory array: contains disk block data
A disk block can never map into more than one buffer at a time => why? Buffer status: combination of following
Locked(or busy) Unlocked(or free) Buffer is valid Write buffer contents before reassigning buffer Currently reading or Writing the contents to disk Currently waiting for the buffer to become free
Buffer Header
device num
block num status
prev buf on hash queue
data area
28 17 98 3
4 5 50 35
64 97 10 99
Blkno
0 1 2 3
Free list headers
mod 4
BUFFER POOL
Free
64
97
98
50
10
35
99
freelist header
(a) Search for Block 4 on First Hash Queue Figure 3.5
64
97
98
50
10
35
99
freelist header
(b) Remove Block 4 from Free List Figure 3.5
64
97
98
50
10
35
99
freelist header
(a) Search for Block 18-Not in Cache Figure 3.7
64
97
98
50
10
18
35
99
freelist header
(b) Remove First Block from Free List, Assign to 18 Figure 3.7
64
5 delay
97
98
50
10
3 delay
35
99
freelist header
(a) Search for Block 18- Delayed Write Blocks on Free List Figure 3.8
64
97
10
18
3 writing
35
99
freelist header
(b) Writing Blocks 3, 5, Reassign 4 to 18 Figure 3.8
Process A
Cannot find block b on hash queue
Process B
blkno0 mod 4
28
64
Sleep
blkno2 mod 4
98
50
10
Sleep
blkno3 mod 4
35
99
freelist header
Process B
Process C
Time
Figure 3.12 Race for a Locked Buffer
Read Ahead
Algorithm breada { if(first block not in cache) { get buffer for first block(algorithm getblk); if(buffer data not valid) initiate disk read; } if(second block not in cache) { get buffer for second block(algorithm getblk); if(buffer data valid) release buffer(algorithm brelse); else initiate disk read; }
Read Ahead()
if(first block was originally in cache) { read first block(algorithm bread); return buffer; } sleep(event first buffer contains valid data); return buffer;
}