STDIN, STDOUT, and STDERR.
Before we finally start programming, we must fully re-educate ourselves about Linux/Unix’s most essential data-streaming rules: STDIN, STDOUT, and STDERR. With this theory in mind, you’re almost ready to start the Linux programming journey.
Of the three essential concepts of Linux, let’s start with the stream. Regarding streaming, we need to discuss system-call and library functions, and we’ll start with the system call for a better understanding of the system structure from the bottom part.
Essential system calls we discuss here are:
- Read – reading data from the stream
- Write – writing data from the stream
- Open – opening a file from the stream
- Close – closing an unneeded stream
We have only four system calls here, but we can cover almost every input/output flow of the Linux/UNIX system with them.
File descriptor
When a process interacts with a file for reading/writing purposes, it must stream it. For a program to stream a file, file descriptor is required. From the program’s point of view, file descriptor is an int-type APIs. In the kernel, there are multiple streaming options, but for many reasons, they can’t be revealed in the process. So, the kernel’s streaming options are corresponding to certain numbers, and the kernel requires the process to specify the number when streaming a file.
Each process has three streaming options, and they are:
File descriptor | Macro | Description |
0 | STDIN_FILENO | standard input |
1 | STDOUT_FILENO | standard output |
2 | STDERR_FILENO | standard error |
Standard input and standard output
Why do we have both standard input and standard output? It all comes down to the piping, which enables us to combine two different commands to create a complicated task. It’s possible because the kernel reads data from standard input and writes down the result in standard output. And the word ‘standard’ here is nearly equal to ‘default’. So, essentially, they are default input/output ports.
In image 01, data typed from the keyboard is displayed on the screen every time you type the enter key.
In image 02, the standard input is streaming to hello.c, and the file contents will be displayed on the screen. What’s interesting here is that the cat command doesn’t even recognize the fact that it reads data from the file because the whole process of streaming the file contents to the STDIN is solely done by the shell.
Finally, here is an example of piping of grep and head commands.
In image 04, the process of both grep and head commands is handled by STDIN and STDOUT, and the combination completes the complicated task as shown in image 05. And even more complicated tasks can be completed by piping, and it’s essentially possible because almost all Linux commands follow the STDIN, STDIN, and STDOUT rules.
Standard error
The main goal of the standard error is to show an error message on the terminal. And the sole reason why we have the standard error is because of the pipe. Since the standard output is likely connected to the next command’s standard input, the user might miss the error message. So, the kernel has the extra STDERR, directing the STDOUT to the next STDIN and STDERR to the terminal display respectively.