9 Mutex Synch 11

download 9 Mutex Synch 11

of 54

Transcript of 9 Mutex Synch 11

  • 8/6/2019 9 Mutex Synch 11

    1/54

    Concurrency:Mutual Exclusion and

    1

  • 8/6/2019 9 Mutex Synch 11

    2/54

    Needs of Processes Allocation of processor time

    Allocation and sharing of resources (e.g. memory)

    Communication among processes Synchronization of multiple processes

    2

    Have seen: scheduling, memory allocationNow: synchronization

    Next: more on resource allocation & deadlock

  • 8/6/2019 9 Mutex Synch 11

    3/54

    Process Synchronization:

    Roadmap in shared memory systems

    in message passing systems

    3

  • 8/6/2019 9 Mutex Synch 11

    4/54

    Shared memory communication

    Recall:

    Ex1: memory mapped files

    Ex2 (interface) POSIX Shared Memory: define shared space, attach/detach

    Btw, a nice link to checkwww.cs.cf.ac.uk/Dave/C/node27.html#SECTION0027200000

    00000000000

  • 8/6/2019 9 Mutex Synch 11

    5/54

    Money flies away

    Bank thread A

    Read a := BALANCEa := a + 5000

    Write BALANCE := a

    Bank thread B

    Read b:= BALANCEb := b 2000Write BALANCE := b

    BALANCE: 20000 kr

    5

    Oooops!! BALANCE: 18000 kr!!!!Problem: need to ensure that each process is executing its critical

    section (e.g updating BALANCE) exclusively (one at a time)

    t

  • 8/6/2019 9 Mutex Synch 11

    6/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 and for n processes

    6

    Semaphores, Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    S nchronization in messa e assin s stems

  • 8/6/2019 9 Mutex Synch 11

    7/54

    The Critical-Section

    (Mutual Exclusion) Problem n processes all competing to use some shared data Each process has a code segment, called critical section, in

    which the shared data is accessed.

    Problem ensure that when one process is executing in itscritical section, no other process is allowed to execute in itscritical section; ie. Access to the critical section must be anatomic action.

    7

    repeatentry section

    critical sectionexit section

    remainder sectionuntil false;

  • 8/6/2019 9 Mutex Synch 11

    8/54

    Requirements from a solution to the Critical-

    Section Problem1. Mutual Exclusion. Only one process at a time is

    allowed to execute in its critical section.

    2. Progress (no deadlock/no livelock). If no process isexecuting in its critical section and there exist someprocesses that wish to enter theirs, the selection ofthe rocesses that will enter the critical section

    8

    next cannot be postponed indefinitely.

    3. Fairness, Bounded Waiting (no starvation). E.g. abound on the number of times that other processesare allowed to enter their critical sections after aprocess has made a request to enter its criticalsection and before that request is granted. No assumption concerning relative speed of the n

    processes.

  • 8/6/2019 9 Mutex Synch 11

    9/54

    9

    Mutual exclusion/critical section (region)

    Figure source A.Tanenbaum MOS book

    B waits

  • 8/6/2019 9 Mutex Synch 11

    10/54

    Initial Attempts to Solve Problem

    Only 2 processes, P0 and P1 Processes may share some common variables (can be read or

    written atomically) to synchronize their actions.Shared variables: var turn: (0..1) (initially 0) turn= i Pican enter its critical section

    10

    repeatwhile turn ido no-op;

    critical sectionturn:= j;

    remainder sectionuntil false;

    (too polite) Satisfies mutual exclusion, but not progress

  • 8/6/2019 9 Mutex Synch 11

    11/54

    Another attempt

    Shared variables var flag: array [0..1] of boolean; (initially false).

    flag[i] = true

    Piready to enter its critical sectionProcess Pi

    repeat

    while flag[j] do no-op;

    11

    flag[i] := true;critical section

    flag[i] := false;

    remainder section

    until false;.

    (unpolite) Progress is ok, but does NOT satisfy mutualexclusion.

  • 8/6/2019 9 Mutex Synch 11

    12/54

    Petersons Algorithm

    (2 processes)Shared variables:

    var turn: (0..1); initially 0 (turn= i Pican enter itscritical section)

    var flag: array [0..1] of boolean; initially false(flag[i] =true Piwants to enter its critical section)Process Pi

    repeatF fla i := true

    12

    (T) turn:= ;(C) while (flag[j] and turn=j) do no-op;critical section

    (E) flag[i] := false;remainder section

    until false;

    Argue that it satisfies the 3 requirements

  • 8/6/2019 9 Mutex Synch 11

    13/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 (and for n) processes

    13

    Semaphores, Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    S nchronization in messa e assin s stems

  • 8/6/2019 9 Mutex Synch 11

    14/54

    Mutual Exclusion:

    Hardware Support A process runs until it invokes an operating-system service or

    until it is interrupted

    Interrupt Disabling disallows interleaving (1-cpu

    system) and can guarantee mutual exclusion

    BUT:

    14

    Processor is limited in its ability to interleave programs Multiprocessors: disabling interrupts on one processor will not

    guarantee mutual exclusion

  • 8/6/2019 9 Mutex Synch 11

    15/54

    Mutual Exclusion:

    Other Hardware Support Special Machine Instructions

    Performed in a single instruction cycle: Reading andwriting together as one atomic step

    Not subject to interference from other instructions

    in uniprocessor system they are executed withoutinterru t

    15

    in multiprocessor system they are executed withe.g. locked system bus

    Memory word(s)

  • 8/6/2019 9 Mutex Synch 11

    16/54

    Mutual Exclusion:

    Hardware SupportTest and Set Instructionboolean testset (int i)

    if (i == 0)

    i = 1; return true;else return false;

    Exchange Instruction (swap)void exchange(int mem1, mem2)

    temp = mem1;

    mem1 = mem2;

    mem2 = temp;

    16

  • 8/6/2019 9 Mutex Synch 11

    17/54

    Mutual Exclusion using Machine

    InstructionsAdvantages Applicable to any number of

    processes on single ormultiple processors sharingmain memory

    Disadvantages (when used in thesimple way shown just now)

    Busy-waiting consumes processortime Even Deadlock possible if used

    in strict riorit -based

    17

    easy to verify

    scheduling systems: ex.scenario: low priority process has the critical

    region higher priority process needs it

    the higher priority process willobtain the processor to wait forthe critical region

    Starvation is possible when usingjust simple methods; cf next formaintaining turn

  • 8/6/2019 9 Mutex Synch 11

    18/54

    Bounded-waiting Mutual Exclusion with TestandSet()

    do {

    waiting[i] = TRUE;

    key = TRUE;

    while (waiting[i] && key)key = TestAndSet(&lock);

    waiting[i] = FALSE;

    j = (i + 1) % n;while ((j != i) && !waiting[j]) // find next one waiting and signal //

    j = (j + 1) % n;

    if (j == i)

    lock = FALSE;else

    waiting[j] = FALSE;

    // remainder section

    } while (TRUE);

  • 8/6/2019 9 Mutex Synch 11

    19/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 and for n processes

    19

    Semaphores and Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    S nchronization in messa e assin s stems

  • 8/6/2019 9 Mutex Synch 11

    20/54

    Semaphores

    Special variables/data-structures used for signaling If a process is waiting for a signal, it is blocked until that

    signal is sent

    Accessible via atomic Waitand signaloperations

    Queue is (can be) used to hold processes waiting on

    20

    Can be binary or general (counting)

  • 8/6/2019 9 Mutex Synch 11

    21/54

    Binary and Counting semaphores:

    functionality

    21

  • 8/6/2019 9 Mutex Synch 11

    22/54

    Binary and Counting semaphores:

    functionality

    22

    Notice: the queue and blocking

    behaviour not

    necessary; actually many semaphoreimplementations involve busy-waiting

    e.g. as in Petersons algo or the TAS metho

  • 8/6/2019 9 Mutex Synch 11

    23/54

    Example: Critical section of nprocesses using

    semaphores Shared variables

    var mutex: semaphore

    initially mutex= 1

    Process Pirepeat

    wait(mutex);

    23

    critical sectionsignal(mutex);

    remainder section

    until false;

  • 8/6/2019 9 Mutex Synch 11

    24/54

    Semaphore as GeneralSynchronization Tool

    E.g. execute Bin Pj only after Aexecuted in Pi: use semaphore flaginitialized to 0

    Pi Pj

    M M

    A wait (flag)

    signal(flag) B

    24

    Let Sand Qbe two semaphores initialized to 1P0 P1

    wait(S); wait(Q);

    wait(Q); wait(S);

    M Msignal(S); signal(Q);

    signal(Q) signal(S);

  • 8/6/2019 9 Mutex Synch 11

    25/54

    Other synchronization constructs: Condition Variables

    condition x;

    Two operations possible on a condition variable: x.wait () a process that invokes the operation is

    blocked.

    x.s gna un oc s one o processes any a

    invoked x.wait () (if any; else, does nothing)

    Other high-level synchronization constructs

    (conditional) critical regions (wait-until-value, ) monitors Cf courses on parallelism

  • 8/6/2019 9 Mutex Synch 11

    26/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 and for n processes

    26

    Semaphores and Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    Synchronization in message passing systems

  • 8/6/2019 9 Mutex Synch 11

    27/54

    Classical Problems of Synchronization

    Bounded-Buffer (producer-consumer)

    Dining-Philosophers (Resource allocation: we use as running

    example problem later, with deadlock avoidance) Readers and Writers (we use as running example later, withlock-free synch)

    27

    train on these: it is very useful and fun!

  • 8/6/2019 9 Mutex Synch 11

    28/54

    Bounded producer-consumer Buffer

    producer process

    do {

    consumer process

    do {

    Data: N locations, each can hold one item

    Synchronization variables:

    Binary semaphore mutex initialized to the value 1

    General semaphore avail-items initialized to the value 0 General semaphore avail-space initialized to the value N.

    // produce an item

    wait (avail-space);

    wait (mutex);

    // add the item to the buffer

    signal (mutex);

    signal (avail-items);

    } while (TRUE);

    wait (avail-items)wait (mutex);

    // remove an item from buffer

    signal (mutex);signal (avail-space);

    // consume the item

    } while (TRUE);

  • 8/6/2019 9 Mutex Synch 11

    29/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 and for n processes

    29

    Semaphores and Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    Synchronization in message passing systems

  • 8/6/2019 9 Mutex Synch 11

    30/54

    Understanding synchronizationbetter

    30

  • 8/6/2019 9 Mutex Synch 11

    31/54

    Mutex for n processes using read/write

    variables

    One idea (out several possible) : Before entering its criticalsection, each process receives a number. Holder of the

    smallest number enters the critical section.

    31

  • 8/6/2019 9 Mutex Synch 11

    32/54

    Lamports Bakery Algorithm

    (Mutex for n processes using R/W variables)

    Idea: Implement nummerlappar using read/write variables only numbering scheme may generate numbers in non-decreasing order

    of enumeration; i.e., 1,2,3,3,3,3,4,5

    If processes Piand Pjreceive the same number: if i

  • 8/6/2019 9 Mutex Synch 11

    33/54

    Lamports Bakery Algorithm (cont)

    Shared var choosing: array [0..n 1] of boolean (init false);number: array [0..n 1] of integer (init 0),

    repeat

    choosing[i] := true;number[i] := max(number[0], number[1], , number[n 1])+1;choosing[i] := false;for j:= 0 to n 1 do begin

    33

    while choosing[j] do no-op;while number[j] 0 and (number[j],j) < (number[i], i) do

    no-op;end;

    critical section

    number[i] := 0;remainder sectionuntil false;

    This is a more decentralized method:

    uses no variable writ-able by all

    processes

  • 8/6/2019 9 Mutex Synch 11

    34/54

    Elaborate, think

    Argue why Bakery algorithm satisfies the 3conditions for mutex

    Bakery algorithm idea not tied with R/W variables:how can it be implemented using e.g. semaphores?

    Having seen these, train on synchronizationconstructs,e.g: implementing counting semaphores from binary ones,

    semaphores using mutex solutions e.g. Petersons, Lamports, theTest-and-set method

    ....

    34

  • 8/6/2019 9 Mutex Synch 11

    35/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 and for n processes

    35

    Semaphores and Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    Synchronization in message passing systems

  • 8/6/2019 9 Mutex Synch 11

    36/54

    Solaris Synchronization

    Implements a variety of locks to supportmultitasking, multithreading (including real-time

    threads), and multiprocessing Uses adaptive mutexes for efficiency (small criticalsections)

    contention Blocked process queues are called turnstiles

    Uses condition variables and readers-writers lockswhen longer sections of code need access to data turnstiles hold threads waiting on reader-writer lock and

    conditional variables as well.

  • 8/6/2019 9 Mutex Synch 11

    37/54

    Windows XP Synchronization

    Uses interrupt masks to protect access to globalresources on uniprocessor systems

    Uses spinlocks on multiprocessor systems and shortcritical sections

    Also provides dispatcher objects which may act asmu exe n ema ore Dispatcher objects may provide events (similar to signal in a

    condition variable)

  • 8/6/2019 9 Mutex Synch 11

    38/54

    Linux Synchronization

    Linux provides:

    semaphores spin locks for multiprocessors and short critical sections

    On uniprocessors: lock at kernel level and kernel disables

  • 8/6/2019 9 Mutex Synch 11

    39/54

    Pthreads Synchronization

    Pthreads API is OS-independent

    It provides:

    mutex locks with blocking queues condition variables

    Non-portable extensions include: read-write locks spin locks

    P S h i ti

  • 8/6/2019 9 Mutex Synch 11

    40/54

    Process Synchronization:

    Roadmap In shared memory systems

    The critical-section (mutual exclusion - mutex) problem

    Mutex for 2 and for n processes

    40

    Semaphores and Other common synchronization structures

    Common synchronization problems

    n process mutex revisited

    Common OS cases (Linux, solaris, windows)

    Synchronization in message passing systems: mutex,coordination problems)

  • 8/6/2019 9 Mutex Synch 11

    41/54

    Synchronization using Message Passing communication

    Recall: Mechanism for rocesses to communicate and to

    synchronize (to some extend) their actions, via send(message) receive(message)

    Can be direct or via mail box

    Communication and synchronization with

  • 8/6/2019 9 Mutex Synch 11

    42/54

    Communication and synchronization with

    messagesMessage passing may be Blocking: synchronous

    Blocking send: sender blocks until the message is received Blocking receive: receiver block until a message is available

    both blocking : rendez-vous

    Non-blocking: asynchronous Non-blocking send: the sender sends the message and

    continues

    Non-blocking receive: receiver receive a valid message or null

    can also have interrupt-driven receive

    M t l x l si si m ss s:

  • 8/6/2019 9 Mutex Synch 11

    43/54

    Mutual exclusion using messages:

    Centralized ApproachKey idea: One processes in the system is chosen to

    coordinatethe entry to the critical section (CS):

    A process that wants to enter its CS sends a requestmessage to the coordinator.

    The coordinator decides which process can enter its CSnext, and sends to it a replymessage

    43

    After exiting its CS, that process sends a releasemessage to the coordinator

    Requires 3 messages per critical-section entry(request, reply, release)

    Depends on the coordinator (bottleneck)

    M t l l i i b

  • 8/6/2019 9 Mutex Synch 11

    44/54

    Mutual exclusion using message-box:(pseudo) decentralized approach

    Key idea: use a token that can beleft-at/removed-from acommon mailbox

    Requires 2 messages per critical-section entry (receive-, send-

    44

    Depends on a central mailbox(bottleneck)

    Distributed Algorithms for mutex using

  • 8/6/2019 9 Mutex Synch 11

    45/54

    Distributed Algorithms for mutex using

    messages Each node has only a partial picture of the total system and

    must make decisions based on this information

    All nodes bear equal responsibility for the final decision

    There exits no system-wide common clock with which toregulate the time of events

    45

    Mutual exclusion using messages:

  • 8/6/2019 9 Mutex Synch 11

    46/54

    Mutual exclusion using messages:

    distributed approach using token-passingKey idea: use a token (message mutex) that circulates among

    processes in a logical ring

    Process Pi

    repeatreceive(Pi-1, mutex);

    critical section

    46

    send(Pi+1

    , mutex);

    remainder section

    until false;

    Requires 2 (++) messages; can optimize to pass the token aroundon-request

    (if mutex-token isreceived when not-needed, must bepassed to Pi+1 at once)

    Mutex using messages:

  • 8/6/2019 9 Mutex Synch 11

    47/54

    Mutex using messages:fully distributed approach based on event ordering

    Key idea: similar to bakery algo (relatively order processesrequests) [Rikard&Agrawala81]

    Process i

    when statei=requestingstatei:= wait;oks:= 0;re _num:= ++C;

    47

    forall k send(k, req, req_num

    i

    )when receive(k,ack)

    if(++oks==n-1)then statei:= in_CS

    when forall kpendingisend(k,ack);

    pendingi:= ; statei:= dontcare;

    when receive(k, req, req_numk)Ci:= max{Ci, req_numk}+ 1;if(statei==dontcare or

    statei== waitand(ticketi,i) >(ticketk,k))

    then send(k,ack)else

  • 8/6/2019 9 Mutex Synch 11

    48/54

    Properties of last algo

  • 8/6/2019 9 Mutex Synch 11

    49/54

    Properties of last algo

    Mutex is guaranteed (prove by way of contradiction) Freedom from deadlock and starvation is ensured,

    since entry to the critical section is scheduledaccording to the ticket ordering, which ensures that there always exists a process (the one with minimum ticket)

    which is able to enter its CS and

    49

    processes are serve n a rs -come- rs -serve or er.

    The number of messages per critical-section entry is2 x (n 1).

    (This is the minimum number of required messages percritical-section entry when processes act independently andconcurrently.)

    Method used: Event Ordering by

  • 8/6/2019 9 Mutex Synch 11

    50/54

    Method used: Event Ordering by

    Timestamping Happened-beforerelation (denoted by ) on a set of events:

    If Aand Bare events in the same process, and Awas executed

    before B, then A

    B. If Ais the event of sending a message by one process and Bisthe event of receiving that message by another process, then A B.

    50

    If A Band B Cthen A C.

  • 8/6/2019 9 Mutex Synch 11

    51/54

    describing : logical timestamps

    Associate a timestampwith each system event. Require thatfor every pair of events Aand B:if A B, then the timestamp(A) < timestamp(B).

    Within eachprocess Pia logical clock, LCi is associated: asimple counter that is:

    51

    incremented between any two successive events executed within

    a process. advanced when the process receives a message whose timestamp

    is greater than the current value of its logical clock.

    If the timestamps of two events Aand Bare the same, thenthe events are concurrent. We may use the process identitynumbers to break ties and to create a total ordering.

  • 8/6/2019 9 Mutex Synch 11

    52/54

  • 8/6/2019 9 Mutex Synch 11

    53/54

    Producer(s)-consumer(s)

  • 8/6/2019 9 Mutex Synch 11

    54/54

    roducer(s) consumer(s)(bounded-buffer) using mailbox

    Key idea: similar as in the mailbox-mutex solution:

    use producer-tokens to allowproduce actions (to non-fullbuffer)

    use consume-tokens to allow

    54

    - -

    empty buffer)