CHAPTER 4: MULTITHREADED PROGRAMMING

lightnewsSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

116 views

I.-C. Lin, Assistant Professor. Textbook: Operating System
Concepts 8ed
CHAPTER 4:
MULTITHREADED PROGRAMMING
Chapter 4: Multithreaded Programming
Chapter 4: Multithreaded Programming

Overview

Multithreading Models

Thread Libraries

Threading Issues

Operating-System Examples
Single and Multithreaded Processes
Single and Multithreaded Processes
Bfit
B
ene
fit
s

Responsiveness
R Shi

R
esource
Sh
ar
i
ng
E

E
conomy

塞梁說︠ヲ若

塞梁說︠ヲ若
Multithreaded Server Architecture
Concurrent Execution on a Single-core System
Parallel Execution on a MulticoreSystem
User Threads
User Threads
Thd d b
ll hd lib

Th
rea
d
management
d
one
b
y

user-
l
eve
l
t
h
rea
d
s
lib
rary
Th i thd libi

Th
ree

pr
i
mary
th
rea
d lib
rar
i
es:

POSIX Pthreads

︳

︳

Java threads
Kernel Threads
Kernel Threads

Std b th Kl

S
uppor
t
e
d b
y
th
e
K
erne
l





Windows XP/2000

ﱡ

ﱡ

Linux

Tru64 UNIX

Mac OS X
Multithreading Models
Multithreading Models
M
t
O

M
any-
t
o-
O
ne

ョ

ョ

ョ



ョ

ﵡ



ﵡ

ﵡ



ﵡ
Many
-
to
-
One
Many
to
One

ﵡ
ﱥﰠﵡ@﹧ﱥ

ﵡ

ﱥﰠﵡ@﹧ﱥ


E
xamp
l
es:

Solaris Green Threads

ﱥ

ﱥ
Many
-
to
-
One Model
Many
to
One Model
One
-
to
-
One
One
to
One










ﰠ


ﵡ



﹥
ﰠ




E
xamp
l
es

Windows NT/XP/2000

ﱩ﹵

ﱩ﹵

Solaris 9 and later
One
-
to
-
one Model
One
to
one Model
Many
-
to
-
Many Model
Many
to
Many Model

ﱯﵡﱥﰠﵡ@ﵡ

ﱯﵡﱥﰠﵡ@ﵡ


Allows the operating system to create a sufficient number
of kernel threads
of kernel threads

ﱡ說說︠

ﱡ說說︠
若
ThdFib
k

Wi
n
d
ows
NT/2000
w
ith th
e

Th
rea
dFib
e
r
pac
k
age
Many
-
to
-
Many Model
Many
to
Many Model
Two
-
level Model
Two
level Model

ﵩﱡ祉若ﱬ@

ﵩﱡﴺ

若ﱬ@
boundto kernel thread

Examples

勒菱

勒菱

HP-UX

Tru64 UNIX

Solaris 8 and earlier
Two
-
level Model
Two
level Model
Thread Libraries
Thread Libraries

Thread library provides programmer with API for
creating and managing threads

Two primary ways of implementing

ﱩ鸞ﱹ︠

ﱩ鸞ﱹ︠

Kernel-level library supported by the OS
Pthreads
Pthreads
M b idd ih
ll kl

M
ay
b
e

prov
id
e
d
e
i
t
h
er

as

user-
l
eve
l
or
k
erne
l
-
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
implementation is up to development of the
library

ﵭョ︠菱葉ﵳ
ﱡ

ﵭョ︠菱葉

ャ拾


ﱩ﹵
Java Threads
Java Threads

Java threads are managed by the JVM

Java threads are managed by the JVM

Typically implemented using the threads model
provided by underlying OS

鸞

鸞



E
xten
di
ng
Th
rea
d
c
l
ass

Implementing the Runnableinterface
Threading Issues
Threading Issues

Semantics of
fork()
and
exec()
system calls

Semantics of
fork()
and
exec()
system calls

Thread cancellation

Thread cancellation

Signal handling

Signal handling

Thread pools

Thread pools

Thread s
p
ecific data
p

Scheduler activations
Semantics of fork() and exec()
Semantics of fork() and exec()

Does fork()duplicate only the calling thread or all
threads?
Thread Cancellation
Thread Cancellation

Terminating a thread before it has finished

Terminating a thread before it has finished

Two general approaches

Two general approaches
:

Asynchronous cancellationterminates the target thread
immediately

Deferred cancellationallows the target thread to periodically
check if it should be cancelled
Signal Handling
Signal Handling

Signals are used in UNIX systems to notify a process that a

Signals are used in UNIX systems to notify a process that a
particular event has occurred


