CENG334 2013 W06 Synchronization Monitors

  • View
    14

  • Download
    0

Embed Size (px)

DESCRIPTION

CENG334-METU

Text of CENG334 2013 W06 Synchronization Monitors

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    1/65

    1

    CENG334

    Introduction to Operating Systems

    Erol Sahin

    Dept of Computer Eng.Middle East Technical University

    Ankara, TURKEY

    URL:http://kovan.ceng.metu.edu.tr/ceng334

    Petersons AlgorithmMonitors, Condition variablessTopics:

    Monitors

    Condition Variables

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    2/65

    2

    Petersons Algorithm

    int flag[2] = {0, 0};

    int turn;

    P0:do{

    flag[0] = 1;

    turn = 1;

    while (flag[1] == 1 && turn == 1)

    { // busy wait

    }

    // critical section

    flag[0] = 0;

    //remainder section

    }while(1);

    P1:do{

    flag[1] = 1;

    turn = 0;

    while (flag[0] == 1 && turn == 0)

    { // busy wait

    }

    // critical section

    flag[1] = 0;

    // remainder section

    } while(1);

    turn : indicates whose turn is it to enter critical section. If turn==iprocess Pi is allowed to get in.

    flag[2]: indicates if process Pi is ready to enter critical section. Ifflag[i]is set, then Pi is ready to enter critical section.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    3/65

    3

    Petersons Algorithm

    int flag[2] = {0, 0};

    int turn;

    P0:do{

    flag[0] = 1;

    turn = 1;

    while (flag[1] == 1 && turn == 1)

    { // busy wait

    }

    // critical section

    flag[0] = 0;

    //remainder section

    }while(1);

    P1:do{

    flag[1] = 1;

    turn = 0;

    while (flag[0] == 1 && turn == 0)

    { // busy wait

    }

    // critical section

    flag[1] = 0;

    // remainder section

    } while(1);

    Mutual Exclusion: Only one process Pi (the one which set turn=i last) enters the criticalsection.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    4/654

    Petersons Algorithm

    int flag[2] = {0, 0};

    int turn;

    P0:do{

    flag[0] = 1;

    turn = 1;

    while (flag[1] == 1 && turn == 1)

    { // busy wait

    }

    // critical section

    flag[0] = 0;

    //remainder section

    }while(1);

    P1:do{

    flag[1] = 1;

    turn = 0;

    while (flag[0] == 1 && turn == 0)

    { // busy wait

    }

    // critical section

    flag[1] = 0;

    // remainder section

    } while(1);

    Progress: If process P1 is not in critical section then flag[1] = 0. Therefore while loop of P0quits immediately and P0 can get into its critical section. And vice versa..

    Bounded waiting: Process Pi keeps waiting in spinlocking only while the other process is inits critical section.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    5/655

    Petersons Algorithm

    int flag[2] = {0, 0};

    int turn;

    P0:do{

    flag[0] = 1;

    turn = 1;

    while (flag[1] == 1 && turn == 1)

    { // busy wait

    }

    // critical section

    flag[0] = 0;

    //remainder section

    }while(1);

    P1:do{

    flag[1] = 1;

    turn = 0;

    while (flag[0] == 1 && turn == 0)

    { // busy wait

    }

    // critical section

    flag[1] = 0;

    // remainder section

    } while(1);

    Uses spinlocking for waiting.No strict alternation is required between processes. That is, P0,P0,P0,P1,P1 is doable.Requires that processes alternate between critical and remainder sections.Can be extended to n processes, only if n is known apriori (in advance). HOW?

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    6/656

    Petersons Algorithm

    int flag[2] = {0, 0};

    int turn;

    P0:do{

    flag[0] = 1;

    turn = 1;

    while (flag[1] == 1 && turn == 1)

    { // busy wait

    }

    // critical section

    flag[0] = 0;

    //remainder section

    }while(1);

    P1:do{

    flag[1] = 1;

    turn = 0;

    while (flag[0] == 1 && turn == 0)

    { // busy wait

    }

    // critical section

    flag[1] = 0;

    // remainder section

    } while(1);

    Prone to priority inversion:Assume that P0 has a higher priority than P1.When P1 is in its critical section, P0 may get scheduled to do spinlocking.P1 never gets scheduled to finish its critical section and both processes end up waiting.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    7/657

    Issues with Semaphores

    Much of the power of semaphores derives from calls to

    down() and up() that are unmatched

    See previous example!

    Unlike locks, acquire() and release() are not always paired.

    This means it is a lot easier to get into trouble with semaphores.

    More rope

    Would be nice if we had some clean, well-defined language

    supportfor synchronization...

    Java does!

    Adapted from Matt Welshs (Harvard University) slides.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    8/658

    Monitors

    A monitoris an object intended to be used safely

    by more than one thread.

    The defining characteristic of a monitor is that its

    methods are executed with mutual exclusion.

    That is, at each point in time, at most one thread may be

    executing any of its methods.

    also provide Condition Variables (CVs) for

    threads to temporarily give up exclusive access,

    in order to wait for some condition to be met,

    before regaining exclusive access and resuming their task.

    Use CVs for signaling other threads that such

    conditionshave been met.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    9/659

    Condition Variables

    Conceptually a condition variable (CV) is a queue of threads,

    associated with a monitor, upon which a thread may wait for

    some assertion to become true.

    Threads can use CVs

    to temporarily give up exclusive access, in order to wait for

    some condition to be met, before regaining exclusive access and resuming their task.

    for signaling other threads that such conditionshave been met.

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    10/6510

    Monitors

    This style of using locks and CV's to protect access to a sharedobject is often called a monitor

    Think of a monitor as a lock protecting an object, plus a queue of waiting threads.

    Shared data

    Methods accessing

    shared data

    Waiting threads

    At most one thread

    in the monitor at atime

    How is this different than a lock???

    Adapted from Matt Welshs (Harvard University) slides.

    M i

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    11/6511

    Monitors

    Shared data

    Methods accessingshared data

    unlocked

    Adapted from Matt Welshs (Harvard University) slides.

    M it

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    12/6512

    Monitors

    Shared data

    Methods accessingshared data

    locked

    zzzz...

    zzzz...

    Sleeping thread no longer inthe monitor.(But not on the waiting queue either! Why?)

    Adapted from Matt Welshs (Harvard University) slides.

    M it

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    13/6513

    Monitors

    Shared data

    Methods accessingshared data

    lockedMonitor stays locked!(Lock now owned bydifferent thread...)

    zzzz...

    notify()

    Adapted from Matt Welshs (Harvard University) slides.

    M it

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    14/6514

    Monitors

    Shared data

    Methods accessingshared data

    locked

    notify()

    Adapted from Matt Welshs (Harvard University) slides.

    Monitors

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    15/65

    15

    Monitors

    Shared data

    Methods accessingshared data

    locked

    No guarantee which order threads get into the monitor.(Not necessarily FIFO!)

    Adapted from Matt Welshs (Harvard University) slides.

    Bank Example

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    16/65

    16

    Bank Example

    monitor Bank{

    int TL = 1000;condition haveTL;

    void withdraw(int amount) {if (amount > TL)

    wait(haveTL);TL -= amount;

    }

    void deposit(int amount) {TL += amount;notify(haveTL);

    }

    }

    Bank Example

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    17/65

    17

    Bank Example

    monitor Bank{

    int TL = 1000;condition haveTL;

    void withdraw(int amount) {while(amount > TL)

    wait(haveTL);TL -= amount;

    }

    void deposit(int amount) {TL += amount;notifyAll(haveTL);

    }

    }

    Hoare vs Mesa Monitor Semantics

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    18/65

    18

    Hoare vs. Mesa Monitor SemanticsThe monitor notify() operation can have two different meanings:

    Hoare monitors (1974) notify(CV) means to run the waiting thread immediately Causes notifying thread to block

    Mesa monitors (Xerox PARC, 1980) notify(CV) puts waiting thread back onto the ready queuefor the monitor

    But, notifying thread keeps running

    Adapted from Matt Welshs (Harvard University) slides.

    Hoare vs Mesa Monitor Semantics

  • 5/28/2018 CENG334 2013 W06 Synchronization Monitors

    19/65

    19

    Hoare vs. Mesa Monitor SemanticsThe monitor notify() operation can have two different meanings:

    Hoare monitors (1974) notify(CV) means to run the waiting thread immediately Causes notifying thread to block

    Mesa monitors (Xerox PARC, 1980) notify(CV) puts waiting thread back onto the ready queuefor