2. BuffersBuffers
• A buffer is simply a collection of bytesA buffer is simply a collection of bytes
– a char[] if you will.a char[] if you will.
• Any information that a program uses canAny information that a program uses can
be stored in a buffer.be stored in a buffer.
• If you have to store a lot of informationIf you have to store a lot of information
you may have more than one buffer toyou may have more than one buffer to
store the informationstore the information
• It makes sense to organize all of theseIt makes sense to organize all of these
buffers in some sort of structure.buffers in some sort of structure.
• A buffer poolA buffer pool
3. Buffer PoolsBuffer Pools
• A buffer pool can be as simple as an arrayA buffer pool can be as simple as an array
of char[].of char[].
• You can use the buffers as you need themYou can use the buffers as you need them
for quick access to the information if it isfor quick access to the information if it is
stored in the pool.stored in the pool.
• If it’s not in the pool then you can put theIf it’s not in the pool then you can put the
information in it.information in it.
• The problem comes in when the pool isThe problem comes in when the pool is
full.full.
4. Organizing Buffer PoolsOrganizing Buffer Pools
• Which buffer should be replaced whenWhich buffer should be replaced when
new data must be read?new data must be read?
– First-in, First-out: Use the first one on theFirst-in, First-out: Use the first one on the
queue.queue.
– Least Frequently Used (LFU): Count bufferLeast Frequently Used (LFU): Count buffer
accesses, reuse the least used.accesses, reuse the least used.
– Least Recently used (LRU): Keep buffers onLeast Recently used (LRU): Keep buffers on
a linked list. When buffer is accessed, bringa linked list. When buffer is accessed, bring
it to front. Reuse the one at end.it to front. Reuse the one at end.
5. Buffer Pool ImplementationsBuffer Pool Implementations
• Two main optionsTwo main options
– Message passingMessage passing
• The pool user asks/gives the buffer pool for aThe pool user asks/gives the buffer pool for a
certain amount of information and tells it where tocertain amount of information and tells it where to
put it.put it.
– Buffer passingBuffer passing
• The pool user is given a pointer to the block theyThe pool user is given a pointer to the block they
request.request.
• Both options have their advantages andBoth options have their advantages and
disadvantages.disadvantages.
6. Design IssuesDesign Issues
Disadvantage of message passing:Disadvantage of message passing:
– Messages are copied and passed back and forth.Messages are copied and passed back and forth.
Disadvantages of buffer passing:Disadvantages of buffer passing:
– The user is given access to system memory (theThe user is given access to system memory (the
buffer itself)buffer itself)
– The user must explicitly tell the buffer pool whenThe user must explicitly tell the buffer pool when
buffer contents have been modified, so that modifiedbuffer contents have been modified, so that modified
data can be rewritten to disk when the buffer isdata can be rewritten to disk when the buffer is
flushed.flushed.
– The pointer might becomeThe pointer might become stalestale when the bufferwhen the buffer
pool replaces the contents of a buffer.pool replaces the contents of a buffer.
7. Buffer Pool ApplicationsBuffer Pool Applications
• Usually, buffer pools are used to mediateUsually, buffer pools are used to mediate
between a program and disk access.between a program and disk access.
• A program will use a buffer pool to makeA program will use a buffer pool to make
requests for information.requests for information.
• The buffer pool will go to the disk and getThe buffer pool will go to the disk and get
the information if it doesn’t have it.the information if it doesn’t have it.
8. Buffer Pool StrategiesBuffer Pool Strategies
• When a buffer pool needs to get theWhen a buffer pool needs to get the
information it has a couple differentinformation it has a couple different
optionsoptions
– It can get just the information it needs fromIt can get just the information it needs from
the filethe file
– It can get the entire block with the informationIt can get the entire block with the information
in itin it
• To the end user the result is the same, theTo the end user the result is the same, the
requested information is returned to themrequested information is returned to them
9. More on StrategiesMore on Strategies
• If you remember the access times for a byte andIf you remember the access times for a byte and
for a block:for a block:
– 15.05 ms/byte15.05 ms/byte
– 15.1 ms/block15.1 ms/block
• It makes sense to grab the entire block at a timeIt makes sense to grab the entire block at a time
• And then have some logic that goes in the bufferAnd then have some logic that goes in the buffer
once it’s in memory and return the requestedonce it’s in memory and return the requested
infoinfo
10. What does this mean for me?What does this mean for me?
• For major project 1 we are asking you toFor major project 1 we are asking you to
implement a buffer pool that will bufferimplement a buffer pool that will buffer
individual recordsindividual records
• Therefore, when a record is requestedTherefore, when a record is requested
from the buffer pool it checks to see if itfrom the buffer pool it checks to see if it
has that recordhas that record
– If it does it returns that recordIf it does it returns that record
– If not, it must go to disk and get it.If not, it must go to disk and get it.