Concurrency is not Parallelism - A deeper look at their key difference
Tags: concurrency, parallelism, os
Published: 2022-03-07 03:02:27

Today, in this article, we'll analyze two common processing techniques used by the operating system (OS). **Concurrency** & **Parallelism**. When we have more than one process waiting to be executed, these 2 concepts come into the picture. Before we jump into the brief explanation about each, let's have a quick look at how CPU works underneath the hood.

Generally, the processor cores are meant to process 1 instruction at a time. So let’s say, you’re playing a video somewhere on a single-core machine. Does it mean you will not get any notification from your mail app that is running in the background? No, your background processes are doing their job nicely. But why is that? Didn’t I tell you that a single core can run one instruction at a time? How is it possible? Ah well, that’s the main job of an Operating System (OS). This job is known as [Context Switching](https://en.wikipedia.org/wiki/Context_switch). All these processes happening not necessarily at the same time but in a scheduled manner. Operating system schedules processes for execution. Let’s say for simplicity's sake, we have 3 processes happening.

![Round-Robin scheduling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mmeue8uj6ygczy037ii.png)
 

If the [Scheduler](https://en.wikipedia.org/wiki/Scheduling_%28computing%29) schedules the processes like the figure above, the CPU will execute **Process 1.** Then after a certain amount of time, move to **Process 2**, then **3**, then again **1**. You get the idea. So when OS moves from one process to another, that act of switching is called [Context Switching](https://en.wikipedia.org/wiki/Context_switch).


![OS context switching](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dp14xivrve324ahbsrcf.png)
 

In the figure above, we see Process A and B, two processes are scheduled. CPU processing **Process A**. While it’s not finished, OS  stopped that task and make the CPU process: **Process B**. Whether or not **Process B** is finished, it moved back to **Process A** again. That’s the operating system, doing its main job. But all these activities happening so fast, from the user's perspective, it feels at the same time. The user has the impression of parallelism. It gives the illusion of parallel execution even though it’s not.

## Concurrent

So first thing first, when we talk about concurrency, concurrent execution is not necessarily executing tasks at the same time. Like the discussion above with a single-core CPU. When we have 2 tasks scheduled, at a given point in time we can say that, either task 1, or task 2 getting executed. Not both. OS scheduler & context-switcher decides how the CPU will finish both tasks.


![At a given point in time, only one task gets processed.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agnqhi3effspynglx500.png)
 

From the figure above, we can see that, while Task 1 is not finished, Task 2 is getting processed. That's the key difference with Parallel execution which we will talk about in a sec. Yes, Task 2 is running while Task 1 is not finished yet, but at that time, Task 1 stopped. The state (code, memory, registry, unique values, etc) of Task 1 is frozen and saved somewhere by Context Switcher for later use when it will go back to Task 1.

> *Concurrency means executing multiple tasks at the same time but not necessarily simultaneously.*
> 

## Parallel

The key point of how parallel is different from concurrent is: **for Parallel, we need different hardware**. It can be a different core or an entirely different machine. From above, if we bring the same example for parallel we can see the execution like the figure below:


![At a given point in time, multiple tasks get processed.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/265t3yvvamequyopoi2x.png)


While Task 1 is getting processed, at a certain point Task 2 started **without** Task 1 getting stopped. They both running at the same time. Even, parallelism does not require two tasks to exist. It literally physically run parts of tasks or, multiple tasks, at the same time using the multi-core infrastructure of CPU, by assigning one core to each task or sub-task.

> *Parallelism is a specific kind of concurrency where tasks are really executed simultaneously.*
> 

---

## Summary

Even though concurrent & parallel both will finish the tasks significantly faster, when it goes to parallel, it will finish a bit faster than concurrent (depending on amount & types of tasks).

But now you might think, why concurrent then? While it's not running multiple tasks at the same time. Isn't it better if we run tasks just sequentially and forget about concurrency? Well, there are some benefits to concurrency. I will talk about those some other time.

So for today, that's all about Concurrency & Parallelism, a very important concept. Hope you will get it simpler now when either of these is in the conversation.

Happy Learning !!!