CS211 Lec 14
CS211 Lec 14
Lecture # 14
1
Agenda for Today
Process synchronization
The Critical Section Problem
Conditions for a Good Solution
2-Process Critical Section Problem Solutions
2
Bounded-Buffer Problem
Producer process
item nextProduced;
…
while (1) {
nextProduced = getNewItem();
while (counter == BUFFER_SIZE) ;
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
3
Bounded-Buffer Problem
Consumer process
item nextConsumed;
while (1) {
while (counter == 0) ;
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
4
Bounded-Buffer Problem
“counter++” in assembly language
MOV R1, counter
INC R1
MOV counter, R1
6
Bounded-Buffer Problem
Assume counter is initially 5. One
interleaving of statements is:
8
Process
Synchronization
Critical Section: A piece of code in a
cooperating process in which the
process may updates shared data
(variable, file, database, etc.).
Critical Section Problem: Serialize
executions of critical sections in
cooperating processes
9
Process
Synchronization
More Examples
Bank transactions
Airline reservation
10
Bank Transactions
D Balance W
Deposit Withdrawal
MOV A, Balance MOV B, Balance
ADD A, Deposited SUB B, Withdrawn
MOV Balance, A MOV Balance, B
11
Solution of the Critical
Problem
Software based solutions
Hardware based solutions
Operating system based solution
12
Structure of Solution
do {
entry section
critical section
exit section
reminder section
} while (1);
13
Solution to Critical-Section
Problem
2-Process Critical Section Problem
N-Process Critical Section Problem
Conditions for a good solution:
1. Mutual Exclusion: If a process is
executing in its critical section, then no
other processes can be executing in their
critical sections.
14
Solution to Critical-Section
Problem
2. Progress: If no process is executing in its
critical section and some processes wish
to enter their critical sections, then only
those processes that are not executing in
their remainder sections can decide which
process will enter its critical section next,
and this decision cannot be postponed
indefinitely.
15
Solution to Critical-Section
Problem
3. Bounded Waiting: A bound must exist on
the number of times that other processes
are allowed to enter their critical sections
after a process has made a request to
enter its critical section and before that
request is granted.
16
Solution to Critical-Section
Problem
Assumptions
Assume that each process executes
at a nonzero speed
No assumption can be made
regarding the relative speeds of
the N processes.
17
Possible Solutions
Only 2 processes, P0 and P1
Processes may share some common
variables to synchronize their actions.
General structure of process Pi
do {
entry section
critical section
exit section
remainder section
} while (1);
18
Algorithm 1
Shared variables:
int turn;
initially turn = 0
turn = i Pi can enter its
critical section
19
Algorithm 1
Process Pi
do {
while (turn != i);
critical section
turn = j;
remainder section
} while (1);
Does not satisfy the progress condition
20
Algorithm 2
Shared variables
boolean flag[2]; // Set to false
flag [i] = true Pi ready to enter its critical section
21
Algorithm 2
Process Pi
do {
flag[i] = true;
while (flag[j]);
critical section
flag[i] = false;
remainder section
} while (1);
Does not satisfy the progress condition 22
Algorithm 3
Combined shared variables of
algorithms 1 and 2.
boolean flag[2]; // Set to false
int turn=0;
23
Algorithm 3
Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j) ;
critical section
flag[i] = false;
remainder section
} while (1);
24
End of
Lecture # 14
25