A Beginner’s Guide to Linux Process and Thread

Process vs Threads differences

Linux operating system is a command line utility to execute commands. When a user inputs a command, a program is initiated that runs several processes to execute that command. To work with the command line operating system, you must know how the Linux processes and threads are created and how they perform a task.

This article aims to provide a basic understanding of Linux processes and threads. Moreover, we will discuss creating, managing, and using the processes and threads throughout this write-up.

Introduction to Process and Thread in Linux

The processes are the main entity to run any program. A process can have a child process called threads, which works separately to execute a program. Several threads can work in parallel to complete a program quickly.

What is Process in Linux?

A program in a running state is called a process in Linux. The process control board (PCB) has all the information related to a program. The Process control board contains the process ID (PID), process state, program counters, CPU registers, page table info, and device information about the process.

The Linux operating system is a multiprocessing utility that simultaneously creates, starts, and terminates multiple processes. The management of processes helps the users to control the system resource usage, such as terminating a program using more system resources or an unresponsive program.

What is Thread in Linux?

A thread is a unit of execution that executes the sequence of instructions as a lightweight process. A process is executed with a single thread or several threads together. Multiple threads perform a process simultaneously that increases the speed and improves the system’s efficiency.

The threads can be of two types: Kernel-level and user-level threads. The Kernal level threads control the entire operating system resources, while the user-level resources manage a single program and are controlled by a specific user. The user-level threads are stored in user space, while the Kernel level threads are stored in Kernel space.

The user-level threads can not access the Kernel-level threads, and similarly, user-level threads are limited to user space only. A thread can have three states:

  1. Running – It means the thread is currently in progress.
  2. Ready – The thread can perform a task when given instruction.
  3. Blocked – This thread can not work within specific processes.

Differences Between Process and Thread

The threads act as the processes running inside a specific process. The process and threads have the following differences:

Process Thread
A process is a program under execution which is an active program. A thread is a sub-part of the process.
Linux Kernel treats every process differently. All users level threads are treated as a single task.
System calls are involved in processes. No system calls are involved in threads.
Context switching time is more. Context switching is fast and takes less time.
Every process has different copies of executable files and data structures. Multiple threads inside the process share the same data structure, executable files, and code.
A process requires more system resources to run a program. A thread uses a portion of process resources.
The process consumes more time for creation and termination. A thread takes very less time to create and terminate.
Linux processes don’t share the same physical memory address space, disks, and system resources. Multiple threads can share the same system resources.
If a process gets blocked, other processes continue their execution. If a process is blocked, all threads inside that process are blocked.
Individual processes work independently. Threads work as child processes to a process, so they depend on each other

How Process and Thread are Related in Linux?

The thread is a lightweight process within a particular process, also known as LWP (lightweight process). A process and threads within that process point to the same data structure that stores information about the threads and process.

While context switching, every process context switch does not refer to its parent process, but all threads when context switching refer to the same address space for the parent process.

How to Manage Processes and Threads in Linux?

The Linux operating system has several built-in commands to manage, create and kill a process. The commands that help us to find the processes related to a program or process are ps, top, htop, and kill commands.

Using the ps Command

We can find the currently running processes under the current shell with the ps command:

$ ps

Currently Running Processes

The above command shows three processes that are currently running. It displays certain details about the processes, such as PID (Unique identification number for every process), TTY (Type of the terminal), TIME (Time for which this system is running), and CMD (The related command that started this process).

We can find processes with the “u” username option to find the processes for a given user:

$ ps -u ubuntu

Processes for a specific user

The output details the processes associated with the user name “ubuntu“.

Using the top Command

To get the detail about separate processes in an interactive way, use the top command:

$ top

Top command for processes

The output shows currently active processes in the system. We can manage the processes with the top command, like sorting according to %CPU, %MEM, and TMIE+ or killing it through the top command interface.

Using the htop Command

The htop is an advanced version of the top command that provides a colorful interactive interface to display the processes in a specific manner. The htop can manage the processes like killing and sorting directly from the interface:

$ htop

htop command

The output shows the details of the processes colorfully and sorts the processes by clicking on that property header. For instance, to sort the process according to %MEM, click on the %MEM header as highlighted above.

We can find the threads created by a process with the “T” option of the ps command:

$ ps -T -p 1782

Threads for specific Process ID

The output displays the list of threads within a specific process ID “1782“.

Alternatively, we can find the threads from the “H” option of the top command:

$ top -H -p 1782

H option of top command

The output shows the threads associated with a single process having PID “1782“.

A limit of maximum threads per process in Linux specifies the number of executions simultaneously for a process. The maximum limit of the processes is present in /proc/sys file, which can be checked with this command:

$ cat /proc/sys/kernel/threads-max

Maximum number of threads for a process

The output shows that a process can simultaneously handle a maximum of “14980” threads.

Advantages and Disadvantages of Using Processes

There are several advantages of using processes in Linux:

  • Processes allow users to run several programs simultaneously.
  • Processes can be managed by terminating or suspending, which helps control the execution of the program.
  • Processes can be scheduled to use the hardware and system resources at pre-defined times.

The process can have a few disadvantages:

  • The processes use memory and CPU resources that can increase the system’s load, decreasing the system’s efficiency.
  • Debugging the processes is complex as hundreds of processes are running simultaneously.
  • Communication between processes is difficult as it requires sockets or another communication mechanism.

Advantages and Disadvantages of Using Threads

There are several advantages to threads, such as:

  • Multiple threads run several programs concurrently that execute the programs faster.
  • Threads share the same resources as the parent process, which frees the system resources.
  • A process can have several child processes that keep the program executing when other threads are executed.

Threads have several advantages, but they also have a few disadvantages:

  • Managing the threads is a complex task.
  • A large number of threads can create impact the system’s performance.

How to Use Processes and Threads in Linux?

Different commands can be used to manage the process in Linux. A process is created when a program or command is initiated, and to manage the process in Linux, several commands are used, which are discussed above.

We can use the process ID (PID) to kill a process. For instance, to kill the process with PID “1611“, run this command:

$ kill 1611

kill a process

Similarly, a process can be run in the foreground or the background. When a normal process is initiated, it runs in the foreground while the “&” sign after the command executes the process in the background.

To initiate a process by running the “ls” command, use:

$ ls

Start a process with ls command

To initiate a process of “ls”  in the background, use the “&”:

$ ls &

Start a process in background

The above command will keep running the program in the background until the process is killed or terminated.

Conclusion

This article gives a basic understanding of the processes and threads in Linux, with differences and similarities between them. You can create, terminate and manage the processes and threads by utilizing the methods mentioned in this article.

Moreover, you can check the running processes with sorting and managing for a particular program. The methods to run the program in the foreground and background are also explained.

Leave a Reply

Your email address will not be published. Required fields are marked *