What is the concept of Multithreading in OS and what are its benefits?

You are already aware of the term multitasking that allows processes to run concurrently. Similarly, multithreading allows sub-processes (threads) to run concurrently or parallelly. Also, we can say that when multiple threads run concurrently it is known as multithreading . Some widely used programming languages like Java and Python allow developers to work on threads in their program. In this blog, we will learn what are the various multithreading models and the benefits of multithreading in OS. So, let's get started.

Multithreading

Multithreading is a phenomenon of executing multiple threads at the same time. To understand the concept of multithreading, you must understand what is a thread and a process .

A process is a program in execution . A process can further be divided into sub-processes known as threads . Please refer to the previous blog for more details . Example: Playing a video and downloading it at the same time is an example of multithreading.

As we have two types of thread i.e. user-level thread and kernel-level thread . So, for these threads to function together there must exist a relationship between them. This relation is established by using Multithreading Models . There are three common ways of establishing this relationship.

  1. Many-to-One Model
  2. One-to-One Model
  3. Many-to-Many Model

Many-to-One Model

As the name suggests there is many to one relationship between threads. Here, multiple user threads are associated or mapped with one kernel thread. The thread management is done on the user level so it is more efficient.

Drawbacks

  1. As multiple users threads are mapped to one kernel thread. So, if one user thread makes a blocking system call( like function read() call then the thread or process has to wait until read event is completed), it will block the kernel thread which will in turn block all the other threads.
  2. As only one thread can access the kernel thread at a time so multiple threads are unable to run in parallel in the multiprocessor system. Even though we have multiple processers one kernel thread will run on only one processor . Hence, the user thread will also run in that processor only in which the mapped kernel thread is running.

One-to-One Model

From the name itself, we can understand the one user thread to mapped to one kernel thread.

Advantages over Many-to-One Model

  1. In this model, the first drawback of the Many-to-One model is solved. As each user thread is mapped to different kernel threads so even if any user thread makes a blocking system call, the other user threads won't be blocked.
  2. The second drawback is also overcome. It allows the threads to run parallel on a multiprocessor. Since it has each kernel threads mapped to one user thread. So each kernel thread can run on different processors . Hence, each user thread can run on one of the processors.

Disadvantages

  1. Each time we create a user thread we have to create a kernel thread. So, the overhead of creating a kernel thread can affect the performance of the application.
  2. Also, in a multiprocessor system, there is a limit of how many threads can run at a time. Suppose if there are four-processors in the system then only max four threads can run at the time. So, if you are having 5 threads and trying to run them at a time then it may not work. Therefore, the application should restrict the number of kernel threads that it supports.

Many-to-Many Model

From the name itself, we can understand the many user threads to mapped to a smaller or equal number of kernel threads. The number of kernel threads is specific to a particular application or machine.

Advantages over the other two models

  1. Whenever a user thread makes a blocking system call other threads are not blocked.
  2. There can be as many user threads as necessary. Also, the threads can run parallel on multiple processors.
  3. Here we don't need to create as many kernel threads as the user thread. So, there is no problem with any extra overhead which was caused due to creating kernel thread.
  4. The number of kernel threads supported here is specific to the application or machine.

So, this is the best model that we can have in a multithreading system to establish the relationship between user-thread and kernel thread.

Benefits of MultiThreading

  1. Resource sharing: As the threads can share the memory and resources of any process it allows any application to perform multiple activities inside the same address space.
  2. Utilization of MultipleProcessor Architecture: The different threads can run parallel on the multiple processors hence, this enables the utilization of the processor to a large extent and efficiency.
  3. Reduced Context Switching Time: The threads minimize the context switching time as in Thread Context Switching, the virtual memory space remains the same.
  4. Economical: The allocation of memory and resources during process creation comes with a cost. As the threads can distribute resources of the process it is more economical to create context-switch threads.

This was about multithreading and its models. Hope you learned something new today.

Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here .

Keep Learning :)

Team AfterAcademy!