0% found this document useful (0 votes)
10 views27 pages

Chapter 4: Threads & Concurrency: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition

Uploaded by

221020
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)
10 views27 pages

Chapter 4: Threads & Concurrency: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition

Uploaded by

221020
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/ 27

Chapter 4: Threads &

Concurrency

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives
 To introduce the notion of a thread
 To examine issues related to multithreaded programming
 To cover operating system support for threads

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

Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Concurrency and Parallelism
 Parallelism implies that a system can perform more than one task
simultaneously

 Concurrency supports more than one task making progress


 Single processor / core, scheduler providing concurrency

 Multi-core or multiprocessor systems putting pressure on


programmers, challenges include:
 Dividing activities
 Balance
 Data splitting
 Data dependency
 Testing and debugging

Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
 Concurrent execution on single-core system:

 Parallelism on a multi-core system:

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

 Example: 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

Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Introduction to Threads and
Multithreading

Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
What is a thread?
 Traditional (single-threaded) process has a single thread of control.

 A thread is a basic unit of CPU utilization


 Also called a “lightweight process (LWP)”  low creation overhead

 The thread shares with other threads (belonging to the same process) its
code section, data section, and other OS resources, such as open files.

 A process with multiple threads can do more than one task at a time.

 What resources are used when a thread is created? How do they differ
from those used when a process is created?

 What two advantages do threads have over multiple processes? What


major disadvantage do they have?
Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded (MT)
Processes

Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Examples of MT Applications
 Web browser

 Word processor

 Web-server
 Problem?
 Two possible solutions

 Suggest another application that would benefit from the use of threads,
and an application that would not.

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

Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
Benefits of MT Applications
 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 multiprocessor


architectures

Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
ST Example
def calculate_sum_single_thread(N):
total = 0
for i in range(1, N + 1):
total += i
return total

N = 10000
result = calculate_sum_single_thread(N)
print({result})

Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
MT Example
import threading

def calculate_sum_partial(N, start, end, result):


partial_sum = 0
for i in range(start, end + 1):
partial_sum += i
result.append(partial_sum)

N = 10000
thread1_result = [ ]
thread2_result = [ ]

Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
MT Example (Cont.)
# Split the work into two threads
half_N = N / 2
thread1 = threading.Thread(target=calculate_sum_partial, args=(N,
1, half_N, thread1_result))
thread2 = threading.Thread(target=calculate_sum_partial, args=(N,
half_N + 1, N, thread2_result))

thread1.start()
thread2.start()
thread1.join()
thread2.join()

# Combine results from both threads


total_sum = thread1_result[0] + thread2_result[0]
print(total_sum)
Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads

Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
User Threads
 Implemented by a thread library at the user level (above the kernel)
 The library provides support for thread creation, scheduling, and
management.
 The programmer of the library writes code to synchronize threads and to
context switch them, and they all run in one process.
 Kernel does not provide any support; it is unaware that user-level
threads are even running.
 Fast to create and manage (i.e., high efficiency), mainly because the
kernel is not involved.
 What if the kernel is single-threaded?
 Three primary thread libraries: POSIX Pthreads, Windows threads, Java
threads

Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Kernel Threads
 Supported directly by the OS
 The kernel performs thread creation, scheduling, and management
in kernel space.
 Slower to create and manage than user threads and have more
overhead in the kernel.
 If a thread performs a blocking system call, the kernel can schedule
another thread in the application for execution.
 In a parallel environment, the kernel can schedule threads on different
processors.
 Examples – virtually all general-purpose OS’s, including Windows,
Solaris, Linux, Mac OS X

Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
Multithreading Models

Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
MT Models
 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Many-to-One MT
 Many user-level threads mapped to
single kernel thread
 One thread blocking causes all to block
 Multiple threads may not run in parallel
on muti-core system because only one
may be in kernel at a time
 Used in systems that do not support
kernel threads
 Few systems currently use this model
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
One-to-One MT
 Each user-level thread maps to a kernel thread

 Creating a user-level thread creates a kernel thread

 More concurrency than many-to-one


 allows a thread to run when another thread makes a blocking
system call
 allows multiple threads to run in parallel on microprocessors

 Examples:
 Windows
 Linux
 Solaris 9 and later

Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Many-to-Many MT
 Allows many user-level threads to be
mapped to many kernel threads

 Allows the operating system to create


a sufficient number of kernel threads

 Addresses the limitations of one-to-


many and one-to-one models

 Examples:
 Solaris prior to version 9
 Windows with the ThreadFiber
package

Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Two-Level MT
 Similar to the many-to-many model, except that it allows a
user thread to be bound to kernel thread

 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

Operating System Concepts – 10th Edition 4.25 Silberschatz, Galvin and Gagne ©2018
MT Models: Summary
 Many-to-one
 The developer can create as many user threads as he wishes
 But the true concurrency is not gained because the kernel can
schedule only one thread at a time

 One-to-one
 Greater concurrency than many-to-one
 But the developer has to be careful (and in some instances be
limited) not to create too many threads within an application

 Many-to-many
 Does not suffer from the above shortcomings
 Developers can create as many user threads as necessary, and the
corresponding kernel threads can run in parallel on a multiprocessor

Operating System Concepts – 10th Edition 4.26 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4

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

You might also like