Mark Hobley
STANDARD STREAMS

Standard Streams

The Unix operating system provides support for device abstraction. This enables application programs to use different input and output devices without the need to be coded with different device handling routines. This technology enables input and output redirection to take place, for example, a program that is expecting input from a keyboard can instead take input from a file, or output to the screen to be sent to a printer or to a file. The standard streams are automatically configured by the operating system, typically allowing user input to be obtained from the operators terminal keyboard, and output and error messages to be sent to the operators screen.

In Unix, each process has its own file descriptor table, and the following standard streams are defined automatically for each process that is created:

Filehandle Name Description
0 stdin Standard Input
1 stdout Standard Output
2 stderr Standard Error

Standard input

Standard Input is the stream where input data being fed into the application program. This typically comes from the keyboard of the operators terminal, unless redirection is being used. Not all programs require user input.

File descriptor 0 is used for standard input.

Standard output

Standard Output is the stream where a program writes its output data. This typically goes to the operators terminal screen, unless redirection is being used. Not all programs generate output.

File descriptor 1 is used for standard output.

Standard error

The Standard Error stream is typically used by programs to output error messages or diagnostics. It is a stream independent of standard output and can be redirected separately. This separation allows error messages to still be output to the operators terminal screen, even if standard output is redirected.

File descriptor 2 is used for standard error.