In this blog, we are going to learn about Buffers and Streams.
Before explaining the concept of buffer and buffering, let me explain the idea of Kernel.
Kernel
A kernel is a central component of an Operating System. The kernel is the one that closely wraps the hardware, the rest of the OS will call the kernel to execute the code and access the hardware.
kernel acts as an interface between user applications and the hardware. The sole aim of the kernel is to manage the communication between the software(user Level Application) and the hardware(CPU, disk, etc).
One of the Main tasks of the kernel is I/O communication, (There are many other tasks of the kernel as well such as Process Management, Device Management, Memory Management, File System Management, etc), the kernel provides many services related to I/O such as Scheduling, buffering, caching, spooling, etc. but our main focus here is on Buffering.
Now Let us understand the concept of Buffering.
What is Buffer?
A buffer is a temporary storage area in memory that stores data being transferred between two devices or between a device and an application to bring compatibility between the devices.
What are the reasons for using Buffers?
Buffering is done mainly for three reasons
- To Cope with the speed mismatch between the producer and consumer of the data stream
Let me explain to you with one example. Suppose one file is being received via modem that must be stored on the hard disk.
As we know that transmission speed of the modem is a thousand times slower than the hard disk. So buffer is created in the main memory which accumulates the bytes received from the modem. So once all the data has arrived at the buffer, now the entire data is written to the hard disk in a single operation.
Since the data that is being written on the hard disk is not done instantly, so modem will require a place to store the additional incoming data, therefore two buffers are used.
The modem will first fill the first buffer with data, after filling the data disk write will be requested, The modem then starts to fill the second buffer, while the first buffer data is being written on the hard disk. by the time the second buffer gets filled, the first buffer should have completed its work on the disk, and now the modem will switch back to the first buffer again and now the second buffer will write on the hard disk. This double buffering decouples the producer of data from the consumer, thus relaxing timing requirements between them.
- The second use of Buffering is that it provides adaptations between the devices having different data transfer sizes.
This is commonly used in Computer Networking where data that is being transferred is fragmented into small network packets and these packets at the receiving end is being accumulated at the buffer and reassembled to form a large message.
- The third use of buffering is to support Copy Semantics for the application I/O.
let me explain to you the meaning of “Copy Semantics”, Suppose there is an application with a buffer of data that it wants to write to disk. It calls the write() system call, and after the write() system call returns, what happens if the application changes the content of the buffer?
So, with the help of Copy Semantics, the version of the data written to the disk is guaranteed to be the version of data at the time of the application System call, irrespective of any subsequent changes to data in the application buffer.
What are Streams?
Streams are a flow of data or a flow of character.
Streams are used for accessing data from outside the program.
Data may be flowing from an external resource to a program or from a program to a resource, for example, data flowing from a file to a program or from a program to a file. The program can get data from the keyboard, send data to monitor, and read and write data to a file or from a network. There are various sources for a program to receive and send data.
In C++ programming language, there is a class with the name ios i.e. input-output stream. From this, we have
istream
class for the input stream andostream
class for the output stream. For accessing files there are classes available calledifstream
input file stream andofstream
output file stream.For input stream from the keyboard, there is already an object present in iostream header file called
cin
and cout object of ostream class used to output to a device your data.
File Handling Code
#include <fstream>
#include <iostream>
int main()
{
ofstream outfile("file.txt",ios::app); // Associate outfile object with the file passed to it. What we write to outfile object gets associated with the file.
// To insert
outfile << "Hello"<<endl; // Write Hello and switch to new line
outfile << 25<<endl; // Write 25 and go to new line.
outfile.close(); // To close the file.
}
If the file is not present in that case file will be created if we are using the output file stream ofstream in our code. If there is some content in the file in that case content will be truncated or removed. If you do not want data to be overwritten with new data you can open file using append mode passed to the object using ios::app
. Similarly to truncate the mode is ios::trunc
.
Types of Streams
In computer science, there are several types of streams that refer to the flow of data or the flow of characters within a computer system. Here are some of the most common types:
Input Stream
This refers to the flow of data into a computer system, such as data entered through a keyboard, mouse, microphone, or other input devices. For example, when you type something into your keyboard, the data is sent as a stream of characters to the computer's input stream.
Output Stream
This refers to the flow of data out of a computer system, such as data displayed on a screen, printed on paper, or sent to an external device like a printer or speaker. For example, when you print something on the screen the data is sent as a stream of characters to the computer's output stream.
Network Stream
This refers to the flow of data between computers over a network, such as an internet or a local area network. For example, when you watch a video online, the video data is streamed from a server to your device over a network stream
File Stream
This refers to the flow of data to or from a file on a computer's storage device, such as a hard drive or USB drive. For example, when you read a file on your computer, you are actually reading a stream of data from the file.
Audio and Video Stream
These refer to the flow of audio and video data, typically captured from a camera, microphone, or stored files, to be processed, transmitted, or displayed on a screen or speakers. For example, when you watch movie or Web Series on Netflix, or Prime Video the audio and video data are sent to your device in real time as a stream.
Conclusion
We have come to the end of this blog, I Hope you had fun reading this blog and understood the concept of Kernel, Buffer, and Streams and why it is used.