0% found this document useful (0 votes)
2K views

Blocking and Buffering

Blocking is the process of grouping file components like records into blocks to improve file access performance. The key aspects are: - Blocking factor determines the number of components per block. A higher blocking factor means fewer disk accesses but may waste space if blocks are not fully used. - Double buffering can improve performance over single buffering for I/O-bound applications by overlapping I/O and CPU processing through the use of two buffers. However, it provides less benefit for CPU-bound applications where processing takes longer than I/O.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Blocking and Buffering

Blocking is the process of grouping file components like records into blocks to improve file access performance. The key aspects are: - Blocking factor determines the number of components per block. A higher blocking factor means fewer disk accesses but may waste space if blocks are not fully used. - Double buffering can improve performance over single buffering for I/O-bound applications by overlapping I/O and CPU processing through the use of two buffers. However, it provides less benefit for CPU-bound applications where processing takes longer than I/O.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Blocking

CS 102
File Structures & File Organizations

Accessing secondary storage takes longer than accessing main memory Usually, processing a file component by the CPU is much less than reading or writing the component in the file. Block smallest amount of data that can be read from or written to secondary storage at a time.

Chapter 02

Blocking and Buffering

CJD

Blocking
Blocking grouping several file components : usually records. If a record is too long to fit a block, it will span several blocks. Blocking improves execution times of programs that access files fewer accesses retrieve the entire file by block instead of by record. less physical accesses means less total execution time
a record
CJD

Blocking Factor
Blocking factor is configurable Blocking Factor = number of components (or records) per block Many points must be considered in deciding the size of a block.

a block of 3 records
CJD

Blocking Examples
10,000 components must be added to a new file. One component to a block requires 10,000 accesses If a blocking factor of 10 is used, there will be 10,000 components / 10 components per block = 1,000 blocks requiring only 1,000 accesses. If a blocking factor of 100 is used, there will be 10,000 components / 100 components per block = 1,00 blocks requiring only 100 accesses. If a blocking factor of 10,000 is used, there will only be 1 block requiring only 1 physical access.
CJD

Block Size Limitations


The larger the blocking factor, the fewer the number of physical accesses. What limits block size ? 1. size of available main memory must be shared by the block with other data of the program, other programs and the system 2. size of programs that access the file larger programs require more main memory
CJD

Block Size Limitations


3. Characteristics of the accesses to a file If only one component in the block needs to be read or written, all components in the block must be read or written. How frequently is the file accessed ? What is the required response time for a given access ? How far is the next block from the previous block ? Clustering grouping components accessed in a short period of time into a block
CJD

Block Size Limitations


4. Characteristics of external storage devices fitting one block to a track or sector of a disk. blocks are stored sequentially on a tape.

ON TAPE ON DISK a block of 3 records a record

a block is a sector of 3 records

a block could be a track of 12 records


CJD

Single Buffering
Buffer a location in main memory used to store a block of data. It has the same size as a block of data. The CPU directs the I/O channel to access a particular device to read or write data. The I/O channel directs a device controller to input or output data for the device. Blocking and Deblocking is executed by the I/O channel of a computer.
Physical I/O Storage Buffer Logical I/O CPU
CJD

Blocking and Deblocking


Deblocking used when reading from a file a software interface that reads one block from the file into the buffer using a physical read. sends one component from the buffer at a time to the program using logical reads. Blocking used when writing to a file a software interface stores the components from a program into a buffer using a logical write when the buffer is full or flushed, a physical write from the buffer to the file is executed
CJD

Physical vs Logical Reads


Physical read reading a block from the physical storage device into the buffer. Logical read reading a component from the buffer. If n is the blocking factor and all components are read by the program, each physical read precedes n logical reads.
CJD

Buffering Input (Deblocking)


1. If the buffer is not empty, go to step 6. 2. The CPU issues an input request. 3. The I/O channel signals the device controller for the device specified in the input request. 4. The device controller locates the requested information and starts reading bytes from the device and sending them to the buffer in main memory. 5. The I/O channel waits until the buffer is filled, then signals the CPU that the input operation is complete. The location indicator for the buffer is set to 1.
CJD

Buffering Input (Deblocking)


6. The next component to which the location indicator points is sent to the program. 7. The location indicator is incremented. 8. The CPU continues to execute the program

Physical vs Logical Writes


Logical write writing a component to the buffer. Physical read writing a block from the buffer. into the physical storage device If n is the blocking factor and all components are written by the program, each physical write follows n logical writes. if a block is not full but needs to be written, it is flushed to the device. Usually on EOF condition.
CJD CJD

Buffering Output (Blocking)


1. If the buffer is not full, go to step 6. 2. The CPU issues an output request. 3. The I/O channel signals the device controller for the device specified in the output request. 4. The device controller starts writing bytes from the buffer to the device. 5. The I/O channel waits until the entire buffer is written to the device then signals the CPU that the output operation is complete. The location indicator for the buffer is set to 1.
CJD

Buffering Output (Blocking)


6. The next component from the program is stored in the buffer in the space to which the location indicator points. 7. The location indicator is incremented. 8. The CPU continues to execute the program

CJD

