0% found this document useful (0 votes)
2 views40 pages

Threads OS

This document outlines the concepts of threads in operating systems, detailing their structure, benefits of multithreading, and various threading models including many-to-one, one-to-one, and many-to-many. It also discusses thread libraries, threading issues such as cancellation and signal handling, and introduces the concept of Lightweight Processes (LWP). The content is designed for an academic course on processes and threads, with a focus on practical applications and examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views40 pages

Threads OS

This document outlines the concepts of threads in operating systems, detailing their structure, benefits of multithreading, and various threading models including many-to-one, one-to-one, and many-to-many. It also discusses thread libraries, threading issues such as cancellation and signal handling, and introduces the concept of Lightweight Processes (LWP). The content is designed for an academic course on processes and threads, with a focus on practical applications and examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COURSE CODE – COURSE TITLE

MODULE II PROCESSES AND THREADS

TOPIC:Threads

CO1: | U

Academic Year: 2026-2027 Class: II ADS B


(ODD)
Course handling Faculty: Nikhil S, Prof./AI&DS
20-07-2026 Module 1 | Topic 1
Lecture Planned / Session Objectives

SDG Relevance
SDG Mapping 4

PowerPoint Presentation
PEDAGOGY
Think- Pair- Share

AI Tools

20-07-2026 Module 1 | Topic 2


Threads
A thread is a basic unit of CPU utilization
It comprises a thread ID, a program counter, a register set, and a stack.
It shares with other threads belonging to the same process its code section, data
section, and other operating-system resources, such as open files and signals.
A traditional (or heavyweight) process has a single thread of control. If a process has
multiple threads of control, it can perform more than one task at a time.

20-07-2026 Module 1 | Topic 3


Threads

20-07-2026 Module 1 | Topic 4


A word processor may have a thread for responding to keystrokes from user,
another thread for displaying it, and a third thread for spelling and grammar
checking in background.
At times a single application may be required to perform several similar tasks.
Example:
•A web server needs to serve multiple clients concurrently.
•A single threaded process can serve only one client at a time thereby keeping the other
clients waiting for a long time.
•On other hand, creating a separate process to service each requests is highly resource
consuming (heavy weight).
•Creating separate threads to service each requests is much more efficient.

20-07-2026 Module 1 | Topic 5


7/20/2026 MODULE 1 | TOPIC 6
Thread States in Operating Systems

7/20/2026 MODULE 1 | TOPIC 7


Benefits of
Multithreading

7/20/2026 MODULE 1 | TOPIC 8


Multithreading Models
▪Support for threads may be provided either at the user level, for user threads, or by the
kernel, for kernel threads.
▪User threads are supported above the kernel and are managed without kernel support,
whereas kernel threads are supported and managed directly by the operating system.
▪All modern OSs support kernel level threads, allowing the kernel to perform multiple
simultaneous tasks and/or to service multiple kernel system calls simultaneously.
▪There must exist a relationship between user threads and kernel threads, Three
common ways of establishing this relationship.
✔Many-to-One Model
✔One-to-One Model
✔Many-to-Many Model

20-07-2026 Module 1 | Topic 9


7/20/2026 MODULE 1 | TOPIC 10
Many-to-One Model
•Many user threads share one kernel thread, making it efficient but limiting parallel execution.

•Thread management is done in user space by the thread library so it is efficient.

•The entire process will be block if a thread makes a blocking system call .

•Because only one thread can access the Kernel at a time, multiple threads are unable to run in parallel
on multiprocessors.

Advantages: Fast and efficient thread management.

Low overhead.

Disadvantages: If one thread performs a blocking system call, the entire process blocks.

Cannot achieve true parallelism on multi-core processors.

Example: Green Threads (Solaris), GNU Portable Threads.

20-07-2026 Module 1 | Topic 11


