Cs2200 Threads

  • Upload
    perry01

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 Cs2200 Threads

    1/52

  • 8/12/2019 Cs2200 Threads

    2/52

  • 8/12/2019 Cs2200 Threads

    3/52

    Our Road Map

    Processor

    Networking

    Parallel Systems

    I/O Subsystem

    Memory Hierarchy

  • 8/12/2019 Cs2200 Threads

    4/52

  • 8/12/2019 Cs2200 Threads

    5/52

    Recall

    Context Switching Requires considerable work

    What about a single users application? Is there a way to make it more efficient

    In effect, allow the user to have multiple

    processes executing in the same space? Yes, solution: Threads or Multithreading

  • 8/12/2019 Cs2200 Threads

    6/52

    What is Multithreading?

    Technique allowing program to do multipletasks

    Example: Java GUI's

  • 8/12/2019 Cs2200 Threads

    7/52

    Java Event Handling

    import java.awt.*;public class HelloGUI {

    public static void main (String[ ] arg) {System.out.println

    (About to make GUI);Frame f = new Frame (Hello GUIs);f.setSize( 200, 200 );f.show();System.out.println

    (Finished making GUI);}// main

    }// class HelloGUI

    cal lback

    Our event

    handling

    code

    The code trapping

    this event appears

    in the graphics thread

  • 8/12/2019 Cs2200 Threads

    8/52

    What is Multithreading?

    Technique allowing program to do multipletasks

    Example: Java GUI's

    Is it a new technique? has existed since the 70s (concurrent Pascal,

    Ada tasks, etc.)

    Why now? Time has come for this technology

    Emergence of SMPs in particular

  • 8/12/2019 Cs2200 Threads

    9/52

    SMP?

    What is an SMP? Multiple CPUs in a single box sharing all the

    resources such as memory and I/O

    Is a dual-processor SMP more costeffective than two uniprocessor boxes?

    Yes, (roughly 20% more for a dual processor

    SMP compared to a uniprocessor). Modest speedup for a program on a dual-

    processor SMP over a uniprocessor will make

    it worthwhile.

  • 8/12/2019 Cs2200 Threads

    10/52

    What is a Thread?

    Basic unit of CPU utilization A lightweight process (LWP)

    Consists of Program Counter

    Register Set

    Stack Space

    Shares with peer threads Code

    Data

    OS Resources Open files

    Signals

  • 8/12/2019 Cs2200 Threads

    11/52

    Threads

    Can be context switched more easily Registers and PC

    Not memory management

    Can run on different processorsconcurrently in an SMP

    Share CPU in a uniprocessor

    May (Will) require concurrency controlprogramming like mutex locks.

  • 8/12/2019 Cs2200 Threads

    12/52

    active

    Process

    Threads in a Uniprocessor?

    Allows concurrency between I/O and user

    processing even in a uniprocessor box

  • 8/12/2019 Cs2200 Threads

    13/52

    Threads and OS

    Traditional OS DOS

    Memory layout

    user

    kernelData

    Protection between user and kernel?

    DataProgram

    DOS Code

  • 8/12/2019 Cs2200 Threads

    14/52

    User

    Kernel Kernel codeand data

    Protection between user and kernel?

    Process codeand data

    PCB

    P1

    Process codeand data

    PCB

    P2

    Threads and OS

    Unix - memory layout

  • 8/12/2019 Cs2200 Threads

    15/52

    Threads and OS

    Programs in a traditional OS are singlethreaded

    One PC per program (process), one stack,

    one set of CPU registers If a process blocks (say disk I/O, network

    communication, etc.) then no progress for the

    program as a whole

  • 8/12/2019 Cs2200 Threads

    16/52

    MultiThreaded Operating Systems

    How widespread is support for threads inOS?

    Digital Unix, Sun Solaris, Win9x, Win NT,

    Win2k, Linux, Free BSD, etc Process vs. Thread?

    In a single threaded program, the state of the

    executing program is contained in a process In a multithreaded program, the state of the

    executing program is contained in several

    concurrentthreads

  • 8/12/2019 Cs2200 Threads

    17/52

    What is this?

    1. Crack in slide2. Hair stuck on lens

    3. Symbol for thread

    4. San Andreas Fault

  • 8/12/2019 Cs2200 Threads

    18/52

  • 8/12/2019 Cs2200 Threads

    19/52

    Memory Layout

    Multithreaded program has a per-threadstack

    Heap, static, and code are common to all

    threads

    MT program

    stk1 stk2 stk3 ...

    HeapStatic

    Code

    ST program

    stack1

    HeapStatic

    Code

  • 8/12/2019 Cs2200 Threads

    20/52

    Threads

    Threaded code different from non-threaded?

    Protection for data shared among threads

    Mutex Synchronization among threads

    Way for threads to talk (signal) to one another

    Thread-safe libraries

    NOTESstrtok is unsafe for multi-thread applications.strtok_r is MT-Safe and should be used instead.

  • 8/12/2019 Cs2200 Threads

    21/52

    Typical Operation

    Main programs creates (or spawns)threads

    Threads may

    perform one task and die last for duration of program

    Threads must

    be able to synchronize activity communicate with one another

  • 8/12/2019 Cs2200 Threads

    22/52

    Programming Support for Threads Creation

    pthread_create(top-level procedure, args)

    Termination

    returnto top-level procedure

    explicit cancel Rendezvous

    creator can waitfor children pthread_join(child_tid)

    Synchronization

    pthread_mutex_...

    conditionvariables (pthread_cond_...)

  • 8/12/2019 Cs2200 Threads

    23/52

    Programming with Threads

    Synchronization

    For coordination of the threads

    Communication

    For inter-thread sharing of data

    Threads can be in different processors

    How to achieve sharing in SMP?

    Software: accomplished by keeping all threads in

    the same address spaceby the OS

    Hardware: accomplished by hardware shared

    memoryand coherent caches

  • 8/12/2019 Cs2200 Threads

    24/52

    Synchronization Primitives lock and unlock

    mutual exclusion among threads

    busy-waiting vs. blocking

    pthread_mutex_trylock: no blocking (rare)

    pthread_mutex_lock: blocking pthread_mutex_unlock

    condition variables

    pthread_cond_wait: block for a signal pthread_cond_signal: signal one waiting

    thread

    pthread_cond_broadcast: signal all waiting

    threads

  • 8/12/2019 Cs2200 Threads

    25/52

    Example

    lock(mutex);while (resource_state == BUSY)

    //spin;

    resource_state = BUSY;

    unlock(mutex);

    use resource;

    lock(mutex);

    resource_state = FREE;

    unlock(mutex);

    Initially mutex

    is unlocked

    resource_state

    is FREE

    Will this work?1 - Yes2 - No3 - Maybe

  • 8/12/2019 Cs2200 Threads

    26/52

    Example

    lock(mutex);while (resource_state == BUSY)

    //spin;

    resource_state = BUSY;

    unlock(mutex);

    use resource;

    lock(mutex);

    resource_state = FREE;

    unlock(mutex);

    Thread 1

  • 8/12/2019 Cs2200 Threads

    27/52

  • 8/12/2019 Cs2200 Threads

    28/52

    Example

    lock(mutex);while (resource_state == BUSY)

    //spin;

    resource_state = BUSY;

    unlock(mutex);

    use resource;

    lock(mutex);

    resource_state = FREE;

    unlock(mutex);

    Thread 1

    Thread 2

  • 8/12/2019 Cs2200 Threads

    29/52

    Example with cond-var

    lock(mutex)

    while(resource_state == BUSY)

    wait(cond_var); /* implicitly give up mutex */

    /* implicitly re-acquire mutex */

    resource_state = BUSY

    unlock(mutex)

    /* use resource */

    lock(mutex)resource_state = FREE;

    unlock(mutex)

    signal(cond_var);

  • 8/12/2019 Cs2200 Threads

    30/52

    Example with cond-var

    lock(mutex)

    while(resource_state == BUSY)

    wait(cond_var); /* implicitly give up mutex */

    /* implicitly re-acquire mutex */

    resource_state = BUSY

    unlock(mutex)

    /* use resource */

    lock(mutex)resource_state = FREE;

    unlock(mutex)

    signal(cond_var);

    T1

  • 8/12/2019 Cs2200 Threads

    31/52

    Example with cond-var

    lock(mutex)

    while(resource_state == BUSY)

    wait(cond_var); /* implicitly give up mutex */

    /* implicitly re-acquire mutex */

    resource_state = BUSY

    unlock(mutex)

    /* use resource */

    lock(mutex)

    resource_state = FREE;

    unlock(mutex)

    signal(cond_var);

    T1

    T2

  • 8/12/2019 Cs2200 Threads

    32/52

    pthreads

    Mutex Must create mutex variables

    pthread_mutex_tpadlock;

    Must initialize mutex variablepthread_mutex_init(&padlock, NULL);

    Condition Variable (used for signaling)

    Must create condition variablespthread_cond_t non_full;

    Must initialize condition variablespthread_cond_init(&non_full, NULL);

  • 8/12/2019 Cs2200 Threads

    33/52

    Classic CS Problem

    Producer Consumer

    ProducerIf (! full)

    Add item to buffer

    empty = FALSE

    if(buffer_is_full)full = TRUE

    ConsumerIf (! empty)

    Remove item from bufferfull = FALSE

    if(buffer_is_empty)

    empty = TRUE

    full

    empty

    buffer

    ...

  • 8/12/2019 Cs2200 Threads

    34/52

    Example Producer Threads Programwhile(forever){

    // produce itempthread_mutex_lock(padlock);

    while (full)

    pthread_cond_wait(non_full,padlock);

    // add item to buffer;

    buffercount++;

    if (buffercount == BUFFERSIZE)

    full = TRUE;

    empty = FALSE;

    pthread_mutex_unlock(padlock);pthread_cond_signal(non_empty);

    }

  • 8/12/2019 Cs2200 Threads

    35/52

    // Producerhil (f ){

  • 8/12/2019 Cs2200 Threads

    36/52

    while(forever){// produce itempthread_mutex_lock(padlock);while (full)

    pthread_cond_wait(non_full,padlock);// add item to buffer;buffercount++;if (buffercount == BUFFERSIZE)

    full = TRUE;empty = FALSE;pthread_mutex_unlock(padlock);pthread_cond_signal(non_empty);

    }// Consumerwhile(forever) {

    pthread_mutex_lock(padlock);while (empty)

    pthread_cond_wait (non_empty,padlock);// remove item from buffer;buffercount--;full = false;if (buffercount == 0)

    empty = true;pthread_mutex_unlock(padlock);pthread_cond_signal(non_full);

    // consume_item;}

  • 8/12/2019 Cs2200 Threads

    37/52

  • 8/12/2019 Cs2200 Threads

    38/52

    Team model

    Pipelined model

    Mailbox

    Mailbox

  • 8/12/2019 Cs2200 Threads

    39/52

    Threads Implementation

    User level threads OS independent

    Scheduler is part of the runtime system

    Thread switch is cheap (save PC, SP, regs) Scheduling customizable, i.e., more

    application control

    Blocking call by thread blocks process

  • 8/12/2019 Cs2200 Threads

    40/52

    Solution to blocking problem in user levelthreads

    Non-blocking version of all system calls

    Switching among user level threads

    Yield voluntarily

    How to make preemptive? Timer interrupt from kernel to switch

  • 8/12/2019 Cs2200 Threads

    41/52

    Kernel Level

    Expensive thread switch Makes sense for blocking calls by threads

    Kernel becomes complicated: process vs.

    threads scheduling

    Thread packages become non-portable

    Problems common to user and kernel level

    threads

    Libraries

    Solution is to have thread-safe wrappers to

    such library calls

  • 8/12/2019 Cs2200 Threads

    42/52

    Questions?

  • 8/12/2019 Cs2200 Threads

    43/52

  • 8/12/2019 Cs2200 Threads

    44/52

    Solaris Terminology

  • 8/12/2019 Cs2200 Threads

    45/52

    More Conventional Terminology

    Thread

    kernel thread

    (user-level view)

    (Inside the kernel)

    Processes

    P1 P2 P3

  • 8/12/2019 Cs2200 Threads

    46/52

    Questions?

    Does kernel see user threads? Are all threads from P3 on same CPU?

    Are all threads in kernel attached to lwps?

    If the left-most thread of P3 issues ablocking system call does P3 block?

    Who schedules lwps?

    Who schedules threads?

  • 8/12/2019 Cs2200 Threads

    47/52

  • 8/12/2019 Cs2200 Threads

    48/52

    Questions?

  • 8/12/2019 Cs2200 Threads

    49/52

    Things to know?

    1. The reason threads are around?2. Benefits of increased concurrency?

    3. Why do we need software controlled "locks"

    (mutexes) of shared data?

    4. How can we avoid potential deadlocks/race

    conditions.

    5. What is meant by producer/consumer thread

    synchronization/communication using pthreads?

    6. Why use a "while" loop around a

    pthread_cond_wait() call?

  • 8/12/2019 Cs2200 Threads

    50/52

    Things to know?

    7. Why should we minimize lock scope (minimizethe extent of code within a lock/unlock block)?

    8. Do you have any control over thread

    scheduling?

  • 8/12/2019 Cs2200 Threads

    51/52

    Questions?

  • 8/12/2019 Cs2200 Threads

    52/52