Site icon DataFlair

Multithreading in Operating System

Multithreading

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.

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.

 

Types of execution in OS

There are two types of execution:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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. PROCESS THREAD
1. Heavy weight/resource intensive Light weight, takes lesser resources
2. Requires interaction with OS during switching Doesn’t requires interaction with OS during switching
3. Each process has its own memory and file resources but execute the same code Each thread shares open files and child processes
4. If one process gets blocked all the others also stop executing If one thread is blocked, other threads in the task don’t stop executing 
5. Multiple processes use more resources without threads Multiple processes use less resources with threads
6. Each process operates independently A 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:

Disadvantages:

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:

Disadvantages:

 

Difference between User-Level & Kernel-Level Thread

S.No. USER-LEVEL THREAD KERNEL-LEVEL THREAD
1. Faster to create and manage Slower to create and manage
2. Implemented with the help of a thread library at user level OS helps in the creation of Kernel threads
3. Generic and can run on any OS Specific to an OS
4. Unable to take advantage of multiprocessing Can 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.

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.

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.

Drawbacks of Multithreading

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

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.

Exit mobile version