Process Forking

In multitasking operating systems, processes (running programs) need a way to create new processes, e.g. to run other programs. fork is an operation whereby a process creates a copy of itself which runs concurrently. Forking is typically the only way of launching new processes in Unix-like systems and is required for compliance with the POSIX and Single UNIX Specification standards.

For a process to start the execution of a different program, it first forks to create a copy of itself. When a process forks, it is deemed the parent process and the newly created process is its child process.

After the fork, both parent and child processes are identical. They both call the kernel and inspect the call’s return to determine their status, child or parent, and act accordingly. The child process then ceases execution of its former program in favor of the new program.

File Descriptors

All open files have a number of file descriptors (between 20 and 63) which describe thing about the file like its input and output. File descriptors are defined as non-negative integers. The first three file descriptors are always defined:

  • 0: Standard input (stdin). The command which started the process.
  • 1: Standard output (stdout). Output generated by the process.
  • 2: Standard error (stderr). Errors generated by the process.

NOTE: When the parent process forks, the child process inherits the file descriptors of the parent.

File Operators

Handling input, output and errors.

  • |: Pipe. Deliver the output of one program as the input to another program.
  • >: Send-to. Left-side is what file descriptor to send, right-side is where to send it. If no left-side value is given, it is assumed 1 as in 1>. Bt default the right-side value is interpreted as a file name.
  • &: Indicates that what follows > is a file descriptor and not an actual filename.

NOTE: 2>1 may look like a good way send stderr (2) to stdout (1). However, it will actually be interpreted as “redirect” stderr (2) to a file named “1”. & is needed because it indicates that what follows > is a file descriptor and not an actual filename.

Pipe all output to file and stdout

file_a 2>&1 | tee -a file_b
Take any output or errors generated by file_a and append them to file_b and also send them to standard output (stdout 1).

file_a 2>&1 | tee -a file_b
   1    2   3    4     5
  1. file_a: The file being executed.
  2. 2>&1: Combine (>&) any error output (stderr 2) from the file with any standard output (stdout 1) from the file.
  3. |: Pipe the output of the executing program into another program.
  4. tee -a: tee reads from the standard input (stdin 0) and writes to both standard output (stdout 1) and one or more files at the same time. -a (--append) – Do not overwrite files instead append to the given files.
  5. file_b: The file to pipe the output to.