views:

108

answers:

4

Hi,

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 file, absolutely dummed down answer with very little tech jargon! I have been using linux for the past 3 years but this is the first time I bother to look into these aspects, so you could consider me to be newbie of sorts, hence an answer that would not spook me to ever again to question such details of the os would be much appreciated! =)

+8  A: 

Standard input - this is the file handle that your process reads to get information from you.

Standard output - your process writes normal information to this file handle.

Standard error - your process writes error information here.

That's about as dumbed-down as I can make it :-)

Of course, that's mostly by convention. There's nothing stopping you from writing your error information to standard output if you wish. You can even close the three file handles totally and open your own files for I/O.

When your process starts, it should already have these handles open and it can just read to and/or write from 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:

my_prog <inputfile 2>errorfile | grep XYZ

which will:

  • create a process for my_prog.
  • open inputfile as your standard input (file handle 0).
  • open errorfile as your standard error (file handle 2).
  • create another process for grep.
  • attach the standard output of my_prog to the standard input of grep.

Re 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 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 a major and minor device number.

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.

paxdiablo
Thats for your response. While I can understand the purpose of the files from what you describe, I would like to move a level more. when I open these files in /dev folder, how come I never get to see the output of a process running. Say I execute top on the terminal, is it not supposed to output its results onto the stdout file periodically, hence when it is being updated I should be able to see an instance of the output being printed onto this file. But this is not so.. So are these files not the same (the ones in the /dev directory).
Shouvik
Because those aren't technically files. They're device nodes, indicating a specific device to write to. UNIX may _present_ everything to you as a file abstraction but that doesn't make it so at the deepest levels.
paxdiablo
Oh okay, that makes a lot more sense now! So incase I want to access the data stream that a process is writing onto these "files" which I am not able to access. How would it be possible for me to intercept it and output it to a file of my own? Is it possible theoretically to log whatever occurs on each one of these files onto a single for a given process?
Shouvik
Use the shell redirection capability. `xyz >xyz.out` will write your standard output to a physical file which can be read by other processes. `xyz | grep something` will connect `xyz` stdout to `grep` stdin more directly. If you want unfettered access to a proces you don't control that way, you'll need to look into something like `/proc` or write code to filter the output by hooking into the kernel somehow. There may be other solutions but they're all probably as dangerous as each other :-)
paxdiablo
So essentially what you are telling me is that every process created its own? Thanks that helps a lot. Hope its not getting annoying. :-)
Shouvik
@Shouvik, note that `/dev/stdin` is a symlink to `/proc/self/fd/0` -- the first file descriptor that the currently running program has open. So, what is pointed to by `/dev/stdin` will change from program to program, because `/proc/self/` always points to the 'currently running program'. (Whichever program is doing the `open` call.) `/dev/stdin` and friends were put there to make setuid shell scripts safer, and let you pass the filename `/dev/stdin` to programs that only work with files, but you want to control more interactively. (Someday this will be a useful trick for you to know. :)
sarnold
@sarnold @oax thanks for all the help, I guess most of my inquisitiveness is satisfied for now! As an when I run into trouble, I know where to find the standard stream gurus now! :)
Shouvik
+6  A: 

It would be more correct to say that stdin, stdout, and stderr are "I/O streams" rather than files. As you've noticed, these entities do not live in the filesystem. But the Unix philosophy, as far as I/O is concerned, is "everything is a file". In practice, that really means that you can use the same library functions and interfaces (printf, scanf, read, write, select, etc.) without worrying about whether the I/O stream is connected to a keyboard, a disk file, a socket, a pipe, or some other I/O abstraction.

Most programs need to read input, write output, and log errors, so stdin, stdout, and stderr are predefined for you, as a programming convenience. This is only a convention, and is not enforced by the operating system.

Jim Lewis
Thanks for your inputs. Would you happen to know how I could intercept the output data stream of a process and output it into a file of my own?
Shouvik
+2  A: 

stdin

Reads input through the console(eg Keyboard input) Used in C with scanf

scanf(<formatstring>,<pointer to storage> ...);

stdout

Produces output to the console Used in C with printf

printf(<string>, <values to print> ...);

stderr

Produces 'error' output to the console Used in C with fprintf

fprintf(stderr, <string>, <values to print> ...);

Redirection

The source for stdin can be redirected eg Intstead of keyboard input in can come from a file(echo < file.txt ) or another program ( ps | grep <userid>).

The destinations for stdout, stderr can also be redirected. For example stdout can be redirected to a file: ls . > ls-output.txt, in this case the output is written to the file ls-output.txt. Stderr can be redirected with 2>.

mikek3332002
+3  A: 

I'm afraid your understanding is completely backwards. :)

Think of "standard in", "standard out", and "standard error" from the program's perspective, not from the kernel's perspective.

When a program needs to print output, it normally prints to "standard out". A program typically prints output to standard out with printf, which prints ONLY to standard out.

When a program needs to print error information (not necessarily exceptions, those are a programming-language construct, imposed at a much higher level), it normally prints to "standard error". It normally does so with fprintf, which accepts a file stream to use when printing. The file stream could be any file opened for writing: standard out, standard error, or any other file that has been opened with fopen or fdopen.

"standard in" is used when the file needs to read input, using fread or fgets, or getchar.

Any of these files can be easily redirected from the shell, like this:

cat /etc/passwd > /tmp/out     # redirect cat's standard out to /tmp/foo
cat /nonexistant 2> /tmp/err   # redirect cat's standard error to /tmp/error
cat < /etc/passwd              # redirect cat's standard input to /etc/passwd

Or, the whole enchilada:

cat < /etc/passwd > /tmp/out 2> /tmp/err

There are two important caveats: First, "standard in", "standard out", and "standard error" are just a convention. They are a very strong convention, but it's all just an agreement that it is very nice to be able to run programs like this: grep echo /etc/services | awk '{print $2;}' | sort and have the standard outputs of each program hooked into the standard input of the next program in the pipeline.

Second, I've given the standard ISO C functions for working with file streams (FILE * objects) -- at the kernel level, it is all file descriptors (int references to the file table) and much lower-level operations like read and write, which do not do the happy buffering of the ISO C functions. I figured to keep it simple and use the easier functions, but I thought all the same you should know the alternatives. :)

sarnold
So is it when the process is being executed that it writes errors onto this stderr file or when the program is being compiled from its source. Also when we talk about these files from the perspective of compiler is it different than when it is compared with say a program?
Shouvik
@Shouvik, the compiler is just another program, with its own stdin, stdout, and stderr. When the compiler needs to write warnings or errors, it will write them to stderr. When the compiler front-end outputs intermediate code for the assembler, it might write the intermediate code on stdout and the assembler might accept its input on stdin, but all that would be behind the scenes from your perspective as a user.) Once you have a compiled program, that program can write errors to its standard error as well, but it has nothing to do with being compiled.
sarnold
Thanks for that token of information. I guess it's pretty stupid of me not to see it in that perspective anyway... :P
Shouvik