Professional Documents
Culture Documents
09/13/20 COP5611 1
Announcement
• Next Tuesday (Feb. 18, 2003) Dr. Ted Baker
will lecture on distributed deadlock detection
(Chapter 7)
– Please read the materials ahead of time
– Please review the local deadlock detection
algorithms (Chapter 3)
– I will post his lecture notes on the class web too
09/13/20 COP5611 2
The Critical Section Problem
• When processes (centralized or distributed)
interact through shared resources, the
integrity of the resources may be violated if
the accesses are not coordinated
– The resources do not record all the changes
– A process may obtain inconsistent values
– The final state of the shared resource may be
inconsistent
09/13/20 COP5611 3
An Example
• Suppose we have two processes, (one is
called producer and the other one consumer),
the shared variable counter is 5 initially
– Producer: counter = counter +1
P1: load counter, r1
P2: add r1, #1, r2
P3: store r2, counter
– Consumer: counter = counter - 1
C1: load counter, r1
C2: add r1, #-1, r2
C3: store r2, counter
09/13/20 COP5611 4
An Example - cont.
• A particular execution sequence
– P1: load counter, r1
– P2: add r1, #1, r2
--- Context switch ----
– C1: load counter, r1
– C2: add r1, #-1, r2
– C3: store r2, counter
--- Context switch ----
– P3: store r2, counter
– What is the value of counter?
09/13/20 COP5611 5
An Example - cont.
• A particular execution sequence
– C1: load counter, r1
– C2: add r1, #-1, r2
--- Context switch ----
– P1: load counter, r1
– P2: add r1, #1, r2
– P3: store r2, counter
--- Context switch ----
– C3: store r2, counter
– What is the value of counter this time?
09/13/20 COP5611 6
An Example - cont.
• A particular execution sequence
– C1: load counter, r1
– C2: add r1, #-1, r2
– C3: store r2, counter
--- Context switch ----
– P1: load counter, r1
– P2: add r1, #1, r2
– P3: store r2, counter
--- Context switch ----
– What is the value of counter this time?
09/13/20 COP5611 7
Mutual Exclusion
• One solution to the problem is that at any
time at most only one process can access the
shared resources
– This solution is known as mutual exclusion
– A critical section is a code segment in a process
which shared resources are accessed
• A process can have more than one critical section
• There are problems which involve shared resources
where mutual exclusion is not the optimal solution
09/13/20 COP5611 8
The Structure of Processes
• Structure of process Pi
repeat
entry section
critical section
exit section
reminder section
until false;
09/13/20 COP5611 9
Mutual Exclusion in Traditional OS
signal (S): S := S + 1;
09/13/20 COP5611 10
Mutual Exclusion in Traditional OS – cont.
• Shared variables
– var mutex : semaphore
– initially mutex = 1
• Process Pi
repeat
wait(mutex);
critical section
signal(mutex);
remainder section
until false;
09/13/20 COP5611 11
Distributed Mutual Exclusion
• Due to the absence of shared memory, solutions
for mutual exclusion based on shared memory
cannot be used in distributed systems
– It must be based on message passing
09/13/20 COP5611 12
Performance Measure for Distributed Mutual Exclusion
09/13/20 COP5611 14
A Centralized Algorithm
• It is a simple solution
– One site, called the control site, is responsible for
granting permission to the CS execution
– To request the CS, a site sends a REQUEST
message to the control site
• When a site is done with CS execution, it sends a
RELEASE message to the control site
– The control site queues up the requests for the
CS and grant them permission
09/13/20 COP5611 15
A Centralized Algorithm – cont.
a) Process 1 asks the coordinator for permission to enter a critical region. Permission is
granted
b) Process 2 then asks permission to enter the same critical region. The coordinator does
not reply.
c) When process 1 exits the critical region, it tells the coordinator, when then replies to 2
09/13/20 COP5611 16
A Centralized Algorithm – cont.
09/13/20 COP5611 17
Distributed Solutions
• Non-token-based algorithms
– Use timestamps to order requests and resolve
conflicts between simultaneous requests
– Lamport’s algorithm and Ricart-Agrawala
Algorithm
• Token-based algorithms
– A unique token is shared among the sites
– A site is allowed to enter the CS if it possess the
token and continues to hold the token until its CS
execution is over; then it passes the token to the
next site
09/13/20 COP5611 18
Lamport’s Distributed Mutual Exclusion Algorithm
09/13/20 COP5611 20
Lamport’s Distributed Mutual Exclusion Algorithm – cont.
09/13/20 COP5611 21
Lamport’s Distributed Mutual Exclusion Algorithm – cont.
09/13/20 COP5611 22
Lamport’s Distributed Mutual Exclusion Algorithm – cont.
09/13/20 COP5611 23
Lamport’s Distributed Mutual Exclusion Algorithm – cont.
• Performance analysis
– Number of messages per CS
– Synchronization delay
– Throughput
– Response time
09/13/20 COP5611 24
Ricart-Agrawala Algorithm
09/13/20 COP5611 25
Ricart-Agrawala Algorithm – cont.
09/13/20 COP5611 26
Ricart-Agrawala Algorithm – cont.
09/13/20 COP5611 27
Ricart-Agrawala Algorithm – cont.
• Performance analysis
– Number of messages per CS
– Synchronization delay
– Throughput
– Response time
09/13/20 COP5611 28
A Simple Toke Ring Algorithm
• When the ring is initialized, one process is
given the token
• The token circulates around the ring
– It is passed from k to k+1 (modulo the ring size)
– When a process acquires the token from its
neighbor, it checks to see if it is waiting to enter its
critical section
• If so, it enters its CS
– When exiting from its CS, it passes the token to the next
• Otherwise, it passes the token to the next
09/13/20 COP5611 29
A Simple Toke Ring Algorithm – cont.
09/13/20 COP5611 30
A Simple Toke Ring Algorithm – cont.
• Performance analysis
– Number of messages per CS
– Synchronization delay
– Response time
– Problems
• Lost token
• Process crash
09/13/20 COP5611 31
Suzuki-Kasami’s Algorithm
• Data structures
– Each site maintains a vector consisting the largest
sequence number received so far from other sites
– The token consists of a queue of requesting sites
and an array of integers, consisting of the
sequence number of the request that a site
executed most recently
09/13/20 COP5611 32
Suzuki-Kasami’s Algorithm – cont.
09/13/20 COP5611 33
Suzuki-Kasami’s Algorithm – cont.
• Performance analysis
– Number of messages per CS invocation
– Synchronization delay
– Response time
– System throughput
09/13/20 COP5611 34
Singhal’s Heuristic Algorithm
• In this algorithm, each site maintains
information about the state of other sites in
the system
– The site only sends the request to a subset of sites
that most likely have the token will in a near
future
– This reduces the number of messages per CS
execution
09/13/20 COP5611 35
Raymond’s Tree-Based Algorithm
• In this algorithm, sites are logically arranged
as a directed tree
09/13/20 COP5611 36
Comparison of Distributed Mutual Exclusion Algorithms
09/13/20 COP5611 37
Summary
• Mutual exclusion is one solution to the
critical section problem
• Due to the absence of shared memory,
distributed mutual exclusion algorithms are
message-based
– Centralized algorithms
– Non-token-based algorithms
– Token-based algorithms
09/13/20 COP5611 38