You are on page 1of 17

Buffer Management System

Buffer Management System


• A buffer is in-memory copy of the disk block.
• A disk block cannot map into more than one buffer at a time.
• Each buffer has two parts: a memory array that contains data
from the disk & a buffer header (next slide)that identifies the
buffer.
• The buffer header maintains a status field, which contains the
following.
1. The buffer is locked or busy.
2. The buffer contains valid data.
3. The buffer is marked “delayed write”.
4. The kernel is currently reading or writing the contents to the disk.

5. The process is currently waiting for the buffer to become free. 2


Buffer Header

Device no. ptr to data


ptr to prev buf on area
Block no.
hash queue.
Status
ptr to next buf
on hash queue.

ptr to prev buf


on free list.
ptr to next buf
on free list.

3
Free list of Buffers

Free list
buf 1 buf 2 buf n
head

Free list
buf 2 buf n
head

The kernel takes a buffer from from the head of the free list when it wants
any free buffer , but it can take a buffer from the middle of the free list if it
identifies a particular block in the buffer pool.
When the kernel returns a buffer to the buffer pool, it usually attaches the
buffer at the tail of the free list
4
Buffers on the Hash Queues
Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99

5
Scenarios of Buffer Retrieval
There are 5 typical scenarios the kernel follows to allocate a buffer for a
disk block

1. The kernel finds the block on its hash queue, and its buffer is free.

2. The kernel can’t find the block on the hash queue, (allocates a buffer
from the free list).

3. Finds the buffer in the free list, and has been marked “delayed write”.
The kernel writes the buffer to the disk and allocates another buffer.

4. The free list of buffers is empty (blocked).

5. The finds the block on the hash queue, but its buffer is currently busy
(blocked).
6
Scenario 1 of Buffer Retrieval
Scenario 1:The kernel finds the block on its hash queue, and its buffer is free

Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99
Freelist

header
a) Search for block 4 on first hash queue
7
Scenario 1 of Buffer Retrieval Contd.

Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99
Freelist

header
b) Remove block 4 from free list
8
Scenario 2 of Buffer Retrieval
Scenario 2: The kernel can’t find the block on the hash queue,
(allocates a buffer from the free list).
Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99
Freelist

header
a) Search for block 18 – Not in cache 9
Scenario 2 of Buffer Retrieval Contd.

Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4 18
98 50 10

Blk 3 mod 4

35 99
Freelist

header
b) Remove first block from free list & assign to 18
10
Scenario 3 of Buffer Retrieval
Scenario 3: Finds the buffer in the free list, and has been marked
“delayed write”. The kernel writes the buffer to the disk and allocates
another buffer.
Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97
delay
Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99
Freelist
delay
header a) Search for block 18, delayed write blocks on free list
11
Scenario 3 of Buffer Retrieval Contd.
Hash queue headers

Blk 0 mod 4 28 64

Blk 1 mod 4 17 5 97
writing
Blk 2 mod 4
98 50 10 18

Blk 3 mod 4

3 35 99
Freelist
writing
header
b) Writing blocks 3, 5, reassign 4 to 18

12
Scenario 4 of Buffer Retrieval
Scenario 4: The free list of buffers is empty (blocked).
Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99
Freelist

header
a) Search for block 18, Empty free list
13
Scenario 5 of Buffer Retrieval Contd.
Scenario 5: The finds the block on the hash queue, but its buffer is
currently busy (blocked).
Hash queue headers

Blk 0 mod 4 28 4 64

Blk 1 mod 4 17 5 97

Blk 2 mod 4
98 50 10

Blk 3 mod 4

3 35 99
Freelist
busy
header
Search for block 99, block busy
14
Other Operations on the Buffer
Reading the buffer
. Reading can either be one block at a given time,
or it could be two blocks, if higher level
kernel modules anticipates a need.
Writing to buffer
. Writing could be synchronous or asynchronous. In
the latter case buffer will be marked for
“delayed write”.
Releasing the buffer
. The waiting processes are woken up, for this
buffer, or any buffer to become free.
. The buffer content, if valid it is put at the
end of the free list, else it is put at the
beginning of the free list.
15
Advantages & Disadvantages
Advantages
• The parts disk that do the I/O, has the same
interface irrespective of the underlying
device.
• Programmer need not worry about word alignment
and can write hardware independent code.
• Helps in reducing disk traffic, thereby
increasing system throughput and reducing the
response time.
• Maintains file system integrity. If two
processes simultaneously attempt to write to
the same disk block, BMS serializes their
access.

16
Advantages & Disadvantges

Disadvantages
1. Since the kernel does not immediately write
data to the disk (“delayed write”), the system
is vulnerable to crashes, that can leave the
disk data in an incorrect state.
2. Use of buffer cache requires an extra data
copy, when transmitting data to and fro
between the user program and the kernel. This
affects performance mainly when there is large
amount of data to be transmitted.

17

You might also like