What is Multithreading in Operating System
What are Threads ?
Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Threads are also known as Lightweight processes. Threads are popular way to improve application through parallelism. The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel.
As each thread has its own independent resource for process execution, multiple processes can be executed parallel by increasing number of threads.
Advantages of Thread
- Thread minimizes context switching time.
- Use of threads provides concurrency within a process.
- Efficient communication.
- Economy- It is more economical to create and context switch threads.
- Utilization of multiprocessor architectures to a greater scale and efficiency.
Types of Thread
Threads are implemented in following two ways –
- User Level Threads ( User managed threads)
- Kernel Level Threads (Operating System managed threads acting on kernel, an operating system core.)
- User Level Threads
In this case, application manages thread management kernel is not aware of the existence of threads. The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. The application begins with a single thread and begins running in that thread.
Advantages
- Thread switching does not require Kernel mode privileges.
- User level thread can run on any operating system.
- Scheduling can be application specific in the user level thread.
- User level threads are fast to create and manage.
Disadvantages
- In a typical operating system, most system calls are blocking.
- Multithreading application cannot take advantage of multiprocessing
- Kernel Level Threads
In this case, thread management done by the Kernel. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process.
The Kernel maintains context information for the process as a whole and for individual’s threads within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management in Kernel space. Kernel threads are generally slower to create and manage than the user threads.
Advantages
- Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
- If one thread in a process is blocked, the Kernel can schedule another thread of the same process.
- Kernel routines themselves can multithreaded.
Disadvantages
- Kernel threads are generally slower to create and manage than the user threads.
- Transfer of control from one thread to another within same process requires a mode switch to the Kernel.
Processes Vs Threads
In many ways threads operate in the same way as that of processes. Some of the similarities and differences are:
Similarities
- Like processes threads share CPU and only one thread active (running) at a time.
- Like processes, threads within processes, threads within a processes execute sequentially.
- Like processes, thread can create children.
- And like process, if one thread is blocked, another thread can run.
Differences
- Unlike processes, threads are not independent of one another.
- Unlike processes, all threads can access every address in the task.
- Unlike processes, thread is designed to assist one other. Note that processes might or might not assist one another because processes may originate from different users.
What is Multithreading in Operating System ?
Multithreading is a type of execution model that allows multiple threads to exist within the context of a process such that they execute independently but share their process resources. A thread maintains a list of information relevant to its execution including the priority schedule, exception handlers, a set of CPU registers, and stack state in the address space of its hosting process.
Multithreading is also known as threading.
Benefits of Multithreading
The benefits of multithreading can be broken down into various major categories:
- Responsiveness
Multithreading in an interactive application may allow a program to continue running even if a part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.
In a non multithreading environment, a server listens to the port for some request and when the request comes, it processes the request and then resumes listening to another request. The time taken while processing of request makes other users wait unnecessarily. Instead a better approach would be to pass the request to a worker thread and continue listening to port.
For example, a multi threaded web browser allows user interaction in one thread while an video is being loaded in another thread. So instead of waiting for the whole web-page to load the user can continue viewing some portion of the web-page.
- Resource Sharing
Processes may share resources only through techniques such as-
- Message Passing
- Shared Memory
Such techniques must be explicitly organized by programmer. However, threads share the memory and the resources of the process to which they belong by default.
The benefit of sharing code and data is that it allows an application to have several threads of activity within same address space.
- Economy
Allocating memory and resources for process creation is a costly job in terms of time and space.
Since, threads share memory with the process it belongs, it is more economical to create and context switch threads. Generally much more time is consumed in creating and managing processes than in threads.
In Solaris, for example, creating process is 30 times slower than creating threads and context switching is 5 times slower.
- Utilization of multiprocessor architectures
The benefits of multithreading can be greatly increased in a multiprocessor architecture, where each thread may be running in parallel on a different processor.
A single-threaded process can only run on one CPU, no matter how many are available.
Multithreading on a multi-CPU machine increases concurrency.
In single processor architecture, the CPU generally moves between each thread so quickly as to create an illusion of parallelism, but in reality only one thread is running at a time.
- Better communication
Thread synchronization functions can be used to provide enhanced process-to-process communication. In addition, sharing large amounts of data through separate threads of execution within the same address space provides extremely high-bandwidth, low-latency communication between separate tasks within an application.
- Minimized system resource usage
Threads impose minimal impact on system resources. Threads require less overhead to create, maintain, and manage than a traditional process.
Disadvantages of Multithreading
These are various disadvantages of Multithreading. Here is Follows –
- Multithreading requires support for thread or processes.
- It requires more careful synchronization.
- It imposes context switching overhead.
- Multithreading may consumes lots of space of stacks of blocked threads.
Multithreading Models
Some operating system provides a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. Multithreading models are three types –
- Many to Many Model
- Many to One Model
- One to One Model
Many to Many Model
Many to many relationship – With such model, many user-level threads can be multiplexed by a smaller or equal number of kernel threads. Also, with such model implementation in the system, developers can run as many user threads as necessary with a corresponding kernel threads that can run in parallel on a multiprocessor. However, the overhead required in creating kernel threading is great compared to the one-to-one relationship.
Many to One Model
Many to one relationship – Where many user-level threads are mapped to a single kernel thread. In such model the thread is managed by thread library in the user space which makes such model efficient. However, the entire threads can be blocked if a thread makes a blocking system call. Also with multiprocessors system; threads can access the kernel with only a single thread at a time and as such; multiple threads are unable to take advantage on multiprocessor and run in parallel on multiprocessors. With such model, developer will be able to create as many threads as required; however the true concurrency is not implemented since the kernel can schedule only one thread at a time.
One to One Model
One to one relationship – Where each user thread is mapped to the kernel thread. Such mechanism provides concurrency operation than the many-to-many model where other threads are allowed to run even when one of these threads makes a blocking system call. Also, such model allows multiple threads to run in parallel on multiprocessors. Since such model creates a corresponding kernel thread with every creating of user thread, an overhead of creating kernel threads can affect the performance of an application and as such; this implementation can be restricted with the number of threads that can be supported by the system. Also, with such model, developer can have a greater concurrency applications; however a limited number of threads within an application can be implemented since a limited threads can be created by the system.
So , it was all about What is Multithreading ? , We hope you understand everything well. If you have still any questions or doubts related with What is Multithreading ? then you can freely ask us in the comment box below.
[…] What is Multithreading in Operating System ? […]