signal handler
is used to process signals


signal handler
is used to process signals
1.
Signal is generated by particular event
2.
Si
g
nal is delivered to a
p
rocess
gp
3.
Signal is handled

Options:

Deliver the signal to the thread to which the signal applies

Deliver the signal to every thread in the process

Deliver the signal to certain threads in the process

Assign a specific threa to receive all signals for the process
Thread
Pools
Thread
Pools
f

The scenario o
f
a web server.

A separate thread to serve a request.

Thread (Created -> discarded) : Request (start and finish)?

Unlimited requests -> unlimited threads?

Thread pools

若

若


Faster to response a request.

The number of threads can be d
y
namicall
y
ad
j
usted.
yyj
Thread Pools
Thread Pools

Create a number of threads in a pool where they await
work

Advantages:

Usually slightly faster to service a request with an existing thread
than create a new thread

ﱯヲ葉ﱩョ鸞

ﱯヲ葉ﱩョ鸞
贈ッ
Thread Specific Data
Thread Specific Data
All h hd h i f d

All
ows

eac
h
t
h
rea
d
to
h
ave
i
ts

own

copy

o
f d
ata

Useful when you do not have control over the thread
creation process (i.e., when using a thread pool)
Scheduler Activations
Scheduler Activations

Both M:M and Two-level models require communication to
maintain the appropriate number of kernel threads allocated
to the application
to the application
Shdl titi id
ll
iti

S
c
h
e
d
u
l
er

ac
ti
va
ti
ons

prov
id
e

upca
ll
s-a

commun
i
ca
ti
on

mechanism from the kernel to the thread library

This communication allows an application to maintain the
correct number kernel threads
correct number kernel threads
Operating
-
system Example
Operating
system Example

Explore how threads are implemented in Windows
XP, Linux, Solaris systems.
Pthreads
Pthreads
POSIX dd (IEEE 10031) API f hd i d

a
POSIX
stan
d
ar
d (IEEE 1003
.
1
c
) API f
or

t
h
rea
d
creat
i
on

an
d
synchronization.
API ifi bhi f th thd lib iltti i

API
spec
ifi
es
b
e
h
av
i
or

o
f th
e
th
rea
d lib
rary,
i
mp
l
emen
t
a
ti
on
i
s

up to development of the library.

User-level thread library

Common in UNIX operating systems.

pthread_create(), pthread_exit(), pthread_join()
Solaris 2 threads
Solaris 2 threads
Lihih (LWP)

Li
g
h
twe
i
g
h
t

processes
(LWP
s
)

Between user-and kernel-threads.

Each process contains at least one LWP.


ﱬ








﹥




ﰠ




ﱬ

A b
oun
d
user-
l
eve
l
t
h
rea
d

Permanently attached to an LWP. (quick response time)
A bd thd

A
n

un
b
oun
d th
rea
d

Multiplexed onto the available LWP pool.
Solaris 2 threads
Solaris 2 threads
Solaris 2 threads (cont.)
Solaris 2 threads (cont.)
U
ll hd hdld d ihd h LWP

U
ser-
l
eve
l
t
h
rea
d
are

sc
h
e
d
u
l
e
d
an
d
sw
i
tc
h
e
d
among

t
h
e
LWP
s

by thread library.

The thread library dynamically adjusts the number of LWPs.

Creates another LWP if all LWPs in a
p
rocess are blocked
p

Deletes unused LWPs (about 5 minutes)

User-level thread: thread ID, register set, stack, priority..

LWP: a register set (for its running user-level thread), misc. info.

Kernel thread: stack, kernel registers, a pointer to the LWP,
priority and scheduling info.
Windows XP Threads
Windows XP Threads

Implements the one
to
one mapping

Implements the one
-
to
-
one mapping

Each thread contains

磻

磻

Register set

Se
p
arate user and kernel stacks
p

Private data storage area

The register set, stacks, and private storage area are known
as the context of the threads

The primary data structures of a thread include:

ETHREAD (executive thread block)

KTHREAD (kernel thread block)

TEB (thread environment block)
Windows XP Threads
Windows XP Threads
Linux Threads
Linux Threads

Linux refers to them as
tasks
rather than
threads

Linux refers to them as
tasks
rather than
threads

Thd ti i d thh
l()
t ll

Th
rea
d
crea
ti
on
i
s
d
one
th
roug
h
c
l
one
()
sys
t
em

ca
ll
l()
ll hld k h h dd f h

c
l
one
()
a
ll
ows

a

c
h
i
ld
tas
k
to

s
h
are

t
h
e

a
dd
ress

space

o
f
t
h
e

parent task (process)
END OF CHAPTER 4