0% found this document useful (0 votes)
16 views20 pages

Unit 2.2 (2 Out of 4) - 1

This document covers the topic of threads in operating systems, detailing their types, creation, and termination, as well as issues related to multithreading such as race conditions and deadlocks. It also discusses multicore programming, emphasizing the benefits, challenges, and the role of the operating system in managing threads and synchronization. Additionally, it highlights various programming models and frameworks used for multicore programming applications.

Uploaded by

vgarenaff01
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)
16 views20 pages

Unit 2.2 (2 Out of 4) - 1

This document covers the topic of threads in operating systems, detailing their types, creation, and termination, as well as issues related to multithreading such as race conditions and deadlocks. It also discusses multicore programming, emphasizing the benefits, challenges, and the role of the operating system in managing threads and synchronization. Additionally, it highlights various programming models and frameworks used for multicore programming applications.

Uploaded by

vgarenaff01
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
You are on page 1/ 20

Operating System

(24CS301PC204)
Unit – 2.2
Threads
Dr. Rajchandar Kannan, M.E., M.B.A., Ph.D.
Assistant Professor, School of CS&AI,
SR University, Warangal.
UNIT – II (Contact Hours: 10)
I. Process Management: Process Concept, Process control
block, Process States, Process Scheduling, Operations on
Processes, Inter process Communication.
II. Threads: Overview, Multi core Programming,
Multithreading Models, Threading Issues.
III.CPU Scheduling: Basic Concepts, Scheduling Criteria,
Scheduling Algorithms
IV.Process Synchronization: Background, The Critical-Section
Problem, Peterson’s Solution, Synchronization Hardware,
Mutex Locks, Semaphores, Classic Problems of
Synchronization, Monitors.
31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 2
II. Threads
1.Overview
2.Multi core Programming
3.Multithreading Models
4.Threading Issues.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 3


Threads
• A thread is the smallest unit of a CPU's execution process.

• Multiple threads can exist within a single process, sharing resources like
memory and file handles.

• Threads can be categorized as:

• User-Level Threads: Managed by user-space libraries, independent of the OS


kernel.
• Kernel-Level Threads: Managed directly by the operating system, with full OS
support for scheduling.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 4


Common Types of Threads

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 5


Type of Thread:
Many-to-One One-to-One Many-to-Many
Model Model Model Two-Level
User-Level Kernel-Level (User-Level (User-Level (Multiple User Model
Threads (ULTs): Threads (KLTs): Threads with Threads with Threads to (Hybrid
One Kernel One Kernel Multiple Kernel Model):
Thread): Thread): Threads):

Managed by the A combination of


Managed entirely Multiple user-
operating system Each user-level one-to-one and
by the application Multiple user- level threads are
kernel, each thread many-to-many
or user-level level threads map mapped to an
thread is corresponds to models where
thread library to a single kernel- equal or smaller
represented as a one kernel-level both user-level
without kernel level thread. number of kernel-
separate kernel- thread. and kernel-level
support. level threads.
level entity. threads coexist.
Process User initiates Operating System
Execute program
program initializes the
code in the process.
Creation: execution. process.

Thread
Create Threads (e.g., Execute code in
Creation within pthread_create()). threads.
a Process:

Termination of Terminate Threads.


Threads:

Termination of
the Entire Terminate Process.
Process:
Threads

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 8


Multi core Programming

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 9


Multicore Processors
• A multicore processor contains two
or more independent cores capable
of executing instructions
simultaneously.
• These cores can handle multiple
threads, providing parallelism and
enhancing performance.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 10


Concurrency vs. Parallelism
• Concurrency: Managing multiple tasks
at once (not necessarily executing them
simultaneously).

• Parallelism: Performing multiple tasks


simultaneously by utilizing multiple
cores.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 11


Multi-core Architecture

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 12


Sequential vs Concurrent vs Parallel

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 13


Threading Issues
1. Context Switching Overhead:
• Switching between threads requires saving and restoring registers, stack
pointers, and program counters, which can add significant overhead.
2. Race Conditions
•When multiple threads access shared resources without proper synchronization,
unpredictable behavior can occur, leading to data corruption.

3. Deadlocks
• If two or more threads hold resources while waiting for each other to
release them, a circular wait situation may arise, leading to a deadlock.
4. Starvation
Some threads may be indefinitely delayed due to priority scheduling, preventing
them 31-01-2025
from gaining access to necessary resources.
Dr. Rajchandar Kannan - Operating System - Unit 2 14
Threading Issues
5. Thread Synchronization Issues
Improper use of synchronization mechanisms (such as mutexes, semaphores, and condition variables)
can lead to inefficiencies or errors like deadlocks and priority inversion.

6. Priority Inversion
A high-priority thread may be blocked because a low-priority thread is holding a required resource,
leading to delays in execution.
7. Resource Contention
Multiple threads competing for CPU time, memory, or I/O resources can cause performance
degradation due to excessive locking and waiting.

8. Thread Safety
Functions and shared data structures must be designed to be thread-safe to avoid unexpected behaviors in
concurrent execution.

9. Thread Termination Issues 10. Scalability Issues


Forcefully terminating a thread can leave shared Poorly designed multithreading can result in overhead
resources in an inconsistent state, causing that outweighs performance gains, especially when
instability.
31-01-2025 dealing
Dr. Rajchandar Kannan - Operating with
System large
- Unit 2 numbers of threads. 15
Benefits of Multicore Programming
• Improved Performance: Tasks are distributed across cores, reducing
execution time.
• Efficient Resource Utilization: Makes full use of the available CPU
cores.
• Responsiveness: Applications remain responsive even during heavy
computation.
• Scalability: Suitable for handling large and complex workloads.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 16


Role of the Operating System in Multicore Programming
• Thread Management:
• The operating system manages the creation, scheduling, and termination of
threads.
• It ensures load balancing across cores by distributing threads efficiently.
• Synchronization:
• The OS provides mechanisms (e.g., mutexes, semaphores, and locks) to manage
shared resources and avoid race conditions.
• Thread Scheduling:
• Modern operating systems use advanced scheduling algorithms (e.g., round-
robin, priority scheduling) to allocate CPU time to threads across multiple cores.
• Inter-Process Communication (IPC):
• Mechanisms like pipes, message queues, and shared memory allow threads and
processes to communicate and coordinate effectively.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 17


Programming Models and Frameworks for Multicore Programming

• POSIX Threads (Pthreads): A standard for threading APIs in UNIX-like


systems.
• OpenMP: A parallel programming model for shared memory systems.
• C++ Standard Library Threads: Provides multithreading support in
C++.
• Java Threads: Java provides built-in support for multithreaded
applications.
• MPI (Message Passing Interface): Focuses on distributed systems but
can work for multicore setups.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 18


Challenges in Multicore Programming
• Synchronization Issues: Managing access to shared resources without
causing data inconsistency or deadlocks.
• Load Balancing: Distributing work evenly across all cores to maximize
performance.
• Debugging Complexity: Identifying and resolving bugs in parallel
execution is harder than in sequential programming.
• Cache Coherency: Ensuring all cores see a consistent view of shared data.

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 19


Applications of Multicore Programming
• High-performance computing (HPC)
• Real-time systems (e.g., gaming and multimedia applications)
• Machine learning and data analysis
• Web servers handling numerous concurrent requests

31-01-2025 Dr. Rajchandar Kannan - Operating System - Unit 2 20

You might also like