Multithreading and Multiprocessing

In this lecture, we will see Multithreading and Multiprocessing in detail with its comparisons. let’s start with multi-threading,

What is Multi-threading?

Execution of multiple threads of a single process simultaneously with the help of single or multiple CPUs is called Multi-threading.

If we have more than one CPU, then the threads of a single process will execute on all the CPUs. So, at a time, there may be only one process will be in the execution state, whether there is a single CPU or Multiple CPUs. Because of multiple threads of a single Process.  

Context switching also occurs in threading, When the CPU moves from one thread to another. As shown in the diagram given below,

Example 01:

As we know, a process may be made up of multiple threads of execution that execute instructions in parallel.

To understand the concept of multi-threading, let us take an example of a MS Word Process. A word process performs the following task at a time

  • Open MS Word
  • Writing Document
  • spelling checker
  • grammar checking

You do not have to open different word processors to do this concurrently (parallel). It is possible in a single word processor because of multiple threads. Explain with the diagram given under,

 Example 02:

In multi-threading, all open Tabs of chrome will work perfectly because chrome is one process, and all opened tabs are threads of that Process. Downloading, listing audio, and email are the some threads of Chrome, which works parallel due to multi-threading.

Note: Process can be viewed from task manager. If chrome is one process, then multiple chromes icons, looks in task manager, are its threads.

Advantages

  • increased responsiveness
  • Threads belonging to the same process share the belongings (Code, DATA, and File) of that process.
  • Less time in context switching
  • Parallel occurrence of tasks.
  • Decreased system cost and maintenance.

Disadvantages

  • Increased complexity.
  • Complex debugging and testing processes.
  • The result is sometimes unpredictable.
  • Increased difficulty in writing a program.

What is Multiprocessing?

So, two or more CPUs present in the same computer and sharing the system resources like bus, memory, and other I/O devices is called a Multiprocessing System.

Multiprocessing is further categorized into symmetric multiprocessing and asymmetric multiprocessing.

In symmetric multiprocessing, all CPUs are free to run any process in a system.

But in Asymmetric multiprocessing, there is a master-slave relationship among the CPUs. The master CPU is responsible for allotting the process to slave CPUs.

Advantages of the multiprocessing

  • Maximum Throughput: By increasing the number of CPUs, more work can be done in a unit of time.
  • Reliability Increased: As workload is distributed among several CPUs.
  • Share Resources: The CPU shares the memory, buses, peripherals, etc.

Disadvantages of Multiprocessing

  • As more than CPUs are working at a time. So, the coordination between these is very complex.
  • Buses, memory, and I/O devices are shared for all CPUs. So, if any CPU is using some resource like buses. Then, another processor has to wait for its turn. As a result, throughput will decrease.
  • Highly costly because the CPU is costly Hardware.

The basic difference between Multithreading and Multiprocessing is given under,

Multi-threading Multiprocessing
Threads of a process can execute on single or multiple CPUs. multiple CPUs are required for execution.

Advance Learning Related to this topic.

To get better results, Multiprocessing Can also be applied to Multi-programming, Multitasking, and multithreading. Let’s explain every term one by one.

Multiprocessing-Programming:

Multiprocessing-Multitasking:

Multiprocessing Multithreading:

Multitasking Vs. Multithreading

Multitasking Multithreading
More than one process gets executed simultaneously. More than one thread of a process is executed.
Multitasking  is a Time-sharing process, but the CPU switches from process to process Multi-threading is also a Time-sharing process, but the CPU switches from thread to thread in a process. After completion of all threads of that process, the CPU switches to the second process.
Heavyweight  Lightweight

Multiprocessing Vs. Multitasking

Multiprocessing Multitasking/Multi-programming
It utilizes multiple CPUs. It utilizes single CPUs.
Multiprocessing allows parallel processing. i.e.

 

In a Dual-core CPU, two processes can run parallel, and in a Quad-core CPU, four processes can run parallel.

Only a single process can run at a time. Context switching takes place to execute other processes. As only one CPU is available in the system.
Less time is taken to complete the process. More time is required to complete a process.
It is much more efficient to the utilization of system resources like hard disks and registers because of multiprocessors. it is Less efficient for utilization of system resources like Hard disks and registers because of a single processor.
Usually, it is more expensive. Usually, it is less expensive.