0% found this document useful (0 votes)
0 views

ch3 (3)

Chapter 3 discusses threads and concurrency in operating systems, highlighting the differences between threads and processes, the benefits of multithreading, and various threading models. It covers user and kernel threads, their characteristics, and the implications for performance and resource management. Additionally, it introduces threading libraries like Pthreads and their application in multithreaded programming.

Uploaded by

anischelly95
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

ch3 (3)

Chapter 3 discusses threads and concurrency in operating systems, highlighting the differences between threads and processes, the benefits of multithreading, and various threading models. It covers user and kernel threads, their characteristics, and the implications for performance and resource management. Additionally, it introduces threading libraries like Pthreads and their application in multithreaded programming.

Uploaded by

anischelly95
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter 3: Threads & Concurrency

Presented by: Dr. Labed Abdeldjalil

Operating System
Outline

▪ Overview

▪ Multicore Programming

▪ Multithreading Models

▪ Thread Libraries

▪ Implicit Threading

▪ Threading Issues

▪ Operating System Examples

Operating System 4.2


Objectives

▪ Identify the basic components of a thread, and contrast threads and


processes

▪ Describe the benefits and challenges of designng multithreaded


applications

▪ Illustrate different approaches to implicit threading including thread pools,


fork-join, and Grand Central Dispatch

▪ Describe how the Windows and Linux operating systems represent threads

▪ Designing multithreaded applications using the Pthreads, Java, and


Windows threading APIs

Operating System 4.3


Motivation

▪ Most modern applications are multithreaded

▪ Threads run within application


▪ Multiple tasks with the application can be implemented by separate
threads
• Update display
• Fetch data
• Spell checking
• Answer a network request

▪ Process creation is heavy-weight while thread creation is light-weight

▪ Can simplify code, increase efficiency

▪ Kernels are generally multithreaded

Operating System 4.4


Single and Multithreaded Processes

Operating System 4.5


Multithreaded Server Architecture

Operating System 4.6


Benefits

▪ Responsiveness – may allow continued execution if part of process is

blocked, especially important for user interfaces

▪ Resource Sharing – threads share resources of process, easier than

shared memory or message passing

▪ Economy – cheaper than process creation, thread switching lower

overhead than context switching

▪ Scalability – process can take advantage of multicore architectures

Operating System 4.7


Multicore Programming

▪ Multicore or multiprocessor systems puts pressure on programmers,


challenges include:
• Dividing activities: The workload must be evenly distributed across
cores to maximize CPU usage.
• Balance: Data must be divided so that each core gets an appropriate
portion to process.
• Data splitting: Data must be divided so that each core gets an
appropriate portion to process.
• Data dependency: Some tasks depend on previous tasks’ results,
limiting parallel execution.
▪ Parallelism implies a system can perform more than one task
simultaneously
▪ Concurrency supports more than one task making progress
• Single processor / core, scheduler providing concurrency

Operating System 4.8


Concurrency vs. Parallelism
▪ Concurrent execution on single-core system:

▪ Parallelism on a multi-core system:

Operating System 4.9


Multicore Programming

▪ Types of parallelism
• Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
• Task parallelism – distributing threads across cores, each
thread performing unique operation

Operating System 4.10


User Threads and Kernel Threads

▪ User threads - User threads are managed entirely in user space by a


thread library without direct involvement of the operating system (OS)
kernel.

▪ Key Characteristics:

• Created and scheduled by user-level thread libraries (e.g., POSIX


pthreads, Java threads).

• The kernel is unaware of these threads.

• Faster context switching since no system calls are needed.

• No direct access to hardware or system resources.

Operating System 4.11


User Threads and Kernel Threads

▪ Kernel threads - are managed directly by the operating system kernel.


▪ Examples – virtually all general-purpose operating systems, including:
• Windows, Linux, Mac OS X, iOS, Android
▪ Key Characteristics:
• Created and scheduled by the OS kernel.
• The kernel is aware of these threads.
• True parallel execution is possible on multi-core systems.

▪ User Threads are faster, lightweight, and managed in user space, but
they don’t achieve true parallelism.
▪ Kernel Threads allow true parallel execution and better resource
management, but they are slower due to kernel overhead.

Operating System 4.12


User and Kernel Threads

Operating System 4.13


Multithreading Models

Multithreading models define how user threads and kernel threads


interact. There are three main models:

▪ Many-to-One

▪ One-to-One

▪ Many-to-Many

Operating System 4.14


Many-to-One

▪ Many user-level threads mapped to single kernel thread


▪ One thread blocking causes all to block
▪ Multiple threads may not run in parallel on multicore system because
only one may be in kernel at a time
▪ Few systems currently use this model
▪ Examples:
• Solaris Green Threads
• GNU Portable Threads

Operating System 4.15


One-to-One

▪ Each user-level thread maps to kernel thread


▪ Creating a user-level thread creates a kernel thread
▪ More concurrency than many-to-one
▪ Number of threads per process sometimes restricted due to overhead
▪ Examples
• Windows
• Linux
• macOS

Operating System 4.16


Many-to-Many Model
▪ Allows many user level threads to be mapped to many kernel threads
▪ Allows the operating system to create a sufficient number of kernel
threads
▪ Windows with the ThreadFiber package
▪ Otherwise not very common

Operating System 4.17


Two-level Model
▪ Similar to M:M, except that it allows a user thread to be bound to
kernel thread

Operating System 4.18


Thread Libraries
▪ Thread library provides a set of functions (API) that allows programmers to
create and manage threads in an application.

▪ Two primary ways of implementing:

• Library entirely in user space Kernel-level

• Library supported by the OS

Feature User-Level Threads Kernel-Level Threads


Managed by User-space library Operating system kernel
Execution Speed Fast (no system calls) Slower (needs system calls)
Yes (true parallelism on
Parallel Execution No (limited to one core)
multiple cores)
Other threads keep running
Blocking Behavior One thread blocks all threads
if one blocks
Java Threads, Pthreads (User Windows Threads, Linux
Examples
mode) Threads

Operating System 4.19


Pthreads

▪ Pthreads is a standard set of C library functions for multithreaded


programming

▪ May be provided either as user-level or kernel-level

▪ 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 (Specification, not implementation)

▪ Common in UNIX operating systems (Linux & Mac OS X)

Operating System 4.20


Pthreads Example

Operating System 4.21


Pthreads Example (Cont.)

Operating System 4.22


Pthreads Code for Joining 10 Threads

Operating System 4.23


Windows Multithreaded C Program

Operating System 4.24


Windows Multithreaded C Program (Cont.)

Operating System 4.25


End of Chapter 3

Operating System

You might also like