19CS2106S Lecture Notes Buffer Cache Algorithms & Logging Session - 5
19CS2106S Lecture Notes Buffer Cache Algorithms & Logging Session - 5
SESSION NUMBER: 05
Session Outcome: 1. understand and explore the Design of Buffer Cache allocation algorithms.
2. Apply Log design and Logging.
Time Topic BTL Teaching - Learning Active Learning
(min) Methods Methods
20 Xv6 functions: bread, bwrite. Design and 3 Lecture & Discussion LTC
Implementation of log.c
References
1. Maurice J. Bach, The Design of The Unix Operating System, 2013 PHI Publishing. CH 3, [3.3, 3.4] Page No [54,55,56],
2. Russ Cox, Frans Kaashoek, Robert Morris, xv6: a simple, Unix-like teaching operating system", Revision
https://pdos.csail.mit.edu/6.828/2018/xv6/book-rev11.pdf CH 6 Page No [79,80],
3. Frans Kaashoek, Robert Morris, and Russ Cox, The xv6 source code booklet (draft) (revision 11).
https://pdos.csail.mit.edu/6.828/2018/xv6/xv6-rev11.pdf T3 Sheet No [44,45],[47]
READING AND WRITING DISK BLOCKS:
To read a disk block, a process uses algorithm getblk to search for it in the buffer cache. If it is in the cache, the kernel can return
it immediately without physically reading the block from the disk
If it is not in the cache, the kernel calls the disk driver to "schedule" a read request and goes to sleep awaiting the event that the
I/O completes. The disk driver notifies the disk controller hardware that it wants to read data, and the disk controller later
transmits the data to the buffer. Finally, the disk controller interrupts the processor when the I/O is complete, and the disk
19CS2106S: Lecture Notes 1
Session: 5
19CS2106S: Lecture Notes
Session: 5 Buffer cache Algorithms & Logging
interrupt handler awakens the sleeping process; the contents of the disk block are now in the buffer. The modules that requested
the particular block now have the data; when they no longer need the buffer they release it so that other processes can access it.
higher-level kernel modules (such as the file subsystem)may anticipate the need for a second disk block when a process reads a
filesequentially. The modules request the second I/O asynchronously in the hope that the data will be in memory when needed,
improving performance. To do this, the kernel executes the block read-ahead algorithm breada. The kernel checks if the first
block is in the cache and, if it is not there, invokes the disk driver
to read that block. If the second block is not in the buffer cache, the kernel instructs the disk driver to read it asynchronously.
Then the process goes to sleep awaiting the event that the I/O is complete on the first block. When it awakens, it returns the
buffer for the first block, and does not care when the I/0 for the second block completes. When the I/O for the second block does
complete, the disk controller interrupts the system; the interrupt handler recognizes that the I/O was asynchronous and releases
the buffer (algorithm brelse). If it would not release the buffer, the buffer would remain locked and, therefore, inaccessible to all
processes. It is impossible to unlock the buffer beforehand, because I/O to the buffer was
active, and hence the buffer contents were not valid. Later, if the process wants to read the second block, it should find it in the
buffer cache, the I/O having completed in the meantime. If, at the beginning of breada, the first block was in the buffer cache,
the kernel immediately checks if the second block is in the cache and proceeds as just described.
The algorithm for writing the contents of a buffer to a disk block is similar (Figure 2.6). The kernel informs the disk driver that it
has a buffer whose contents should be output, and 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 it awakens. If
the write is asynchronous, the kernel starts the disk write but does not wait for the write to complete. The kernel will release the
buffer when the I/O completes. A delayed write is different from an asynchronous write. When doing an asynchronous write, the
kernel starts the disk operation immediately but does not wait for its completion. For a "delayed write," the kernel puts off the
physical write to disk as long as possible; then, recalling the third scenario in algorithm getblk, it marks the buffer "old" and writes
the block to disk asynchronously.
the implementation of bread and bwrite algorithms in xv6 is shown here.
b = bget(dev, blockno);
if((b->flags & B_VALID) == 0) {
iderw(b);
}
return b;
}