Difference Between Process and Thread

Learn via video courses
Topics Covered

Understanding computer processes is crucial for various computer science topics, including threads, parallel computing, and concurrency.

To grasp the difference between Process and Thread, consider preparing a store for a sale: the overall store preparation represents a process, while smaller independent tasks, like arranging shelves, represent threading.

This abstract example provides a starting point before delving into technical difference between process and thread.

What is Process?

Put simply, a process is a task that is being accomplished by the computer as instructed in the program file. While a program is only a set of instructions for the computer system, a process is an entity which is actually executing those instructions. Formally speaking:

A process is an instance of a computer program in execution state along with all the necessary information required for its proper execution, like program counter, CPU registers, its activation state (like new, running, waiting, ready), memory security and management information etc.

What is Process

For any operation to be performed on a process, it must be identified uniquely, otherwise, we won’t know which process we want to target for our purpose, for example, maybe terminating it.

Therefore, the operating system provides each process with a unique ID, which is known as the process ID.

Features of Process

Below mentioned are the important features of the processes:

  • Independent units of execution in an OS.
  • Each process has its own memory and resources.
  • Processes operate in isolation.
  • Concurrent execution allows multitasking.
  • Processes communicate through inter-process communication.
  • OS manages process scheduling and resource allocation.
  • Fault isolation prevents system crashes.
  • Creation, termination, and management of processes.
  • Enables simultaneous execution of multiple tasks.

How does a Process Work?

When a process is created, the OS loads the required portion of the program (pages or segments, depending on the operating system) into the memory and some of the values of the struct are initialized, like the ones which define the state in which the process is currently present in (like running, waiting, ready, etc).

When loaded into the memory, this struct “object” is termed as a Process Control Block (PCB) or less commonly, Task Control Block.

How does a Process Work

The PCB keeps track of resources allocated to the process, like CPU registers, IO (Input-Output) device list, virtual address space, a pointer to the stack, heap, the executable code, program counter, etc. You can also think of PCB as the way the operating system understands/visualizes the process.

The operating system in itself doesn’t need to know everything about the process, it can ask the PCB to provide whatever information it (the OS) needs.

When the PCB communicates to the OS that it is in the ready state, the OS puts the PCB of the process in its ready queue, from where it is selected by the CPU scheduler to be executed using appropriate scheduling criteria.

What is Thread?

A thread is a “lightweight” and efficient process that can be executed to perform some meaningful task. More formally:

A thread is a dispatchable unit of work within a process. It includes a processor context (which includes the program counter and stack pointer) and its own data area for a stack (to enable subroutine branching). A single thread in itself executes sequentially and is interruptible so that the processor can turn to another thread.

Feature of Thread

  • Threads are lightweight units of execution within an operating system.
  • Multiple threads can run concurrently within a single process, enabling parallelism.
  • Threads share the same memory space and resources of the process they belong to.
  • Each thread has its own program counter and stack.
  • Threads can communicate and synchronize with each other using synchronization primitives.
  • The operating system schedules threads for execution and allocates CPU time to each thread.
  • Threads allow for efficient multitasking and responsiveness.
  • Proper synchronization mechanisms must be employed to ensure thread safety and avoid race conditions.

How does a Thread Work?

How does a Thread Work

Threads operate within the context of a process and share the same memory space. Multiple threads can run concurrently within a single process, allowing for parallelism.

Each thread has its own program counter and stack, but they share the process's resources, such as memory and file handles. Threads can communicate and synchronize with each other using synchronization primitives.

The OS schedules threads for execution, allocating CPU time to each thread. Threads can execute independently, allowing for efficient multitasking and responsiveness.

Differences between Threads and Processes

We’ve seen multiple differences between threads and processes above in their respective descriptive sections. Lets us summarize these differences in a tabular form as given below:

PROCESSTHREADS
An instance of a computer program that is in execution.A dispatchable unit of work/task within a process.
Has its own code, data, and files. Independent from other processes.Shares the code, data, and files inherited from the parent process with other sibling threads, so only partly interdependent from other threads.
The addressable memory space of processes are isolated (or independent) from other processes, that is, each process has its own memory/address spaceAll the sibling threads share a common memory since they belong to the same parent process
Memory independence makes inter-process communication quite heavyInter thread communication is relatively fast because of the shared memory
Process creation and termination is a heavy CPU overheadThread creation and termination are faster and more efficient than corresponding operations in processes.
Context switch in processes (switching from one PCB to another) is a heavy operationContext switch in threads is a comparatively lighter operation
Each process is identified uniquely by the OS with its unique process IDEach thread is identified uniquely by the OS using its thread ID
A process is defined by a Process Control BlockThread is defined using a Thread Control Block
A process does not require special hardware supportMultithreading sometimes requires special hardware support (for example, in the case of explicit multithreading)
Processes have to handle scheduling and execution along with resource ownershipThreads are concerned only with scheduling and execution, not resource ownership

Conclusion

To recap, the following things were discussed in the course of this article:

  1. A process is a digital manifestation of a computer program.
  2. A process exists in the form of a PCB, which contains instruction code, program counter, and other such useful information required for successful execution of the intended task.
  3. All processes are created by some other process, thereby creating a parent-child relationship among the different processes, which is termed as a process tree.
  4. A thread is a “lightweight” and efficient process that can be executed partly independently to perform some meaningful task.
  5. All threads are owned by some process.
  6. Thread creation, switching, and termination are more efficient than the corresponding operations in processes.
  7. Difference between process and thread.