Process Synchronization

49

Click here to load reader

Transcript of Process Synchronization

Synchronization

Synchronization

Baljit Singh Saini

Objectives To introduce critical section problemTo study solutions to critical section problemBaljit Singh Saini

SituationLet: counter = 5If I run two operations counter ++ and counter in any order:counter ++;counter--;orcounter -- ;counter++ ;What should be the value of counter?Ans: 5Baljit Singh Saini

Implementation in machine languagecounter ++counter--register1 = counterregister1 = register1 + 1counter = register1 register2 = counterregister2 = register2 - 1counter = register2

Baljit Singh Saini

Problem

P1 -> counter++register1 = counterregister1 = register1 + 1counter = register1

P2 -> counter--register2 = counterregister2 = register2 - 1counter = register2

TimeEquationregister1Register2CounterT=0register1 = counter55

Let P1 starts at t=0 and P2 starts at t=1Baljit Singh Saini

Problem

P1 -> counter++register1 = counterregister1 = register1 + 1counter = register1

P2 -> counter--register2 = counterregister2 = register2 - 1counter = register2

TimeEquationregister1Register2CounterT=0register1 = counter55T=1register1 = register1 + 1register2 = counter655

Let P1 starts at t=0 and P2 starts at t=1Baljit Singh Saini

Problem

P1 -> counter++register1 = counterregister1 = register1 + 1counter = register1

P2 -> counter--register2 = counterregister2 = register2 - 1counter = register2

TimeEquationregister1Register2CounterT=0register1 = counter55T=1register1 = register1 + 1register2 = counter655T=2counter = register1 register2 = register2 - 1646

Let P1 starts at t=0 and P2 starts at t=1Baljit Singh Saini

Problem

P1 -> counter++register1 = counterregister1 = register1 + 1counter = register1

P2 -> counter--register2 = counterregister2 = register2 - 1counter = register2

TimeEquationregister1Register2CounterT=0register1 = counter55T=1register1 = register1 + 1register2 = counter655T=2counter = register1 register2 = register2 - 1646T=3counter = register2 44

Let P1 starts at t=0 and P2 starts at t=1

Baljit Singh Saini

If the execution of the processes were interchanged i.e. P2 and then P1 the value of counter would have been?Ans: 6Baljit Singh Saini

Race ConditionA situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition.Baljit Singh Saini

Critical section problemCritical section that portion of code in which a process may be changing variables, updating a table, writing a file and so on.No two processes should be allowed to execute in their critical section at the same time.Critical section problem is to design a protocol that the processes can use to cooperate. Baljit Singh Saini

Structure of a Process Pdo {entry sectioncritical sectionexit sectionremainder section}while(TRUE);

Entry section : request permission to enter critical sectionExit section : to inform others that it has finished its critical sectionRemainder section : the remaining code

Baljit Singh Saini

Requirements Any solution should satisfy these three conditions:Mutual exclusionProgressBounded waitingBaljit Singh Saini

The First AlgorithmOnly two processProcesses are cyclicEach also executes some code other than critical sectionCode of both process/threads is presented as part of single module called MUTEX1Processes are individually schedulable and their individual actions may be interleavedTURN control variable, to access to shared resourceTURN can have value proc1 or proc2KEEPTESTING suggests that the test is repeatedly executed.

Baljit Singh Saini

program mutex 1;typewho = (proc1, proc2);varturn:= who;process p1;

beginwhile true dobeginwhile turn = proc2 do {keeptesting};critical_section;turn=proc2;other_p1_processingend[while]end ; {p1}}Baljit Singh Saini

process p2;

