When the kernel accesses a disk block, it searches for a buffer with the appropriate device-block number combination.

This line is from the book "The design of the unix operating system". So my question is how does the kernel decides which buffer to use for the operation, ,and from where does it get the device-block number for that buffer?

  • 1
    Realize that you're asking about a data structure/storage approach that was used in a version of Unix that no longer exists in the form that was there when that book was written. I doubt you'll get "the answer". – Andy Dalton Aug 6 '18 at 20:20
  • Many details may have changed, but the principles still apply. – RalfFriedl Aug 7 '18 at 16:55

Does your question refer to the case when the data is already present in a buffer, or when a new buffer has to be allocated?

The device block number comes from the device file this is used to mount the file system (something like /dev/sda1 in Linux or /dev/hd0a in BSD). The device number is needed whether buffers are used or not. The buffers are merely a cache to save time reading the same data again. The block number within the device is determined by the layout of the file system, read that part of the book for more information on the format of the file system. There is an explanation about directories, inodes, direct and indirect blocks and so on.

So once the kernel has determined which block is needed, it will look at the buffers in memory to find the needed block. If it is present, a read operation is not needed and the data is returned immediately.

If the content of theblock is not in memory, the kernel will select a buffer in the hope that the content of this block will not be used in the near future. This is usually the least recently used buffer.

  • I'm talking about the case when the data is present in the buffer? – Naren Aug 7 '18 at 6:46
  • Everything except the last paragraph is about finding the data in the buffer. If you have and specific questions, ask. – RalfFriedl Aug 7 '18 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.