Multi-threaded vs Multi-process parallelism : How to choose what's RIGHT ?

Multi-threading or Multi-processing , both are the ways to achieve multi-tasking in computer world.

Process is instance of a program and threads live within the process sharing the same memory address space.

So, how we decide which one to choose?

For that, we need to understand one more thing :
=> Is it CPU bound or I/O bound program ?

CPU Bound processes :

  • Sensitive to speed of CPU, so keep it PARALLEL and use multi-processing.
  • Multi-processing means a new process is started independent from first process
  • Memory is not shared between processes

{Multi-process}If the program is CPU bound i.e the processes which are sensitive to the speed of the CPU, its good idea to keep it PARALLEL AND USE PROCESSES. That means go for Multi-Processing.
In general, programs which are implementing algorithms with a large number of computations will hold the CPU for longer time. They can be expected to hold the CPU for as long as the scheduler will allow. Users do not typically expect an immediate response from the computer when running CPU bound programs. They should be given a lower priority by the scheduler.

{Multi-thread} If the program is I/O bound, keep it CONCURRENT AND USE THREADS. I/O bound processes that are mostly waiting for the completion of input or output (I/O) are I/O Bound. Short CPU burst processes(I/O bound processes) are given higher priority. A process in lower priority queue can only execute when higher priority queues are empty.

Main idea is to get the best efficiency by keeping the CPU and I/O devices as busy as possible.

Lets try to elaborate the difference between Concurrency vs Parallelism :

Concurrency means executing multiple tasks at the same time but not necessarily simultaneously(but its possible).

Parallelism means that an application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time.

Parallelism needs the hardware with multiple processing units. In single-core CPU, you may get concurrency but NOT parallelism. Parallelism is a special kind of concurrency where tasks are really executed simultaneously.

In simpler words — Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
svalak

svalak

Passionate about problem solving; #WritesOnQuora #VoraciousReader #MBTIEnthusiast #LovePsychology https://www.quora.com/profile/Shaila-Hegde-2