Multithreading in Operating System

FREE Online Courses: Click for Success, Learn for Free - Start Now!

Multithreading allows multiple concurrent tasks to run within a single process for the maximum utilization of a CPU. A thread is the basic unit of the process code and is called a lightweight process within a process that cannot exist outside a process. It has its own program counter. It keeps track of:

1. instructions to execute next
2. system registers that have the process’ current working variables
3. a stack containing the execution history.

Threads share information like code segments, data segments, and open files with each other. If any change is made in the information of one thread all the other threads can see that.

Some common examples of a multithreaded program are: Word processor, multiple threads display a document, check the spelling and grammar of the content, generate a PDF version of the document, all the while when you are typing on the document. Internet browser, where multiple threads load content, display animations, play a video, and so much more at the same time on multiple tabs.

Following is the difference between a single thread and multithreading system.

Threads in OS

Lifecycle of a thread

There are various stages in the lifecycle of a thread. Following are the stages a thread goes through in its whole life.

  • New: The lifecycle of a born thread (new thread) starts in this state. It remains in this state till a program starts.
  • Runnable: A thread becomes runnable after it starts. It is considered to be executing the task given to it.
  • Waiting: While waiting for another thread to perform a task, the currently running thread goes into the waiting state and then transitions back again after receiving a signal from the other thread.
  • Timed Waiting: A runnable thread enters into this state for a specific time interval and then transitions back when the time interval expires or the event the thread was waiting for occurs.
  • Terminated (Dead): A thread enters into this state after completing its task.

 

Types of execution in OS

There are two types of execution:

1. Concurrent Execution: This occurs when a processor is successful in switching resources between threads in a multithreaded process on a single processor.

2. Parallel Execution: This occurs when every thread in the process runs on a separate processor at the same time and in the same multithreaded process

Difference between Process and Thread

S.No.PROCESSTHREAD
1.Heavy weight/resource intensiveLight weight, takes lesser resources
2.Requires interaction with OS during switchingDoesn’t requires interaction with OS during switching
3.Each process has its own memory and file resources but execute the same codeEach thread shares open files and child processes
4.If one process gets blocked all the others also stop executingIf one thread is blocked, other threads in the task don’t stop executing 
5.Multiple processes use more resources without threadsMultiple processes use less resources with threads
6.Each process operates independentlyA thread can read or write another thread’s data

Advantages of Multithreading

1. It is economical and takes less time to create threads.

2. It allows resource sharing among threads like data, memory, files, etc. Thus, multiple threads can share the same address space.

3. It increases the responsiveness towards the user as it allows the program to continue running irrespective of the situation.

4. It increases parallelism on multiple CPU machines and enhances performance of the multi-processor machines.

5. It uses CPU resources more efficiently.

Types of Thread in Operating System

There are two types of threads:

1. User Level Threads: Users are the ones managing these threads. The thread managing kernel is not aware of this thread’s existence. The thread library has codes for creating, passing messages and data, scheduling execution, shaving and restoring, and destroying threads. These are used at the application level and have no involvement of the OS.

Advantages:

  • Kernel mode privileges are not required during switching.
  • Can run on any OS
  • Scheduling is application specific
  • Fast to create and manage

Disadvantages:

  • Most system calls are blocking.
  • Applications that have been multithreaded cannot take advantage of multiprocessing.

Thread LifeCycle

2. Kernel Level Threads: Operating System is the one i.e., the core of an operating system. The thread management is done by the Kernel and there is no thread management code in the application area. These are directly supported by the OS and any application can be programmed to be multithreaded. managing these threads. It acts on the kernel

These threads are usually slower to create and manage. Thread creation, scheduling and management is done in the Kernel space by the Kernel.

Advantages:

  • Kernel can simultaneously schedule multiple threads
  • If one thread gets blocked, another can be scheduled.
  • Kernel routines multithread themselves.

Disadvantages:

  • Slower to create and manage
  • Transfer of control within a process requires a mode switch to the Kernel.

Kernel level Threads

 

Difference between User-Level & Kernel-Level Thread

S.No.USER-LEVEL THREADKERNEL-LEVEL THREAD
1.Faster to create and manageSlower to create and manage
2.Implemented with the help of a thread library at user levelOS helps in the creation of Kernel threads
3.Generic and can run on any OSSpecific to an OS
4.Unable to take advantage of multiprocessingCan be multithreaded

Multithreading Models

There are three types of multithreading models, namely,
1. Many to many relationship.
2. Many to one relationship.
3. One to one relationship.

1. Many to Many Multithreading Model

In this model any number of user threads can be multiplied into equal or smaller numbers of kernel threads. Developers are capable of creating multiple number of user threads and their corresponding Kernel threads can run in parallel on a multiprocessor machine.

The many to many model not only provides the best accuracy on concurrency but also when a thread performs a blocking system call, the kernel schedules another thread for execution.

Kernel Level Threads

2. Many to One Multithreading Model

This model maps multiple user-level threads to one Kernel-level thread. When a blocking system call is made, the thread library carries out thread management in the user library blocking the entire process. Multiple threads cannot run in parallel only one thread can access the Kernel at a time.

Many to One Model in OS

3. One to One Multithreading Model

In this model, the user-level thread and the kernel-level thread share a one-to-one relationship. The concurrency provided by this model is higher than the many-to-one model. It allows the parallel execution of multiple threads on microprocessors.

One to One in OS

Drawbacks of Multithreading

Multithreading is complex and many times difficult to handle. It has a few drawbacks. These are:

  • If you don’t make use of the locking mechanisms properly, while investigating data access issues there is a chance of problems arising like data inconsistency and dead-lock.
  • If many threads try to access the same data, then there is a chance that the situation of thread starvation may arise. Resource contention issues are another problem that can trouble the user.
  • Display issues may occur if threads lack coordination when displaying data.

Summary

Multithreading is running multiple tasks within a process. It is of two types, namely user level threads and kernel level threads. It is economical, responsive, scalable, efficient, and allows resource sharing. There are three models in multithreading: Many to many model, Many to one model, and one to one model.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

2 Responses

  1. Peterchimakile Mwasame says:

    Fine niye,👍👍👍
    Love it

  2. Kiprotich Tore says:

    Good job 👍

Leave a Reply

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