Csi 3131 Mod 3 Threads
Csi 3131 Mod 3 Threads
Reading: Chapter 4
Objective:
Understand the concept of the thread and its
relationship to the process.
Study and understand the different methods of
multithreading used by operating systems to
manage and use threads.
Review issues and challenges that threads present.
Review thread library examples and thread
implementation examples
1
Threads
Overview Threading
Examples
Mulithreading
Threading
Models
Issues
2
Threads
Overview Threading
Examples
Mulithreading
Threading
Thread of Models
execution Issues
Single thread
verus multithread
Motivation
3
Process characteristics
Resource ownership – a process owns:
A virtual address space that contains the
image of the process
Other resources (files, I/O devices, etc.)
Execution (scheduling) – a process
executes along a path in one or more
programs.
The execution is interleaved with the
execution of other processes.
The process has an execution state and
priority used for scheduling
4
Process Characteristics
5
Resource Ownership
Process image
Global
Data
7
Threads vs Processes
Process
A unit/thread of execution, together with
code, data and other resources to support
the execution.
Idea
Make distinction between the resources
and the execution threads
Could the same resources support several
threads of execution?
Code, data, .., - yes
CPU registers, stack - no
8
Threads = Lightweight Processes
A thread is a subdivision of a process
A thread of control in the process
Different threads of a process share the
address space and resources of a process.
When a thread modifies a global variable
(non-local), all other threads sees the
modification
An open file by a thread is accessible to
other threads (of the same process).
9
Single vs Multithreaded Processes
10
Example
11
Threads et processus [Stallings]
12
Motivation for Threads
Responsiveness
One thread handles user interaction
Another thread does the background work (i.e. load
web page)
Utilization of multiprocessor architectures
One process/thread can utilize only one CPU
Many threads can execute in parallel on multiple
CPUs
Well, but all this applies to one thread per process
as well – why use threads?
13
Motivation for Threads
Switching between threads is less expensive than
switching between processes
A process owns memory, files, and other resources
Changing from one process to another may involve
dealing with these resources.
Switching between threads in the same process is
much more simple – implies saving the CPU
registers, the stack, and little else.
Given that threads share memory,
Communication between threads within the same
process is much more efficient that between
processes
The creation and termination of threads in an
existing process is also much less time
consuming than for new processes
In Solaris, creation of a thread takes about 30 times
less time than creating a process
14
Threads
Overview Threading
Examples
Mulithreading
Threading
Thread of Models
execution Issues
User Thread
Kernel Thread
Single thread
verus multithread Many to One
Model
One to One
Model
15
Kernel Threads and User Threads
Where and how to implement threads:
In user libraries
• Controlled at the level of the user program
• POSIX Pthreads, Java threads, Win32 threads
In the OS kernel
• Threads are managed by the kernel
• Windows XP/2000, Solaris, Linux, True64 UNIX,
Mac OS X
Mixed solutions
• Both the kernel and user libraries are involved
in managing threads
16 • Solaris 2, Windows 2000/NT
User and Kernel Threads
User threads: supported with the use of user libraries
or programming language
Efficient since operations on threads do not involve
system calls
Disadvantage: the kernel cannot distinguish between
the state of the process and the state of the threads in
the process
• Thus a blocking thread blocs the whole process
17
Multithreading Models
18
Many to one model
All code and data
structures for thread
management in user
space
No system calls
involved, no OS
support needed
Many (all) user
threads mapped to
single kernel thread
Kernel thread gets
19
allocated to the CPU
Many to One Model
Properties:
Cheap/fast, but runs as one process to the
OS scheduler
What happens if one thread blocks on an I/O?
• All other threads block as well
How to make use of multiple CPUs?
• Not possible
Examples
Solaris Green Threads
GNU Portable Threads
20
One to One Model: the kernel controls threads
Code and data structures in kernel space
Calling a thread library typically results in
system call
Each user thread is mapped to a kernel
thread
21
One to One Model
Properties
Usually, limited number of threads
Thread management relatively costly
But provides better concurrency of
threads
Examples
Windows NT/XP/2000
Linux
Solaris Version 9 and later
22
Many-to-Many Model
Allows many user level threads
to be mapped to many kernel
threads
The thread library cooperates
with the OS to dynamically
map user threads to kernel
threads
Intermediate costs and most of
the benefits of multithreading
If a user thread blocs, its
kernel thread can be
associated to another user
thread Examples:
If more than one CPU is
Solaris prior to version 9
available, multiple kernel
threads can be run Windows NT/2000 with the
concurrently ThreadFiber package
23
Many to many Model: Two-level Model
Similar to M:M, except that it allows a user
thread to be bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and
earlier
24
Threads
Overview Threading
Examples
Mulithreading
Threading
Thread of Models
execution Issues
User Thread
Kernel Thread
C
Single thread Te rea
Many to One rm tion
verus multithread in a Signal
Model at nd Handling
Fork/exec io
n
One to One Pools
Model Cancellation
Thread
Motivation Many to Many Specific
Model Data
Scheduler
Activations
25
Threading Issues – Creation/Termination
Semantics of fork() and exec()
Does fork() duplicate only the calling thread
or all threads?
Often two versions provided
Which one to use?
26
Threading Issues – Creation/Termination
Thread Cancellation
Terminating a thread before it has finished
Two general approaches:
Asynchronous cancellation terminates the
target thread immediately
• Might leave the shared data in corrupt state
• Some resources may not be freed
Deferred cancellation
• Set a flag which the target thread periodically
checks to see if it should be cancelled
• Allows graceful termination
27
Threading Issues – Creation/Termination
Thread Pools
A server process might service requests by
creating a thread for each request that needs
servicing
But thread creation costs are wasted time
No control over the number of threads, possibly
overwhelming the system
Solution
Create a number of threads in a pool where they
await work
Advantages:
• The overhead for creating threads is paid only at the
beginning
• Allows the number of threads in the application(s) to
28
be bound to the size of the pool
Threading Issues - Signal Handling
Signals are used in UNIX systems to notify a
process that a particular event has occurred
Essentially software interrupt
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
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 thread to receive all signals for
29 the process
Threading Issues - Thread Specific Data
30
Threading Issues - Scheduler Activations
The many to many models (including two
level) require communication from the
kernel to inform the thread library when a
user thread is about to block, and when it
again becomes ready for execution
When such event occurs, the kernel makes
an upcall to the thread library
The thread library’s upcall handler handles
the event (i.e. save the user thread’s state
and mark it as blocked)
31
Process 2 is using a many to one model (user level threads) Stallings
Process 4 is using one to one model (kernel level threads)
Level of parallelism can be adjusted for process 3 and process 5 (many to
32 many model)
Threads
Overview Threading
Examples
Mulithreading Li
Threading br
Thread of Models ar
execution Issues ie
s
User Thread
PThreads
Kernel Thread
C
Single thread Te rea
Many to One rm tion
verus multithread in a Signal
Model at nd Win32
io Handling
Fork/exec n
One to One Java
Pools
Model Cancellation
Thread
Motivation Many to Many Specific
Model Data
Scheduler
Activations
33
Pthreads
A POSIX standard (IEEE 1003.1c) API for thread
creation and synchronization
API specifies behavior of the thread library,
implementation is up to the developer of the
library
Common in UNIX operating systems (Solaris,
Linux, Mac OS X)
Typical functions:
pthread_create (&threadid, attr, start_routine, arg)
pthread_exit (status)
pthread_join (threadid, status)
pthread_attr_init (attr)
34
35
Thread Programming Exercise
Goal: Write multithreaded matrix multiplication algorithm, in order to
make use of several CPUs.
36
Multithreaded Matrix Multiplication
Idea:
• create 6 threads
• have each thread compute 1/6 of the matrix C
• wait until everybody finished
• the matrix can be used now
Thread 0
Thread 1
Thread 2
Thread 3
Thread 4
Thread 5
37
Let’s go!
pthread_t tid[6];
pthread_attr_t attr;
int i;
pthread_init_attr(&attr);
for(i=0; i<6; i++) /* create the working threads */
pthread_create( &tid[i], &attr, worker, &i);
38
Let’s go!
void *worker(void *param)
{
int i,j,k;
int id = *((int *) param); /* take param to be
pointer to integer */
int low = id*n/6;
int high = (id+1)*n/6;
int id[6];
.
.
for(i=0; i<6; i++) /* create the working threads */
{
id[i] = i;
pthread_create( &tid[i], &attr, worker, &id[i]);
}
Would it work now?
• should, …
40
Win32 Thread API
// thread creation:
ThreadHandle = CreateThread(
NULL, // default security attributes
0, // default stack size
Summation, // function to execute
&Param, // parameter to thread function
0, // default creation flags
&ThreadId); // returns the thread ID
if (ThreadHandle != NULL) {
WaitForSingleObject(ThreadHandle, INFINITE);
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
}
41
See Silbershatz for simple example
Java Threads
Java threads are created by calling a start()
method of a class that
extends Thread class, or
implements the Runnable interface:
public interface Runnable
{
public abstract void run();
}
Java threads are inherent and important part of
the Java language, with rich API available
42
Extending the Thread Class
class Worker1 extends Thread
{
public void run() {
System.out.println("I Am a Worker Thread");
}
}
44
Joining Threads
class JoinableWorker implements Runnable
{
public void run() {
System.out.println("Worker working");
}
}
task.start();
try { task.join(); }
catch (InterruptedException ie) { }
System.out.println("Worker done");
45 }
}
Thread Cancellation
. . .
// now interrupt it
thrd.interrupt();
46
Thread Cancellation
public class InterruptibleThread implements Runnable
{
public void run() {
while (true) {
/**
* do some work for awhile
*/
if (Thread.currentThread().isInterrupted()) {
System.out.println("I'm interrupted!");
break;
}
}
/**
* get the error code for this transaction
*/
public static Object getErrorCode() {
return errorCode.get();
48
}
Thread Specific Data
class Worker implements Runnable
{
private static Service provider;
Assume there were different errors in the transactions of both workers, but
both transactions finished before any of the prints in the run() methods started.
50
Windows XP Threads
51
Linux Threads
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system
call
The clone() system call allows to specify which
resources are shared between the child and the
parent
Full sharing threads
Little sharing like fork()
52
Java Threads
Java threads are managed by the JVM
53
Threads
Overview Threading
Examples
Mulithreading
Threading
Li
Thread of Models
br
execution Issues
ar
ie
User Thread
s
PThreads
Kernel Thread
C e rm
re
Single thread
T
at i na
verus multithread Many to One
io ti
Signal
ns
Win32
n on
Model Handling
an
t io
Fork/exec
nta
me
One to One Java
Pools
p le
Model Cancellation
Im
Thread
Motivation Many to Many Specific Windows Linux
Model Data XP
Scheduler
Activations Java
54