Process & Thread: A detailed explanation

Karkiayush
5 min readJan 2, 2024

--

Fig: Process vs Thread

Hey folks 👋, here I am with the new article about one of the core concept of operating system and computer science in general where I’ll be talking about Process, Threads, Multithreading, Parallelism, Scheduler and many more.

Let us suppose a real life example, where bunch of people(employees) are working in a software dev company. Here in the company each and every employees are provided with some specific resources and facilities to one of them. For example:

  1. Employee A, is provided with MacBook which he/she need for iOS development.
  2. Employee B, is provided with android phone which he/she need to test the android apps developed by B.

Thus each of the employees are provided with some resource which only they can use. Also there may be some resources that all of the employee have to share like: company server where all of the employee back up their data.

Now when the employees are provided with some task to do by the company. All the employee does the task with the use of private and shared resources and finally generate a product that can be delivered back to the company.

Thus in this scenario, Employee is the smallest unit in this company that can contribute in the company. So it makes the company a process where contribution of thread(i.e. employee) is vital.

Based on this concept we can define Process & Thread as follows:

i. Process
Process is an independent program which run in its own memory space and has its own resources. Breaking it down, we can say it is the running program that consist of the program code, data and the current activity. It’s an isolated program that means the process don’t interfere each other.

ii. Thread
Thread is the smallest unit of execution within a process. Every process mandatorily have a main thread which run program sequentially one after another. Process can have multiple thread which can share resources such as memory space but run independently and can communicate in way more easily than process as they share the same memory space than comparison to the communication between the process. One of the major pros of thread is that inside a process, threads can run concurrently and OS scheduler can decided which thread need to be executed. Threads can also run in parallel based on the language and system.

But one of the things to remember is that there are also things in the thread that are not shared like the stack or registers where each of the thread have their own stack and registers whereas the sections like heap memory and code section is shared among all.

Fig: Demonstration of dedicated and shared resource of Threads

Lets compare Thread & Process one on one

  1. Isolation:
    Processes are isolated from each other and have their own memory space. Threads share the same memory space within a process.
  2. Communication:
    Processes may communicate with each other using inter-process communication (IPC) mechanisms. Threads can communicate more easily, as they share the same memory space.
  3. Overhead:
    Processes have more overhead due to separate memory spaces.
    Threads have less overhead as they share resources.

Knowing up to this, it may have already lead question to our mind that when a process have multiple thread, it should be capable to execute several tasks at the same time or in parallel.

Before answering this question lets know about CPU core and parallelism. As we have been hearing things like Quadcore, OctaCore etc overtime. It actually means the the processor can run the same number of process as the number of core it have in parallel theoretically.

Fig: CPU Core and their use in parallelism

But in our programming we can see we can run as many threads as we want like we can run 50 threads in a process. But the actual thing is when the number of thread that we create in a process exceeds the number of core that our CPU have, then in such case our processor will trap in a illusion of parallelism by slicing the time associated with every task in multiple tiny pieces like in millisecond such that it makes us feel that our threads are being executed in parallel but in reality there are only that much number of threads equal to number of core, running actively at any point of time.

In the above paragraph, we said the term theoretically which means the parallelism running is only a hypothetical statement. Lets say, we have 2 threads running in parallel each in its own CPU core, but there comes one point of time both of them need to access same resources like file or anything. At that point of time one of the thread need to be idle leaving another thread to use it and waiting for it’s turn to use it. In this condition we move from the parallel thread to concurrent thread. Therefore to minimize these condition of making thread to sit idle, there comes the use of CPU Scheduler.

CPU Scheduler is a part of OS which carries out the scheduling activity, keeping all the computer resources busy, ensures that the distribution of process is efficient among the process.

Fig: CPU Scheduler task

Thus in a nutshell, we can conclude the concept of process, thread,

Thread: It is the smallest possible unit of execution that lies inside of the process and can be in groups inside of a process and can be managed individually by Scheduler.

Process: It is the program having its individual existence and runs inside the isolated space. It is the instance of the computer program that is executed by one or many threads.

--

--

Karkiayush

Undegraduate IT Engineering student from Nepal 🇳🇵