27
Multithreaded Processes Threading Models Some Considerations of Threads Threading Implementation The Critical-Section Problem Synchronisation Hardware Overview Concurrent and Parallel Execution User and Kernel Threads Single and Multithreaded Processes A thread is an execution state of a process (e.g. the next instruction to execute, the values of CPU registers, the stack to hold local variables, etc.) Thread state is separate from global process state, such as the code, open files, global variables (on the heap), etc. Eike Ritter Operating Systems

Multithreaded Processes Threading Implementation The ... · Threading Models Some Considerations of Threads Threading Implementation The Critical-Section Problem Synchronisation Hardware

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Single and Multithreaded Processes

    A thread is an execution state of a process (e.g. the nextinstruction to execute, the values of CPU registers, the stackto hold local variables, etc.)

    Thread state is separate from global process state, such as thecode, open files, global variables (on the heap), etc.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Benefits of Threads

    Responsiveness - user interaction in a GUI can be respondedto by a separate thread from that, say, doing long runningcomputation (e.g. saving a file, running some algorithm, etc.)Resource Sharing - Threads within a certain process shareits address space and can therefore use shared variables tocommunicate, which is more efficient than passing messages.Economy - Threads are often referred to as light-weightprocesses, since running a system with multiple threads has asmaller memory footprint than the equivalent with multipleprocesses.Scalability - For multi-threaded processes it is much easier tomake use of parallel processing (e.g. multi-core processors,and distributed systems)Reduce programming complexity - Since problems can bebroken down into parallel tasks, rather than more complexstate machines.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Multithreaded Server Architecture

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Multicore Programming

    Multicore systems are putting pressure on programmers, withchallenges 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 in acertain order, so synchronisation of tasks will be necessary.How to test and debug such systems?

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Concurrent and Parallel Execution

    Single-core Concurrent Thread Execution

    Multicore Parallel Thread Execution

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    User Threads

    Thread management done by user-level threads libraryThree primary thread libraries:

    POSIX PthreadsWin32 threadsJava threads

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    OverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Kernel Threads

    Threading is supported by modern OS KernelsExamples:

    Windows XP/2000SolarisLinuxMac OS X

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Threading Models

    A particular kernel (e.g. on an embedded device, or an olderoperating system) may not support multi-threaded processes,though it is still possible to implement threading in the userprocess.Therefore many threading models exist for mapping userthreads to kernel threads:

    Many-to-OneOne-to-OneMany-to-Many

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    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 kernel function?

    This will block the whole process (i.e. all the other userthreads)Complex solutions exist where the user-mode thread packageintercepts blocking calls, changes them to non-blocking andthen implements a user-mode blocking mechanism.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Many-to-One

    You could implement something like this yourself, by having aprocess respond to timer events that cause it to perform acontext switch in user space (e.g. store current registers, CPUflags, instruction pointer, then load previously stored ones)

    Since most high-level languages cannot manipulate registersdirectly, you would have to write a small amount of assemblercode to make the switch.

    Examples:

    Solaris Green Threads: http://bit.ly/qYnKAQGNU Portable Threads: http://www.gnu.org/software/pth/

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Many-to-One

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    One-to-One

    Each user-level thread maps to kernel threadBut, to switch between threads a context switch is required bythe kernel.Also, the number of kernel threads may be limited by the OSExamples:

    Windows NT/XP/2000LinuxSolaris 9 and later

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    One-to-One

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Many-to-Many

    Allows many user level threads to be mapped to many kernelthreads

    Best of both worlds

    Allows the operating system to create a sufficient number ofkernel threadsSolaris prior to version 9Windows NT/2000 with the ThreadFiber package

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Many-to-Many

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Two-Level Model

    Similar to many-to-many, except that it allows a user threadoptionally to be bound directly to a kernel threadExamples:

    IRIXHP-UXTru64 UNIXSolaris 8 and earlier

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Threading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Two-Level Model

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Semantics of fork()Thread CancellationSignal HandlingThread Pools

    Unclear Semantics of UNIX fork() system call

    Does fork() duplicate only the calling thread or all threads?Sometimes we want this, and sometimes we don’t, so someUNIX systems provide alternative fork functions.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Semantics of fork()Thread CancellationSignal HandlingThread Pools

    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 to periodicallycheck 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.)

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Semantics of fork()Thread CancellationSignal HandlingThread Pools

    Signal Handling

    Signals are used in UNIX systems to notify a process that aparticular 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. Optionsare:

    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 the process

    In most UNIX systems a thread can be configured to receiveor block (i.e. not handle) certain signals to help overcomethese issues.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Semantics of fork()Thread CancellationSignal HandlingThread Pools

    Thread Pools

    Under a high request-load, multithreaded servers can waste alot processing time simply creating and destroying threads.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 of servicefor a finite number of clients.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Thread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads

    Thread Libraries

    Thread library provides programmer with API for creating andmanaging threadsTwo primary ways of implementing

    Library entirely in user spaceKernel-level library supported by the OS

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Thread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads

    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, implementationis up to development of the libraryCommon in UNIX operating systems (Solaris, Linux, Mac OSX)Example: threadtest.c. Note, this is an implementation ofPOSIX Pthreads, so compiles differently!

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Thread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads

    Windows XP Threads

    Implements the one-to-one mapping (i.e. kernel-level threads)Each thread contains

    A thread idRegister setSeparate user and kernel stacksPrivate data storage area

    The register set, stacks, and private storage area are known asthe context of the threadsThe primary data structures of a thread include:

    ETHREAD (executive thread block) - Stores general info about athread: its parent process, address of the instruction where thethread starts execution.KTHREAD (kernel thread block) - Stores kernel-level state of thethread: kernel stack, etc.TEB (thread environment block) - Stores user-level state of thethread: user stack, thread-local storage.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Thread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads

    Windows XP Threads

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Thread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava 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 of theparent task (process) and can be passed flags to controlexactly what resources are shared.

    Eike Ritter Operating Systems

  • Multithreaded ProcessesThreading Models

    Some Considerations of ThreadsThreading Implementation

    The Critical-Section ProblemSynchronisation Hardware

    Inefficient SpinningSemaphores

    Semaphore Examples

    Thread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava 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

    Eike Ritter Operating Systems

    ThreadingMultithreaded ProcessesOverviewConcurrent and Parallel ExecutionUser and Kernel Threads

    Threading ModelsThreading ModelsMany-to-OneOne-to-OneOne-to-OneMany-to-ManyTwo-Level Model

    Some Considerations of ThreadsSemantics of fork()Thread CancellationSignal HandlingThread Pools

    Threading ImplementationThread LibrariesPthreadsWindows XP ThreadsLinux ThreadsJava Threads