Professional Documents
Culture Documents
Department ofcomputer Science, University College, The University ofNew South Wales,
Australian Defence Force Academy, Canberra, Australian Capital Territory, Australia
(processes). The algorithms are optimal in the sense that a When a process i wants to enter its critical section it sends
symmetrical, distributed algorithm cannot use fewer mes- the message request( i, p( i))-in the case of a P-system-
sages if requests are processed by each node computer con- or request( i, T( i))-in the case of the RT-system-to all
currently and a network is error-free. It ensures that each other processes in the system. It is kept waiting until it re-
process requesting entry to its critical section will receive ceives the token. When the process j holding the token is
the token within a finite time of requesting entry. in its critical section, other processes wanting to enter their
critical sections can send request messages. An incoming
2. THE ALGORITHMS priority request queue Q is formed in the node computer
running process j. All processes in this queue are ordered
Two algorithms developed utilizing a priority-based according to relation ), or relation ) ) (defined above). If
event-ordering sharing the same concept were developed. two processes have the same value of priority, the process
One is suitable for the environment requiring priorities, and with a smaller value of index i is placed first.
the other can be used in real-time environment. In the sys- A process j holding the token, after exiting the critical
tem, each node of the computer network executes an identi- section, examines the incoming request queue Q and the
cal algorithm, appropriate to the environment. The differ- queue P received with the token, if any. In the case of the
ences between these two algorithms are implied by the fea- RT-system, the clock routine must perform decrementing
tures of environments as well as methods used to ensure operations on appropriate fields of the queue P and request-
freedom from starvation. The general description of these ing queue Q elements, i.e., T( i), to maintain real-time con-
two algorithms is common; however, their important fea- sistency.
tures and specific differences will be pointed out. In the pre- -If the queue P is empty and the queue Q is empty, the
sentation of these algorithms we use the notation based on process continues with its normal execution until it receives
an abstract definition of a queue [ 5 1. The following opera- a request message from some other process.
tions are used: Head-returns the head of a queue; De- -If the queue P is not empty and the queue Q is empty,
lete-deletes the head of a queue; IsNew-tests whether a the process j deletes (using function Delete) the process
queue is empty; Append-concatenates two queues; with the highest priority (the greatest item), Delete(P) .
Merge-merges two priority queues; Add-inserts an item -If the queue P is empty and the queue Q is not empty,
to a queue. Due to this notation, the presentation is short the process j deletes (using function Delete) from the prior-
and facilitates the easy identification of the most important ity queue Q the process with the highest priority (the great-
features of both algorithms. est item), Delete(Q), and creates the new queue P by
appending (function Append)-P-system or merging
2.1. Basic Concepts (function Merge)-RT-system the queue P and the
queue Q.
There are N processes located in N nodes of the network -If the queue P is not empty and the queue Q is not
indexed from 1 to N. Each process i is characterized: in the empty, the construction of the queue P is different for both
case of a system with priorities (called here the P-system) cases.
by its priority p(i), or in the case of a real-time system
(called here the RT-system) by remaining time T(i) to run For a P-system, the process j creates a new queue P by ap-
the process. pending (function Append) the old queue P and the queue
In the P-system, associated with the token is a priority Q. Processes from the second part (from Q) and each subse-
queueP=(i,,&,. . . , i,), where ij indicates a process which quent part (appended, if necessary, in the same way as the
wants to enter its critical section, i, , i2, . . . , i, is a permuta- first part) cannot be served while processes from the preced-
tion of the sequence of numbers 1,2, . . . , m, such that m ing part are still in the queue. This requirement must be
< N, and defined by relation ) in the following way: ik ) i, fulfilled to avoid starvation of processes with smaller values
ifandonlyifforeachk<t,p(&)>p(&). of priorities.
In the RT-system, associated with the token is a priority For a RT-system, the process j constructs (function
queueP=((i,, T(i,)),(&, T(&)),. . . ,(i,, T(i,))),where Merge) the new queue P by merging the old queue P and
ij indicates a process which wants to enter its critical section, the requesting queue Q .
i, , i2, . . . , i, is a permutation of a sequence of numbers 1, The process finally sends the token with the new queue P
2 . . >m, such that m =GN, and defined by relation )) in to the first process recorded in the queue P, Head(P) .
the following way: i,+)> i, if and only if for each k < t their
remaining times satisfy the condition T( ik) < T( it). 2.2. The Basic Algorithm
For both cases, if two processes have the same priority or
remaining times, the process with the smaller value of index Figure 1 shows the algorithm, referred to as Algorithm,
i is placed first. for an error-free network for the case of a system with prior-
TWO ALGORITHMS FOR MUTUAL EXCLUSION 79
procedure wants-to-enter;
begin
Requesting : = true;
if not HaveToken
then begin
foralljin{1,2,...,N}\{/}do
Send request(l, p) to node j;
Wait until token is received;
HaveToken := true
end;
Critical Section;
procedure request-arrives;
begin
if not HaveToken
then forj = {1,2, , N}\{/} do
discard request( j, p)
else begin
Delete(P);
forj={1,2,...,}\{I}do
Add( j, 0) + 0 ; {insert an additional request}
Append(P, 0) + P ; {Merge(P, Q) -. P)
Send token, Token(P), to Head(P)
end
end;
ities, P-system. The structure of the algorithm for a real- function Append with instances of function Merge. This is
time system, RT-system, is identical to the structure of this presented in comments to several lines of Algorithm. Proce-
algorithm. Moreover, algorithm for RT-system can be eas- dure wants-to-enter is called when a node attempts to en-
ily obtained from Algorithm by replacing all instances of ter the critical section and procedure request-arrives is exe-
80 ANDRZEJ GOSCINSKI
cuted when a request message arrives. In both cases the performed, a time of waiting for entering a critical section
same suitable algorithm is executed by each node. is finite. Thus again, starvation is impossible.
procedure wants-to-enter;
begin
Requesting : = true;
if not HaveToken {modification starts}
then begin
repeat
foralljin{1,2,...,N}\{/}do
Send request(l, p. r-id, ret-id) to nodej;
Set up req.-time-out;
Wait until token is received;
until HaveToken;
HaveToken := true;
Send acknowledgment, Ack(Head(P));
Delete(f)
end
end;
with time stamps event-ordering algorithms, priority-based 6. Lamport, L. Time, clocks and the ordering of events in a distributed
system. Comm. ACMZI, 7 (July 1978), 558-565.
event-ordering of processes has been proposed. For real-
7. Le Lann, G. Distributed systems-Towards a formal approach. Proc.
time systems and systems using priorities for scheduling, a IFIP Congress, Toronto. North-Holland, Amsterdam, Aug. 1977.
priority-based approach is more natural and improves the
8. Le Lann, G. Algorithms for distributed data sharing systems which use
performance of the total system. Both algorithms are con- tickets. Proc. 3rd Berkeley Workshop, Aug. 1978.
current and distributed, and use N messages per critical sec- 9. Mullender, S. J. Report on the Second European SIGOPS Workshop
tion. In both cases freedom from starvation is ensured. “Making Distributed Systems Work”, Amsterdam, Netherland, Sept.
There are mechanisms to handle node insertion and re- 1986, Oper. Syst. Rev. 21, 1 (Jan. 1987), 49-84.
moval, node failure, the loss of the token, existence of more 10. Peterson, J. L., and Silbershatz, A. Operating Systems Concepts. Addi-
than one token, and out of order arrival of messages. son-Wesley, Reading, MA, 1985.
I I. Ricart, G., and Agrawala, A. K. An optimal algorithm for mutual ex-
clusion in computer networks. Comm. ACM 24, 1 (Jan. I98 1), 9- 17.
ACKNOWLEDGMENTS
12. Suzuki, I., and Kasami, T. A distributed mutual exclusion algorithm.
ACM Trans. Comput. Syst. 3,4 (Nov. 1985), 344-349.
I thank my colleagues A. Quaine and C. Vance for reading drafts of this
paper and for their valuable comments. 1 am also grateful to the referees
for valuable comments on the paper.
ANDRZEJ GOSCINSKI received the M.Sc. degree in automatic control
in 1968, the Ph.D. degree in control engineering and computer science in
REFERENCES 1973, and the D.Sc. degree in computer science in 1976 from the St. Staszic
University ofMining and Metallurgy, Krakow, Poland. From 1968 to 1984
1. Chandy, K. M. A mutual exclusion algorithm for distributed systems. he was with the Institute of Computer Science and Control Engineering, at
Tech. Rep., University of Texas, 1982. that University. In 1977 Dr. Goscinski was with the Department ofcontrol
Engineering, Computer Engineering and Information Sciences, Case
2. Fran&t, W. R., and Chlamtac, I. Local Networks. Lexington Books,
Lexington, MA, 1982. Western Reserve University, Cleveland, Ohio. In January 1985 he joined
the Department of Computing Science at the University of Wollongong,
3. Garcia-Molina, H. Elections in a distributed computing system. IEEE and since January 1987 has been with the Department of Computer Sci-
Trans. Camp. C-31, I (Jan. 1982), 48-59.
ence, University College, University of New South Wales, Australian De-
4. Goscinski, A. Distributed Operating Systems. The Logical Design. Ad- fence Force Academy. He is currently a Senior Lecturer. His current re-
dison-Wesley, in press. search activities are in distributed operating systems, applications of dis-
5. Hille, R. F. Data Abstraction and Program Development. Prentice- tributed systems, networks, and communication protocols. Dr. Goscinski
Hall, New York, 1987. is a member of the IEEE Computer Society.