DEV Community

Cover image for Concurrency is not the parallelism
Omkar Ajnadkar
Omkar Ajnadkar

Posted on • Originally published at omkar-ajnadkar.github.io

Concurrency is not the parallelism

This post is inspired by two things:

  1. 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 for some other interesting posts...

Top comments (7)

Collapse
 
drbearhands profile image
DrBearhands

So I'm going to be rather negative here, because I think I should be.

The first line on wikipedia:

In computer science, concurrency refers to the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or in partial order, without affecting the final outcome.

vs. your post:

Concurrency: Fooling you and running processes alternatively

There is quite a difference.

And parallelism does not require multiple cores. That's just one way of doing things.

Although we have all made mistakes and said something inaccurate at some point in time, for posts, which take time to make anyway, you should really make a minimal effort to check your assumptions. Otherwise, you spread misinformation and confusion among your readers, essentially making them slightly worse devs after reading this.


  • Concurrent: not (completely) ordered; at least some parts can run simultaneously.

  • Parallel: parts do run simultaneously.

  • Interleaved: 'mixed' but not run at the same time (situation 2 from the post).

So concurrent code can be executed in parallel, interleaved or entirely sequentially.

In software development you don't generally have full control over how your program will run exactly; you can give hints but your OS will make decisions based on e.g. the current load.
That means that generally we talk about concurrent software and parallel hardware, but that is not part of the distinction.

There is quite a variety in how things can be parallel (multi-cores, hardware threads, GPU, VLIW, FPGA...), you can even sort-of run sequential code in parallel (pipelines), which we do all the damn time.

Collapse
 
blackbird profile image
Omkar Ajnadkar

Hey,
Thanks for your suggestions. Although I am no expert in this topic, what I am trying to explain here is the difference between parallelism and concurrency. This is for those who think that parallelism is equivalent to concurrency, which is not and that's what I am trying to explain here.
In the line,

Concurrency: Fooling you and running processes alternatively

I am just trying to explain that concurrency does not mean processor is running more than one process simultaneously but user is thinking that more than one process is running.

Collapse
 
drbearhands profile image
DrBearhands • Edited

I'm sorry, but your intention is irrelevant in this. Whatever it is you intended to do, what you have achieved is misinforming people.

You should edit your post to correct this for future readers (or delete it if you do not feel up to editing it).

Collapse
 
tchupp profile image
Theo Chupp

This has a similar title to one of my favorite talks, by Rob Pike.
blog.golang.org/concurrency-is-not...

Collapse
 
bgadrian profile image
Adrian B.G.

I was sure that is a Go post, it is a common phrase in Gos community.

Collapse
 
trueneu profile image
Pavel Gurkov

Please don't misinform other people.

Collapse
 
smailg profile image
SmailG

I guess if you talk about this threads should also be spoken about, as well as multiple cores dealing with the same process eg. games