Differences between a thread and a process

In this article, we will try to clarify the differences between an operating system process and a thread. But before jumping into the main disparities, we will first define what is the role of each of these operating systems’ important entities.

What is an operating system process?

A process is simply a program that is being executed. When the programmer writes and runs a program, that program becomes a process. It performs the tasks according to the program’s instructions.

In operating systems, a process is a single executable module, which runs concurrently with other executable modules. For example, in a multi-threaded environment (eg Unix) a word processor, a browser and a database system are separate processes that can operate simultaneously.

Read: Processes in Linux – Guide for beginners

When a process is loaded into memory, it further splits into four memory segments : stack, heap, text and data. The stacks store temporary data such as function parameters and local variables. The heap dynamically allocates memory to the process at runtime. The text section contains the contents of the processor registers and the value of the program counter and finally, the data section contains the static and global variables.

A computer process can be in different states. They are as follows:

Image source: Medium

New – The process is created in the new state

Ready – The process is waiting to be assigned to the processor so it can run. A process can enter this state after the New state. If a scheduler allocates CPU to a process while another process is already running, the already running process goes into the ready state.

Running – In the running state, the process is assigned to a processor in order to execute its assigned instructions.

Waiting – The waiting state indicates that the process is waiting for an event. For instance : Waiting for a file to become available or for the I/O to complete.

Exit – The exit status indicates that the process has completed its execution. It is now possible to remove it from main memory.

In addition, an operating system maintains a process control block (PCB) for each process. It contains information about the process, e.g. B. Process ID (PID), program counters, CPU registers, CPU scheduling information, memory management information and I/O status information. PCB will be deleted when the process ends.

Read: The structure of Linux operating system

What is a Thread?

A Thread is the execution flow in which a process divides itself into two or more tasks, which can be executed simultaneously. Indeed, it is not very effective to create processes for each task as it requires more resources. Therefore, a process is divided into several sub-processes, and each sub-process performs a sub-task. This sub-process is a single unit in the process and is called a thread. Mainly, threads provide parallelism within a process. Indeed, splitting a process into multiple threads and having these threads run in parallel is called multithreading. Threads, therefore, improve application performance through parallelism.

Image source: Particleincell

Threads can also use the same memory space owned by a given process since in a single program, all threads are logically contained in a process. Systems that support a single thread are called mono-threaded, while systems that support multiple threads are called multi-threaded. Multithreading has some disadvantages since multiple threads do not create complexity, but the interaction between them does.

Threads are usually divided into two categories:

  • ULT (User Level Thread)
    – They are scheduled by the programmer. The operating system in this type of threads does not schedule, as it usually does, not even know that they exist . In this mode, the programmer is responsible for creating, executing, scheduling and destroying the thread.
  • KLT (Kernel Level Thread)
    They are scaled directly by the operating system, and are slower than ULTs because at each call they need to query the system, thus requiring the total context switch of the processor, memory and other involved resources.
  • Any application can undergo a multithreading operation. All of the application threads are handled within a single process.
  • Each process context is maintained by the Kernel. The Kernel creates, schedules and manages the thread in Kernel space.

Threads include only the calculation status and not the resource allocation and communication status which reduces the switching effort. It improves concurrency which in turn, increases speed.

A thread must have the priority property when multiple threads are active. The time to execute a given thread in the same process is specified by the thread’s priority.

Read: Linux directories explained

Properties of a thread:

  • Only one system call can create multiple threads (lightweight process).
  • Threads share data and information.
  • Threads share instruction, global, and heap regions, but have their own stack and registers.
  • Thread management uses no or fewer system calls because communication between threads can be achieved using shared memory.
  • The isolation property of the process increases the overhead in terms of resource consumption
  • A thread is a unit of execution and contains a program counter, stack and register.

Key Differences Between Process and Thread

  • All threads of a program are logically contained in a process.
  • A process is an isolated unit of execution while thread is non-isolated and shares memory.
  • A thread cannot have an individual existence since it is tied to a process. A process however, can exist independently.
  • At the time a thread expires, its stack could be restored since each thread has its own stack. On the other hand, if a process dies, all threads including the process die.
  • A process is a running computer program instance. A thread is a component of a process, which is the smallest unit of execution.
  • A process is heavy while a thread is lightweight (performance difference Between Thread and Process).
  • Process switching requires interaction with the operating system. In contrast, thread switching now requires interaction with the operating system.
  • Each process has its own memory space. The memory cannot be shared between different processes. The process memory is used by its own threads. Therefore, threads share memory with other threads of the same process (memory related difference between Process and Thread).
  • A process requires more resources. A thread requires minimal resources.
  • It is difficult to create a process. It’s easier to create a thread.
  • Communication between processes is slow because each process has a different memory address. In contrast, communication between threads is fast because the threads share the same memory address of the process they belong to.
  • In a multi-processing environment, each process runs independently. However, a thread can read, write, or modify another thread’s data.

Conclusion

Processes are used to achieve sequential and concurrent execution of programs. While a thread is a unit of program execution that uses the environment of the process, when many threads use the environment of the same process to share code, data, and resources. The operating system uses this fact to reduce overhead and improve computation.

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply