Process
Synchronization
Zohaib Latif
Office: 7e433
Email: [email protected]
Quick Review
• Why do we need Process Synchronization?
• What is the difference between Bounded and Unbounded Buffer?
• Define Race Condition?
• What is meant by Critical Section?
• Differentiate Entry and Exit Sections in Critical Section Problem?
• What are the three requirements to satisfy in Critical Section Problem?
Today’s Lecture
• Peterson’s Solution
• Locks (Acquire and Release)
Peterson’s Solution
• A classic software-based solution to the critical-section problem.
• May not work on the modern computer architectures.
• However, it provides a good algorithmic description of solving the critical section
problem and illustrates some of the complexities involved in designing software that
addresses the requirement of mutual exclusion, progress, and bounded waiting
requirements.
This solution is restricted to two processes that alternate execution between their
critical-sections and remainder-sections.
Let, there are two processes; Pi and Pj
Peterson’s Solution Requires Two Data Items to be Shared B/W Processes
int turn Indicates whose turn it is to enter its critical-section.
boolean flag[2] Indicates if a process is ready to enter its critical-section.
Structure of Process Pi Structure of Process Pj
do { Working do {
flag[i] = true; Turn = flag[j] = true;
j-i
turn = j; turn = i;
while( flag[j] && turn == j ); Flag[i] = while( flag[i] && turn == i );
False
-
True
Critical Section Critical Section
Flag[j] = False
True
-
flag[i] = false ; flag[j] = false ;
Remainder Section Remainder Section
} while(TRUE); } while(TRUE);
Peterson’s Solution
1. Mutual Exclusion (MUTEX)
2. Progress
3. Bounded Waiting
An Example NOT Following Progress
Two processes, P1 and P2, need to access a critical section of code. Consider the
following synchronization construct used by the processes.
Which process should enter the critical section first, when the value of turn = 0?
Mutual Exclusion Locks
acquire() {
• Acquire and Release Locks while (Lock == 1);
• Test and Set /* busy wait */
Lock = 1;
Acquire() and Release() Lock }
• A simplest hardware solution to the Synchronization Problem.
release() {
• There is a shared lock variable, which can take either of two Lock = 0;
}
values, 0 or 1.
• Before entering into critical section, a process inquires about the do {
lock.
Acquire lock
Critical Section
• If it is locked, it keeps on waiting till it becomes free.
Release lock
• If it is not locked, it takes the lock and execute the critical Remainder Section
section.
} while(TRUE);
Acquire and Release Lock
acquire() {
while (Lock == 1); release() {
/* busy wait */ Lock = 0;
Lock = 1; }
}
do {
Acquire lock Case 2
1
Critical Section Lock = 1
0
Release lock
Process = 1
Remainder Section
Process = 2
} while(TRUE);
Readings
• Dino Book
1. Peterson’s Solution 5.3
2. Mutex Locks 5.5
Next Lecture
• Lab Practice