Standard streams

Standard streams

In Unix and Unix-like operating systems, as well as certain programming language interfaces, the standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution. The three I/O connections are called standard input, standard output and standard error.

Background

In most operating systems predating Unix, programs had to explicitly connect to the appropriate input and output data. On many of those systems, this could be an intimidating programming challenge created by OS-specific intricacies such as obtaining control environment settings, accessing a local file table, determining the intended data set, and handling the correct case of a card reader, magnetic tape drive, disk drive, line printer, card punch, or interactive terminal.

Unix provided several groundbreaking advances, one of which was to provide "abstract devices": it removed the need for a program to know or care what kind of devices it was communicating with. Older operating systems forced upon the programmer a record structure and, frequently non-orthogonal data semantics and device control. Unix eliminated this complexity with the concept of a data stream: an ordered sequence of data bytes which can be read until the end of file. A program may also write bytes as desired and need not (and can't easily) declare how many there will be, or how they will be grouped.

Another Unix breakthrough was to automatically associate input and output by default—the program (and programmer) did absolutely nothing to establish input and output for a typical input-process-output program (unless it chose a different paradigm). In contrast, previous operating systems usually required some—often complex—job control language to establish connections, or the equivalent burden had to be orchestrated by the program.

Since Unix provided standard streams, the Unix C runtime environment was obligated to support it as well. As a result, most C runtime environments (and C's descendants), regardless of the operating system, provide equivalent functionality.

tandard input (stdin)

Standard input is data (often text) going into a program. The program requests data transfers by use of the "read" operation. Not all programs require input. For example, the "dir" or "ls" program (which displays file names contained in a directory) performs its operation without any stream data input.

Unless redirected, input is expected from the text keyboard which started the program.

The file descriptor for standard input is 0 (zero); the corresponding "" variable is FILE* stdin; similarly, the "" variable is std::cin.

tandard output (stdout)

Standard output is the stream where a program writes its output data. The program requests data transfer with the "write" operation. Not all programs generate output. For example the "file rename" command (variously called "mv", "move", "ren") is silent on success.

Unless redirected, standard output is the text terminal which initiated the program.

The file descriptor for standard output is 1 (one); the corresponding "" variable is FILE* stdout; similarly, the "" variable is std::cout.

tandard error (stderr)

Standard error is another output stream typically used by programs to output error messages or diagnostics. It is a stream independent of standard output and can be redirected separately. The usual destination is the text terminal which started the program to provide the best chance of being seen even if "standard output" is redirected (so not readily observed). For example, output of a program in a pipeline is redirected to input of the next program, but errors from each program still go directly to the text terminal.

It is acceptable—and normal—for "standard output" and "standard error" to be directed to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unless buffering is involved. (For example, a common situation is when the standard error stream is unbuffered but the standard output stream is line-buffered; in this case, text written to standard error later may appear on the terminal earlier, if the standard output stream's buffer is not yet full.)

The file descriptor for standard error is 2; the corresponding "" variable is FILE* stderr. The C++ "" standard header provides two variables associated with this stream: std::cerr and std::clog, the former being unbuffered and the latter using the same buffering mechanism as all other C++ streams.

Most shells allow both "standard output" and "standard error" to be redirected to the same file using >& filename

Bourne-style shells allow "standard error" to be redirected to the same destination that standard output is directed to using 2>&1

Timeline

1950s: Fortran

Fortran had the equivalent of Unix file descriptors, UNIT=5 for stdin, and UNIT=6 for stdout.! FORTRAN 77 example PROGRAM MAIN READ(UNIT=5,*)NUMBER WRITE(UNIT=6,'(F5.3)')' NUMBER IS: ',NUMBER END

1960: ALGOL 60

ALGOL 60 was criticized for having no standard file access.

1968: ALGOL 68

ALGOL 68's input and output facilities were collectively referred to as the transput. Koster coordinated the definition of the "transput" standard. This standard included: stand in, stand out, stand error and stand back.

Example:


# ALGOL 68 example #main:( REAL number; getf(stand in,($g$,number)); printf(($"Number is: "g(6,4)"OR "$,number)); # OR # putf(stand out,($" Number is: "g(6,4)"!"$,number)); newline(stand out))

1970s: C and Unix

In the C programming language stdin, stdout and stderr streams were attached to the existing Unix file descriptors 0, 1 and 2 respectively.

1995: Java

In Java, the standard streams are referred to by Javadoc:SE|java/lang|System|in (for stdin), Javadoc:SE|java/lang|System|out (for stdout), and Javadoc:SE|java/lang|System|err (for stderr).public static void main(String args [] ) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); double number = double.Parse(s); System.out.println("Number is:", number); } catch (Exception e) { System.err.println("Error:" + e.getMessage());

2000s: .NET

In C# and other .NET languages, the standard streams are referred to by System.Console (for stdin and stdout) and System.Console.Error (for stderr).// C# examplepublic static int Main(){ double number; string s;

try { s = System.Console.ReadLine(); number = double.Parse(s); System.Console.WriteLine("Number is: {0:F3}", number); return 0; } catch (System.FormatException e) { // if Parse() threw an exception System.Console.Error.WriteLine("No number was entered!"); return 1;

' Visual Basic .NET example

Public Function Main() As Integer Dim number As Double Dim s As String

Try s = System.Console.ReadLine() number = CDbl(s) System.Console.WriteLine("Number is: {0:F3}", number) Return 0 Catch e As System.InvalidCastException ' if CDbl() threw an exception System.Console.Error.WriteLine("No number was entered!") Return 1 End TryEnd Function

GUIs

Graphical user interfaces (GUIs) rarely make use of the standard streams. Consequently, redirecting GUI programs or constructing a GUI pipeline is neither practical nor useful. The nearest analog is probably "cutting" (or "copying") from one application and "pasting" into another. Since manual user operations are required, moving large numbers of "pastes" is not especially efficient. One notable exception is the dwm tiling window manager, which displays data directed through stdin on a status bar.

Some GUI programs, primarily on Unix, still write debug information to standard error.

GTK-server can use stdin as communication interface with an interpreted program to realize a GUI.

ee also

* Redirection (Unix)
* Pipeline (Unix)
* Stream (computing)
* Input/output
* C file input/output

References

* "KRONOS 2.1 Reference Manual", Control Data Corporation, Part Number 60407000, 1974
* "NOS Version 1 Applications Programmer's Instant", Control Data Corporation, Part Number 60436000, 1978
* [http://www.bitsavers.org/pdf/honeywell/multics/AG90-03_PgmgIntro_Dec81.pdf Level 68 Introduction to Programming on MULTICS] , Honeywell Corporation, 1981
* [http://www.research.ibm.com/journal/rd/255/auslander.pdf Evolution of the MVS Operating System] , IBM Corporation, 1981
* "Lions' Commentary on UNIX Sixth Edition", John Lions, ISBN 1-57398-013-7, 1977
* [http://msdn2.microsoft.com/en-us/library/system.console.aspx Console Class, .NET Framework Class Library] , Microsoft Corporation, 2008

External links

* [http://www.linfo.org/standard_output.html Standard Output Definition] - by The Linux Information Project (LINFO)
* [http://compsecblog.bruceblacklaws.com/2008/07/redirect-stdout-and-stderr.html Video tutorial demonstrating stdout and stderr]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Standard error — can refer to:* Standard error (statistics), the estimated standard deviation or error of a series of measurements * Standard error stream, one of the standard streams in Unix like operating systems …   Wikipedia

  • Standard-Datenströme — Die Standard Datenströme für Eingabe, Ausgabe, und Fehler Die Standard Datenströme (englisch: standard streams) sind drei Datenströme für die Ein und Ausgabe im Betriebssystem Unix oder in verwandten Betriebssystemen. Sie werden auch von der… …   Deutsch Wikipedia

  • Standard-definition television — or SDTV refers to television systems that have a resolution that meets standards but not considered either enhanced definition or high definition. The term is usually used in reference to digital television, in particular when broadcasting at the …   Wikipedia

  • Standard conditions for temperature and pressure — Not to be confused with Standard state. In chemistry, standard condition for temperature and pressure (informally abbreviated as STP) are standard sets of conditions for experimental measurements, to allow comparisons to be made between different …   Wikipedia

  • Standard (warez) — Standards in the warez scene are defined by groups of people who have been involved in its activities for several years and have established connections to large groups. These people form a committee, which creates drafts for approval of the… …   Wikipedia

  • Standard Template Library — Als Standard Template Library (STL) werden verschiedene in der Programmiersprache C++ geschriebene Bibliotheken bezeichnet. Ursprünglich wurde mit Standard Template Library eine in den 1980er Jahren bei Hewlett Packard (kurz: HP) entwickelte, in… …   Deutsch Wikipedia

  • System (C standard library) — In the C standard library, system is a function used to execute subprocesses and commands, residing in stdlib.h. It differs from the exec/spawn family of functions in that instead of passing arguments to an executed object, a single string is… …   Wikipedia

  • Java Platform, Standard Edition — or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use.In practical terms, Java SE consists of a virtual machine, which must be used to run Java… …   Wikipedia

  • Daylighting (streams) — Here Marin Creek has been daylighted. For the use of natural light in indoor illumination, see Daylighting. For a similar operation with transportation tunnels, see Daylighting (tunnels). In urban design and urban planning, daylighting is the… …   Wikipedia

  • Open standard — An open standard is a standard that is publicly available and has various rights to use associated with it, and may also have various properties of how it was designed (e.g. open process). There is no single definition and interpretations vary… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”