Buffer and Block Device

Operation System

In Linux 0.11, the all kinds of reading/writing operations are done with the help of buffer. Every changes is reflected on the buffer and then synchronize with devices.

Buffer Management

In the initiation step, the system will reserve the memory between the “end” of kernel and 0x3FFFFF (except the 0x9FFFF to 0xFFFFF for BIOS and VGA) for buffer. The “end” of kernel is an external variable setted by ld in linking, so we can not find the define of the symbol. The memory for buffer is devided into many 1KB buffer blocks. A hash table with 307 buckets and a double linked list are used to manage all the buffer blocks. Each buffer block has a device number and block number, denoting the device and corresponding data in this device. And the device number and block number are used to calculate the hash value to quickly find the buffer block. Except the data, each buffer block has some flags denoting the status for multi processes visiting and device synchronization.

Block device

Linux 0.11 only support 3 kinds of devices, floppy, hard disk and ramdrive. Ramdrive is in the memory so it is very easy to operate. But floppy and hard disk are much more complicated that they have a lot of seeking and synchronization operations. Here we don’t talk about how to read/write a certain block device and we focus on the interaction between block devices and buffer.

The Linux 0.11 has an block device request list for reading and writing operations. The length of the list is 32 (As most of the operations are reading, $2/3$ of the items in the list are for reading and the other $1/3$ are for writting). Every device operations must register the request list and then read or write the buffer blocks, except the two conditions:

  1. For writing operation, the data in the buffer block has not been changed, since it is read from the device.
  2. For reading operation, the data in the buffer block has already been updated and it is the same to the data in the device.

When the request list is full, the current will sleep, waiting for an unused request item. When there is available request item in the list, check whether the device has other requests. If it has, push the request into the queue. The system will process all the requests in the queue, one by one, until the queue is empty.

Post Directory

Directory

  1. Buffer Management
  2. Block device