Single Buffering Example


Problem: Suppose 2,000 records, each 100 bytes long, are stored unblocked (1 record per block) in file A. File B employs a blocking factor of 25 records per block. Suppose 100 bytes can be transferred in 0.008ms with an overhead of 50ms per physical read or write, how long will it take to read each file ?

Single Buffering Example


File A Number of blocks 2,000 records 1 record per block = 2,000 blocks Number of 2,000 reads physical reads or writes required Time for each 50 + 0.008x1 = physical read or 50.008ms/read write Time to read or 50.008 ms/read x 2,000 write file = 100,016ms = 1.667 minutes File B 2,000 records 25 records per block = 80 blocks 80 reads

50 + 0.008x25 = 50.2ms/read 50.2 ms/read x 80 = 4,016ms = 0.067 minutes

There are more records per block in File B. File B has less blocks per file. Because of the overhead per physical I/O, accessing file B is faster.
CJD CJD

Double Buffering
Single Buffering : The CPU waits until each physical I/O operation is completed. The I/O channel is idle while the CPU is executing the non I/O parts of a program. Double Buffering : providing two buffers per file improves program execution when I/O operations and CPU processing overlap in time.

Deblocking with Double Buffering


Input from file steps : 1. The first block is input into buffer 1 and sent to the program component-by-component while the second block is input into buffer 2. 2. When buffer 1 becomes empty, components in buffer 2 are sent to the program while the next block is input into buffer 1. 3. When buffer 2 becomes empty, components in buffer 1 are sent to the program while the next block is input into buffer 2. 4. Repeat steps 2 and 3 until the program ends or the end of file is reached.
CJD CJD

Blocking with Double Buffering


Output to file steps : 1. The program writes component by component to buffer 1. 2. When buffer 1 becomes full, components are written by the program to buffer 2 while buffer 1 is output to file by I/O channel. 3. When buffer 2 becomes full, components are written by the program to buffer 1 while buffer 2 is output to file by I/O channel. 4. Repeat steps 2 and 3 until the program ends and any remaining components in the buffers are flushed (outputted) to file.
CJD

I/O Bound vs CPU Bound


I/O Bound - The time to do I/O on each block is more that the time for CPU to process each block. CPU Bound - The time to do I/O on each block is less that the time for CPU to process each block.
Implementation is also called processor bound.

CJD

I/O Bound Example


Suppose it takes 50 ms to input a block and the CPU processes each block in 25 ms. Suppose there are 1,000 blocks in a file. Compare the time required to input from the file using single and double buffering. Single Buffering Input
50 50 50 I/O 25 25 25 CPU 50 25

I/O Bound Example


Suppose it takes 50 ms to input a block and the CPU processes each block in 25 ms. Suppose there are 1,000 blocks in a file. Compare the time required to input from the file using single and double buffering.

Double Buffering Input I/O bound


50 50 50 I/O 25 25 CPU 50 25 25

Single Buffering Total File (50 ms to read + 25 Processing ms to process) per time block x 1000 blocks per file = 75,000 ms = 75 seconds

Double Buffering (50 ms to read each block x 1000 blocks per file) + 25 ms to process first block = 50,025ms 50 seconds
CJD

CJD

I/O Bound Example


Suppose it takes 50 ms to input a block and the CPU processes each block in 25 ms. Suppose there are 1,000 blocks in a file. Compare the time required to input from the file using single and double buffering.

CPU Bound Example


Suppose it takes 50 ms to output a block and the CPU processes each block in 75 ms. Suppose there are 1,000 blocks in a file. Compare the time required to output to the file using single and double buffering. Single Buffering Output
75 75 CPU 75 50 50 50 I/O 75 50

Double buffering savings is 75 50 = 25 seconds or about ((75 50) / 75 ) * 100% = 33% of single buffering time.

Double Buffering Output CPU bound


CPU 75 I/O
CJD

75 50

75 50

75 50

50

CJD

CPU Bound Example


Suppose it takes 50 ms to output a block and the CPU processes each block in 75 ms. Suppose there are 1,000 blocks in a file. Compare the time required to output to the file using single and double buffering.

CPU Bound Example


Suppose it takes 50 ms to output a block and the CPU processes each block in 75 ms. Suppose there are 1,000 blocks in a file. Compare the time required to output to the file using single and double buffering.

Single Buffering Total File (50 ms to write + 75 Processing ms to process) per time block x 1000 blocks per file = 125,000 ms = 125 seconds

Double Buffering (75 ms to process each block x 1000 blocks per file) + 50 ms to write last block = 75,050ms 75 seconds
CJD

Double buffering savings is 125 75 = 50 seconds or about ((125 75) / 125 ) * 100% = 40% of single buffering time.
CJD

Exercise
Question : When a certain job is executed in a computer system, 40 percent of the total processing time of the job is used for CPU and the rest is spent waiting for I/O to complete. If a newer CPU whose speed is two times faster than the current one is installed, approximately how many times faster is the new system than the current system? Here, the system environment except for the CPU remains unchanged. Solution : 40 + 60 ------------- = 100/80 = 1.25 40/2 + 60

End

CJD

CJD

You might also like