Professional Documents
Culture Documents
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
■ 1. Overview
■ 2. Multicore Programming
OBJECTIVE
■ To introduce the notion of a thread—a fundamental unit of
CPU utilization that forms the basis of multithreaded computer
systems
Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
1. OVERVIEW
■ Thread: a basic unit of CPU utilization;
■ It composes of:
» Program counter
» Set of registers
» Stack
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©20133
■ 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 Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Process vs Thread
■ Process
Typically independent
Has considerably more state
Separate address space
Interact only through system IPC
■ Thread
Subsets of a process
Multiple threads within a process share process state,
memory, etc
Threads share their address space
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
■ Multiple actions executing simultaneously
a) Heavyweight process (traditional process)
Owns the resources
Processes with
Passive element
multiple
b) Lightweight process (Thread) subprocesses
Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©20136
Process vs Thread
Process Thread
Process are heavy weight operations Threads are lighter weight
operations
Each process has its own memory Threads use the memory of the
space process they belong to
Inter-process communication (IPC) is Inter-thread communication (ITC)
slow as processes have a different can be faster than IPC because
memory addresses threads of the same process share
memory with the process they
belong to
Context switching between Context switching between threads
processes is more expensive of the same process is less
expensive
Processes don’t share memory with Threads share memory with other
other processes threads of the same processes
Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Components of Threads vs. Process
■ Items shared by all threads in a process
■ Items private to each thread
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©20138
Single and Multithreaded Processes
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
■ Threads are scheduled on cores by the OS
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Applications
■ Use by many software packages that run on modern desktop
PCs.
4.12
12
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Benefits
■ Responsiveness – may allow continued execution if part of
process is blocked, especially important for user interfaces.
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
2. MULTICORE PROGRAMMING
■ Multi-CPU systems. Multiple CPUs are placed in the
computer to provide more computing performance.
■ Multicore systems. Multiple computing cores are placed on
a single processing chip where each core appears as a
separate CPU to the operating system
■ Multithreaded programming provides a mechanism for more
efficient use of these multiple computing cores and improved
concurrency.
■ Consider an application with four threads.
On a system with a single computing core, concurrency means
that the execution of the threads will be interleaved over time.
On a system with multiple cores, however, concurrency means
that some threads can run in parallel, because the system can
assign a separate thread to each core
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
A dual core with two cores place on the same chip
Multiprocessors
Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
■ There is a fine but clear distinction between concurrency
and parallelism.
Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Concurrency vs Parallelism
Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
■ 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 – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
■ Multicore or multiprocessor systems are placing pressure
on programmers. Challenges include:
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
■ 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 Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
■ Concurrent execution on single-core system: Interleave
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Concurrency
■ In a uniprocessor multiprogramming system:
4.22
22
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
■ Uniprocessor system:
Interleaving
P1
P2
P3
Time
4.23
23
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
■ Multiprocessor system:
P1
P2
P3
Time
4.24
24
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
■ Concurrency happens in three different contexts*:
__________________________________
* Stalling, W. Operating Systems: Internals and Design Principles (5th Edition). Prentice Hall. (2005)
4.25
25
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
■ Interleaving and overlapping execution of processes can be
viewed as examples of concurrent processing;
Need control synchronization and data access
synchronization (Chapter 6).
4.26
26
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Another Example of Concurrency
■ Two or more threads need access to a shared resource that
must be used by only one thread at a time
Eg. When one thread is writing to a file, a second thread
must be prevented from writing to the file at the same time.
4.27
27
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Concurrent Programming
■ Concurrent processing system
4.28
28
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Applications of Concurrent Programming
A = 3 * B * C + 4 / (D + E) ** (F – G)
Uniprocessor
(Sequential Computation)
4.29
29
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
A = 3 * B * C + 4 / (D + E) ** (F – G)
Steps Operation
1 A = 3 * B * C + 4 / (D + E) ** T1
2 A = 3 * B * C + 4 / T2 ** T1
3 A = 3 * B * C + 4 / T1
4 A = 3 * B * C + T2
5 A = T1 * C + T2 Uniprocessor
A = T1 + T2 (Sequential Computation)
6
7 A
4.30
30
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Multiprocessor
(Concurrent processing)
A = 3 * B * C + 4 / (D + E) ** (F – G)
4.31
31
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Multiprocessor
(Concurrent processing)
1 2 3
A = 3 * B * C + 4 / (D + E) ** (F – G)
4.32
32
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Example
■ Screenshot of
TaskManager from
MacBook Pro
4.33
33
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
4.34
34
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
4.35
35
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013