Buffering Techniques
Overview
Buffer technology is widely used in modern operating systems, mainly to solve the speed mismatch problem between the CPU and peripheral devices, and to improve the work efficiency and parallel working capabilities of each component of the system. By introducing buffers, the system can better coordinate the input and output of data, reduce the need for CPU interrupt frequency, and improve overall performance.
Introduction of buffering
Ease the speed mismatch between CPU and I/O devices
- High-speed CPUs work at the microsecond or micromillisecond level, while I/O devices work at the millisecond or second level.
Reduce the frequency of CPU interrupts and relax the limit on CPU interrupt response time
- Setting the buffer can reduce the number of CPU interrupts and reduce interrupt processing time. For example, an 8-bit buffer can reduce the interrupt frequency by 1/8.
Increase parallelism between CPU and I/O devices
- The introduction of buffering significantly improves the degree of parallel operations between the CPU and I/O devices, improving system throughput and device utilization.
Single buffer
- How it works: Set up a single buffer between the device and the processor. When exchanging data, data is first written into the buffer and then read from the buffer.
- Limitations: Since the single buffer is a critical resource, input and output devices can only access the buffer serially, and parallel operations between devices cannot be achieved.
Double buffering
- Concept: Double buffering, that is, buffer swapping, refers to setting two buffers for input and output devices. The device can input data into one buffer while the processor reads data from another buffer.
- Advantages: By using two buffers alternately, the parallelism of the CPU and I/O devices is further improved and the waiting time is reduced.
- Limitations: When the speed of the input/output device and the processing process do not match, double buffering is also difficult to fully adapt and cannot solve the problem of speed imbalance between multiple peripheral devices.
Multiple buffers
- Mechanism: The system allocates multiple buffers from main memory to form a multi-buffer mechanism. These buffers can form a circular buffer that is used to pass data between the input process and the calculation process.
- Advantages: By sharing multiple buffers, the system can improve buffer utilization, reduce waiting time during data transmission, and adapt to more parallel operation scenarios.
Buffer pool
Concept: The buffer pool is a common buffer structure, consisting of multiple shareable buffers. It is used to improve buffer utilization and is suitable for multiple I/O processes and computing processes.
Buffer Classification:
- Empty buffer: A buffer with no data stored.
- Buffer filled with input data: Contains input data waiting to be processed.
- Buffer filled with output data: Contains the data to be output.
Management method: By chaining buffers of the same type into queues, the following three queues can be formed:
- empty buffer queue (emq): stores empty buffers.
- Input Queue (inq): Stores a buffer filled with input data.
- Output Queue (outq): Stores a buffer filled with output data.
Four types of working buffers:
- hin: Buffer used to receive input data.
- sin: Buffer used to extract input data.
- hout: Buffer used to receive CPU output data.
- sout: Buffer used to extract CPU output data.