Multi-threaded Programming and its Benefits

369

In this tutorial, you will learn about the benefits of multithreaded programming and models of multithread programming in detail with diagrams.

The processes are of two types. The first one is where it can control a single thread and a single task. The second type is where a process control multiple threads. A process having multiple threads can control many tasks at the same time. Each thread has a thread ID, a program counter, a register set, and a stack.

1 single thread process in os
1 single thread process in os
multi thread process in os
multi thread process in os

Generally, almost all processes in modern operating systems are multi-threaded. Even in a simple program such as a text editor, there are multiple threads. One of the threads is running for responding to keyboard input, another to display it on screen, another one to check spelling and grammatical errors, etc.

Having such a multi-threaded architecture has vast benefits and increases the efficiency of the overall system.

Benefits of Multithreaded programming

Multi-threaded programming involves many benefits. Some of these are discussed below –

Resource Sharing

Processes share the resources by two different methods i.e., message passing and shared memory. But, in the case of threads, resource sharing is simple. The process that contains the threads, shares its memory and resources by default. This allows the process to contain different sub-tasks and a different thread for each subtask.

Economy

Creating a process is much easier than creating a thread. Also, the allocation of resources and memory to a process is costlier than thread. Creating a thread rather than a process is much more economical. Also, it is very much faster than the process, because the resource is directly shared.

Responsiveness

Having multiple threads also improves responsiveness. Even if a part of it is not loaded or blocked, it will not affect the complete program. For example, if an image is loading in a browser, the other task such as downloading can be run concurrently.

Scalability

The multithreaded process also increases scalability as it increases the use of all the processors. In the case of a single thread, it will use only a single processor even if more than one processor can be used. But, multi-threading supports parallelism where each thread can use a different CPU.

Multithreading Models

The threads provide support either at the user level or at the kernel level. The thread giving support at the user level is called a user-level thread whereas the threads giving support at the kernel level are kernel-level Threads. A relationship needs to be maintained between the user-level threads and the kernel-level threads. The different ways of establishing the relationship are –

Many-to-one Model

In this model, many user-level threads are connected to one user-level thread. The problem with this model is that if one user-level thread makes a blocking system call, the entire thread execution is blocked.

2 many to one model
many to one model

One-to-one Model

In this, each user-level thread is associated with a kernel-level thread. This is better than many to one model because here, the entire system will not get blocked if one user thread makes a blocking system call. But still, it has a disadvantage that for each user-level thread, a corresponding kernel-level thread needs to be created which is an overhead for the system.

3 One to one model
One to one model

Many-to-many models

In this model, many user-level threads are associated with many but certainly less number of kernel-level threads.

many to many model
many to many model

So, in this article, we learned about multi-threaded programming. Multi-threading is widely used in almost modern-day applications. It is of great benefit in maximizing CPU utilization.

Previous QuizProcess Scheduling, Types of Schedulers, Context Switching in OS
Next QuizMedicinal Chemistry By Dr. Shilpi Shrivastava, Kalinga University

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.