Chapter2-Process Managment(Threads)-partII
Chapter2-Process Managment(Threads)-partII
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
g
in
ra
lex
lle
tip
Exl
ul
Thread #2
ec
m
uti
n
Thread #1
on
io
Thread #3
s
ivi
eD
Tim
a)
Thread execution Vs Timeline. b)
a) Uniprocessor system
b)multiprocessor system
Sem II,2011-IT3106 for 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 for 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.
Threaded applications often run faster than non-threaded
applications (as context-switches between kernel and user-
space are avoided).
Sem II,2011-IT3106 for groups G5-G8 8
Examples of benefits of threads
(a) return_page(&page);
}
(b)
h re ad ed
t
Multi ecture
t
archi
Sem II,2011-IT3106 for groups G5-G8 13
Combinations of Threads and Processes
Advantages Inconveniences
Thread switching does Most system calls are
not involve the kernel: blocking and the kernel
no mode switching. blocks processes. So all
Scheduling can be threads within the process
application specific: will be blocked.
choose the best The kernel can only assign
The following are some of the OSs which support kernel-Level threads:
Windows XP/2000 Tru64 UNIX
Solaris Mac OS X
Linux
Sem II,2011-IT3106 for groups G5-G8 22
Advantages and inconveniences of KLT
Advantages Inconveniences
The kernel can thread switching
simultaneously schedule
within the same
many threads of the same
process on many
process involves the
processors. kernel. We have 2
blocking is done on a mode switches per
thread level. thread switch.
kernel routines can be This results in a
multithreaded. significant slow
Sem II,2011-IT3106 for groups G5-G8 23
down.
3) Hybrid ULT/KLT Approaches
• Thread creation done in the user
space.
• Bulk of scheduling and
synchronization of threads done
in the user space.
• The programmer may adjust the
number of KLTs.
• May combine the best of both
approaches.
• Example is Solaris prior to
version 9.
Sem II,2011-IT3106 for groups G5-G8
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Two-level Model
Thread Pools
Create a number of threads in a pool where they await work.
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)