beginwhile true dobeginwhile turn = proc1 do {keeptesting};critical_section;turn=proc1;other_p2_processingend[while]end ; {p2}{parent process}begin {mutex 1}turn :=;initiate p1, p2;end{mutex1Baljit Singh Saini

IssuesProcess is required to know the identity of all other processes.while turn:=proc2 do [keeptesting];For large projects it would be difficult to know and keep track to all processes.2. Turn takingP1, P2, P1, P2, P1, P2, ..This forces the two processes to work at the collective speed of the slower one.Process crash/termination outside its critical sectionNo progress

Baljit Singh Saini

Second algorithmAvoidsTurn takingOverdependence on other processTwo flags: P1using and P2usingEach process updates its own flagBaljit Singh Saini

program mutex 2..varp1using, p2using:boolean

process p1

beginwhile true dobeginwhile p2using do keeptesting;p1using = truecritical sectionp1using = falseother_p1_processingend {while}end; {p1}

Baljit Singh Saini

process p2

beginwhile true dobeginwhile p1using do keeptesting;p2using = truecritical sectionp2using = falseother_p2_processingend {while}end; {p1}

[parent process]begin {mutex 2}piusing := false;p2using := false;initiate p1, p2end {mutex2}Baljit Singh Saini

Working

Initially P1using=falseP2using=false

Process P1While p2using do keeptesting;P1using = trueCSP1using = falseOther_p1_processing

Process P2While p1using do keeptesting;P2using = trueCSP2using = falseOther_p2_processing

Baljit Singh Saini

AdvantageProcess crashing outside its critical section does not pose a problemTurn-taking restriction is also removedDisadvantage Removes the primary restriction that at most one process should be in its critical sectionWhen both the processes try to enter their critical section at the same time.Baljit Singh Saini

Third Algorithmprogram mutex 3..varp1using, p2using:boolean

process p1

beginwhile true dobeginp1using = truewhile p2using do keeptesting;critical sectionp1using = falseother_p1_processingend {while}end; {p1}

Baljit Singh Saini

Process P2

beginwhile true dobeginp2using = truewhile p1using do keeptesting;critical sectionp2using = falseother_p2_processingend {while}end; {p2}

[parent process]begin {mutex 3}piusing := false;p2using := false;initiate p1, p2end {mutex3}

Baljit Singh Saini

Working

Initially P1using=falseP2using=false

Process P1P1using = trueWhile p2using do keeptesting;CSP1using = falseOther_p1_processing

Process P2P2using = trueWhile p1using do keeptesting;CSP2using = falseOther_p2_processing

Baljit Singh Saini

Third algorithmIssueIndefinite wait i.e. progress condition is not satisfied Baljit Singh Saini

Dekkers solutionBaljit Singh Saini

program dekker;..typewho:= (proc1, proc2)varturn : who;p1using, p2using:boolean

process p1

beginwhile true dobeginp1using := true;while p2using do if turn = proc2 then begin p1using := false; while turn = proc2 do keeptesting; p1using := true end; [if]critical section;turn := proc2;p1using := falseother_p1_processingend {while}end; {p1}

Baljit Singh Saini

process p2

beginwhile true dobeginp2using := true;while p1using do if turn = proc1 then begin p2using := false; while turn = proc1 do keeptesting; p2using := true end; [if]critical section;turn := proc1;p2using := falseother_p2_processingend {while}end; {p2}

Baljit Singh Saini

[parent process]begin {dekker}p1using:=false;p2using:=false;turn:=proc1;initiate p1,p2;end{dekker}Baljit Singh Saini

MCQA situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which access takes place is called :a) data consistencyb) race conditionc) agingd) starvationThe segment of code in which the process may change common variables, update tables, write into files is known as :a) programb) critical sectionc) non critical sectiond) synchronizingBaljit Singh Saini

The following three conditions must be satisfied to solve the critical section problem : (choose three)a) Agingb) Mutual Exclusionc) Deadlockd) Progresse) Bounded WaitingBounded waiting implies that there exists a bound on the number of times a process is allowed to enter its critical section :a) after a process has made a request to enter its critical section and before the request is grantedb) when another process is in its critical sectionc) before a process has made a request to enter its critical sectiond) None of theseBaljit Singh Saini

Petersons SolutionSoftware based solutionRestricted to two processesAlternate execution between their critical sections and remainder sectionsTwo processes Pi and Pj (where j = 1 i )Both shareint turn;boolean flag[2];turn indicates whose turn it is to enter critical sectionflag array indicates if a process is ready to enter critical sectionBaljit Singh Saini

Petersons Solutiondo {flag[i] = TRUE;turn = j;while (flag[j] && turn ==j); //keeptestingcritical sectionflag[i] = FALSE;remainder section }while(TRUE);

Baljit Singh Saini

All the conditions are satisfiedMutual ExclusionProgressBounded waitBaljit Singh Saini

SemaphoresSemaphore S is an integer variable which apart from initialization is accessed only through two standard atomic operations: wait() and signal().Wait() was originally termed as P (proberen : to test)Signal() was originally termed as V (verhogen: to increment)

Baljit Singh Saini

Definition of wait():wait(S) {while S