Process Vs Thread is one of the most widely asked questions in the technical interview rounds. Hence, it becomes important to be familiar with the difference between process and thread as they are related to each other and are very similar and hence often confusing while mentioning. The process and threads take different spaces and have different modes of execution.
In this blog article, we will understand some of the major differences between Processes and threads. We will also know some of the fundamentals related to processes and threads in the operating system.
What is a Process in an Operating System?
A Process is an independent program in execution mode. They have their own memory space, resources, and execution context with all information stored in the PCB (Process Control Block). Each process runs on a separate path and does not share memory with any other processes until specifically directed to do so.
For instance, let us understand with a real life example of a process. imagine you open a browser on your computer and the browser itself is a process that runs on your system and if you open multiple tabs, each tab might run as a separate process with their own memory space and no interference with other tabs.
Major Highlights of Process in Operating System
- A process is an instance of a program in action.
- Processes can create multiple processes to execute tasks at a time, these processes are known as child processes or clones.
- The main process is known as the parent process in the operating system.
- A process consists of multiple states such as New, Ready, Running, Waiting, and Terminated.
Process vs Thread: Working of Process
During the execution of a program, a process comes into action. After loading the program in memory in binary format, the process loads all information within its PCB i,e. Process Control Block. Important information of processes such as registers, program counters, and stacks all are loaded into the process by the operating system.
Register is used to contain instructions, storage addresses, and other important information, while a program counter holds the address of the next program in sequence. The stack contains information and a schedule of subroutines in the computer program. The program maintains multiple instances and each running instance works as an individual process.
What is a Thread in an Operating System?
A thread is the smallest unit of execution within a process, where a process can have multiple threads that share the same memory space and resources but execute independently. Thread is a part of the process and is also known as a “lightweight process”.
Suppose you open a web browser where rendering web pages or refreshing is done using “threads”. Threads are used to handle user input like clicks or typing and update it on the go. Threads are also used to download files in the background. As threads share common memory within the browser process, they can communicate efficiently and complete tasks.
Single Thread in OS
Major Highlights of Threads in Operating System
- Threads are often known as “lightweight processes” and are a subset of processes.
- Threads provide information such as data segment, files, code segment, etc, and contain their own such as stack, register, and program counter.
- All the threads within one process are interconnected with each other.
- There are single thread processes and multithreaded processes in the operating system.
- Each thread shares memory and resources within a process and is used to improve the processing of an application.
Process Vs Threads: Working of Threads
Threads is a subprocess inside a process that is used to improve the processing capacity of an application. In a process, there can be single or multiple threads working at a time. During the start of a process, the operating system assigns the memory and resources to the process, and threads within the process share the memory and resources allotted within the same process.
Threads are executed at a time but due to context switching, an illusion is made which depicts that threads are running parallelly within the process.
Process vs Thread: Memory Distribution
Memory distribution in threads and processes takes place in slightly different ways affecting their operations and communication ability with the OS. When comparing process vs thread it becomes important to compare their memory distribution.
Memory Distribution in Process
Each process has its own separate memory space which is divided into different sections. Each process has its own stack and heap. One process cannot directly access memory from another process.
- The code segment stores the executable program code.
- The data segment is used to store global and static variables with initialized and uninitialized parts of data.
- The third segment is known as the heap segment which is used for dynamic memory allocation using malloc() in C, new in Java or Python.
- The stack segment is used to store function calls, local variables, and return addresses of the thread execution details.
Memory Distribution in Threads
Threads share the same memory space within a process and hence do not have a separate memory segment but do have their own stack.
- The code segment in a thread is shared across all threads within a process.
- The data segments in threads are shared across all threads. Any changes in one thread are visible to other threads.
- Threads can modify heap memory which affects other threads and processes. It is shared for dynamic memory allocation.
- Each thread has its own private stack to store function calls, local variables, and return addresses from function calls.
- Threads share code, data, and heap, which makes it lightweight and efficient.
- Each thread maintains its own stack which prevents local variables from interfering with other threads.
Process Vs Thread: Degree of Independence
When comparing process vs thread based on the degree of independence, we might find some significant differences in both these entities of the operating system.
- Each process runs independently with its own memory space, resource, and execution context. A process does not depend on another to function correctly.
- Processes have separate memory and one process cannot hinder another process’s memory directly.
- For example, when you are running Google Chrome and MS Word, then both these processes are independent and do not interfere with each other.
- Threads are dependent entities and they belong to the parent process and share their memory space being the subpart of a process entity. If the main process terminates, all its threads terminate as well.
- If one thread fails, it might cause the entire process to crash. For example, if one browser tab crashes in Google Chrome, it might affect the other tabs already opened and running in the browser.
Process Vs Thread: Difference Table
Let us compare process vs thread in the difference table below.
Process | Thread |
A process is an instance of a running program that is being executed. | A thread is a subpart of a process that is used to maintain the processing capacity of a process in an application. |
New, Ready, Running, Waiting, and Terminated are some of the important states in a process. | User threads and kernel threads are important types of threads in an operating system. |
We use more than one system call to produce more than one process in a memory. | We can create more than one thread with one system call easily. |
Processes stay independent from each other within an operating system and do not share memory or other resources. | Threads are dependent on each other and often share memory and resources. |
If one process gets blocked another process can run and execute independently. | In the case of user level threads if a thread gets blocked by OS then other threads are also blocked from running. |
Context switching is slow in the process when compared to threads. | Threads generally provide faster context switching as compared to processes. |
A process requires inter-process communication (IPC) like sockets, message queues, pipes, etc. | Threads can directly communicate as they share memory and resources. |
It is more time consuming and resource intensive to create. | It is lightweight and faster to create compared to processes. |
A system call is always used in the process. | No system call exists within a thread. |
Learn DevOps and Cloud Computing with PW Skills
Become a DevOps pro engineer with PW Skills DevOps and Cloud Computing Course. Learn advanced devOps tools like Ansible, Kubernetes, Jenkins, Terraform, Dockers, etc.
Work on real world projects and get in-depth tutorials, practice exercises, assignments, and more under the supervision of expert mentors. Know more about our DevOps course only at pwskills.com.
Process vs Thread FAQs
Q1. What is a Process?
Ans: A Process is an independent program in execution mode. They have their own memory space, resources and execution context with all information stored in PCB (Process Control Block).
Q2. What is a Thread?
Ans: A thread is the smallest unit of execution within a process where a process can have multiple threads that share the same memory space and resources but execute independently.
Q3. What is a major difference between Process vs thread?
Ans: Process is an instance of a program in execution while thread is a subpart of process while threads exist within the process. Threads are not independent and share memory while processes exist separately.
Q4. Are process independent entities?
Ans: Each process runs independently with its own memory space, resource, and execution context. A process does not depend on another to function correctly.