Thread Implementation - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Thread Implementation

Description:

Pre-emption - thread is the lowest priority thread and something that makes a ... Pre-emption and time-slicing require help from kernel 'Two-Level' Many-to Many Model ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 15
Provided by: r335
Category:

less

Transcript and Presenter's Notes

Title: Thread Implementation


1
Thread Implementation
  • Scheduling
  • and
  • Synchronization

2
Implementation Issues
  • Context switching
  • Example
  • Thread Models Mapping threads to light-weight
    processes (LWPs)
  • Many-to-one
  • One-to-one
  • Many-to-Many

3
Thread Implementation
  • Multiplex execution of threads over computing
    resources
  • CPUs (assume shared memory multiprocessor)
  • Light-weight processes (LWPs) - virtual CPUs
  • Here, an LWP is equivalent to a kernel level
    thread
  • Threads may need to block
  • Wait on mutex, condition variables
  • Wait on CPUs or LWPs
  • Priority rules
  • Higher priority thread preempts lower priority
    threads
  • Threads of same priority may be executed in any
    order

4
Context Switches
  • So how does this really work???
  • Observation State of computation is captured by
  • Processor registers (PC, SP, R0Rn, MMU)
  • Contents of memory (stack, globals)
  • State of I/O devices (disk, tape, etc.)
  • To switch execution to another thread, one need
    only
  • Save the registers of currently executing thread
  • Restore registers of new thread

5
The Scheduler Procedure
  • To switch execution to another thread, a thread
    (T1) simply calls the scheduler procedure
  • Scheduler()
  • Select thread to resume execution (T2)
  • Save T1s registers in thread data structure for
    T1
  • Load registers from T2s thread data structure
    (e.g., resets SP to that of new thread)
  • Return

Voila! We are now executing T2 at the statement
after it called Scheduler() when it blocked!
6
Example
7
Thread Models(Revisited)
  • Kernel-level threads or lightweight processes
    (LWPs)
  • Unit of computation that is scheduled and run by
    the OS viewed by OS as having a single thread
    of execution
  • View each kernel-level thread as a virtual CPU
  • User-level threads must be mapped to kernel-level
    threads - three main models
  • Many-to-one multiple user-level threads map to a
    single LWP
  • One-to-one each user level thread maps to a
    different LWP
  • Many-to-many multiple user-level threads execute
    on each LWP

8
Many-to-One Model
user-level threads
LWP
  • Map all user-level threads to one LWP aka
    co-routines
  • Thread creation, scheduling, synchronization done
    in user space
  • Advantages
  • Fast - no system calls required
  • Few system dependencies portable
  • Disadvantages
  • No parallel execution of threads - cant exploit
    multiple CPUs
  • All threads block when one uses synchronous I/O
  • Can avoid w/ wrapper for I/O call do
    asynchronous I/O
  • Mainly used in language systems, portable
    libraries

9
One-to-One Model
user-level threads
LWP
LWP
LWP
  • Map each user-level thread to its own LWP
  • Thread creation, scheduling, synchronization
    require system calls
  • Advantages
  • Can exploit multiple CPUs
  • blocking system call only blocks one thread
  • Disadvantages
  • Thread creation expensive - kernel level call
  • Each thread requires kernel resources may be
    limited number of total threads
  • Used in Linux Threads, Windows NT, Windows 2000,
    OS/2

10
More Terminology(just to confuse you!)
  • Bound user-level threads
  • A user-level thread that is permanently bound to
    a specific LWP
  • The bound thread is the only user-level thread
    mapped to the LWP
  • Also called system contention scope (SCS) thread
  • Contends with all threads in the system
  • Scheduling the execution of the thread is done by
    the kernel

11
Strict Many-to Many Model
user-level threads
LWP
LWP
LWP
  • User level threads map to a set of LWPs
  • Unbound threads - a user-level thread can run
    on any available LWP
  • Also called process contention scope (PCS)
    thread
  • Contends with other threads in process for
    available LWPs
  • scheduling done by threads library
  • Thread creation, scheduling, synchronization done
    in user space
  • Not implemented in real systems

12
On the Number of LWPs
U user-level Threads
LWP
LWP
LWP
L LWPs
  • No benefit to having L gt U
  • If U gt L, some threads may have to wait for an
    LWP to run
  • Active thread - executing on an LWP
  • Runnable thread - waiting for an LWP
  • Executing synchronous system call blocks LWP
  • Need at least k LWPs to allow k concurrent I/O
    operations
  • Blocking on synchronization (mutex, condition
    variable) - LWP switches to run another thread
  • If N CPUs, need at least N LWPs to keep them all
    busy
  • Kernel may take away LWPs if some are idle for a
    long time, or allocate more LWPs as needed

13
Scheduling Unbound Threads(Process contention
scope)
  • Threads library decides which thread to run on
    each LWP
  • A running thread may context switch to another
    thread because of
  • Synchronization - thread blocks on a mutex or
    condition variable
  • Pre-emption - thread is the lowest priority
    thread and something that makes a higher priority
    thread become runnable (e.g., releasing a lock)
  • Yielding - a thread explicitly yields to another
    thread of the same priority
  • Time-slicing - threads of same priority may be
    context switched periodically
  • Pre-emption and time-slicing require help from
    kernel

14
Two-Level Many-to Many Model
user-level threads
LWP
LWP
LWP
LWP
  • Combination of one-to-one strict many-to-many
    models
  • Supports both bound and unbound threads
  • Bound threads - permanently mapped to a single,
    dedicated LWP
  • Unbound threads - may move among LWPs in set
  • Thread creation, scheduling, synchronization done
    in user space
  • Flexible approach, best of both worlds
  • Used in Solaris implementation of Pthreads and
    several other Unix implementations (IRIX, HP-UX)
Write a Comment
User Comments (0)
About PowerShow.com