One-to-One Model
There is one-to-one relationship of user-level thread to the kernel-level thread.
Each user thread is mapped to one kernel thread.
Every user thread has its own corresponding kernel thread. If one thread is
blocked (e.g., during a system call), other threads can continue executing.
Supports true parallel execution on multicore or multiprocessor systems.
Provides higher concurrency than the many-to-one model.
Because the overhead of creating kernel threads can burden the performance
of an application, most implementation of the model restrict the number of
threads supported by the system.

20-07-2026 Module 1 | Topic 12


Advantages
Better responsiveness.
Allows multiple threads to run simultaneously.
Blocking of one thread does not stop the entire process.
Disadvantages
Creating a user thread also creates a kernel thread.
Kernel thread creation increases memory and CPU overhead.
Most operating systems limit the maximum number of threads to avoid performance issues.
Operating Systems Using One-to-One Model
Linux
Windows 95
Windows 98
Windows XP

20-07-2026 Module 1 | Topic 13


Many to many model
The many to many model multiplexes any number of
user threads onto an equal or smaller number of kernel
threads, combining the best features of the one-to-one
and many-to-one models.
Maps many user threads to a smaller or equal
number of kernel threads.
Multiple user threads share a pool of kernel threads.
Blocking the kernel system calls does not block the
entire process.
Kernel threads execute user threads in parallel on
multicore/multiprocessor systems.

20-07-2026 Module 1 | Topic 14


