Essential Linux Concepts for DevOps - Part 11- Threads
A Thread is an active entity that executes a part of a process. It is a sequential flow of tasks within a process. It is also called a lightweight process as they share common resources.
A process can contain multiple threads. Threads are used to increase the performance of the applications. Each thread has its own program counter, stack, and set of registers. But the threads of a single process might share the same code and data/file.
Why Multithreading?
As Thread is a lightweight process, The idea is to achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc.
Multithreading is a technique used in operating systems to improve the performance and responsiveness of computer systems. Multithreading allows multiple threads (i.e., lightweight processes) to share the same resources of a single process, such as the CPU, memory, and I/O devices.
Process vs Thread:
The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces. Threads are not independent of one another like processes are, and as a result, threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like a process, a thread has its own program counter (PC), register set, and stack space.
Advantages of Thread over Process
Responsiveness: If the process is divided into multiple threads, if one thread completes its execution, then its output can be immediately returned.
Faster context switch: Context switch time between threads is lower compared to the process context switch. Process context switching requires more overhead from the CPU.
Effective utilization of multiprocessor system: If we have multiple threads in a single process, then we can schedule multiple threads on multiple processors. This will make process execution faster.
Resource sharing: Resources like code, data, and files can be shared among all threads within a process. Note: stack and registers can’t be shared among the threads. Each thread has its own stack and registers.
Communication: Communication between multiple threads is easier, as the threads share a common address space. while in the process we have to follow some specific communication techniques for communication between the two processes.
Enhanced throughput of the system: If a process is divided into multiple threads, and each thread function is considered as one job, then the number of jobs completed per unit of time is increased, thus increasing the throughput of the system.
In Linux, you can check the processes running in the system using ps command

The threads in a process by using the ps command with the -L option.

To check the stack and registers of a thread in Linux, you can use the gdb debugger.


In the bt command output, the 0x00005555555552a8 value is the address of the instruction pointer at the time of the function call.
Each frame in the backtrace represents a function call in the call stack. When a function is called, the CPU pushes the address of the next instruction to be executed onto the call stack. This address is known as the return address.
The 0x00005555555552a8 the value you see in the backtrace is the return address for frame #1, which corresponds to the main function. This value is displayed in hexadecimal format and represents the memory address of the instruction that will be executed after the main function returns.
Note that the exact value of the return address will depend on the architecture and operating system on which the program is running.