Chapter 4: Threads
Chapter 4: Threads
Objectives
A thread is a path of execution within a process. A process can contain multiple threads.
A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads.
For example, In a browser, multiple tabs can be different threads.
MS Word uses multiple threads: one thread to format the text,
another thread to process inputs, etc
The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.
Threads
Threads
Thread is a single sequence stream within a process.
Threads have same properties as of the process so they are called as light weight processes.
Threads are executed one after another but gives the illusion as if they are executing in parallel.
Each thread has different states. Each thread has
Threads are not independent of each other as they share the code, data, OS resources etc.
Threads
Similarity between Threads and Processes –
Differences between Threads and Processes –
Single and Multithreaded Processes
Need of Thread
Benefits
A program may be allowed to continue running even if part of it is
blocked. Thus, increasing responsiveness to the user.�
By default, threads share the memory (and resources) of the process to
which they belong. Thus, an application is allowed to have several
different threads of activity within the same address-space.
Allocating memory and resources for process-creation is costly. Thus, it is
more economical to create and context-switch threads.�
In a multiprocessor architecture, threads may be running in parallel on
different processors. Thus, parallelism will be increased.
Types of Threads
In the operating system, there are two types of threads.
User-level thread
User-level thread
Advantages of User-level threads
address space of the user-level process.
5. It is The user threads can be easily implemented than the kernel thread.
6. Simple to create, switch, and synchronize threads without the
intervention of the process.
Disadvantages of User-level threads
�
Kernel level thread
Example: Window Solaris.
Advantages of Kernel-level threads
threads being large numerical.
3.The kernel-level thread is good for those applications that block the
frequency.
Disadvantages of Kernel-level threads
Multicore Programming
Concurrent Execution on a Single-core System
User Threads
Kernel Threads
S.N. | User-Level Threads | Kernel-Level Thread |
1 | User-level threads are faster to create and manage. | Kernel-level threads are slower to create and manage. |
2 | Implementation is by a thread library at the user level. | Operating system supports creation of Kernel threads. |
3 | User-level thread is generic and can run on any operating system. | Kernel-level thread is specific to the operating system. |
4 | Multi-threaded applications cannot take advantage of multiprocessing. | Kernel routines themselves can be multithreaded. |
Difference between User-Level & Kernel-Level Thread
Multithreading Models
Multi threading-It is a process of multiple threads executes at same time.
Multithreading allows the application to divide its task into individual threads.
In multi-threads, the same process or task can be done by the number of threads, or we can say that there is more than one thread to perform the task in multithreading. With the use of multithreading, multitasking can be achieved.
Many operating systems support kernel thread and user thread in a combined way. Example of such system is Solaris.
Application-level multithreads runs on multiple processors controlled by Kernel.
Multithreading Models
Multithreading Models
Importance of Multithreading in Operating System
Multithreading facilitates concurrent execution of multiple program codes and these program codes are easily managed in Java program including its sequence of execution and it plays an important role in
Multithreading Models
The relationship between application threads and kernel threads can be categorized into:
Many to Many Model
In this model, we have multiple user threads multiplex to same or lesser number of kernel level threads.
Number of kernel level threads are specific to the machine, advantage of this model is if a user thread is blocked we can schedule others user thread to other kernel thread.
Thus, System doesn’t block if a particular thread is blocked.
This model provides the best accuracy on concurrency.
Many to Many Model
Example:
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Many to One Model
Many-to-one model maps many user level threads to one Kernel-level thread.
Thread management is done in user space by the thread library. When thread makes a blocking system call, the entire process will be blocked.
Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.
If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes.
Many to One Model
Examples:
Solaris Green Threads
GNU Portable Threads
One to One Model
There is one-to-one relationship of user-level thread to the kernel-level thread.
This model provides more concurrency than the many-to-one model.
It also allows another thread to run when a thread makes a blocking system call.
It supports multiple threads to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the corresponding Kernel thread.
One to One Model
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Two-level Model
Thread Libraries
Thread Libraries
The main thread libraries which are used are
Thread Libraries
Thread Libraries
Threading Issues
Semantics of fork() and exec()
Thread Cancellation
(a thread which we want to cancel is termed ) immediately.
Signal Handling
Thread Pools
Whenever the request arrives at the server, it invokes a thread from the pool and assigns it the request to be serviced.
The thread completes its service and return back to the pool and wait for the next request.
Thread Specific Data