Stream

From LQWiki
Jump to navigation Jump to search

In Unix and Unix-like operating systems, the fundamental concept of the stream (filestream) a file which is attached to ("opened in") a program. There are three "standard streams" which are nearly always present in any running program:

  • a standard input stream, stdin - for reading data
  • a standard output stream, stdout - for outputting data
  • a standard error stream, stderr - for reporting errors

Only daemon programs usually ignore or run without these streams.

These names are also occasionally used for the underlying file descriptor which must be open for the stream to function. These file descriptors can only be manipulated directly inside of programs and have names in the standards, but also have numbers which have been used since the beginnings of UNIX, and are also commonly used. The names in the C standard, and the corresponding numbers are:

* standard input   descriptor 0 named "STDIN_FILENO" in the C standard
* standard output  descriptor 1 named "STDOUT_FILENO" in the C standard
* standard error   descriptor 2 named "STDERR_FILENO" in the C standard

User programs generally use the file streams rather than descriptors directly for a number of reasons:

  1. It allows the flexibility to work with any file descriptor specified by the shell
  2. Formatting and input deciphering are designed to work on file streams; by comparison the file descriptor interface is very primitive.
  3. Operations on filestreams are often much faster because they provide buffering by default (except stderr which is commonly unbuffered for other reasons).

The concept of a file is broad enough to include just about any stream of bytes. This includes keyboard input, the monitor, sound card, joystick, microphone, pipe, FIFO, internet connection, GPS receiver as well as the normal files on a hard drive, CD or USB device and more. Even parts of the computer memory can be treated as a file (see ramdisk or the file /proc/kcore). This is commonly described as "In UNIX, everything is a file." This does not mean that all files have the same capabilities, however. Some can only be read (GPS receiver), or only written (printer); some cannot respond to seek, truncate or rewind commands (keyboard, pipe, internet connection), and so on.

NOTE: it is almost never a good idea to mix file stream operations with file descriptor operations on the same file because it is almost guaranteed to do something different from what you want or expect.

When a program is first started, the standard streams are created on the corresponding file descriptors when first accessed. It is common for a shell command to rearrange the file descriptors before starting a command, so that the command can be using its normal file streams, but they are connected to whatever actual file the file descriptor indicates. Pipelines are implemented in this way.

Streams can also be redirected into files and devices. You can play a stream on your sound card, print it on your printer or save it as a backup in the file system.

The idea of a separate standard error stream is useful in that it allows the more efficient diagnoses of errors in a long pipeline, which may not be seen if error messages are included with standard output. See stderr for more.