Multithreading

Let’s first understand few related terminologies before understanding multithreading.

Program

Program or computer program can be viewed as a set of instructions. We generally write programs in high level languages like C or C++. The computer processors can not execute these programs directly. We compile and link them into a binary equivalent that the processors can understand. It is also called as executable. The high level programs or the executables are still a passive set of instructions that are not getting executed. They are called programs. Programs are stored in computer disks.

Process

When a program is loaded into the memory (RAM) and the processor starts executing its instructions, it becomes a process. That’s why a process is called ‘program in execution‘ or an ‘instance of a program‘. We can create multiple instances or processes from a single program.

When a program gets instantiated, few things get associated with that.

Memory space: A portion of the virtual memory gets associated with the process. It is also referred as process space. It includes 1) the executable code and data, 2) a call stack to track the active routines and associated variables, 3) a heap to store the runtime computation data etc.

Resource handlers: Process in its lifetime can access different types of computer resources like – files, input output devices, network devices etc. The process accesses these resources via file descriptors (fd) or handles.

Processor state: Register values are important information for process execution.

Operating system holds these information of an active process in a structure called Process Control Block (PCB).

Thread

A process gets executed in the context of one or more threads. A thread is a sequence of executable instructions that can be scheduled independently by the operating system scheduler. Thread scheduling is a mechanism to decide when to assign CPU for execution and when to put that in wait or ready queue. All threads of a process share the process resources like memory space and file descriptors.

Each process will have at least one thread, aka, main thread, if we don’t create any other thread explicitly. In this case, all instructions get executed in the context of main thread.

Unlike process, threads can’t have their own independent existence. Threads exist inside a process.

Multithreading

Multithreading is about creating more than one threads in a process to do multiple tasks parallelly. Each task will be executed independently by a thread. They can be executed at the same time by multiple CPUs or CPU cores. Or one CPU can switch through multiple threads to run them parallelly. One CPU / core executes only one thread in a particular moment of time, but it will switch to another thread even before completing the current one. From the process perspective, the threads run parallelly – not one after another. The operating system scheduler does this scheduling.

Multithreading is used to parallelize multiple (time consuming) tasks without being blocked in one task. It also helps to utilize the available CPU resources in an optimized way.

We can achieve parallelism by multiprocessing also. But multithreading has several benefits over multiprocessing.

  • Thread creation and termination are much more faster and efficient.
  • Inter-thread communication is much faster.
  • Threads can share resources, like memory or file descriptors, easily.
  • Context switching between threads is much efficient and faster.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
0