Similar to the many-to-many model.
Allows a user thread to be permanently bound to a specific kernel thread when needed.
Advantages
Provides the flexibility of many-to-many mapping.
Allows dedicated kernel threads for important user threads.
Improves performance for certain applications.
Operating Systems
IRIX
HP-UX
Tru64 UNIX
Solaris (versions earlier than Solaris 9

20-07-2026 Module 1 | Topic 15


Thread Library
A Thread Library provides an API (Application Programming Interface) for creating,
managing, and controlling threads.
It helps programmers develop multithreaded applications easily.
Types of Thread Libraries
1. User-Level Thread Library
Implemented completely in user space.
No kernel support is required.
Thread operations are performed using local function calls.
No system calls are involved.

20-07-2026 Module 1 | Topic 16


2. Kernel-Level Thread Library
Implemented with direct operating system support.
Library code and data structures are stored in kernel space.
Thread operations require system calls to the kernel.
Advantages
Supports true parallel execution.
Better scheduling and resource management.
If one thread blocks, other threads continue to execute.
Disadvantages
Higher overhead due to system calls.
Slower than user-level thread libraries.

20-07-2026 Module 1 | Topic 17


Popular
Thread
Librarie
s

7/20/2026 MODULE 1 | TOPIC 18


Threading Issues -The fork() and exec() System Calls

fork() System Call exec() System Call


Creates a new (child) process by duplicating Replaces the current process with a new
the existing process. program.
Behavior in Multithreaded Programs Behavior
There are two possible implementations: The new program replaces the entire
process, including all existing threads.
Duplicate all threads
◦ The child process contains all the threads of After exec(), the old program and its
the parent process. threads no longer exist.
Duplicate only the calling thread
◦ The child process contains only the thread that
called fork().

20-07-2026 Module 1 | Topic 19


Threading Issues -Thread
Cancellation
Thread cancellation is the process of terminating a thread before it finishes its execution.
Why is it needed?
▪To stop unnecessary work.
▪To save CPU time and system resources.
▪To improve application performance.
Examples:
Database Search: If multiple threads search a database and one thread finds the result
first, the remaining threads can be cancelled.
Web Browser: A web page may load images using separate threads. When the user
clicks Stop, the loading threads are cancelled.

20-07-2026 Module 1 | Topic 20


Target Thread
The thread that is requested to terminate is called the Target Thread.
Other threads or the operating system may request its cancellation.

Types of Thread Cancellation


1. Asynchronous Cancellation
2. Deferred Cancellation

20-07-2026 Module 1 | Topic 21


1. Asynchronous Cancellation
One thread immediately terminates the target Advantages:
thread.
Quick response.
Characteristics:
Useful when immediate stopping is
Immediate termination. necessary.
Target thread gets no chance to clean up Disadvantages:
resources.
May leave files, memory, or locks
Fast but potentially unsafe. unreleased.
Example: May corrupt shared data.
Thread A → cancels Thread B instantly. Can create resource leaks and inconsistent
program state.

20-07-2026 Module 1 | Topic 22


2. Deferred Cancellation
The target thread checks periodically whether Example:
it should terminate and then exits safely. Thread A sets a cancellation flag → Thread
Characteristics: B checks the flag → Thread B cleans up
and terminates.
Cancellation request is delayed.
Advantages:
Target thread decides when it is safe to stop.
Resources can be released properly.
Safer and more commonly used.
Shared data remains consistent.
Safer for multithreaded programs.
Disadvantages:
Cancellation is not immediate.
Thread may continue running for a short
time before stopping.

20-07-2026 Module 1 | Topic 23


Comparison Table

20-07-2026 Module 1 | Topic 24


7/20/2026 MODULE 1 | TOPIC 25
Cancellation Points
In Pthreads, deferred cancellation occurs only at specific safe locations called Cancellation
Points.
At a cancellation point, the thread checks:
“Has a cancellation request been made?”
If YES → the thread cleans up and terminates safely.
Examples of cancellation points:
▪ read()
▪ write()
▪ sleep()
▪ pthread_cond_wait()
▪ pthread_join()

These points are chosen because the thread can be stopped without leaving the program in an
inconsistent state.

20-07-2026 Module 1 | Topic 26


7/20/2026 MODULE 1 | TOPIC 27
Threading
Issues -
Signal
Handling
A signal is a notification sent by
the operating system to a process
indicating that a particular event
has occurred.

It is mainly used in UNIX/Linux


operating systems.

Purpose:

Notify a process about an event.

Interrupt normal execution.

Allow the process to respond


appropriately.

Module 1 | Topic 20-07-2026 28


7/20/2026 MODULE 1 | TOPIC 29
7/20/2026 MODULE 1 | TOPIC 30
Threading Issues - Scheduler
Activations
Scheduler Activation is a mechanism that enables communication between the
kernel and the user-level thread library.
It is mainly used in:
Many-to-Many Thread Model
Two-Level Thread Model
Purpose:
Coordinate user threads and kernel threads.
Improve performance.
Dynamically adjust the number of kernel threads.
Scheduler Activation acts as a bridge between them.

20-07-2026 Module 1 | Topic 31


Lightweight Process (LWP)
A Lightweight Process (LWP) is an intermediate layer between user threads and kernel
threads.
It acts as a virtual processor for user threads.
Key Points
User threads run on LWPs.
Each LWP is attached to one kernel thread.
The operating system schedules kernel threads, not user threads directly.
How LWP Works
Think of an LWP as a virtual CPU.
The thread library assigns user threads to an available LWP.
The LWP uses its associated kernel thread to execute on the CPU.

20-07-2026 Module 1 | Topic 32


User Threads (T1, T2, T3):
These are the high-level
application threads.
Lightweight Processes
(LWPs): These act as the
interface between user-space
threads and the kernel.
Kernel Threads and CPU: The
diagram shows how the
operating system kernel maps
these processes to the physical
CPU for execution.

20-07-2026 Module 1 | Topic 33


AI Enabled Learning
AI Activity

20-07-2026 Module 1 | Topic 34


Think- Pair- Share
Discussion

20-07-2026 Module 1 | Topic 35


Summary
Discussion Like Example

Today We Learned
✓ Concept of Feedback
✓ Positive vs Negative Feedback
✓ Gain with Feedback
✓ Benefits of Negative Feedback
✓ Industrial Applications

20-07-2026 Module 1 | Topic 36


Mind Map

20-07-2026 Module 1 | Topic 37


Exit Quiz | Video Demonstration
Content

20-07-2026 Module 1 | Topic 38


Students may able
◦ To understand the mathematical fundamentals and its applications
◦ To apply

Linear Algebra Problems

Probability

20-07-2026 Module 1 | Topic 39


20-07-2026 Module 1 | Topic 40

You might also like