Chapter 2 Part II Threads
Chapter 2 Part II Threads
o Overview
o Thread Characteristics and Benefits
o Multithreading Models
o Threading Issues
o Pthreads
o Windows XP Threads
o Linux Threads
o Java Threads
Pa
ra
ll e
lE
xe
Thread
ng n
xi io
cu
#2
le is
Thread
ti
Thread
tip iv
on
#1
ul D
#3
m me
Ti
a)
Thread execution Vs Timeline. b)
a) Uniprocessor system
b)multiprocessor system
Sem II,2011-IT3106 groups G5-G8 6
Thread Characteristics
Like processes each thread has an execution context/state.
Has an execution stack and some per-thread static storage
for local variables.
Has access to the memory address space and resources of
its task:
All threads of a task share the same memory.
When one thread alters a (non-private) memory item, all
other threads (of the task) see that.
A file open with one thread, is available to others.
Threads has three key states: running, ready, blocked
They have no suspend state because all threads within
same task share the same address space.
Indeed: suspending (i.e., swapping) a single thread
involves suspending all threads of the same task.
Termination of a task, terminates all threads within the
task.
Sem II,2011-IT3106 groups G5-G8 7
Benefits of Threads
o Less time to create and terminate a thread than
a process (because we do not need another
address space).
o Less time to switch between two threads than
between processes.
o Inter-thread communication and synchronization
is very fast.
o Since threads within the same process share
memory and files, they can communicate with
each other without invoking the kernel.
Threads allows parallel activity inside a single
address space:
While one server thread is blocked and
waiting, a second thread in the same task can
run. Sem II,2011-IT3106 groups G5-G8 8
Examples showing multi-threading is giving improved performance
over single threading solution
r eaded
ultith ture
M itec
ar c h
Sem II,2011-IT3106 groups G5-G8 13
Combinations of Threads and Processes
Advantages Inconveniences
The kernel can thread switching
simultaneously within the same
schedule many process involves
threads of the same the kernel. We have
process on many
2 mode switches
processors.
per thread switch.
blocking is done on a
This results in a
thread level.
kernel routines can be
significant slow
multithreaded.
down.
Two-level Model
Advantages:
Pthreads
• 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 library
• Common in UNIX operating systems (Solaris, Linux,
Mac OS X)
Sem II,2011-IT3106 groups G5-G8 35
Windows XP Threads
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
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)