OS Module2.1 Threads

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Chapter 4: Threads

Chapter 4: Threads
● Overview
● Multithreading Models
● Thread Libraries
● Threading Issues
Objectives

● To introduce the notion of a thread — a fundamental unit of CPU


utilization that forms the basis of multithreaded computer systems.

● To discuss the APIs for the Pthreads, Win32, and Java thread
libraries.

● To examine issues related to multithreaded programming.


Threads
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
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


1.A program counter
2.A register set
3.A stack space

Threads are not independent of each other as they share the code, data, OS
resources etc.
Threads
Similarity between Threads and Processes –

•Only one thread or process is active at a time

•Within process both execute sequential.

•Both can create children.

Differences between Threads and Processes –

•Threads are not independent, processes are.

•Threads are designed to assist each other, processes may or may not do it.
Single and Multithreaded Processes
Need of Thread
•It takes far less time to create a new thread in an existing process than to
create a new process.

•Threads can share the common data, they do not need to use Inter-
Process communication.

•Context switching is faster when working with threads.

•It takes less time to terminate a thread than a process.


Benefits
● Responsiveness
A program may be allowed to continue running even if part of it is
blocked. Thus, increasing responsiveness to the user.

● Resource Sharing
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.

● Economy
Allocating memory and resources for process-creation is costly. Thus, it is
more economical to create and context-switch threads.

● Scalability
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.

1.Kernel level thread.

2.User-level thread.

User-level thread
• The operating system does not recognize the user-level thread.

• User threads can be easily implemented and it is implemented by the user.

• If a user performs a user-level thread blocking operation, the whole process is


blocked.

• The kernel level thread does not know nothing about the user level thread.

• The kernel-level thread manages user-level threads as if they are single-threaded


processes? examples: Java thread, POSIX threads, etc.
User-level thread
Advantages of User-level threads
1. It is faster and efficient.
2. Context switch time is shorter than the kernel-level threads.
3. It does not require modifications of the operating system.
4. The register, PC, stack, and mini thread control blocks are stored in the
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


1.User-level threads lack coordination between the thread and the kernel.

2.If a thread causes a page fault, the entire process is blocked.


Kernel level thread

• The kernel thread recognizes the operating system.

• There is a thread control block and process control block in the system for
each thread and process in the kernel-level thread.

• The kernel-level thread is implemented by the operating system.

• The kernel knows about all the threads and manages them.

• The kernel-level thread offers a system call to create and manage the
threads from user-space.

• Context switch time is longer in the kernel thread.

Example: Window Solaris.


Advantages of Kernel-level threads

1.The kernel-level thread is fully aware of all threads.

2.The scheduler may decide to spend more CPU time in the process of
threads being large numerical.

3.The kernel-level thread is good for those applications that block the
frequency.

Disadvantages of Kernel-level threads

1.The kernel thread manages and schedules all threads.

2.The implementation of kernel threads is difficult than the user thread.

3.The kernel-level thread is slower than user-level threads.


Multicore Programming
Multicore systems putting pressure on programmers, challenges include

◦ Dividing activities

◦ Balance

◦ Data splitting

◦ Data dependency

◦ Testing and debugging


Concurrent Execution on a Single-core System
User Threads

● Thread management done by user-level threads library

● Three primary thread libraries:


● POSIX Pthreads
● Win32 threads
● Java threads
Kernel Threads
● Supported by the Kernel

● Examples
● Windows XP/2000
● Solaris
● Linux
● Tru64 UNIX
● Mac OS X
Difference between User-Level & Kernel-Level Thread
S.N. User-Level Threads Kernel-Level Thread

1 User-level threads are faster to Kernel-level threads are slower to


create and manage. create and manage.

2 Implementation is by a thread Operating system supports


library at the user level. creation of Kernel threads.

3 User-level thread is generic and Kernel-level thread is specific to


can run on any operating system. the operating system.

4 Multi-threaded applications cannot Kernel routines themselves can be


take advantage of multiprocessing. multithreaded.
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

•Improving Front end response to the users.

•Enhancing application performance.

•Effective utilization of Computer resources.

•Low maintenance cost.

•Faster completion of tasks due to parallel operation.

•Simplifying development process and increasing productivity.


Multithreading Models

The relationship between application threads and kernel threads can be


categorized into:

1. Many-to-one model

2. One-to-one model

3. Many-to-many model.
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
● Similar to M:M, except that it allows a user thread to be bound to
kernel thread

● Examples
● IRIX
● HP-UX
● Tru64 UNIX
● Solaris 8 and earlier
Thread Libraries
● Thread library provides programmer with API for creating and managing
threads.

● Two primary ways of implementing

• Library entirely in user space. All code and data structures for the library
exist in a local function call in user space and not in a system call.

• Kernel-level library supported by the OS. In this case the code and data
structures for the library exist in kernel space.
Thread Libraries
The main thread libraries which are used are

•POSIX threads − Pthreads, the threads extension of the POSIX standard,


may be provided as either a user level or a kernel level library.

● A POSIX standard (IEEE 1003.1c) API for thread creation and


synchronization.

● API specifies behavior of the thread library, implementation is up to


development of the library.

● Common in UNIX operating systems (Solaris, Linux, Mac OS X).

●The Pthread program must always have a pthread.h header file.


Thread Libraries

•JAVA thread − The JAVA thread API allows threads to be created and
managed directly as JAVA programs.

● Java threads are managed by the JVM.

● Typically implemented using the threads model provided by underlying


OS.

● Java threads may be created by:

● Extending Thread class.


● Implementing the Runnable interface.
Thread Libraries

•WIN 32 thread − The windows thread library is a kernel level library


available on windows systems.

❑Creation of thread in Win2 library is similar to pthread library.

❑To create a thread using the Win32 library always


include windows.h header file in the program.

❑The Win32 thread library is a kernel-level library which means invoking


the Win32 library function results in a system call.
Threading Issues
● Semantics of fork() and exec() system calls

● Thread cancellation of target thread


● Asynchronous or deferred

● Signal handling

● Thread pools

● Thread-specific data
Semantics of fork() and exec()

● Does fork() duplicate only the calling thread or all threads?

● One that duplication all threads- The child thread does not call exec()
after Forking.

● Only the thread that invoked Fork() system call is duplicated-exec() is


called immediately after Forking.
Thread Cancellation
● Terminating a thread before it has finished.

● Two general approaches:

● Asynchronous cancellation terminates the target thread


(a thread which we want to cancel is termed ) immediately.

● Deferred cancellation allows the target thread to periodically


check if it should be cancelled.
Signal Handling
● Signals are used in UNIX systems to notify a process that a particular
event has occurred.

● A signal handler is used to process signals


1. Signal is generated by particular event.
2. Signal is delivered to a process.
3. Signal is handled.

● Options:
● Deliver the signal to the thread to which the signal applies.
● Deliver the signal to every thread in the process.
● Deliver the signal to certain threads in the process.
● Assign a specific thread to receive all signals for the process.
Thread Pools
● Create a number of threads in a pool where they await work.

● Advantages:
● Usually slightly faster to service a request with an existing thread
than create a new thread.

● Allows the number of threads in the application(s) to be bound to


the size of the pool.

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
● If allows each thread to have its own copy of data. So the specific data
associated with the specific thread is referred to as thread-specific data.

● Useful when you do not have control over the thread creation process
(i.e., when using a thread pool).

You might also like