본문 바로가기

OS/Linux

[Theorem] Standard Streams/File Descriptors

1. Standard Streams

A stream refers to the flow of data, and all processes in progress interact with the environment through the stream. And process refers to a program being executed. A parent process is a process that occurs before other processes, and a child process is a process that runs after the parent process.

 

Standard Streams refers to input/output channels that are preconnected between computer program and environements in Unix-like operating systems. In general, there are three standard streams that operate on Unix-like operating systems.

 

  • Standard Input, stdin, 0
  • Standard output, stdout, 1
  • Standard error, stdout, 2

 

1.1 Redirecting/Piping Streams 

Redirecting a processi s sending stdout to a file. A pipeline that connects one process to another is to connect the stdout of the previous process to the stdin of the next process. The redirection of stderr is performed by 2> using 2 which is the file descriptor of stderr. The command takes the input from stdin.

 

user@host: /home$ [command] [option] [file] 1> file1
user@host: /home$ [command] [option] [file] 2> file2
user@host: /home$ [command] [option] [file] 0< file3

 

2. File Descriptors

The method of recognizing a standard stream in the operating system is abstracted by expressing it as an integer of 0, 1, and 2. The integer is called a File Descriptor. A file descriptor is a concept used by processes to handle files in Unix-like systems, and is an abstract value used by processes to access specific files.

 

user@host: /home$ ls file1 file2 >file3 2>&1

 

>& operator above designates the destination of stderr the same as the destination of stdout.