Threads
Outlook
Motivation and Threading Models g
Thread Libraries
Th di I
Threading Issues
Motivation and Threading Models
What is a Thread?
Threads Discussed
Benefits
Responsiveness
Resource Sharing esou ce S a g
Economy
Utilization of multiprocessor architectures
Utilization of multiprocessor architectures
Multithreading is used frequently
Multithreading is used frequently
RPC server
RMI system
OS kernel
5
User and Kernel Threads
User thread
Managed without kernel supportg pp
Thread library in user space; no system call
Kernel thread
d b h k l
Managed by the kernel
Thread library in kernel space; API invokes a system call
Main thread libraries
Main thread libraries
POSIX Pthreads (kernel- or user-level library)
Java (uses thread library of the host system)
Win32 (kernel level)
To run on a CPU each user-level thread must be mapped to an associated kernel-level thread!
Relationship between user and kernel level thread
Many-to-one, one-to-one, many-to-many
Many-to-One Model y
Thread library in user space
user space
Efficient
Entire process
blocks on blocking blocks on blocking system call
Multiple threads
can’t run in parallel on multiprocessor on multiprocessor
Green threads
7
One-to-One Model
Another thread may run on blocking system call
Allows threads to run in parallel on multiprocessors
Allows threads to run in parallel on multiprocessors
Overhead of creating kernel threads (e.g. limitations on number of threads)
of threads)
Many-to-Many Model y y
Compromise between the previous two
the previous two extremes
As many user threads as necessary
Corresponding kernel threads can run in parallel on a
parallel on a multiprocessor
On blocking system call the kernel can schedule another schedule another thread
9
Two-Level Model
Thread Libraries
Pthreads (1) ( )
POSIX standard (IEEE 1003.1c) defining an ( ) g API for thread creation and synchronization Specification for thread behavior and not an
Specification for thread behavior and not an implementation
Systems implementing Pthread
specification: Solaris, Linux, Mac OS X, specification: Solaris, Linux, Mac OS X, Tru64 UNIX, Shareware implementations for Windows Systems
for Windows Systems
Pthreads Example p
#include <pthread.h>
/ /
int i; /* global variable */
int main(int argc, char *argv[]) {
pthread t tid; /* the thread id */
p _ ; / /
pthread_attr_t attr; /* thread attributes */
/* e.g. stack size, scheduling info */
pthread_attr_init(&attr); /* set default attributes */
pthread create(&tid &attr pthread_create(&tid, &attr,
runner, argv[1]); /* create thread */
pthread_join(tid, NULL); /* wait for thread to exit */
printf(“sum = %d\n”, i);
}
/* thread function */
void runner(void* param) {( p ) { i = atoi(param);
/* … */
pthread_exit(0);
} }
13
Win32 Threads (quite similar) (q )
#include <windows.h>
DWORD i; /* global variable */
DWORD i; / global variable / int main(int argc, char *argv[]) {
DWORD tid; /* the thread id */
HANDLE handle;
DWORD P 42
DWORD Param = 42;
handle = CreateThread(
NULL, // default security attributes 0, // default stack size
runner, // thread function
&Param, // parameters to be passed 0, // default creation flags
&ThreadId); // returns the thread identifier if(handle != NULL) {
WaitForSingleObject(handle,INFINITE);
WaitForSingleObject(handle,INFINITE);
CloseHandle(handle);
}
printf(“sum = %d\n”, i);
}
/* thread function */
DWORD WINAPI runner(LPVOID param) { i = *(DWORD*)param;
/* … */
Java Threads
Language support for multithreading
Two approaches for creating threads
public class Worker1 extends Thread { public void run() {
public void run() { ...
} } }
Thread runner = new Worker1();
runner.start();
15
Java Threads
public class Worker2 implements Runnable { ()
public void run() { ...
} } }
Thread runner = new Thread(new Worker2());
runner start();
runner.start();
Why two approaches?Why two approaches?
Java does not support multiple inheritance
Object-oriented design principle: unless enhancing Thread, the class should not be extended
class should not be extended
Pthread and Win32 Examples with Java p
class Runner implements Runnable { private int par;
private Result res;
class Result {
private int res;
public void set(int i) { private Result res;
public Summation(int par, Result res){
this.par = par;
this.res = res;
}
public void run() {
public void set(int i) { res = i;
}
public int get{
return res;
public void run() { } int i;
// implementation here // result stored in i res.set(i);
} }
} }
...
i = 42;
Result res = new Result();
Thread thrd = new Thread(new Runner(i,res));
thrd.start();
try { try {
thrd.join();
// use result in object res ...
} catch (InterruptedException e) { ...
} }
17
Threading Issues
fork(), exec(), and clone() (), (), ()
Semantics of fork and exec change in case of multithreading
What happens when a thread calls fork()?
Duplicate all threads
Duplicate the calling thread only
Duplicate the calling thread only
Both variants exist for example in some UNIX systems
What happens if the thread calls exec()?
What happens if the thread calls exec()?
Replace entire process including its threads
Linux: clone() (Linux does not distinguish between processes and
Linux: clone() (Linux does not distinguish between processes and threads; the mode is adjusted by clone)
#include <sched.h>
int clone (int (*fn) (void *), void *child stack, int flags, void *arg);
int clone (int ( fn) (void ), void child_stack, int flags, void arg);
Flags
CLONE_FS : File-system info is shared
CLONE_VM : Same memory space is shared_
CLONE_SIGHAND : Signal handlers are shared
CLONE_FILES : Set of open files are shared
19
Cancellation
The task of terminating a thread before it has completed
Asynchronous cancellation
Immediately terminate the target thread
Immediately terminate the target thread
May not free a system wide resource
Deferred cancellation
Deferred cancellation
The target thread periodically checks whether it should terminate
terminate
Allows a thread to cancel safely Example: Java 1 0 and later
Example: Java 1.0 and later
Java Thread Cancellation
Asynchronous cancellation y
stop() (deprecated)
Deferred cancellation
interrupt()
isInterrupted()
isInterrupted()
21
Java Thread Cancellation: Example p
class InterruptibleThread implements Runnable { public void run() {
p () {
while(true) { ...
if(Thread.currentThread().isInterrupted()) { break;
} }
... // cleanup }
} ...
Th d th d Th d( I t tibl Th d())
Thread thrd = new Thread(new InterruptibleThread());
thrd.start();
...
thrd interrupt();
thrd.interrupt();
Signal Handling g g
A signal notifies a process that a particular event has occurred
Synchronous signal: delivered to the same process that caused the signal (e.g. illegal memory access, division by zero)
Asynchronous signal: generated by an event external to the receiving process (e.g. control C, expired timer)
Where to deliver the signal in a multithreaded scenario?
Synchronous: deliver to the thread causing the signal
Asynchronous: deliver to all (e g control C) thread specifies if it is willing
Asynchronous: deliver to all (e.g. control C), thread specifies if it is willing to receive, or deliver to one specific thread
Examples
Examples
UNIX: kill(aid_t process_id, int signal)
Pthreads: pthread_kill(pthread tid, int signal)
Wi d l t d ith h d ll (APC ) f ti
Windows: emulated with asynchronous procedure calls (APCs); function that is to be called when an event is notified
23
Thread Pools
Potential problems with multithreaded servers
Ti i d f ti d i i
Time required for creating and removing a service thread
Number of simultaneous requests is unboundu be o s u a eous eques s s u bou d
Solution
Initial number of threads reside in a thread pool
Incoming request is assigned a free thread
A thread completing the service is returned to the pool
Requests are queued if no thread is available
Selecting the size of the thread pool
Heuristically
Heuristically
Thread Pool Example p
Win32 API
DWORD WINAPI PoolFunction(AVOID Param) { // code to be executed here
// code to be e ecuted e e }
// API function to let PoolFunction be executed by // API function to let PoolFunction be executed by
a free thread in the thread pool
QueueUserWork(PoolFunction NULL 0);
QueueUserWork(PoolFunction, NULL, 0);
J h l k i t j til t
Java: have a look into java.util.concurrent package
25
Thread Specific Data p
General concept which allows each thread p to have its own copy of data
OOP provides this by class fields
OOP provides this by class fields
Why is such a concept reasonable in Java?
Useful when data has to be stored thread
specific but outside of the thread p
Thread Specific Data: Example p p
class Service {
private static ThreadLocal errorCode = errorCode
new ThreadLocal();
public static void transaction() {
try { Thread 1
...
}
catch(Exception e) { errorCode.set(e);
Thread 2
Thread 3
} }
public static Object getErrorCode() { return errorCode get();
Thread 3
Thread 1 : return errorCode.get();
} }
Thread 1 :
errorCode.set(42) Thread 2 :
errorCode.set(50) ...
public void run() {
Service transaction();
Thread 1 :
errorCode.get() = 42 Thread 2 :
errorCode get() = 50 Service.transaction();
System.out.println(Service.getErrorCode());
} 27
errorCode.get() = 50
Time
Scheduler Activations
Communication between user level thread library and kernel in case of many-to-many library and kernel in case of many to many
mapping UT
Lightweight process (LWP): virtual processor on which the application can schedule a user thread to be executed
LWP
Kernel informs application when a thread is about to block by using an upcall
LWP
about to block by using an upcall
User-level thread library handles upcalls by an upcall handler
Ke nel eates a ne LWP to n the p all handle KT
Kernel creates a new LWP to run the upcall handler
Upcall handler relinquishes the blocked LWP and schedules an unblocked thread on its own LWP Unblocking of a thread is notified by an upcall as
Unblocking of a thread is notified by an upcall as
Summary and References
Summaryy
Difference between thread and process: thread is one (of
ibl l) fl f t l i
possibly several) flow of control in one process
Threads of one process share the same address space User level threads
User level threads
visible to the user; unknown to the kernel
different types of mapping user level thread to a kernel level thread
realized by thread libraries
requires communication with the kernel: lightweight processes and upcalls
Operating system manages kernel level threads
Operating system manages kernel level threads
Multithreading is supported by most of the modern operating systems: e.g. Windows, Solaris, Linux, ...y g do , o a , u ,
Common thread libraries: POSIX Pthreads, Win32 API, Java threads
References
Silberschatz, Galvin, Gagne, „Operating , , g , „ p g
System Concepts“, Seventh Edition, Wiley, 2005
2005
Chapter 4 „Threads“
31