Threads vs Processes: A deep dive

Threads vs Processes: A deep dive

SystemsLinux

Fri Nov 17 2023

In addition to signals, i have also been thinking about a few other things in linux to write on. A common item that usually comes up in technical interviews or just in doing everyday work, is the difference between threads and processes. I have met some who think they are the same thing. They would be wrong. In function, they are similar. But what exactly is the difference between a thread and a process? This article aims to demystify these terms, highlighting their distinct roles in the execution of programs.

What is a Process?

A process can be thought of as an instance of a computer program that is being executed. It is a self-contained unit of execution that comprises:

  • Its own memory space: A process operates in its own isolated memory area.
  • Resources: This includes files, network connections, and system resources.
  • Process ID: A unique identifier assigned by the operating system.

Processes are independent of each other. This isolation ensures that a crash in one process does not affect others. Operating systems like Windows, Linux, and macOS manage multiple processes simultaneously, allocating them the necessary resources and CPU time.

What is a Thread?

A thread, often considered the smallest unit of execution, is a component of a process. Key characteristics include:

  • Shared Memory: Threads within the same process share the same memory space.
  • Lightweight: Threads are more lightweight in terms of the resources they need compared to processes.
  • Execution Flow: Each thread represents a separate flow of control.

Threads allow for parallelism within a single process, enabling different parts of a program to run concurrently. This is particularly useful in applications that require multitasking, such as a web browser running multiple tabs.

The Key Differences

  • Memory Space:
    • Process: Has its own independent memory space.
    • Thread: Shares memory space with other threads of the same process.
  • Communication:
    • Process: Inter-process communication (IPC) is more complex and involves mechanisms like pipes, sockets, or shared files.
    • Thread: Communication between threads is simpler since they share the same memory.
  • Creation Time and Resources:
    • Process: Creating a new process is resource-intensive and takes more time.
    • Thread: Threads are quicker and easier to create compared to processes.
  • Effect of a Crash:
    • Process: If a process crashes, it doesn’t usually affect other processes directly.
    • Thread: A crash in one thread can affect all threads within the process, as they share the same memory space.
  • Use Case:
    • Process: Suitable for applications that require isolation and security, like running a separate instance of a program.
    • Thread: Ideal for tasks that require concurrent execution within the same application, such as a server handling multiple client requests.

Viewing Processes/Threads

There are several commands you can run to view processes and/or their associated threads:

  • ps (Process Status): The ps command is used to display information about active processes. To view threads, you can use it with specific flags:This command shows all processes and their threads (also known as LWPs - Light Weight Processes). It provides details like the process ID (PID), thread ID (LWP), and the parent process ID (PPID).
bashCopy codeps -eLf
shell-session
  • top: The top command is an interactive command that shows a dynamic view of system processes, including threads. To view threads:
    • Run top
    • Press H to toggle the display of threads.
top
shell-session
  • htop: An enhanced version of top, htop provides a more user-friendly interface and additional features. To view threads in htop:
    • Run htop.
    • Press F5 (Tree view) which can help in visualizing threads grouped with their parent processes.
    • Alternatively, you can press H to toggle the display of threads on and off.
  • pstree: The pstree command shows running processes as a tree. It can be used to display threads:This command displays the process tree and includes thread names (the -l flag), shown as {thread_name}.
pstree -p -l
shell-session

Anyway, thats pretty much it. A short article that just covers the basics which is what i wanted to convey. A simple article is also a more memorable article. So keeping the length short is good. Processes and threads come up all the time with system administration and development.