Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Threads
Threads
Eike Ritter1
Modified: October 16, 2012
Lecture 8: Operating Systems with C/C++School of Computer Science, University of Birmingham, UK
1Based on material by Matt Smart and Nick Blundell
Threads
Outline1 Multithreaded Processes
OverviewConcurrent and Parallel ExecutionUser and Kernel Threads
2 Threading ModelsThreading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model
3 Some Considerations of ThreadsSemantics of fork()Thread CancellationSignal HandlingThread Pools
4 Threading ImplementationThread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads
Threads
Multithreaded Processes
Overview
Single and Multithreaded Processes
A thread is an execution state of a process (e.g. the nextinstruction to execute, the values of CPU registers, thestack to hold local variables, etc.)
Thread state is separate from global process state, such asthe code, open files, global variables (on the heap), etc.
Threads
Multithreaded Processes
Overview
Benefits of Threads
Responsiveness - user interaction in a GUI can beresponded to by a separate thread from that, say, doinglong running computation (e.g. saving a file, running somealgorithm, etc.)Resource Sharing - Threads within a certain processshare its address space and can therefore use sharedvariables to communicate, which is more efficient thanpassing messages.Economy - Threads are often referred to as light-weightprocesses, since running a system with multiple threadshas a smaller memory footprint than the equivalent withmultiple processes.Scalability - For multi-threaded processes it is mucheasier to make use of parallel processing (e.g. multi-coreprocessors, and distributed systems)Reduce programming complexity - Since problems canbe broken down into parallel tasks, rather than morecomplex state machines.
Threads
Multithreaded Processes
Overview
Multithreaded Server Architecture
Threads
Multithreaded Processes
Overview
Multicore Programming
Multicore systems are putting pressure on programmers,with challenges that include:
Dividing activities - How can we make better use of parallelprocessing?Balance - How should we balance the parallel tasks on theavailable cores to get maximum efficiency?Data splitting - How can data sets be split for processing inparallel and then rejoined (e.g. SETI@home)Data dependency - Some processing must be performed ina certain order, so synchronisation of tasks will benecessary.How to test and debug such systems?
Threads
Multithreaded Processes
Concurrent and Parallel Execution
Concurrent and Parallel Execution
Single-core Concurrent Thread Execution
Multicore Parallel Thread Execution
Threads
Multithreaded Processes
User and Kernel Threads
User Threads
Thread management done by user-level threads libraryThree primary thread libraries:
POSIX PthreadsWin32 threadsJava threads
Threads
Multithreaded Processes
User and Kernel Threads
Kernel Threads
Threading is supported by modern OS KernelsExamples:
Windows XP/2000SolarisLinuxMac OS X
Threads
Threading Models
Threading Models
Threading Models
A particular kernel (e.g. on an embedded device, or anolder operating system) may not support multi-threadedprocesses, though it is still possible to implement threadingin the user process.Therefore many threading models exist for mapping userthreads to kernel threads:
Many-to-OneOne-to-OneMany-to-Many
Threads
Threading Models
Many-to-One
Many-to-One
Many user-level threads mapped to single kernelthread/processUseful if the kernel does not support threadsBut what if one user thread calls a blocking kernelfunction?
This will block the whole process (i.e. all the other userthreads)Complex solutions exist where the user-mode threadpackage intercepts blocking calls, changes them tonon-blocking and then implements a user-mode blockingmechanism.
Threads
Threading Models
Many-to-One
Many-to-One
You could implement something like this yourself, byhaving a process respond to timer events that cause it toperform a context switch in user space (e.g. store currentregisters, CPU flags, instruction pointer, then loadpreviously stored ones)
Since most high-level languages cannot manipulateregisters directly, you would have to write a small amount ofassembler code to make the switch.
Examples:
Solaris Green Threads: http://bit.ly/qYnKAQGNU Portable Threads: http://www.gnu.org/software/pth/
Threads
Threading Models
Many-to-One
Many-to-One
Threads
Threading Models
One-to-One
One-to-One
Each user-level thread maps to kernel threadBut, to switch between threads a context switch is requiredby the kernel.Also, the number of kernel threads may be limited by theOSExamples:
Windows NT/XP/2000LinuxSolaris 9 and later
Threads
Threading Models
One-to-One
One-to-One
Threads
Threading Models
Many-to-Many
Many-to-Many
Allows many user level threads to be mapped to manykernel threads
Best of both worlds
Allows the operating system to create a sufficient numberof kernel threadsSolaris prior to version 9Windows NT/2000 with the ThreadFiber package
Threads
Threading Models
Many-to-Many
Many-to-Many
Threads
Threading Models
Two-Level Model
Two-Level Model
Similar to many-to-many, except that it allows a userthread optionally to be bound directly to a kernel threadExamples:
IRIXHP-UXTru64 UNIXSolaris 8 and earlier
Threads
Threading Models
Two-Level Model
Two-Level Model
Threads
Some Considerations of Threads
Semantics of fork()
Unclear Semantics of UNIX fork() system call
Does fork() duplicate only the calling thread or allthreads?Sometimes we want this, and sometimes we don’t, sosome UNIX systems provide alternative fork functions.
Threads
Some Considerations of Threads
Thread Cancellation
Thread Cancellation
How to terminate a thread before it has finished?Two general approaches use by programmers:
Asynchronous cancellation terminates the target threadimmediately
Useful as a last resort if a thread will not stop (e.g. due to abug, etc.)
Deferred cancellation allows the target thread toperiodically check if it should be cancelled
This approach is often considered to be much cleaner, sincethe thread can perform any clean-up processing (e.g. closefiles, update some state, etc.)
Threads
Some Considerations of Threads
Signal Handling
Signal Handling
Signals are used in UNIX systems to notify a process thata particular event has occurredA signal handler is used to process signals
Signal is generated by particular eventSignal is delivered to a processSignal is handled by some function
Not so straightforward for a multi-threaded process.Options are:
Deliver the signal to the thread to which the signal appliesDeliver the signal to every thread in the processDeliver the signal to certain threads in the processAssign a specific thread to receive all signals for theprocess
In most UNIX systems a thread can be configured toreceive or block (i.e. not handle) certain signals to helpovercome these issues.
Threads
Some Considerations of Threads
Thread Pools
Thread Pools
Under a high request-load, multithreaded servers canwaste a lot processing time simply creating and destroyingthreads.Solution:
Pre-create a number of threads in a pool, where they awaitwork
Advantages:
Usually slightly faster to service a request with an existingthread than create a new threadAllows the number of threads in the application(s) to bebound to the size of the pool, to ensure some level ofservice for a finite number of clients.
Threads
Threading Implementation
Thread Libraries
Thread Libraries
Thread library provides programmer with API for creatingand managing threadsTwo primary ways of implementing
Library entirely in user spaceKernel-level library supported by the OS
Threads
Threading Implementation
Pthreads
Pthreads
May be provided either as user-level or kernel-levelA POSIX standard (IEEE 1003.1c) API for thread creationand synchronizationAPI specifies behaviour of the thread library,implementation is up to development of the libraryCommon in UNIX operating systems (Solaris, Linux, MacOS X)Example: threadtest.c. Note, this is an implementationof POSIX Pthreads, so compiles differently!
Threads
Threading Implementation
Windows XP Threads
Windows XP Threads
Implements the one-to-one mapping (i.e. kernel-levelthreads)Each thread contains
A thread idRegister setSeparate user and kernel stacksPrivate data storage area
The register set, stacks, and private storage area areknown as the context of the threadsThe primary data structures of a thread include:
ETHREAD (executive thread block) - Stores general infoabout a thread: its parent process, address of theinstruction where the thread starts execution.KTHREAD (kernel thread block) - Stores kernel-level state ofthe thread: kernel stack, etc.TEB (thread environment block) - Stores user-level state ofthe thread: user stack, thread-local storage.
Threads
Threading Implementation
Windows XP Threads
Windows XP Threads
Threads
Threading Implementation
Linux Threads
Linux Threads
Linux refers to them as tasks rather than threadsThread creation is done through clone() system callclone() allows a child task to share the address space ofthe parent task (process) and can be passed flags tocontrol exactly what resources are shared.
Threads
Threading Implementation
Java Threads
Java Threads
Java threads are managed by the JVMTypically implemented using the threads model provided byunderlying OSJava threads may be created by:
Extending Thread classImplementing the Runnable interface
Threads
Threading Implementation
Java Threads
Summary
l
We looked at:
1 Multithreaded ProcessesOverviewConcurrent and Parallel ExecutionUser and Kernel Threads
2 Threading ModelsThreading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model
3 Some Considerations of ThreadsSemantics of fork()Thread CancellationSignal HandlingThread Pools
4 Threading ImplementationThread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads
Next time:Process synchronisation