Lecture 4: Threads (线程)  Objectives  To introduce the notion of ...

burgerraraSoftware and s/w Development

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

161 views

Lecture 4: Threads (
线程
)



Objectives



To introduce the notion of a thread


a fundamental unit of CPU utilization that
forms the basis of multithreaded computer systems.



To discuss the APIs for Pthreads, Win32 and Java thread libraries.

Overview



A thread is
a basic unit of CPU utilization which comprises a thread ID, a program counter,
a register set and a stack.

Heavyweight process with a single thread of control and a process with multiple threads of
control


Overview: Motivation



Examples of multithreaded
program



web browser



one thread displays images or text



one thread retrieves data from network



word processor



one thread displays graphics



one thread responds to keystrokes from user



one thread performs spelling and grammar checking



Benefits



Responsiveness



Resource Sharing



Economy



Utilization of Multiprocessor Architectures

Multithreading Models



Two categories of threads



user thread provided at user level



kernel thread provided by OS



Relationship between user threads and kernel threads



Many
-
to
-
One Model



One
-
to
-
One Model



Many
-
to
-
Many Model

Multithreading Models: Many
-
to
-
One Model



Many user
-
level threads mapped to one kernel thread.



Multiple threads are unable to run in parallel on multiprocessors.



Example: Green threads


a thread library for Solaris.


Multit
hreading Models: One
-
to
-
One Model



Each user
-
level thread maps to a kernel thread.



More concurrency and parallel than Many
-
to
-
One model.



Drawback: creating a user thread requires creating corresponding kernel thread.



Examples: Linux, Windows 95/98/NT/2000/X
P.


Multithreading Models: Many
-
to
-
Many Model



Multiplexes many user
-
level threads to a smaller or equal number of kernel threads.



Developers can create as many user threads as necessary, and the corresponding kernel
threads can run in parallel on multipro
cessor.



Examples: IRIX, HP
-
UX.


Thread Libraries



A thread library provides the programmer an API for creating and managing threads.



Provide a library entirely in user space with no kernel support (local function
call)



Implement a kernel
-
level library supp
orted directly by OS (system call)



Three main thread libraries:



POSIX Pthreads (user
-
/kernel
-
level library)



Win32 (kernel
-
level library)



Java (a library based on other thread library)

Pthread
s



A POSIX standard (IEEE 1003.1c) API for thread creation and syn
chronization.



Pthreads is a specification for thread behavior, not an implementation. The
implementation is up to the specific OS.



Common in UNIX operating systems.



A multithreaded C program using the Pthreads API. (Figure 4.6)

Win32 Threads



The technique
for creating threads using Win32 thread library is similar to Pthreads.



A multithreaded C program using the Win32 API. (Figure 4.7)

Java Threads



Java threads can be created in two techniques.



Create a new class derived from Thread class and override run()
method.



Define a class implementing the Runnable interface.



Java threads are managed by the JVM.



Java threads are mapped to the underlying host operating system.



An example using Java threads. (Figure 4.8)

Threading Issues



The fork() and exec() system call
s



Two versions of fork() called by one thread



To duplicate all the threads



To duplicate the calling thread only



No change for exec().



Cancellation.



Thread cancellation is the task of terminating a thread before it has completed.



Asynchronous cancellation:
one thread immediately terminates the target
thread



Deferred cancellation: the target thread can periodically check whether it should
terminate, allowing the target thread an opportunity to terminate itself in an
orderly fashion.

Threading Issues



Signal H
andling



Signal is generated by the occurrence of a particular event. A generated signal is
delivered to a process. Once delivered, the signal must be handled.



Signal handling: default or user
-
defined signal handler.



Where should a signal be delivered in m
ultithreaded programs?



To the thread to which the signal applies



To every thread in the process



To certain threads in the process

Assign a specific thread to receive all signals for the process.

Threading Issues



Thread Pools



Thread pool: To create a number

of threads at the process startup and place
them into a pool, where they sit and wait for work.



Servicing a request with an existing thread is usually faster than waiting to
create a thread.



A thread pool limits the number of threads that exist at any one

point.



Thread
-
Specific Data



Specific data needed by each thread.



Win32, Pthreads, Java supports thread
-
specific data.



Scheduler Activations



Scheme for communication between user
-
thread library and kernel.



Kernel provides an application with a set of virtu
al processors (lightweight
process, LWP), and the application can schedule user threads onto an available
virtual processor.

Windows XP Threads



Win32 API implements the one
-
to
-
one mapping.



Fiber library supports many
-
to
-
many model.



Each thread contains gen
eral components



a thread id



a register set



separate user and kernel stacks



private data storage area



Each thread includes three primary data structures (Figure 4.10)



ETHREAD (executive thread block)



KTHREAD (kernel thread block)



TEB (thread environment blo
ck)

Linux Threads



Thread creation is done through clone() system call.



Linux generally uses the term task rather than process or thread.



clone() allows a child task to share a set of flags

(CLONE_FS, CLONE_VM,
CLONE_SIGHAND, CLONE_FILES) with its parent task.



fork() allows a child task to have a copy of all the associated data structures of its parent
task.