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 legit questions from a conversation…
Many think it as the same entity. But it is not.
This question was also asked in job interviews for many companies like Petasense.
Here, I am elaborating the points from our discussion.
It is good to get an understanding of the process and thread before finding the difference between process and thread.
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.
The process may contain multiple threads.
Let’s take the example. It will be easy to understand.
Suppose you have notepad application installed on your system. You click on the notepad icon to note down some points. The process starts running. Then you open another instance of notepad to read from other text files.
The two threads are created to run two instances of the notepad application.
You can work on any of the opened notepad windows independently.
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.
In the above example, if there is no multi-threading, we can not create multiple threads to run multiple instances of notepad application. There will be only one notepad thread working.
This is a high-level understanding of the process and thread. Let’s get into the detail to see how the Operating System manages process and thread, internally.
To execute any process, it requires resources (hardware or software resources). Some of the most common software resources utilized by the process are code, data, files, registers, stack, etc. Resources are allocated to the process before running the process.
When one process is using resources, the same resource can not be utilized by any other process.
When the process done with using any resource, the resource gets released so that it can consumed by other process.
Suppose, a process “A” is using resource “R” and process “B” need that resource to run. The process “B” has to wait until the process “A” releases the resource “R”.
But in the 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 share the same code, data and file resources.
If you are developing any multithreading, you have to use the resources wisely.
If the threads are not handled properly to utilize the resources, it can lead to the deadlock. That’s a different topic. You can read more about the deadlock in the Operating System.
Operating System uses synchronization mechanism to avoid this issue. As per the synchronization mechanism, if any of the thread is running in the critical section, no other threads should be executed in the critical section.
Here writing data to any resource is one of the critical section.
Let us see some points that help will help you to understand the difference between process and thread.
Take an instance of the client-server application. Taking real-time examples 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 the 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 overhead.
It is not possible to create the process for each request. Rather, creating the thread for each request has many benefits.
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 clears your questioning. If you have any of the points to discuss the difference between process and thread, write in the comment below.
Process are heavy-weight whereas threads are lightweight. Threads share virtual address space and system resources. Threads are having less overhead but processes are having high overhead.
That’s true!