ch4_Threads

errorhandleΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

108 εμφανίσεις

Multithreaded Programing

Outline



Overview of threads


Threads


Multithreaded Models


Many
-
to
-
One


One
-
to
-
One


Many
-
to
-
Many


Thread Libraries


Pthread


Win32


Java


Threading Issues


The fork() & exec() system call


Cancellation


Signal Handling


Thread Pools


Thread
-
Specific Data


Scheduler Activations

Threads
--
an overview


Process can contain multiple threads of control


APIs for Pthreads, Win32 and Java thread libraries


Support of threads in WinXP and Linux


Thread: A basic unit of CPU utilization. ThreadID,
program counter, register set and a stack


It shares its code and data sections with the
other thread of the same process.


A process can perform more than one task at a
time if a process has multiple threads.

Why Threads
(contd..)


A web browser might have one thread to display
images or text while another retrieves data from
the network.


Word processor and Web server


Operating system kernels are multithreaded


A set of threads in Solaris kernel manage interrupt
handling


A kernel thread in Linux manages amount of free
memory

Threads
(contd)


Threads (Benefits)


Responsiveness


Program can continue even if part of it is blocked or is
performing lengthy operation.


Web browser can still allow user interaction in one
thread during an image load thread


Resource sharing


Several different threads of activity within the same
address space
--
share the code and data section


Economy


Costly Memory and resource allocation for process


Comparatively time consuming to create and manage
processes than threads


Solaris: creating a process 30 times slower


Context switching 5 times slower


Utilization of multiprocessor architecture

Multithreading Models



User Threads: managed without kernel support


Kernel threads: managed directly by OS


Relationship b/w user and kernel threads


Many
-
to
-
one model


One
-
to
-
One model


Many
-
to
-
Many model


Multithreading Models

Many
-
to
-
One


Thread management by thread library in
user space


Entire process will block if a thread
makes a blocking system call


One thread can access the kernel at a
time


Multiple threads cannot run in parallel on
multiprocessors.


Developer can create as many threads
as he/she wishes


Examples


Solaris

Green threads (uses this model)


GNU Portable threads


Multithreading Models

One
-
to
-
One


Maps each user thread to a kernel thread
--

More concurrency


Multiple threads can run in parallel on multiprocessors


Drawbacks?


Crating a user thread requires creating a kernel thread


Overhead of creating kernel thread

Performance


Implementation of this model may limit the number of supported threads


Examples:


Linux, Windows




Multithreading Models

Many
-
to
-
Many


This model multiplexes many user
-
level
threads to a smaller or equal number of
kernel threads.


Developer can create as many user
threads as necessary

corresponding
kernel thread can run in parallel on a
multiprocessor


Examples:


Solaris 9


Outline



Overview of threads


Threads


Multithreaded Models


Many
-
to
-
One


One
-
to
-
One


Many
-
to
-
Many


Thread Libraries


Pthread


Win32


Java


Threading Issues


The fork() & exec() system call


Cancellation


Signal Handling


Thread Pools


Thread
-
Specific Data


Scheduler Activations

Thread Libraries


provides an API for creating & managing threads


Two ways of implementing thread libraries


Provide a library entirely in user space with no kernel support


All code & data structures exist in user space


Invoking a function in the library results in a local function call in
user space

not system call


Implement a kernel
-
level library supported directly by the OS


Code & data structures for library exist in kernel space.


Invoking a function in the API for library results in a system call to
the kernel


Three main thread libraries


POSIX Pthread, Win32 and Java

Thread Libraries
(contd..)


Pthread Library


Thread extension of the POSIX standard

provided
as either a user or kernel level library


Win 32 Library


Kernel level library for windows


Java thread API


thread creation and management in Java program


Pthreads


POSIX standard (IEEE1003.1c)

API for thread
creation and synchronization


Just a specification of thread behavior

not an
implementation


OS designers may implement it as they wish.


Solaris, Linux, MacOSX, Tr64 UNIX


A multithreaded program for summation:


sum =
n

i=0
i


Win32 Threads


Similar technique

windows.h


Threads creation by
CreateThread
()


Pthread_join
() =
WaitForSingleObject
()


Java Threads


Fundamental model of program execution in a java
prog
.


Rich set of features

creation & management of threads


Every Java program has at least one thread+


Create a thread

start()


Pthread_join
()
=
join()

Threading Issues


If one thread in a program calls fork(), does the new
process duplicate al threads, or the new process single
-
threaded?


Two versions


Thread Cancellation:
searching database, web browser


Target Thread

thread to be cancelled:
Two scenarios



Asynchronous Cancellation: One thread immediately terminates
the target thread


Deferred Cancellation: Target thread periodically checks whether it
should terminate

termination in orderly fashion.


If resources have been allocated to cancelled thread?


A thread is cancelled while in the midst of updating data?


Cancellation points


Threading Issues
(contd)


Consider multithreading in web server


Problems?


Time required to create a thread prior to servicing a request


Thread will be discarded once it has completed its work


No bound in the number of threads concurrently active in the
system


Solution ?


Thread Pool: Threads sit and wait for work


Benefits?


Servicing a request is comparatively faster


Thread pool limit the number of threads that exist at any point


Thread pool architectures can dynamically adjust the # of threads in
the pool according to usage patterns


Smaller pool
--
larger benefit

less memory consumption



Threading Issues
(contd)


Thread Specific Data:


Threads share the data of the process


Thread might need its own copy of certain data


TSD support in Win32, Pthreads & Java

K

LWP

Outline



Overview of threads


Threads


Multithreaded Models


Many
-
to
-
One


One
-
to
-
One


Many
-
to
-
Many


Thread Libraries


Pthread


Win32


Java


Threading Issues


The fork() & exec() system call


Cancellation


Signal Handling


Thread Pools


Thread
-
Specific Data


Scheduler Activations


Operating
-
System Examples