All You Need To Know About Processes in Linux


If you're a user of Linux, you've probably heard term "process" thrown around a lot. But what exactly are processes in Linux, and how do they work? In this article, we'll take a deep dive into everything you need to know about processes in Linux.

What are processes in Linux?

In simple terms, a process is a program that is currently executing on your Linux system. Each process has its own unique process ID (PID), which is used to identify and manage it. Every time you run a program on your Linux system, a new process is created to run that program. This process runs independently of other processes, allowing multiple programs to run simultaneously.

How do processes work in Linux?

Processes in Linux work in a hierarchical structure, with each process having a parent process and potentially having child processes. init process is parent process for all other processes on your system. When you run a program on your system, a new process is created with init process as its parent.

Each process has its own address space, which is used to store code and data for that process. This address space is isolated from other processes, preventing one process from accessing another process's memory. kernel manages this isolation by using memory protection mechanisms.

Process states in Linux

Processes in Linux can be in one of several states −

  • Running − process is currently running and executing instructions.

  • Sleeping − process is waiting for some event to occur, such as waiting for user input or waiting for data from a file.

  • Stopped − process has been stopped by a signal and is not currently executing any instructions.

  • Zombie − process has completed execution but has not yet been reaped by its parent process.

Managing processes in Linux

There are several commands in Linux that you can use to manage processes −

ps

This command displays a list of currently running processes on your system. You can use ps aux command to display a more detailed list of processes, including process ID, CPU usage, and memory usage.

kill

This command is used to send a signal to a process, which can be used to terminate a process or to send a specific signal to a process. For example, you can use kill -9 <pid> command to force a process to terminate.

top

This command displays a real-time view of processes running on your system, including CPU and memory usage. This command is useful for monitoring system performance.

htop

htop is an interactive process viewer that provides a real-time view of processes running on your system. It is similar to top command but provides more advanced features, such as ability to filter and sort processes based on specific criteria. To install htop on your system, you can use following command −

sudo apt-get install htop

pstree

pstree is a command that displays a hierarchical view of processes running on your system. It displays processes in a tree-like structure, with init process at root and child processes branching out from it. This command can be useful for visualizing relationships between processes on your system.

killall

killall is a command that allows you to kill processes by name instead of by process ID. This can be useful when you need to terminate multiple processes with a single command. For example, to terminate all instances of firefox browser, you can run following command −

killall firefox

nice

nice is a command that allows you to set priority of a process. Processes with a higher priority will be given more CPU time, while processes with a lower priority will be given less CPU time. This command can be useful for managing system performance and ensuring that critical processes are given higher priority. For example, to set priority of a process to a lower value, you can run following command −

nice -n 10 <command>

Replace <command> with command you want to run with a lower priority.

Examples of managing processes in Linux

Let's take a look at some examples of managing processes in Linux using commands we just discussed.

Example 1: Using ps to display a list of running processes

To display a list of running processes on your system, you can use ps command. For example, to display a list of all running processes, you can run following command −

ps

This will display a list of running processes on your system, including process ID and other information.

Example 2: Using kill to terminate a process

If you need to terminate a process, you can use kill command. For example, to terminate a process with a specific process ID, you can run following command −

kill <pid>

Replace <pid> with process ID of process you want to terminate.

Example 3: Using top to monitor system performance

To monitor system performance, you can use top command. This command displays a real-time view of processes running on your system, including CPU and memory usage. For example, to display a real-time view of processes running on your system, you can run following command −

top

This will display a real-time view of processes running on your system, sorted by CPU usage.

One more important aspect of processes in Linux is process signals. Signals are a way for kernel to communicate with processes and notify them of various events. For example, a signal can be used to terminate a process or to interrupt a process to handle a critical event.

There are several signals that can be sent to processes in Linux, each with its own specific purpose. Some of most commonly used signals include −

  • SIGTERM − This signal is used to terminate a process gracefully. When a process receives this signal, it is given a chance to clean up its resources before exiting.

  • SIGKILL − This signal is used to forcibly terminate a process. When a process receives this signal, it is immediately terminated without any chance to clean up its resources.

  • SIGHUP − This signal is used to notify a process that its parent process has terminated. When a process receives this signal, it is typically expected to terminate as well.

  • SIGINT − This signal is used to interrupt a process. It is typically sent when user presses Ctrl+C in terminal to interrupt a running process.

To send a signal to a process in Linux, you can use kill command, followed by process ID and signal number. For example, to send a SIGTERM signal to a process with a process ID of 1234, you can run following command −

kill -TERM 1234

This will send a SIGTERM signal to process with a process ID of 1234, asking it to terminate gracefully.

In addition to signals we discussed earlier, Linux also supports user-defined signals. User-defined signals can be used to implement custom communication between processes or to handle specific events within a process.

Conclusion

Processes are an essential part of Linux operating system, allowing multiple programs to run simultaneously on a single system. Understanding how processes work and how to manage them is crucial for maintaining system performance and stability. By using commands we discussed in this article, you can effectively manage processes on your Linux system.

Updated on: 20-Apr-2023

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements