Title: The Critical-Section Problem (Two-Process Solution)
1The Critical-Section Problem (Two-Process
Solution)
- Algorithm 1
- What is the problem here?
//turn is global and initialized to
0 repeat while turnltgti do no-op //enters
critical section turn j //remainder
section until false
2The Critical-Section Problem (Two-Process
Solution)
- Algorithm 2
- What is the problem here?
var flag array 0..1 of boolean repeat flagi
true while flagj do no-op //enters
critical section flagi false //remainder
section until false
3The Critical-Section Problem (Two-Process
Solution)
- Algorithm 3
- Why does this solution work?
- Does it satisfy all three properties?
var flag arry 0..1 of boolean turn
0..1 repeat flagi true turn j while
(flagj and turn j) do no-op //enters
critical section flagi false //remainder
section until false
4The Critical-Section Problem (using Test-and-Set)
- What does this hardware primitive do? (how is it
defined?) - How do we use it to solve the critical section
problem?
5The Critical-Section Problem (using Test-and-Set)
- Solution (p.165 in the book)
- Does it satisfy all three properties?
//lock is global and initialized to
false repeat while Test-and-Set(lock) do
no-op //enters critical section lock
false //remainder section until false
6The Critical-Section Problem (using Semaphores)
- Semaphore definition
- Usage for solving critical section problem
- What about changing semaphore definition to
- Does it satisfy all three properties?
wait(S) while slt0 do no-op
S S-1 signal(S) S S1
//mutex is a semaphore and is initialized to
1 repeat wait(mutex) //enters critical
section signal(mutex) //remainder
section until false
wait(S) S S-1 signal(S) S S1
7The Bounded-Buffer Problem (using Semaphores)
//mutex, empty, full are all semaphores //initiall
y, mutex1 emptyn full0 //producer repeat /
/produce r P(empty) P(mutex) add r to
buffer V(mutex) V(full) until
false //consumer repeat P(full) P(mutex) re
move r from buffer V(mutex) V(empty) process
r until false
8The Bounded-Buffer Problem (using Semaphores)
- Question Is this solution efficient? (Imagine a
buffer with n slots) - How can it be changed to improve its efficiency?
9More on Semaphores...
- Historical background
- What is the problem with semaphores?
First introduced by Dijkstra when describing the
THE system (Edsger W. Dijkstra. The Structure of
the "THE" Multiprogramming System.
Communications of the ACM 11(5), May 1968.)
Semaphores only work when all processes are
well-behaved!
10More on Semaphores...
- How to overcome the problem with untrustworthy
semaphores? - Critical regions
- Monitors
- Using message buffering and message queues for
each process - (Per Brinch Hansen. The Nucleus of a
Multiprogramming System. Communications of the
ACM 13(4), April 1970. ) - send message (receiver, message, buffer)
- wait message (sender, message, buffer)
- send answer (result, answer, buffer)
- wait answer (result, answer, buffer)
- What are the potential complications here?