Buffering in Operating System
The buffer is an area in the main memory used to store or hold the data temporarily. In other words, buffer temporarily stores data transmitted from one place to another, either between two devices or an application. The act of storing data temporarily in the buffer is called buffering.
A buffer may be used when moving data between processes within a computer. Buffers can be implemented in a fixed memory location in hardware or by using a virtual data buffer in software, pointing at a location in the physical memory. In all cases, the data in a data buffer are stored on a physical storage medium.
Most buffers are implemented in software, which typically uses the faster RAM to store temporary data due to the much faster access time than hard disk drives. Buffers are typically used when there is a difference between the rate of received data and the rate of processed data, for example, in a printer spooler or online video streaming.
A buffer often adjusts timing by implementing a queue or FIFO algorithm in memory, simultaneously writing data into the queue at one rate and reading it at another rate.
Purpose of Buffering
You face buffer during watching videos on YouTube or live streams. In a video stream, a buffer represents the amount of data required to be downloaded before the video can play to the viewer in real-time. A buffer in a computer environment means that a set amount of data will be stored to preload the required data before it gets used by the CPU.
Computers have many different devices that operate at varying speeds, and a buffer is needed to act as a temporary placeholder for everything interacting. This is done to keep everything running efficiently and without issues between all the devices, programs, and processes running at that time. There are three reasons behind buffering of data,
- It helps in matching speed between two devices in which the data is transmitted. For example, a hard disk has to store the file received from the modem. As we know, the transmission speed of a modem is slow compared to the hard disk. So bytes coming from the modem is accumulated in the buffer space, and when all the bytes of a file has arrived at the buffer, the entire data is written to the hard disk in a single operation.
- It helps the devices with different sizes of data transfer to get adapted to each other. It helps devices to manipulate data before sending or receiving it. In computer networking, the large message is fragmented into small fragments and sent over the network. The fragments are accumulated in the buffer at the receiving end and reassembled to form a complete large message.
- It also supports copy semantics. With copy semantics, the version of data in the buffer is guaranteed to be the version of data at the time of system call, irrespective of any subsequent change to data in the buffer. Buffering increases the performance of the device. It overlaps the I/O of one job with the computation of the same job.
Types of Buffering
There are three main types of buffering in the operating system, such as:
1. Single Buffer
In Single Buffering, only one buffer is used to transfer the data between two devices. The producer produces one block of data into the buffer. After that, the consumer consumes the buffer. Only when the buffer is empty, the processor again produces the data.
Block oriented device: The following operations are performed in the block-oriented device,
- System buffer takes the input.
- After taking the input, the block gets transferred to the user space and then requests another block.
- Two blocks work simultaneously. When the user processes one block of data, the next block is being read in.
- OS can swap the processes.
- OS can record the data of the system buffer to user processes.
Stream oriented device: It performed the following operations, such as:
- Line-at a time operation is used for scroll made terminals. The user inputs one line at a time, with a carriage return waving at the end of a line.
- Byte-at a time operation is used on forms mode, terminals when each keystroke is significant.
2. Double Buffer
In Double Buffering, two schemes or two buffers are used in the place of one. In this buffering, the producer produces one buffer while the consumer consumes another buffer simultaneously. So, the producer not needs to wait for filling the buffer. Double buffering is also known as buffer swapping.
Block oriented: This is how a double buffer works. There are two buffers in the system.
- The driver or controller uses one buffer to store data while waiting for it to be taken by a higher hierarchy level.
- Another buffer is used to store data from the lower-level module.
- A major disadvantage of double buffering is that the complexity of the process gets increased.
- If the process performs rapid bursts of I/O, then using double buffering may be deficient.
Stream oriented: It performs these operations, such as:
- Line- at a time I/O, the user process does not need to be suspended for input or output unless the process runs ahead of the double buffer.
- Byte- at time operations, double buffer offers no advantage over a single buffer of twice the length.
3. Circular Buffer
When more than two buffers are used, the buffers' collection is called a circular buffer. Each buffer is being one unit in the circular buffer. The data transfer rate will increase using the circular buffer rather than the double buffering.
- In this, the data do not directly pass from the producer to the consumer because the data would change due to overwriting of buffers before consumed.
- The producer can only fill up to buffer x-1 while data in buffer x is waiting to be consumed.
How Buffering Works
In an operating system, buffer works in the following way:
- Buffering is done to deal effectively with a speed mismatch between the producer and consumer of the data stream.
- A buffer is produced in the main memory to heap up the bytes received from the modem.
- After receiving the data in the buffer, the data get transferred to a disk from the buffer in a single operation.
- This process of data transfer is not instantaneous. Therefore the modem needs another buffer to store additional incoming data.
- When the first buffer got filled, then it is requested to transfer the data to disk.
- The modem then fills the additional incoming data in the second buffer while the data in the first buffer gets transferred to the disk.
- When both the buffers completed their tasks, the modem switches back to the first buffer while the data from the second buffer gets transferred to the disk.
- Two buffers disintegrate the producer and the data consumer, thus minimising the time requirements between them.
- Buffering also provides variations for devices that have different data transfer sizes.
Advantages of Buffer
Buffering plays a very important role in any operating system during the execution of any process or task. It has the following advantages.
- The use of buffers allows uniform disk access. It simplifies system design.
- The system places no data alignment restrictions on user processes doing I/O. By copying data from user buffers to system buffers and vice versa, the kernel eliminates the need for special alignment of user buffers, making user programs simpler and more portable.
- The use of the buffer can reduce the amount of disk traffic, thereby increasing overall system throughput and decreasing response time.
- The buffer algorithms help ensure file system integrity.
Disadvantages of Buffer
Buffers are not better in all respects. Therefore, there are a few disadvantages as follows, such as:
- It is costly and impractical to have the buffer be the exact size required to hold the number of elements. Thus, the buffer is slightly larger most of the time, with the rest of the space being wasted.
- Buffers have a fixed size at any point in time. When the buffer is full, it must be reallocated with a larger size, and its elements must be moved. Similarly, when the number of valid elements in the buffer is significantly smaller than its size, the buffer must be reallocated with a smaller size and elements be moved to avoid too much waste.
- Use of the buffer requires an extra data copy when reading and writing to and from user processes. When transmitting large amounts of data, the extra copy slows down performance.