Professional Documents
Culture Documents
Chapter 2
Chapter 2
Memory Hierarchy
• Now we know that the files are stored on the hard
drive and the processes can access these files and
create new files on the disk.
• When a process requests for a file the kernel brings
the file into the main memory where user process can
change, read or access the file.
• The kernel could read and write the file directly from
the hard disk and put it in memory and vice versa but
the response time and throughput will be very low in
this case because of disks sow data transfer speed.
• To minimize the frequency of disk usage/access the
kernel keeps a buffer to store the recently
accessed files and/or frequently accessed files. This
buffer is called the buffer cache.
Main
Buffer Cache Memory Buffer
Cache
• Block 10
Header
2 15 32
• Block 10
10
• So while writing continuous this buffer will be
locked , when the writing is over then buffer will
be free.
• Now buffer is written with data from block no 10
i.e. this is one which is most recently used buffer.
Header
15 32 10
Free list
• Whenever I have to take a buffer for writing in a data
block , in that case always take a buffer from the
header of the free list.
• Whenever I want to return a buffer to free list , I will
always return at the tail of the list.
• Whenever a buffer just been used and it becomes
free then that is the buffer “Most recently used”
• Most recently used buffer are placed always at the
tail of the list.
•
Most
least Recently
Recently used
used
Header
15 32 10
Free list
• Now suppose some process wants to request data
block no – 12
• 1st check in buffer cache, if it is not in Buffer cache
then only read it from disk of block no 12 and put it
into one of the buffer cache.
• WORST CASE: if block no 12 does not exist and if
there are 1000 of buffer cache then we have to check
all 1000 buffer cache before I declare block no -12
does not exist.
• i.e search time to find out a particular block is present
or not is quite high IF I MAINTAINED A SINGLE LIST
• So instead of maintaining a single list , Maintain a
Number of list i.e called as HASH QUEUE.
• Suppose I have 4 hash queue, will put those buffers
in hash queue which contains a block number say n
Mod operation n mod 4
• n means a block number contained in buffer
• So we decide which buffer will be put in which hash
queue
• Ex; if n mod 4 is zero ,then buffer will be put in HQ0
N mod 4
HQ 0
• Suppose if N=6
HQ 1
• 6 mod 4 = 2
• Then buffer containing block no 6
HQ 2
will present in HQ2
HQ 3
N mod 4
HQ 0 28 4 64
HQ 1 17 5 97
HQ 2 98 50 10
HQ 3 3 35 99
• Free list is subset of nodes which are already
there in different hash queues.
• For example :
HQ 0 28 4 64
HQ 1 17 5 97
98 50 10
HQ 2
3 35 99
HQ 3
Free list
Header
• In above situation some of buffer are free, which
exist in free list and buffers which are not free exist
only in hash queue , they don’t exist in free list.
Structure of the buffer pool
Implementation Concept:
• Buffer pool according to LRU
• The kernel maintains a free list of buffer
– doubly linked list
– take a buffer from the head of the free list.
• When returning a buffer, attaches the buffer to the
tail.
• When the kernel accesses a disk block
– separate queue (doubly linked circular list)
– hashed as a function of the device and block num
– Every disk block exists on one and only once on the
queue
• Determine the logical device num and block num
Hash queue headers
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
freelist header
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
freelist header
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
freelist header
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10 18
blkno2 mod 4
blkno3 mod 4 35 99
freelist header
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
delay
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
delay
freelist header
28 64
blkno0 mod 4
17 5 97
blkno1 mod 4
writing
98 50 10 18
blkno2 mod 4
blkno3 mod 4 3 35 99
writing
freelist header
58
Retrieval of a Buffer: 4th Scenario
• The kernel cannot find the buffer on the hash queue, and the free list is
empty
Hash queue headers
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
blkno2 mod 4 98 50 10
blkno3 mod 4 3 35 99
freelist header
28 4 64
blkno0 mod 4
17 5 97
blkno1 mod 4
98 50 10
blkno2 mod 4
blkno3 mod 4 3 35 99
busy
freelist header
• Uniform disk access => system design simpler (bcz the kernel
does not need to know the reason for the I/O)