I am rather confused with the purpose of these three files. If my understanding is correct, stdin
is the file in which a program writes into its requests to run a task in the process, stdout
is the file into which the kernel writes its output and the process requesting it accesses the information from, and stderr
is the file into which all the exceptions are entered. On opening these files to check whether these actually do occur, I found nothing seem to suggest so!
What I would want to know is what exactly is the purpose of these files, absolutely dumbed down answer with very little tech jargon!
The three standard file handles opened before your main
function starts running are as follows:
Of course, that's mostly by convention. There's nothing stopping you from writing your diagnostic information to standard output if you wish. You can even close the three file handles totally and open your own files for I/O. Just be careful if you're using any code (such as 3rd party code like libraries) that expects those file handles to be open :-)
When your process starts, it should already have these handles open, and it can just read from and/or write to them.
By default, they're probably connected to your terminal device (e.g., /dev/tty
) but shells will allow you to set up connections between these handles and specific files and/or devices (or even pipelines to other processes) before your process starts (some of the manipulations possible are rather clever).
An example being, in a UNIX-like environment:
my_prog <inputfile 2>errorfile | grep XYZ
This command will:
my_prog
to run.inputfile
as your standard input.errorfile
as your standard error.grep
program to run.my_prog
to the standard input of grep
.Regarding your comment:
When I open these files in /dev folder, how come I never get to see the output of a process running?
It's because they're not actually normal files. While UNIX presents everything as a file in a file system somewhere, that doesn't make it so at the lowest levels.
Most files in the /dev
hierarchy are either character or block devices, effectively a device driver. They don't have a size, but they do have major and minor device numbers.
When you open them, you're connected to the device driver rather than a physical file, and the device driver is smart enough to know that separate processes should be handled separately.
The same is true for the Linux /proc
filesystem. Those aren't real files, just tightly controlled gateways to kernel information.