0% found this document useful (0 votes)
20 views21 pages

CH 4

Uploaded by

thaider.99eccae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

CH 4

Uploaded by

thaider.99eccae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Operating system 323

Topic: Threads & Concurrency

Dr. Ijaz Khan


[email protected]
Slides: 21

Operating System Concepts – 10th Edition 4.1 Silberschatz, Galvin and Gagne ©2018
Objectives
 In this lecture

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


processes.

 Describe the major benefits and significant challenges of designing


multithreaded processes.

 Multithreaded models

Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Motivation
 Most modern applications are multithreaded

 Multiple tasks with the application can be implemented by


separate threads

• Web browser: one thread load image, other thread


retrieves data from the network
• Word processor: 1 thread load graphic, 2nd thread respond
to key strokes, 3rd thread spell checking

Such applications can perform several CPU-intensive tasks


in parallel across the multiple computing cores.

If a process has multiple threads of control, it can perform


more than one task at a time

Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded Processes

Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Multithreaded Server Architecture

Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
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 Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
 The need for more computing performance, single-CPU
systems evolved into multi-CPU systems.

 Multiple computing cores on a single processing chip where


each core appears as a separate CPU to the operating system.
Such systems are called multicore systems

 Multithreaded programming/Multicore programming provides a


mechanism for more efficient use of these multiple computing
cores and improved concurrency.

Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism

 Concurrency supports more than one task making progress


Single core, scheduler providing concurrency
 Parallelism implies a system can perform more than one task
simultaneously
 Concurrent execution on single-core system:

 Parallelism on a multi-core system:

Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Multicore Programming challenges
 Identifying tasks :This involves examining applications to find areas
that can be divided into separate, concurrent tasks.

 Balance: Tasks perform equal work of equal value. In some instances,


a certain task may not contribute as much value to the overall process
as other tasks.

 Data splitting: The data accessed and manipulated by the tasks must
be divided to run on separate cores.

 Data dependency: When one task depends on data from another,


programmers must ensure that the execution of the tasks is
synchronized to accommodate the data dependency

 Testing and debugging: Testing and debugging such concurrent


programs is inherently more difficult

Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Multicore Programming

 Types of parallelism

• Data parallelism – distributes subsets of the same data


across multiple cores, same operation on each.
• Example: Sum of an Array of size N on two different cores

• Task parallelism – distributing threads across cores, each


thread performing unique operation
• Example: different tasks on the same array in parallel

Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Data and Task Parallelism

Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018
Amdahl’s Law
 Identifies performance gains from adding additional cores to an
application that has both serial and parallel components
 S is serial portion
 N processing cores

 That is, if application is 75% parallel / 25% serial, moving from 1 to 2


cores results in speedup of 1.6 times
 As N approaches infinity, speedup approaches 1 / S

Serial portion of an application has disproportionate effect on


performance gained by adding additional cores

 But does the law take into account contemporary multicore systems?

Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads
 User threads - management done by user-level threads library
 Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
 Kernel threads - Supported by the Kernel
 Examples – virtually all general -purpose operating systems, including:
• Windows
• Linux Kernel threads

• Mac OS X Memory Manager threads


(Multiple threads working
• iOS together)

• Android responsible for managing


the system's memory.

1. Allocate memory
2. Deallocate
3. Page swapping

Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads

Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
Multithreading Models
 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
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:
• LINUX

Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
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

Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
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 Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Two-level Model
 Similar to M:M, except that it allows a user thread to be bound to
kernel thread

Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
(Next Lecture)Thread Libraries
 Thread library provides programmer with API for creating and
managing threads
 Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS

Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018

You might also like