Process Management In Linux | Stages, Types & Commands
Process management is an essential component of the Linux operating system. Linux is known for its robust and stable performance, and it owes a lot to its process management capabilities. In simple terms, a program in execution is considered a process. The code is available in a readable format in an executable file inside a program's address space. The elements of activity inside a process are called threads and every thread has a unique program counter. This subsystem is responsible for managing all the processes and task lists on a Linux system. This article will discuss the basics of process management, types of process management, and commands used to manage these processes in Linux.
Stages of a Process in Linux
In Linux, a process goes through several stages during its lifetime. Understanding these stages and checking how they run in the background is important for process management and troubleshooting. The states of a process in Linux are as follows:
-
Created: A process is created when a program is executed. At this stage, the process is in a "created" state, and its data structures are initialized.
-
Ready: The process enters the "ready" state when it is waiting to be assigned to a processor by the Linux scheduler. At this stage, the process is waiting for its turn to execute.
-
Running: The process enters the "running" state when it is assigned to a processor and is actively executing its instructions.
-
Waiting: The process enters the "waiting" state when it is waiting for some event to occur, such as input/output completion, a signal, or a timer. At this stage, the process is not actively executing its instructions.
-
Terminated: The process enters the "terminated" state when it has completed its execution or has been terminated by a signal. At this stage, the process data structures are removed, and its resources are freed.
-
Zombie: A process enters the "zombie" state when it has completed its execution but its parent process has not yet read its exit status. At this stage, the process details still have an entry in the process table, but it does not execute any instructions. The zombie process is removed from the process table when its parent process reads its exit status.
To view active processes and the current task lists in Linux we can use two popular commands i.e. ps and top command. The ps command displays information about the active programs or active processes on the system whereas the top command gives you a real-time view of the active processes in the running system.
Types of Process Management in Linux
In Linux, there are two types of process management :
1. Foreground Processes:
Foreground processes are interactive processes that require user input. These processes are executed in the foreground and are associated with a terminal. Examples of foreground processes include editors, shells, and commands that require user input. The Linux operating system provides several tools to manage foreground processes. One of the most commonly used tools is the shell, which is a command-line interface to the operating system. The shell allows the user to start, stop, pause, and resume foreground processes. It also provides tools to monitor and manage the resources used by these processes, such as virtual memory and CPU usage.
Another tool for managing foreground processes is the "ps" command, which displays information about running processes. The "ps" command can be used to list all processes or filter the list based on various criteria, such as the user who started the process, the process ID (PID), and the process status.
The foreground process can also be managed using signals. A signal is a software interrupt that is sent to a process to request it to take a specific action. For example, the "Ctrl+C" signal is sent to a process to request it to terminate.
The foreground process can also be managed using job control commands. Job control allows the user to start, stop, and resume foreground processes. The shell provides several job control commands, such as "bg" to move a process to the background, "fg" to move a process to the foreground, and "kill" to terminate a process.
2. Background Processes (Non-Interactive Processes):
Background process management in Linux refers to the management of processes that run in the background and do not require user interaction. These processes are also referred to as non-interactive processes and automatic processes. These processes can be started by the operating system or by a user, and they do not have an associated terminal.
Background processes are typically used for system services and daemon processes that run continuously in the background. They can also be used for running long-running jobs or task lists that do not require user interaction.
Linux provides several tools for managing background process execution. One of the most commonly used tools is the "ps" command, which displays information about running processes. The "ps" command can be used to filter the list of processes to show only background processes.
Another tool for managing background processes is the "top" command, which provides a real-time view of system processes, including background processes. The "top" command can be used to monitor the resources used by background processes, such as memory and CPU usage.
Background processes can be started using several methods. One method is to use the "&" symbol at the end of a command, which runs the command in the background. For example, the command "sleep 60 &" starts a process that sleeps for 60 seconds in the background.
Another method for starting background processes is to use the "nohup" command, which allows a process to continue running even after the user logs out. The "nohup" command redirects the output of the process to a file system, which can be later examined by the user.
Background processes can be managed using the "kill" command, which terminates a process during execution. The "kill" command can be used with the process ID (PID) of the background process or with a signal number to request a specific action.
Commands Used to Manage Processes in Linux
There are several commands used in process management in Linux. Here are some of the most commonly used commands:
-
ps: This command is used to display information about running processes. The "ps" command can be used to list all processes or filter the list based on various criteria, such as the user who started the process, the process ID (PID), and the process status.
-
top: This command is used to display a real-time view of system processes. The "top" command provides information about the processes running on the system, including their resource usages, such as CPU and memory.
-
kill: This command is used to terminate a process. The "kill" command can be used with the process ID (PID) of the process or with a signal number to request a specific action.
-
nice: This command is used to adjust the priority of a process. Higher-priority processes get more CPU time than lower-priority processes. The "nice" command can be used to increase or decrease the priority of a process, which affects its CPU usage.
-
renice: This command is used to change or adjust the priority of a running process, which affects its CPU usage.
-
pkill: This command is used to send a signal to a process to request it to terminate. The "pkill" command can be used with a current process name or a regular expression to match multiple processes.
-
top: This command is used to display a real-time view of system processes. The "top" command provides information about the processes running on the system, including their resource usages, such as CPU and memory.
-
jobs: This command is used to display a list of background jobs running in the current shell session.
-
fg: This command is used to move a background process to the foreground. The "fg" command can be used with the job ID of the background process.
-
bg: This command is used to move a suspended process to the background. The "bg" command can be used with the job ID of the suspended process.
Also read - Prepare For Linux Interview Questions
Process Family Tree
In Linux, every process is a descendant of the init process, whose PID is one. Each process has a unique Process ID (PID) assigned to it, and each process may have one or more child processes. These child processes are created using a fork() system call and become a new process with their own PID.
The process family tree in Linux is also known as the Process Hierarchy, which shows the relationship between the parent process and the child process. The parent process is the one that creates a child process, and the child process inherits some attributes from its parent process. The process identifier for the init task is locally assigned as init_task.
To view the process family tree in Linux, you can use the "pstree" command. Here's an example:
pstree -p <PID>
Replace "<PID>" with the process ID of the parent process you want to view the family tree of. The "-p" option displays the process IDs of all processes in the tree.
For example, to view the process family tree of the current shell process, you can run the following command:
pstree -p $$
The "$$" represents the PID of the current shell process.
The output of the "pstree" command displays the hierarchy of parent and child processes in a tree-like format. Each process is represented by a node, and the process IDs are shown in parentheses next to the process name.
You might also be interested in reading the following:
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Comments
Add comment