What is Multithreading in OS?

Learn via video courses
Topics Covered

Overview

A path that is followed during the execution of the program is called a thread. Almost all the programs written today run at a single thread. But there are some issues with using a single thread and hence the concept of multi-thread is introduced. Using multi-thread, the programs can be executed at the same time, i.e. simultaneously.

What is Multithreading in OS (Operating System)?

The term multithreading means a process and a thread. Process means a program that is being executed. Processes are further divided into independent units also known as threads, also known as collections of threads. It is a process that is small and light-weighted residing inside a process.

What is Multithreading in OS

How does multithreading work in OS?

Multithreading divides the task of application into separate individual threads. The same process or tasks in multithreading can be done by several threads or it can be said that more than one thread is used for performing the tasks in multithreading.

It has a segment that divides the code into a small set of tasks that are lightweight and give less load to CPU memory.

For example, client 1, client 2, and client 3 in the above example are accessing the web server without having to wait for other tasks to be complete.

The threads in this are divided into user-level and kernel-level threads. The user-level thread is used for handling an independent form of the kernel-level thread without any support from the kernel. On the other hand, the kernel-level threads are directly managed by the operating system.

How does multithreading work in OS

Examples of Multithreading Operating Systems

Multithreading is widely used by applications. Some of the applications are processing transactions like online bank transfers, recharge, etc.

