The Design Of Unix Operating System (4) Buffer cache

来源:互联网 发布:我是大主宰升阶数据 编辑:程序博客网 时间:2024/06/10 00:13
  • A logical disk block should not be mapped to two buffers.
  • Each buffer contains a buffer header, which include fileds: the logic device number, logic block number, the status of the buffer, and two sets of pointers to data arrays.

device number

block number

status

pointer to data area

pointer to next buffer on hash queue

pointer to previous buffer on hash queue

pointer to next buffer on free list

pointer to previous buffer on free list

                                      buffer header

  • The kernel uses Least Recently Used algorithm to allocate a buffer to the data block.
  • The kernal maints a double linked list of free buffers, and in the mean while it uses a hash queue to organise the buffers allocated to the device blocks, as shown below, which will help to find the blocks ASAP. The hash is calculated with device number and block number. A buffer might be contained in both the free list and the hass queue  list.                                                                                                                   
    blockno 0 mod 4 | pointer to buff queue blockno 1 mod 4 | pointer to buff queue blockno 2 mod 4 | pointer to buff queue blockno3 mode 4| pointer to buff queue         buffers on the hash queues
     
  • The user programs do Not need to care about data alignment in his program(not a network communication program), because the kernel will copy the data from user buffer to kernel buffer, and the kernel will handle data alignemtn subsequently.