Difference Between Process and Thread in Operating System
There is the biggest misconception over the difference between process and thread in the operating system.
A few days back, I had a good discussion with one of the CSEStack portal candidates on the topic “process vs thread”.
Some of the legitimate questions from a conversation…
What is the purpose of the thread?
Many think it as the same entity. But it is not. Then, how is thread different from the process?
And here I am elaborating the points from our discussion.
It is good to get the understanding of process and thread before finding the difference between process and thread.
What is the Process in OS?
Applications run on the computer. The running or execution instance of an application is called as the process. It is also considered as an execution unit.
What is the Thread in OS?
The process may contain multiple threads.
Suppose you have notepad application installed on your system. You click on notepad icon to note down some points. The process starts running. Then you open another instance of notepad to save other data.
The two threads are created to run two instances of the notepad. You can work on any of the opened notepad windows independently.
What is Multithreading?
Multithreading is the ability of the program to manage and execute multiple requests at the same time.
Multiple threads are created in the single process.
If there is no multi-threading we can not create multiple threads to run multiple instances of notepad application. There is only one notepad window will work.
Resources are allocated to the process. In a case of multi-threading, resources are shared among the multiple threads of the single process.
Every thread has its own allocated registers and stack for execution.
All threads in a process shares same code, data and resource files. So you have to program multithreading programs carefully. It prevents from anything bad happening.
This precaution is taken by synchronisation mechanism. As per the synchronisation mechanism, if any of the thread is running in the critical section, no other threads should be executed in the critical section.
Difference Between Process and Thread
Let us see some points that help us to understand the difference between process and thread.
- The thread is also called as a lightweight process. It requires fewer resources as compare to process to run.
- One single process can consist of multiple threads.
- Every process requires its own address space to run on a processor. Whereas threads within a single process share the single address space. So threads are easier to create than process.
- Threads read and write at the same place. It is good and easy for the communication between multiple threads in the single process.
- The data communications between multiple processes are difficult and it is carried out by IPC (inter-process communication).
- Context switching overhead is less in the thread as compare to process. So threads decrease overall execution time and the cost of the communication.
- Threads are useful to execute lightweight task whereas processes are responsible for running heavyweight tasks.
What is the real life Purpose of Thread?
To get high-level understanding take an instance of the client-server architecture. Taking real time example reveals many points on the difference between process and thread.
Threads are highly useful in client-server architecture. There are the huge numbers of client visits server through the website. The server gets request and processes it. Then it sends the result back to the client.
Now if we create the process to run every client request, the server will go out of resource within a moment. It will also add excessive processing.
So it is not possible to create the process for each request.
Rather, creates the thread for each request.
A server has the same program to execute any client request. So instead of replicating address code by creating a different process, threads are created for each request. Here, threads share the same address space.
Note: Don’t get confused with the term task and process. Some operating system uses the term as “task” for the process.
Hope this article in the category of difference between clears your questioning. Any of the point you want to elaborate or want to understand in a better way in the perspective of the difference between process and thread, write in the comment below.