Concurrency is not the parallelism
Tags: computerscience, discuss, codenewbie
Published: 2018-11-24 18:51:09

This post is inspired by two things:
1. {% link https://dev.to/rhymes/how-to-make-python-code-concurrent-with-3-lines-of-code-2fpe %}
2. Recently completed __Operating System__ classes

Although you can find complete book chapters on this topic, I will keep this short and simple.

## Situation 1

This is the situation in say 80-90's when personal computer was a new thing. At that time, computers were very simple. So what was inside them? They used to have only one processor. So what does that mean? That means your computer can run only one process at a time. Good !!! That means if computer is running a process and now it wants to run another process, it has to close that one to start it. So strange...🤨

## Situation 2

Now how to solve problem in situation 1? Computer has only one processor, so there is no way that it can run more than 1 process at a time. OK, so computer decides to fool the people! How? 😋

Here comes the concept of __concurrency__...There will be multiple processes in the background. Processor will give time to each alternatively so that you(and I) think that every process is running. Little confused? Let's assume there are 2 processes P1 and P2. Both the processes want to start at same time. CPU will give first T time to P1 and after that T time to P2, again T to P1 and this goes on...

Now you are thinking, but can't see the one process stopping and another running. The reason is that this T is very very small(in the order of 10^-9 seconds), so we think that every process is running. 
This concept is still valid and there are various algorithms in which processor selects the process to which it wants to give time to. One of the most famous and easy one is __Round Robin Algorithm__ in which processor gives time to each process in sequence until last and then again comes back to first process.

## Situation 3

Now you know, that processor is fooling you and it is actually does not running more than 1 process at a time. It's time for up-gradation. You went to market to buy new computer and asks what's the specification of the CPU. Now you must have heard the name __cores__ which have values form 2 to 4 to 8 to even 16. You have lot of money 🤑 and you think bigger is better(that's actually true here), so you bought 8-core CPU.

Now essentially your CPU has 8 cores instead of one you previously had. That means each processor is independent of another can do whatever it want. So each CPU can run one process at a time. So simultaneously 8 processes are running, each processor running one. This is true thing, actually more than one processes are running at a time and this is known as __parallelism__. Here each processor will also use concurrency to further optimize processes running time and you will feel that more than 8 processes are running simultaneously although only 8 are running at a time. 

It is also important to note that GPU's (Graphical Processing Units) which are used for rendering of high quality games or train deep learning models, also use parallelism which allows it to perform multiple calculations or rendering at a time.

## Conclusion

Concurrency: Fooling you and running processes alternatively
Parallelism: Actually running processes simultaneously using various cores

Feel free to discuss doubts and other concepts in comments.
Thanks for reading!!!♥ 
Here is my [blog](https://omkar-ajnadkar.github.io/blog) for some other interesting posts...