Process Synchronization[2]

Embed Size (px)

Citation preview

  • 7/25/2019 Process Synchronization[2]

    1/44

    PROCESS

    SYNCHRONIZATION

    OPERATING SYSTEM

  • 7/25/2019 Process Synchronization[2]

    2/44

    TOPICS COVERED:

    BackgroundThe Critical-Section Problem

    Peteron! SolutionS"nchroni#ation $ard%areSema&hore'onitorClaic Problem o( S"nchroni#ationS"nchroni#ation E)am&le*lternati+e *&&roache

  • 7/25/2019 Process Synchronization[2]

    3/44

    B*C,RO./DPROD.CER-CO/S.'ER PROB0

    R*CE CO/DIT

  • 7/25/2019 Process Synchronization[2]

    4/44

    PROD.CER-CO/S.'ERPROB0E'The &roducer1conumer &roblem i a claic e)am&

    multi-&roce "nchroni#ation &roblem2 The &roblemdecribe t%o &rocee3 the &roducer and the con%ho hare a common3 4)ed-i#e bu5er2

    The &roducer6 7ob i to generate a &iece o( data3 &uinto the bu5er and tart again2 *t the ame time3 th

    conumer i conuming the one &iece at a time2

  • 7/25/2019 Process Synchronization[2]

    5/44

    R*CE CO/DITIO/

    %here e+eral &rocee acce and mani&uthe ame data concurrentl"

    the outcome o( the e)ecution de&end on th&articular order in %hich the acce take &l

  • 7/25/2019 Process Synchronization[2]

    6/44

    CRITIC*0-SECTIO/PROB0E'

  • 7/25/2019 Process Synchronization[2]

    7/44

    General structureof a typical processEntry SectionCode re8ueting entr" into the

    critical ection2

    Critical Section Code in %hich onl" one&roce can e)ecute at an" onetime2

    Exit Section The end o( the critical ection3releaing or allo%ing other in2

    Reain!er Section Ret o( the code *9TER

    the critical ection2

  • 7/25/2019 Process Synchronization[2]

    8/44

    Critical-Section Problem

    Conider "tem o( n&rocee P0, P1, , Pn-1

    Each &roce ha a critical section egment o( c

    1 Process may be changing common variables, uptable, writing fle, etc.

    1 When one process is in its critical section, no otexecuting in its critical section

    Critical-section problem i to deign a &rotocolthi2

  • 7/25/2019 Process Synchronization[2]

    9/44

    Critical-Section ProblemRe8uirement1.Mutual Exclusion - If process P

    i is executing in its critical section, the

    processes can be executing in their critical sections

    2. Progress - If no process is executing in its critical section and t

    processes that wish to enter their critical section, then

    processes not in their remainder section can participate

    of the process that will enter its critical section next; the be postponed indefinitely

    3. Bounded Waiting - A bound must exist on the number of times that o

    are allowed to enter their critical sections after a process

    request to enter its critical section and before that reque

  • 7/25/2019 Process Synchronization[2]

    10/44

    PETERSO/!SO0.TIO/

    Click icon to add &icture

  • 7/25/2019 Process Synchronization[2]

    11/44

    Structure o( &roce Piin Peteron! olutionSoftware solution; no hardware support

    Two process solutionThe two processes share two variables:

    int turn;

    Boolean flag[2];

    turnindicates whose turn it is to enter thecritical section

    Theflagarray indicates whether aprocess is ready to enter the criticalsection

    flag[i]= true ==> processPiis ready

    Click icon to add &icture

  • 7/25/2019 Process Synchronization[2]

    12/44

    PETERSO/!S SO0.TIO/

  • 7/25/2019 Process Synchronization[2]

    13/44

    S=/C$RO/I>*TIO/$*RD

  • 7/25/2019 Process Synchronization[2]

    14/44

    S"nchroni#ation $ard%are*OC+ING( &rotecting critical ection through the

    lock

    .ni&roceor 1 could diable interru&t1 Currently running code would execute without 1 Generally too inecient on multiprocessor syst

    'an" "tem &ro+ide hard%are u&&ort (or criticcode more e?cient and eaier (or &rogrammer

    'odern machine &ro+ide &ecial atoic ,ar!)ainstructions

    *tomic @ non-interru&table1 Either test memory word and set value1 r swap contents o! two memory words

  • 7/25/2019 Process Synchronization[2]

    15/44

    tetAandAet intruction

    De4nition o( thetetAandAet intruction

    Boolean test_and_set(boolean *target)

    {

    boolean rv = *target;

    *target = TRUE;

    return rv:

    }

    'utual-e)cluionim&lementation

    do { while(test_and

    ; #* do nothi#* !riti!al se!t

    lo!" = $alse;

    #* re%ainder se!

    } while (true);

    T%o test_and_set()cannot be e)ecutedimultaneoul"

    Shared boolean +ariablelo!"

    3 initiali#ed to9*0SE

  • 7/25/2019 Process Synchronization[2]

    16/44

    com&are AandA%a& intruct

    De4nition o( thecom&areAandA %a&intruction

    int !o%&are_and_swa&(int*value' int e&e!ted' int

    new value){int te%& = *value;

    i$ (*value == e&e!ted)*value = new value;

    return te%&;

    }

    'utual-e)cluionim&lementationdo {while (!o%&are_and_s) += ) ##value'e

    ; #* do nothing

    #* !riti!al se!t

    lo!" = ;

    #* re%ainder se!

    } while (true);

    The com&are and %a& intruction3 in contrat to theet intruction3 o&erate on three o&erand

  • 7/25/2019 Process Synchronization[2]

    17/44

    '.TE 0OC,

  • 7/25/2019 Process Synchronization[2]

    18/44

    '.TE 0OC,S

    OS deigner build o(t%are tool to ol+e critical ect

    Sim&let "nchroni#ation i done %ith utex -utuaexclusion. loc/

    Protect critical region b" 4rt a!,uire()a lock then&rocee attem&ting to get the lock are blocked3 andrelease()it

    a!,uire() { while (+available)

    ; #* bus- wait *#

    available = $alse;

    }

    Sol tion to critical

  • 7/25/2019 Process Synchronization[2]

    19/44

    Solution to critical(section pro'le

    usin% utex loc/s Calls toa!,uire()andrelease()must beatomic Usually implemented via hardware atomicinstructions

    But this solution requiresbusy waiting

    All other processes trying to get the lock

    must continuously loop This lock is therefore called aspinlock

    Very wasteful of CPU cycles

    They are often employed on multiprocessor

    systems

  • 7/25/2019 Process Synchronization[2]

    20/44

    SE'*P$ORE

  • 7/25/2019 Process Synchronization[2]

    21/44

    Sema&hore

    S"nchroni#ation tool that doe not re8uire bu" %ai

    Sema&hore S1 integer +ariable

    T%o tandard o&eration modi(" S :wait()and sig

    0e com&licated

    Can onl" be acceed +ia t%o indi+iible atomico&eration

    wait (.) {while (. /= )

    ; ## bus- wait .00;}

  • 7/25/2019 Process Synchronization[2]

    22/44

    Sema&hore .age

    Countin% seap,ore 1 integer +alue can range o+er an u

    domain0inary seap,ore 1 integer +alue can range onl" bet%ee

    1 "hen e#uivalent to a mutex lock

    Can ol+e +ariou "nchroni#ation &roblem

    Conider P1 and P2 that re8uireS1 to ha&&en be(ore S2

    P1: S1;

    signal(synch);

    P2:

    wait(synch); 0

    S2;

  • 7/25/2019 Process Synchronization[2]

    23/44

    Deadlock and Star+ation

    1ea!loc/ 1 t%o or more &rocee are %aiting inde4nitee+ent that can be caued b" onl" one o( the %aiting &roc

    0et S andQbe t%o ema&hore initiali#ed to F P2 P"

    wait(.); //exec 1st wait(2);

    wait(2); //exec 3rd wait(.);

    333 333

    signal(.); signal(2); signal(2); signal(.);

    Star3ation1 in!e4nite 'loc/in%1 $ process may never be removed !rom the semaphor

    which it is suspended

  • 7/25/2019 Process Synchronization[2]

    24/44

    C*ASSIC PRO0*EMO5 SYNCHRONIZATIO

  • 7/25/2019 Process Synchronization[2]

    25/44

    The Bounded-Bu5er Problem

    Thi i a generali#ation o( the &roducer-conumer &ro

    %herein acce i controlled to a hared grou& o( bu5o( a limited i#e2

    In our &roblem3 the &roducer and conumer &roceehare the (ollo%ing data tructure:

  • 7/25/2019 Process Synchronization[2]

    26/44

    The Bounded-Bu5er Problem

    tructure o( the&roducer &roce

    do {

    333 #* &rodu!e an ite% in net_&rodu!ed *#

    333

    wait(e%&t-);

    wait(%ute);

    333 #* add net_&rodu!ed to the bu$$er *#

    333

    signal(%ute);

    signal($ull);

    } while (true);

    structure of theconsumer pr

    do {

    wait($ull);

    wait(%ute);

    333#* re%ove an ite% $ro% bu$$er to

    333

    signal(%ute);

    signal(e%&t-);

    333#* !onsu%e the ite% in net_!onsu

    333

    } while (true);

  • 7/25/2019 Process Synchronization[2]

    27/44

    The Reader1

  • 7/25/2019 Process Synchronization[2]

    28/44

    The Reader1

  • 7/25/2019 Process Synchronization[2]

    29/44

    The Dining-Philoo&her Probl Conider 4+e &hiloo&her itting around a

    table3 in %hich there are 4+e inglecho&tick e+enl" ditributed and an

    endle bo%l o( rice in the center3 aho%n in the diagram2

    Thee &hiloo&her &end their li+ealternating bet%een t%o acti+itie: eatingand thinking2

  • 7/25/2019 Process Synchronization[2]

    30/44

    The Dining-Philoo&her Probl

    One im&le olution i to re&reent each cho&tick %ith a ema&hiloo&her trie to grab a cho&tick b" e)ecuting a )ait-. o&ema&hore2 She releae her cho&tick b" e)ecuting the si%n

    Some &otential olution to the &roblem include:

    Onl" allo% (our &hiloo&her to dine at the ame time2 0imultaneou &rocee2

    *llo% &hiloo&her to &ick u& cho&tick onl" %hen both aa critical ection2

    .e an a"mmetric olution3 in %hich odd &hiloo&her &cho&tick 4rt and e+en &hiloo&her &ick u& their right c

  • 7/25/2019 Process Synchronization[2]

    31/44

    'O/ITOR

    ' i

  • 7/25/2019 Process Synchronization[2]

    32/44

    'onitor

    * high-le+el abtraction that&ro+ide a con+enient ande5ecti+e mechanim (or&roce "nchroni#ation

    $bstract data type3 internal+ariable onl" acceible b"code %ithin the &rocedure

    Onl" one &roce ma" beacti+e %ithin the monitor at atime

    Sc,eatic 3ie)

    C diti V i bl

  • 7/25/2019 Process Synchronization[2]

    33/44

    Condition Variable

    * con!ition 3aria'lei baicall" a container o( threthat are %aiting (or a certain condition2

    * +ariable o( t"&e condition ha onl" t%o legalo&eration3 )aitand si%nal2

    1 "he wait operation bloc)s a process until some otprocess calls signal, and adds the bloc)ed procesa list associated with that condition.

    1 "he signal process does nothing i! there are noprocesses waiting on that condition.

  • 7/25/2019 Process Synchronization[2]

    34/44

    Condition Variable

  • 7/25/2019 Process Synchronization[2]

    35/44

    S=/C$RO/I>*TIO/E*'P0E

    S nchroni ation in

  • 7/25/2019 Process Synchronization[2]

    36/44

    S"nchroni#ation in

  • 7/25/2019 Process Synchronization[2]

    37/44

    'ute) di&atcher ob7ect

  • 7/25/2019 Process Synchronization[2]

    38/44

    S"nchroni#ation in 0inu)

    The im&let "nchroni#ation techni8ue %ithin the 0

    kernel i an atomic integer3 %hich i re&reented uo&a8ue data t"&e atoic8t#

    The (ollo%ing code illutrate declaring an atomic incounterand then &er(orming +ariou atomic o&era

    S"nchroni#ation in 0inu)

  • 7/25/2019 Process Synchronization[2]

    39/44

    S"nchroni#ation in 0inu) *tomic integer are &articularl" e?cient in ituation

    %here an integer +ariable uch a a counternebe u&dated3 ince atomic o&eration do not re8uire

    o+erhead o( locking mechanim2 0inu) alo &ro+ide &inlock and ema&hore a %

    reader1%riter +erion o( thee t%o lock (or lockinthe kernel2

    S h i ti i S l i

  • 7/25/2019 Process Synchronization[2]

    40/44

    S"nchroni#ation in Solari Im&lement a +ariet" o( lock to u&&ort multitaking3

    multithreading including real-time thread3 and multi&r

    .e a!apti3e utex loc/s(or e?cienc" %hen &rotect(rom hort code egment

    1 +tarts as a standard semaphore spin0loc)

    .e con!ition 3aria'les

    .e rea!ers()riterslock %hen longer ection o( codacce to data

    .e turnstilesto order the lit o( thread %aiting to acan ada&ti+e mute) or reader-%riter lock

  • 7/25/2019 Process Synchronization[2]

    41/44

    Pthread S"nchroni#ation

    Pthread *PI i OS-inde&endent

    It &ro+ide:

    1 mutex loc)s

    1 condition variables

    1 read0write loc)s

    Pthread ue the pt,rea!8utex8t data t"&e (or m

  • 7/25/2019 Process Synchronization[2]

    42/44

    *0TER/*TIV*PPRO*C$E

    Tranactional 'emor"

  • 7/25/2019 Process Synchronization[2]

    43/44

    Tranactional 'emor"

    *ure that o&eration ha&&en a a ingle logical uin it entiret"3 or not at all

    Related to the 4eld o( databae "tem

    Challenge i auring atomicit"3 de&ite com&uter (ailure

    Tranaction - collection o( intruction or o&eration&er(orm ingle logical (unction

    1 1ere we are concerned with changes to stable s1 "ransaction is series o! readand writeoperation

    Soft)are transactional eory ST'3 a the nugget3 im&lement tranactional memor" e)cluo(t%are

    Har!)are transactional eory $T' re8uire

  • 7/25/2019 Process Synchronization[2]

    44/44

    9unctional Programming0anguage9unctional language do not maintain tate

    1 nce a variable is assigned a value, it canchange anymore

    /o uch &roblem a race condition and

    deadlockThere(ore uitable (or concurrentJ&arallel

    &rogramming on multicore

    E2g2: Erlang3 Scala