Operating Systems -ch5-F06

Embed Size (px)

Citation preview

  • 8/2/2019 Operating Systems -ch5-F06

    1/26

    1

    Operating Systems

    404452 Section 1

    25 October 2004

    Chapter 5

    Threads

  • 8/2/2019 Operating Systems -ch5-F06

    2/26

    Chapter 4 Operating Systems 2

    Chapter 4: Threads

    Overview

    Multithreading Models Threading Issues

    Pthreads

    Windows XP Threads

    Linux Threads

    Java Threads

  • 8/2/2019 Operating Systems -ch5-F06

    3/26

    Chapter 4 Operating Systems 3

    Overview

    Process characteristics can be grouped into two

    categories: Scheduling and execution

    dispatching priority and execution state

    Resource ownership

    memory, I/O devices,

    Unit of execution and a collection of resourceswith which the unit of execution is associatedcharacterize the concept of aprocess.

    From this perspective, a:

    unit of resource ownership is aprocess, and

    unit of execution is athread.

  • 8/2/2019 Operating Systems -ch5-F06

    4/26

    Chapter 4 Operating Systems 4

    What is a Thread??

    A thread, also referred to as a light-weight process

    (LWP), is an execution unit that can be independentlyscheduled but that shares a single address space withother threads making up a process.

    As a basic unit of CPU utilization, a thread consists of:

    an instruction pointer (also referred to as the PC or

    instruction counter), CPU register set,

    some storage for its local variables and

    a stack.

    A thread shares its code and data, as well as systemresources and other OS related information, with itspeer group (other threads of the same process).

  • 8/2/2019 Operating Systems -ch5-F06

    5/26

    Chapter 4 Operating Systems 5

    Single and Multithreaded Processes

  • 8/2/2019 Operating Systems -ch5-F06

    6/26

    Chapter 4 Operating Systems 6

    Threads: an example

    A good example of an application that could make use

    of threads is a file serveron a local area network (LAN). A controller thread accepts file service requests and

    spawns a worker thread for each request, thereforemay handle many requests concurrently. When aworker thread finishes servicing a request, it is

    destroyed.

    RPC is done using threads also.

  • 8/2/2019 Operating Systems -ch5-F06

    7/26

    Chapter 4 Operating Systems 7

    Threads versus processes

    A thread operates in much the same way as a process:

    can be one of the several states,executes sequentially (within a process and shares the

    CPU),

    can issue system calls.

    Creating a thread is less expensive than creating a process.

    Switching to a thread within a process is cheaper thanswitching between threads of different processes.

    Threads within a process share resources (including the samememory address space) conveniently and efficiently comparedto separate processes.

    Threads within a process are NOT independent and are NOTprotected against each other.

  • 8/2/2019 Operating Systems -ch5-F06

    8/26

    Chapter 4 Operating Systems 8

    Benefits

    Responsiveness (responsiveness is how quickly an

    interactive program responds to user request). Ex.Multithreaded web server.

    Resource Sharing (threads belongs to the sameprocess shares the same memory/resources no

    need to replicate the resources). Economy ( creating process instead of thread is

    costly in terms of time and resources).

    Utilization of MP Architectures (running different

    threads on different processors concurrently).

  • 8/2/2019 Operating Systems -ch5-F06

    9/26

    Chapter 4 Operating Systems 9

    Thread implementations User level

    implemented by thread library (as a set of library functions)

    OS is unaware of this type of threads therefore user level threadscannot be scheduled independently each thread gets partial time quantum of a process Blocking system calls like read() blocks the entire set of threads of a

    single process Low overhead, high computation performance and less costly

    (thread) operations.

    Kernel level implemented as system calls, can be scheduled directly by the OS, independent operation of threads in a single process, more expensive (thread) operations.

    Hybrid approach combines the advantages of the above two; e.g., Solaris threads,

    user-level threads that operates independently can be mapped ontodifferent kernel-level threads,

    similarly, user-level threads, depending on each other, can bemapped onto the same kernel-level thread.

  • 8/2/2019 Operating Systems -ch5-F06

    10/26

    Chapter 4 Operating Systems 10

    User Threads Kernel Threads

    Thread management

    done by user-levelthreads library

    Three primary

    thread libraries: POSIX Pthreads

    Win32 threads

    Java threads

    Supported by the

    Kernel

    Examples

    Windows XP/2000

    Solaris

    Linux

    Tru64 UNIX

    Mac OS X

  • 8/2/2019 Operating Systems -ch5-F06

    11/26

    Chapter 4 Operating Systems 11

    Multithreading Models

    Many-to-One

    One-to-One

    Many-to-Many

  • 8/2/2019 Operating Systems -ch5-F06

    12/26

    Chapter 4 Operating Systems 12

    Many-to-One

    Many user-level threads mapped to single kernel thread

    Thread management is done by thread library efficientand blocking.

    One user thread can access the kernel at a time.

    Examples:

    Solaris Green Threads

    GNU Portable Threads

  • 8/2/2019 Operating Systems -ch5-F06

    13/26

    Chapter 4 Operating Systems 13

    One-to-One

    Each user-level thread maps to kernel thread thus

    achieves higher concurrency Requires kernel thread for each user thread costly.

    Examples

    Windows NT/XP/2000

    Linux

    Solaris 9 and later

  • 8/2/2019 Operating Systems -ch5-F06

    14/26

    Chapter 4 Operating Systems 14

    Many-to-Many Model

    Allows many user level threads to be mapped to many

    kernel threads Allows the operating system to create a sufficient

    number of kernel threads

    Solaris prior to version 9

    Windows NT/2000 with the ThreadFiberpackage

  • 8/2/2019 Operating Systems -ch5-F06

    15/26

    Chapter 4 Operating Systems 15

    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

  • 8/2/2019 Operating Systems -ch5-F06

    16/26

    Chapter 4 Operating Systems 16

    Threading Issues

    Semantics offork() and exec()system calls

    Does fork() duplicate only thecalling thread or all threads?

    Thread cancellation Signal handling

    Thread pools

    Thread specific data Scheduler activations

  • 8/2/2019 Operating Systems -ch5-F06

    17/26

    Chapter 4 Operating Systems 17

    Thread Cancellation

    Terminating a thread before it has finished

    Thread to be canceled is called target thread

    Two general approaches:

    Asynchronous cancellation one thread

    terminates the target thread immediately Deferred cancellation allows the target

    thread to periodically check if it should becancelled (usually there is a flag indicates if

    it should be cancelled).

  • 8/2/2019 Operating Systems -ch5-F06

    18/26

    Chapter 4 Operating Systems 18

    Signal Handling

    Signals are used in UNIX systems to notify a

    process that a particular event has occurred 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 thesignal applies (synchronous signals )

    Deliver the signal to every thread in the process

    Deliver the signal to certain threads in theprocess

    Assign a specific thread to receive all signals forthe process

  • 8/2/2019 Operating Systems -ch5-F06

    19/26

    Chapter 4 Operating Systems 19

    Thread Pools

    At process startup, create a number of threads

    and store them in a pool where they await work Advantages:

    Usually slightly faster to service a request withan existing thread than create a new thread

    Allows the number of threads in theapplication(s) to be bound to the size of thepool. This is important to systems that cantsupport large number of concurrent threads.(we refer to this issue as the degree of

    multithreading (concurrency)). The number of the threads in the pool depends

    on many factors like the number of CPUs, theavailable memory and system concurrency level.

  • 8/2/2019 Operating Systems -ch5-F06

    20/26

    Chapter 4 Operating Systems 20

    Thread Specific Data

    In some cases each thread requires a specific

    copy of specific data. Thread specific data allows each thread to have

    its own copy of data

    Useful when you do not have control over the

    thread creation process (i.e., when using athread pool)

  • 8/2/2019 Operating Systems -ch5-F06

    21/26

    Chapter 4 Operating Systems 21

    Scheduler Activations

    Scheduler activation: scheme for communication betweenthread library and the kernel.

    Both M:M and Two-level models require communication tomaintain the appropriate number of kernel threads allocatedto the application

    The kernel provide a set of virtual processors and theapplication can schedule user threads onto an available

    virtual processor The goal is to adjust the number of kernel threads

    dynamically in order to achieve the best possibleperformance.

    Scheduler activations provide upcalls - a communicationmechanism from the kernel to the thread library to inform itabout happening events.

    This communication allows an application to maintain thecorrect number kernel threads.

  • 8/2/2019 Operating Systems -ch5-F06

    22/26

    Chapter 4 Operating Systems 22

    Pthreads

    A POSIX standard (IEEE 1003.1c) API for

    thread creation and synchronization API specifies behavior of the thread library,

    implementation is up to development of thelibrary

    Common in UNIX operating systems (Solaris,Linux, Mac OS X)

  • 8/2/2019 Operating Systems -ch5-F06

    23/26

    Chapter 4 Operating Systems 23

    Windows XP Threads

    Implements the one-to-one mapping Each thread contains

    A thread id

    Register set

    Separate user and kernel stacks

    Private data storage area

    The register set, stacks, and private storage areaare known as the context of the threads

    The primary data structures of a thread include:

    ETHREAD (executive thread block) KTHREAD (kernel thread block)

    TEB (thread environment block)

  • 8/2/2019 Operating Systems -ch5-F06

    24/26

    Chapter 4 Operating Systems 24

    Linux Threads

    Linux refers to them as tasks rather thanthreads

    Thread creation is done through clone()system call

    clone() allows a child task to share theaddress space of the parent task (process)

  • 8/2/2019 Operating Systems -ch5-F06

    25/26

    Chapter 4 Operating Systems 25

    Java Threads

    Java threads are managed by the JVM

    Java threads may be created by:

    Extending Thread class

    Implementing the Runnable interface

  • 8/2/2019 Operating Systems -ch5-F06

    26/26

    Chapter 4 Operating Systems 26

    Java Thread States