28
Chapter 4: Threads Chapter 4: Threads

ch4[1]

Embed Size (px)

DESCRIPTION

OS

Citation preview

  • Chapter 4: Threads

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Chapter 4: ThreadsOverviewMultithreading ModelsThreading IssuesPthreadsWindows XP ThreadsLinux ThreadsJava Threads

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Single and Multithreaded Processes

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    BenefitsResponsiveness Resource Sharing Economy Utilization of MP Architectures

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    User ThreadsThread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Kernel ThreadsSupported by the Kernel ExamplesWindows XP/2000SolarisLinuxTru64 UNIXMac OS X

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Multithreading ModelsMany-to-One One-to-One Many-to-Many

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Many-to-OneMany user-level threads mapped to single kernel threadExamples:Solaris Green ThreadsGNU Portable Threads

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Many-to-One Model

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    One-to-OneEach user-level thread maps to kernel threadExamplesWindows NT/XP/2000LinuxSolaris 9 and later

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    One-to-one Model

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Many-to-Many ModelAllows many user level threads to be mapped to many kernel threadsAllows the operating system to create a sufficient number of kernel threadsSolaris prior to version 9Windows NT/2000 with the ThreadFiber package

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Many-to-Many Model

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Two-level ModelSimilar to M:M, except that it allows a user thread to be bound to kernel threadExamplesIRIXHP-UXTru64 UNIXSolaris 8 and earlier

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Two-level Model

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Threading IssuesSemantics of fork() and exec() system callsThread cancellationSignal handlingThread poolsThread specific dataScheduler activations

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Semantics of fork() and exec()Does fork() duplicate only the calling thread or all threads?

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Thread CancellationTerminating a thread before it has finishedTwo general approaches:Asynchronous cancellation terminates the target thread immediatelyDeferred cancellation allows the target thread to periodically check if it should be cancelled

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Signal HandlingSignals are used in UNIX systems to notify a process that a particular event has occurredA signal handler is used to process signalsSignal is generated by particular eventSignal is delivered to a processSignal is handledOptions: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 threa to receive all signals for the process

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Thread PoolsCreate a number of threads in a pool where they await workAdvantages:Usually slightly faster to service a request with an existing thread than create a new threadAllows the number of threads in the application(s) to be bound to the size of the pool

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Thread Specific DataAllows each thread to have its own copy of dataUseful when you do not have control over the thread creation process (i.e., when using a thread pool)

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Scheduler ActivationsBoth M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the applicationScheduler activations provide upcalls - a communication mechanism from the kernel to the thread libraryThis communication allows an application to maintain the correct number kernel threads

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    PthreadsA POSIX standard (IEEE 1003.1c) API for thread creation and synchronizationAPI specifies behavior of the thread library, implementation is up to development of the libraryCommon in UNIX operating systems (Solaris, Linux, Mac OS X)

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Windows XP ThreadsImplements the one-to-one mappingEach thread containsA thread idRegister setSeparate user and kernel stacksPrivate data storage areaThe register set, stacks, and private storage area are known as the context of the threadsThe primary data structures of a thread include:ETHREAD (executive thread block)KTHREAD (kernel thread block)TEB (thread environment block)

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Linux ThreadsLinux 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 the parent task (process)

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Java ThreadsJava threads are managed by the JVM

    Java threads may be created by: Extending Thread classImplementing the Runnable interface

    4.*Silberschatz, Galvin and Gagne 2005Operating System Concepts 7th edition, Jan 23, 2005

    Java Thread States

  • End of Chapter 4