Chapter 4: Threads
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
■ 1.1 Overview
■ 1.2 Multicore Programming
OBJECTIVES
■ 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
Thread
• Thread: a basic unit of CPU utilization;
• It composes of:
» Program counter
» Set of registers
» Stack
• A thread has its own thread ID.
• Thread shares the following items with other threads that are
created in the same process:
» code section
» data section
» Other operating system resources
4.1.1 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 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 4.4
• Multiple actions executing simultaneously
Processes with
a) Heavyweight process (traditional process) multiple
• Owns the resources subprocesses
• Passive element
b) Lightweight process (Thread)
Proces with
• Uses CPU and scheduled for execution multiple threads
• Active element
c) Multithreaded applications programs
• Contain several threads running at one time
• Same or different priorities
• Examples:
– Web browsers and time-sharing systems
Components of Threads vs. Process
• Items shared by all threads in a process
• Items private to each thread
Single and Multithreaded Processes
Figure 4.1
Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture
Figure 4.2
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Applications
• Use by many software packages that run on modern desktop
PCs.
Word Processor a thread for displaying graphic, another
thread for reading keystrokes
Web browser one thread displays image or text, while
another thread retrieves data from the
network
Web server when server receives a request, a thread
will be created to service that request
4.1.2 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 multiprocessor
architectures.
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
4.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
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)
■ There is a fine but clear distinction between concurrency
and parallelism.
■ A concurrent system supports more than one task by
allowing all the tasks to make progress.
■ In contrast, a system is parallel if it can perform more
than one task simultaneously.
■ Thus, it is possible to have concurrency without
parallelism
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)
■ 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
■ As number of threads grows, so does architectural
support for threading
CPUs have cores as well as hardware threads
Consider Oracle SPARC T4 with 8 cores, and 8
hardware threads per core
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
■ 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.14 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
■ Concurrent execution on single-core system:
■ Parallelism on a multi-core system:
Figure 4.4
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Concurrency
• In a uniprocessor multiprogramming system:
– Execution of processes / threads are interleaved
in time to yield the appearance of simultaneous
execution;
– Known as concurrent processes / threads
execution;
– Note that actual parallel processing does not
occur;
Interleavin
g
P1
P2
P3
Tim
e
• Multiprocessor system:
– Overlapping and interleaving of
processes execution;
– Parallel processing sometime
occurs;
P1
Chapter 3
P2
P3
• Concurrency arises / occurs in three different contexts*:
Multiple • Multiprogramming was invented to
applications allow processing time to be
dynamically shared among a
number of active applications.
Structured • Application can be a set of concurrent
application processes.
Operating-system • Operating system is a set of
structure processes or threads.
• Example: Solaris and Linux
Chapter 3
(MS- DOS – no concurrency)
* Stalling, W. Operating Systems: Internals and Design Principles (5th Edition). Prentice Hall. (2005)
• Interleaving and overlapping execution of
processes can be viewed as examples of
concurrent processing;
Need control synchronization and data access
synchronization (Chapter 6).
• Both present the same problems such as in sharing
global resources. Example:
– Global variables;
– Shared files or data bases;
Chapter 3
– Managing the allocation of resources optimally such
as the request use of a particular I/O device (may
lead to deadlock);
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.
Concurrent Programming
• Concurrent processing system
– One job uses several processors (multiprocessors)
– Executes sets of instructions in parallel
– Requires programming language and computer
system support
Applications of Concurrent
Programming
A = 3 * B * C + 4 / (D + E) ** (F – G)
Uniprocessor
(Sequential Computation)
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
6 A = T1 + T2
A Uniprocessor
7 (Sequential Computatio n)
Multiprocessor (Concurrent
processing)
Chapter 3 A = 3 * B * C + 4 / (D + E) ** (F – G)
1 2 3
A = 3 * B * C + 4 / (D + E) ** (F – G)
Steps Processor Operation
1 1 A = T1 * C + 4 / (D + E) ** (F-G)
2 A = T1 * C + 4 / T2 ** (F-G)
3 A = T1 * C + 4 / T2 ** T3
2 1 A = T4 + 4 / T2 ** T3
2 A = T4 + 4 / T5
Chapter 3
3 1 A = T4 + T1
Multiprocessor
4 1 A (Concurrent processing)
EXAMPLE:
Screenshot from
MacBook Pro
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013