ruk·si

🐧 Linux
Processes

Updated at 2024-10-30 03:40

A process is an instance of a running program

When a program is executed, the kernel creates a new process and loads the code into virtual memory and allocates space for program variables.

Process ID (PID) is a unique identifier for a process.

On boot, kernel creates a special init process from /sbin/init. The init process always has PID 1 and runs with root privileges.

A process is logically divided into the following parts, known as segments:

  • Text: the instructions of the program (the code)
  • Data: the static variables used by the program
  • Heap: an area from which programs can dynamically allocate extra memory
  • Stack: a piece of memory that grows and shrinks as functions are called

Each process has a current working directory. This is from where relative paths are resolved for the process.

A process inherits three file descriptors from its parent process:

  • standard input (0) from where the process reads input
  • standard output (1) to where the process writes output
  • standard error (2) to where the process writes errors and notifications

A process can create more processes by using the fork() system call. The new process is called a child process while the original process is called the parent process.

Forked processes frequently use the execve() system call to load and execute an entirely new program. This program replacement replaces all process segments discussed above.

Each process has an environment list; a set of environment variables.

  • Environment variables are in the user-space memory of the process.
  • A process inherits a copy of its parent's environment on fork().
  • A process may choose to copy parental environment on exec() program replacement.

This can be used to pass basic information from the parent to the child.

All processes report their status on exit. A "return code" of 0 indicates that the process succeeded and, although a convention, it is a very strong convention.

Most shells allow reading the return code of the last command executed using the $?.

# true = do nothing, successfully
true
echo $?
# 0

# false = do nothing, unsuccessfully
false
echo $?
# 1

Types of Processes

The simples process is a filter that reads its input from standard input, performs some transformation of that input, and writes the transformed data to standard output. Examples of filters include cat, grep, sort, and wc.

Interprocess communication (IPC)

Processes can communicate with each other using various mechanisms:

  • Signals: Used to indicate that an event has occurred.
  • Pipes (|) Used to transfer data between processes.
  • FIFOs: Used to transfer data between processes.
  • Sockets: Used to transfer data from one process to another, either on the same host computer or on different hosts connected by a network.
  • File locking: Allows a process to lock regions of a file to prevent other processes from reading or updating the file contents.
  • Message queues: Used to exchange packets of data between processes.
  • Semaphores: Used to synchronize the actions of processes.
  • Shared memory: Allows two or more processes to share a piece of memory. When one process changes the contents of the shared memory, all other processes can immediately see the changes.

Sources

  • The Linux Programming Interface, Michael Kerrisk