Implementation Report: Concurrent Genetic Algorithm with ...
peterson algorithm and implementation of algorithm
-
Upload
vijay-verma -
Category
Documents
-
view
234 -
download
0
Transcript of peterson algorithm and implementation of algorithm
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 1/29
Process Synchronization
&Mutual Exclusion
Sumit RVijay Verma
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 2/29
Synchronization Concepts
In uniprocessor systems the only way concurrency isachieved is through interleavings of instruction
In that case ,an instruction executions of one process
may affect those of other processes if the instructionexecutions reference the same piece of shared data
In multiprocess systems,programs execute and access
data from their respective private address spaces.
From time to time ,they execute system call serviceroutines from kernel space.
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 3/29
The system call may access the same kerneldata .
Here also problems will arise because of concurrency .The operating system mustensure that process see consistent values of shared data .The data values must satisfy apriori specified integrity constraints
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 4/29
Race Condition
A situation where the outcome is unpredictablelike in a race ,is called race condition
It is also known as data race.A data conflictoccurs when proccess access the same shareddata concurrently and at least one modifies the
data.
When the conflicting accesss are notsynchronized a data race is said to occur
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 5/29
Critical sectionThat part of the program where shared resources areaccessed
When a process executes code that manipulates shared data(or resource), we say that the process is in a critical section (CS) (for that resource)
The value of shared variables collectively determine the stateof critical section
A state is said to be valid or consistent if it satisfies someintegrity constraint
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 6/29
An integrity constraint specifies permittedvalues of the shared variables
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 7/29
Critical Section or SynchronizationProblem
Problem: is to order executions of the critical sectionsatisfying some specific constraints on the sharedvariables,so that this constraints are not voilated
Process 1
Process 2
CS 1
Time
CS 2
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 8/29
The Mutual Exclusion Problem
It is the most fundamental problem of synchronizationIn this the synchronization constraintsspecify mutually exclusive executions of the critical section.That is,no two processes can execute the
critical section concurrently
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 9/29
Solution to Critical section problem
The process follow some protocols in their executions of the critical section
A solution to a synchronization problem entails designing
these protocols for processes to be followed beforeentering and after exiting the critical section
The steps in the protocol are executed in the parts are
called entry section and exit section
A solution to a critical section problem involvesdesigning codes for the entry and exit sections
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 10/29
Framework for analysis of solutionsEach process
executes at nonzerospeed but noassumption on therelative speed of n
processesGeneral structure of aprocess:
No assumptions about
order of interleavedexecution
The central problem is
to design the entry and exit sections
repeat
entry section critical section
exit section remainder section
forever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 11/29
Three Key Requirements for a ValidSolution to the Critical Section Problem
Safety :The intented condition will never be violated .For example in mutual exclusion at any time, at most oneprocess can be executing critical section (CS) code
Progress : If no process is in its CS and there are one or more processes that wish to enter the CS, this selectioncannot be postponed indefinitely• no process in its remainder section can participate in this
decision
Bounded Waiting : After a process P has made a request toenter its CS, there is a limit on the number of times that theother processes are allowed to enter their CS, before P’srequest is granted
• (otherwise the process could suffer from starvation)
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 12/29
Types of Solutions
No special OS mechanisms (software approach.)• algorithms whose correctness relies only on the
assumption that only one process at a time canaccess a memory location
Hardware solutions • rely on special machine instructions for “locking”
Operating System and ProgrammingLanguage solutions (e.g. Java) • provide specific functions and data structures for
the programmer to use for synchronization
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 13/29
Faulty Algorithm 1 - Turn takingThe shared variableturn is initialized (to 0or 1) before executingany PiPi’s critical section is
executed iff turn = iPi is busy waiting if Pjis in CS
Process Pi:// i,j= 0 or 1
repeat
while(turn!=i){}; CS
turn:=j; RS
forever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 14/29
Process P0:
repeat
while(turn!=0){}; CS
turn:=1; RSforever
Process P1:repeat
while(turn!=1){}; CS
turn:=0; RS
forever
Faulty Algorithm 1 side-by-side view
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 15/29
Analysis
Achieves Mutual Exclusion (busy wait)
But Progress requirement is not satisfiedsince it requires strict alternation of CS’s.• If one process requires its CS more often than
the other, it can’t get it.
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 16/29
Faulty Algorithm 2 – Ready flagKeep a Boolean variable
for each process: flag[0]and flag[1]Pi signals that it is readyto enter its CS by:
flag[i]:=true but waitsuntil the other hasfinished its CS.
Process Pi:repeat
flag[i]:=true;
while(flag[j]){}; CSflag[i]:=false;
RSforever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 17/29
Process P0:
repeatflag[0]:=true;
while(flag[1]){}; CS
flag[0]:=false; RS
forever
Process P1:
repeatflag[1]:=true;
while(flag[0]){}; CS
flag[1]:=false; RS
forever
Faulty Algorithm 2 side-by-side view
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 18/29
Analysis
Mutual Exclusion is satisfied but not theprogress requirementFor the (interleaved) sequence:
• flag[0]:=true• flag[1]:=true
Both processes will wait forever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 19/29
Algorithm 3(Peterson’s Algorithm)
Initialization:flag[0]:=flag[1]:=falseturn:= 0 or 1Wish to enter CSspecified byflag[i]:=trueEven if both flags goup, and no matter howthe instructions areinterleaved,• ..turn will always end
up as either 0 or 1
Process Pi:repeat
flag[i]:=true;// I want in
turn:=j;// but you can go first!
while(flag[j]&& turn==j); CS
flag[i]:=false;// I’m done
RS
forever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 20/29
Process P0:
repeatflag[0]:=true;
// 0 wants in
turn:= 1;// 0 gives a chance to 1
while(flag[1]&turn=1); CS
flag[0]:=false;// 0 is done
RSforever
Process P1:repeat
flag[1]:=true;// 1 wants in
turn:=0;// 1 gives a chance to 0
while(flag[0]&turn=0);
CSflag[1]:=false;
// 1 is done
RSforever
Peterson’s algorithm side -by-side view
Peterson’s Algorithm
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 21/29
Peterson’s Algorithm: Proof of Correctness
Mutual exclusion holds since:• For both P 0 and P 1 to be in their CS
both flag[0] and flag[1] must be true and :turn=0 and turn=1 (at same time): impossible
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 22/29
Proof (“progress”)
Progress requirement:
• Pi can be kept out of CS only if stuck in while loopflag[j] = true and turn = j.
• If Pj not ready to enter CS then flag[j] = falsePi can then enter its CS
• If Pj has set flag[j], it is also in its while loop, then either P i or P j will go depending on value of turn
• Therefore the progress condition is met
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 23/29
Proof (“Bounded Waiting”) • Suppose P j gets to go this time
Can it go a second time withoutletting P i go?
• If Pj enters CS , then turn=jbut will then reset flag[ j]=false on
exit:allowing P i to enter CS
• What if P j tries again, and hastime to reset flag[ j]=true before P i gets to its CS?
It must also set turn=i• since Pi is (stuck) past the point
where it sets turn= j:Pi will get to enter CS
after at most one CS entry by Pj
Process Pi:repeat
flag[i]:=true;// I want in
turn:=j;// but you can go first!
while(flag[j]&& turn==j); //(loop)
CSflag[i]:=false;
// I’m done
RSforever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 24/29
What About Process Failures?
If all 3 criteria are satisfied, a valid solution will berobust for failure of a process in its remainder section (RS) since failure in RS is just like havingan infinitely long RS.
However, no valid solution can providerobustness against a process failing in its criticalsection (CS).Therefore a process Pi that fails in its CS mustsignal this fact to other processes.
Al i h 3
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 25/29
Algorithm 3(Peterson’s Algorithm)
Initialization:flag[0]:=flag[1]:=falseturn:= 0 or 1Wish to enter CSspecified byflag[i]:=trueEven if both flags goup, and no matter howthe instructions areinterleaved,• ..turn will always end
up as either 0 or 1
Process Pi:repeat
flag[i]:=true;// I want in
turn:=j;// but you can go first!
while(flag[j]&& turn==j); CS
flag[i]:=false;// I’m done
RS
forever
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 26/29
f f
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 27/29
Peterson’s Algorithm: Proof of Correctness
Mutual exclusion holds since:• For both P 0 and P 1 to be in their CS
both flag[0] and flag[1] must be true and :turn=0 and turn=1 (at same time): impossible
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 28/29
Proof (“progress”)
Progress requirement:
• Pi can be kept out of CS only if stuck in while loopflag[j] = true and turn = j.
• If Pj not ready to enter CS then flag[j] = falsePi can then enter its CS
• If Pj has set flag[j], it is also in its while loop, then either P i or P j will go depending on value of turn
• Therefore the progress condition is met
7/29/2019 peterson algorithm and implementation of algorithm
http://slidepdf.com/reader/full/peterson-algorithm-and-implementation-of-algorithm 29/29
Proof (“Bounded Waiting”) • Suppose P j gets to go this time
Can it go a second time withoutletting P i go?
• If Pj enters CS , then turn=jbut will then reset flag[ j]=false on
exit:allowing P i to enter CS
• What if P j tries again, and hastime to reset flag[ j]=true before P i gets to its CS?
It must also set turn=i• since Pi is (stuck) past the point
where it sets turn= j:Pi will get to enter CS
after at most one CS entry by Pj
Process Pi:repeat
flag[i]:=true;// I want in
turn:=j;// but you can go first!
while(flag[j]&& turn==j); //(loop)
CSflag[i]:=false;
// I’m done
RSforever