What is Multi-Threading, Multi-Processing and how do they work.

What is Multi-Threading, Multi-Processing and how do they work.

ยท

5 min read

What is a process?. What is a thread?. What is even a CPU?.

These are the kinds of questions regular people ask when they're just getting to know their computer from the inside, or software developers when they're dealing with some lower level programming languages.

As a developer, having an idea of how your code gets chopped into binaries, how it's fed to the memory, in fact having an understanding of how computer works will make you stand out as an engineer.

In this article, we will be learning what multi-threading and multi-processing are and what makes them possible. Read on.

What is a process:

In the operating system, a process is basically an instance of a program in execution. It is an actively running program like your MS Word application when launched or opened. When a program is run, it becomes a process in the Operating System and each process has its own address space which contains code, data, stack and heap segments or sections, e.t.c

What is a thread:

Now a thread is a segment of a process that can run independently. A process can have different threads, but since they're all parts of the parent process, they all share the same address space with it. In typical programming, this thread can just be a function which has access to global variables.

What is multi processing:

Now you may wondering how you're able to run different programs (listening to your favorite music while browsing the internet) at the same time, even on a CPU with single core.

Now here's where multi-processing comes in. It is the ability of a computer to run different processes (active programs) at the same time. Multi-processing is achieved either by concurrency or parallelism depending on the number of CPUs or cores (single or multi-core) available in the computer.

What is concurrency:

This is when the computer is running two or more processes or threads in an overlapping time (not at same instant) by periodically switching execution between the two processes or threads.

This strategy is used when the CPU is single-core, so it seems different programs are running at the same time whereas they're just taking turns and due to the amazing speed of the CPU, you won't notice what's happening underneath.

What is parallelism:

With advances in technology, CPU manufacturers have built CPUs with multi-cores which the Operating System sees as multi CPUs, and you can now even combine more than one actual processors in a single computer.

With this technology, the computer can now achieve true multi-processing by running two or more processes or threads at the same instant (not taking turns) by sharing these processes or threads to it's different cores which can handle different tasks simultaneously. This strategy is called parallelism.

Summary:

The computer does it's best to serve you even when its not equipped with full fledged technologies like a multi-core CPU. So when the CPU is single core, it uses concurrency to do it's job, and when the CPU is multi-core it uses parallelism to keep the fun going.

I hope you learnt something from this article, please drop your critics, opinions, corrections, and your views below. And don't forget to share with friends. Thanks for reading...๐Ÿฅ•๐Ÿ˜‹