For instance, in the banking system, many users perform day-to-day activities using bank servers like transfers, payments, deposits, `opening a new account, etc. All these activities are performed instantly without having to wait for another user to finish.

In this, all the activities get executed simultaneously as and when they arise. This is where multithreading comes into the picture, wherein several threads perform different activities without interfering with others.

Multithreading vs. Multitasking vs. Multiprocessing

MultithreadingMultitaskingMultiprocessing
Involves running multiple threads within a single process, sharing the same memory space.Involves running multiple tasks or processes concurrently on a single CPU.Involves running multiple processes on multiple CPUs or CPU cores.
Threads within a process can communicate and share data easily.Tasks or processes are independent and communicate through inter-process communication (IPC) mechanisms.Processes are independent and can run on separate CPUs or cores, often with no need for direct communication.
Efficient for tasks that benefit from sharing data and resources within the same memory space.Suitable for scenarios where tasks are loosely related and don't need direct data sharing.Ideal for compute-intensive tasks and when high performance is required by leveraging multiple CPU resources.
More lightweight and efficient than multiprocessing, as threads share the same memory space.Slightly heavier than multithreading due to the need for separate process memory spaces.Can provide the highest level of parallelism and performance, but requires more hardware resources.
Easier to implement and manage as threads are typically easier to create and coordinate.Requires more complex coordination mechanisms between tasks or processes.More complex to set up and coordinate due to separate processes, but offers significant performance advantages.
Commonly used in applications that require responsiveness and parallelism, like web servers and GUI applications.Widely used in general-purpose operating systems for task management.Applied in high-performance computing, scientific simulations, and large-scale data processing.
Vulnerable to thread-related issues like data races and deadlocks.Processes are less vulnerable to direct data conflicts but can still experience synchronization issues.Provides better isolation and stability, with fewer data sharing issues, but may still require inter-process communication.
Offers a good balance between resource utilization and concurrency for many applications.Balances resource usage and concurrent processing but may have more overhead due to process separation.Maximizes resource usage and parallelism, making it suitable for compute-intensive workloads.

Multithreading vs. Parallel Processing and Multicore Processors

MultithreadingParallel ProcessingMulticore Processors
Involves running multiple threads within a single process, allowing concurrent execution of tasks within that process.Refers to distributing tasks among multiple processors or cores to perform operations simultaneously.Consists of multiple CPU cores on a single chip, enabling parallel execution of multiple tasks.
Efficient for managing multiple tasks in a single program or process, making better use of system resources.Suited for computationally intensive tasks that can be divided into smaller, independent sub-tasks for simultaneous execution.Provides improved performance for multitasking, as each core can execute separate processes concurrently.
Uses shared memory space, making it easier for threads to communicate and share data within the same process.Often requires inter-process communication (IPC) mechanisms for data exchange among independent processes.Utilizes multiple cores that can operate independently or in parallel, without the need for inter-process communication.
Offers improved responsiveness and multitasking capabilities within a single application.Enhances performance for specific tasks, such as data processing, scientific simulations, or rendering.Provides better overall system performance and responsiveness, benefiting a wide range of applications and tasks.
Requires careful synchronization to avoid race conditions and data conflicts between threads.Involves task division, synchronization, and merging of results, which can be complex to manage.Requires proper utilization of multithreading or parallel programming techniques to fully leverage available cores.
Can be implemented within a single CPU core, taking advantage of time-sharing and context switching.Typically involves multiple CPUs or cores working in tandem to process tasks concurrently.Combines multiple cores on a single chip, reducing power consumption and hardware footprint.
Suitable for applications where responsiveness and resource utilization are critical, such as user interfaces and web servers.Beneficial for tasks that can be divided into parallelizable sub-tasks, like scientific simulations and rendering.Provides improved performance across a wide range of applications, from office tasks to high-performance computing.

Multithreading Models in OS

Some operating systems provide a combination of both, user-level thread and kernel-level thread. One such example of this could be Solaris. In this combined system, multiple threads run in parallel in the same system. There are 3 types of models in multithreading.

Many to many relationships.

In this, the OS multiplexes 'n' number of threads of user onto kernel thread of equal or small numbers. The diagram represents the many-to-many relationship thread model.

Many to many relationships In this, 6 user-level threads are multiplexed with 6 kernel-level threads. The developers using this thread can create as many user threads as possible and the corresponding kernel thread can also run parallel on a multiprocessor machine. This model provides the best accuracy and concurrency on a thread.

Many-to-one relationship.

This model maps many user-level threads to one kernel-level thread. By using a thread library, the management of the thread can be done in the user space. When the blocking system call is made by a thread, the entire process of the thread gets blocked. Since only one thread can be able to access a thread at a time, multiple threads cannot be run parallelly on a multiprocessor. If an implementation of user-level threads is done in an operating system in a way such that the system does not support it then the kernel uses the many-to-one relationship model. Many to one relationship

One-to-one relationship.

The user-level thread has a one-to-one relationship with the kernel-level thread. It is better than the many-to-one model as it provides more concurrency. When a blocking system call is made by a thread, it allows another thread to execute. Supports execution of multiple threads in parallel on a microprocessor. One-to-one relationship

Advantages of Multithreading in OS

  • Minimize the time of context switching- Context Switching is used for storing the context or state of a process so that it can be reloaded when required.
  • By using threads, it provides concurrency within a process- Concurrency is the execution of multiple instruction sequences at the same time.
  • Creating and context switching is economical - Thread switching is very efficient because it involves switching out only identities and resources such as the program counter, registers, and stack pointers.
  • Allows greater utilization of multiprocessor architecture

Disadvantages of Multithreading in OS

  • A multithreading system operates without interruptions.
  • The code might become more intricate to comprehend.
  • The costs associated with handling various threads could be excessive for straightforward tasks.
  • Identifying and resolving problems may become more demanding due to the intricate nature of the code.

Conclusion

  • The term multithreading means, a process and a thread. Process means a program that is being executed. Processes are further divided into independent units also known as threads, also known as collections of threads. It is a process that is small and light-weighted residing inside a process. Some of the applications are processing transactions like online bank transfers, recharge, etc.
  • Multithreading Models in OS
    • `Many to many relationships.
    • Many to one relationship.
    • `One-to-one relationship.
  • Advantages of Multithreading in OS
    • Minimize the time of `context switching
    • By using threads, it provides concurrency within a process
    • Creating and context switching is economical
    • Allows greater utilization of multiprocessor architecture