When an application is launced in linux, it becomes a process. A process is a special instance provided by the operating system that inclused all the resources that are used by the running app.

A process in linux has a five digit identifier called process id or PID and it represents process for all it’s life cycle. There cannot be two process with same PID but once the process is terminated same PID can be reused for another process if needed.

Other than PID process has different properties that characterize a process

PPID - Parent Process ID of the process that started this process
Nice Number - Degree of friendliness towards other processes
Terminal / TTY - Terminal to which process is connected
RUID / EUID -  User ID of owner of the process
RGID / EGID - Group owner ID of the group owner of the process

In terminal typing ps -f will show the currently running process

➜  ~ ps -f
UID   PID  PPID   C STIME   TTY           TIME CMD
501 58001 58000   0  5:37PM ttys004    0:00.13 -zsh
501 56796  1695   0  5:28PM ttys005    0:00.12 /bin/zsh -il

Proces Life Cycle

The creating of process happens in two different ways

  1. fork - This duplicates the calling process. The child (new process) is an exact copy (memory) of the parent (calling process), except for the following:
    • PIDs are different.
    • The PPID of the child equals the PID of the parent.
    • The child does not inherit the following from the parent:
      • Memory locks
      • Semaphore adjustments
      • Outstanding asynchronous I/O operations
      • Asynchronous I/O contexts
    • exec: This replaces the current process image with a new one, loading the program into the current process space and running it from its entry point.

Background and Foreground process

A process is often launched in the foreground, preventing communication with the shell until the task is completed or the process is interrupted. It is possible to continue using the shell by starting a process in the background by adding a & symbol to the end of the command (cat file.txt &). By pressing Ctrl + Z, the user can suspend the foreground process from the shell by sending the SIGTSTP signal. With the fg command or in the background with the bg command, it can be continued.

The jobs command reports the jobs running and their numbers. In the output, the numbers in square brackets are the job numbers that are used by the process control commands, such as fg and bg.

Killing a Job

Ctrl + Z can be used to end the foreground process with the SIGINT signal. The kill command can be used to terminate a background process or give it any signal.

The argument passed to the kill command can be one of the following:

The signal sent to the process The PID or job number (prefixed with a%)

The following are some of the more prominent signals:

SIGINT: Indicates a termination caused by user input and can be sent by kill command with the -2 value SIGTERM: Represents a general purpose termination request not generated by a user as well as a default signal for the kill command with a -6 value SIGKILL: A termination handled directly by the operating system that kills the process immediately and has a -9 value