# An Asynchronous Message-Passing Distributed Algorithm for the Generalized Local Critical Section Problem

^{1}

^{2}

^{*}

## Abstract

**:**

## 1. Introduction

- One application is a formulation of the dynamic invocation of servers for the load balancing. The minimum number of servers that are always invoked for quick responses to requests for ${P}_{i}$ is ${l}_{i}$. The number of servers is dynamically changed by the system load. However, the total number of servers is limited by available resources like bandwidth for ${P}_{i}$, and the number is ${k}_{i}$.
- Another is fault-tolerance services if each process in the CS provides a service for the network. Because every process has direct access to at least ${l}_{i}$ servers, it guarantees fault-tolerant services. However, because providing services involve a significant cost, the number of servers should be limited at most ${k}_{i}$ for each process.
- The other is that each process in the CS provides service A, and other processes provide service B for the network. Then, every process in the network has direct access to at least ${l}_{i}$ servers of A and has direct access to at least $|{N}_{i}|+1-{k}_{i}$ servers of B.

## 2. Preliminaries

#### 2.1. System Model

#### 2.2. Problem

${\mathit{state}}_{i}:=\left(\mathrm{Initial}\phantom{\rule{3.33333pt}{0ex}}\mathrm{state}\phantom{\rule{3.33333pt}{0ex}}\mathrm{of}\phantom{\rule{3.33333pt}{0ex}}{P}_{i}\phantom{\rule{3.33333pt}{0ex}}\mathrm{in}\phantom{\rule{3.33333pt}{0ex}}\mathrm{the}\phantom{\rule{3.33333pt}{0ex}}\mathrm{initial}\phantom{\rule{3.33333pt}{0ex}}\mathrm{configuration}\phantom{\rule{3.33333pt}{0ex}}{C}_{0}\right);$while true{$\mathbf{if}({\mathit{state}}_{i}=\mathsf{OutCS})\{$ Entry-Sequence; ${\mathit{state}}_{i}:=\mathsf{InCS}$; /* Critical Section */ } $\mathbf{if}({\mathit{state}}_{i}=\mathsf{InCS})\{$ Exit-Sequence; ${\mathit{state}}_{i}:=\mathsf{OutCS}$; /* Remainder Section */ } } |

**Definition**

**1.**

- Safety: For each process ${P}_{i}\in V$, ${l}_{i}\le \left|{\mathcal{CS}}_{i}\left(C\right)\right|\le {k}_{i}$ at any time.
- Liveness: Each process ${P}_{i}\in V$ changes $\mathsf{OutCS}$ and $\mathsf{InCS}$ states alternately infinitely often.

#### 2.3. Performance Measure

## 3. Proposed Algorithm for the Generalized Local ${\mathit{l}}_{\mathit{i}}$-Mutual Inclusion

- ${\mathit{state}}_{i}$: The current state of ${P}_{i}$: $\mathsf{InCS}$ or $\mathsf{OutCS}$.
- ${\mathit{ts}}_{i}$: The current value of the logical clock [19].
- ${\mathit{nGrants}}_{i}$: The number of grants that ${P}_{i}$ obtains for exiting the CS.
- ${\mathit{grantedTo}}_{i}$: A set of timestamps $({\mathit{ts}}_{j},{P}_{j})$ for the requests to ${P}_{j}$’s exiting the CS that ${P}_{i}$ has been granted, but that ${P}_{j}$ has not yet released.
- ${\mathit{pendingReq}}_{i}$: A set of timestamps $({\mathit{ts}}_{j},{P}_{j})$ for the requests to ${P}_{j}$’s exiting the CS that are pending.
- ${\mathit{preemptingNow}}_{i}$: A process id ${P}_{j}$ such that ${P}_{i}$ preempts a permission for ${P}_{j}$’s exiting the CS if the preemption is in progress.

Algorithm 1 Local variables for process ${P}_{i}$ in algorithm ${l}_{i}$-LMUTIN |

${\mathit{state}}_{i}\in \left\{\mathsf{InCS},\mathsf{OutCS}\right\}$, initially ${\mathit{state}}_{i}=\left\{\begin{array}{cc}\mathsf{InCS}\hfill & ({P}_{i}\in \mathcal{CS}\left({C}_{0}\right))\hfill \\ \mathsf{OutCS}\hfill & ({P}_{i}\notin \mathcal{CS}\left({C}_{0}\right))\hfill \end{array}\right.$${\mathit{ts}}_{i}$ : integer, initially 0;${\mathit{nGrants}}_{i}$: integer, initially 0;${\mathit{grantedTo}}_{i}$: set of (integer, processID), initially $\left\{(0,{P}_{j}\in {\mathcal{CS}}_{i}\left({C}_{0}\right))\right\}$;${\mathit{pendingReq}}_{i}$: set of (integer, processID), initially ∅;${\mathit{preemptingNow}}_{i}$: processID, initially nil; |

Algorithm 2 Algorithm ${l}_{i}$-LMUTIN: exit-sequence, entry-sequence and message handlers. |

Exit-Sequence:${\mathit{ts}}_{i}:={\mathit{ts}}_{i}+1$; ${\mathit{nGrants}}_{i}:=0$; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{Request},{\mathit{ts}}_{i},{P}_{i}\rangle $ to ${P}_{j}$;wait until $({\mathit{nGrants}}_{i}=|{N}_{i}|+1)$;${\mathit{state}}_{i}:=\mathsf{OutCS}$; Entry-Sequence:${\mathit{state}}_{i}:=\mathsf{InCS}$; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{Release},{P}_{i}\rangle $ to ${P}_{j}$;On receipt of a $\langle \mathsf{Request},{\mathit{ts}}_{j},{P}_{j}\rangle $ message:${\mathit{pendingReq}}_{i}:={\mathit{pendingReq}}_{i}\cup \left\{({\mathit{ts}}_{j},{P}_{j})\right\}$; if $\left(\right|{\mathit{grantedTo}}_{i}|<|{N}_{i}|-{l}_{i}+1\left)\right\{$$({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{i}\right)$; ${\mathit{grantedTo}}_{i}:={\mathit{grantedTo}}_{i}\cup \left\{({\mathit{ts}}_{h},{P}_{h})\right\}$; send $\langle \mathsf{Grant}\rangle $ to ${P}_{h}$;} else if $({\mathit{preemptingNow}}_{i}=\mathbf{nil})\{$$({\mathit{ts}}_{h},{P}_{h}):=max\left({\mathit{grantedTo}}_{i}\right)$; if $(({\mathit{ts}}_{j},{P}_{j})<({\mathit{ts}}_{h},{P}_{h}))\{$${\mathit{preemptingNow}}_{i}:={P}_{h}$; send $\langle \mathsf{Preempt},{P}_{i}\rangle $ to ${P}_{h}$;} } On receipt of a $\langle \mathsf{Grant}\rangle $ message:${\mathit{nGrants}}_{i}:={\mathit{nGrants}}_{i}+1$; On receipt of a $\langle \mathsf{Release},{P}_{j}\rangle $ message:if $({P}_{j}={\mathit{preemptingNow}}_{i})\phantom{\rule{3.33333pt}{0ex}}{\mathit{preemptingNow}}_{i}:=\mathbf{nil}$;Delete $(*,{P}_{j})$ from ${\mathit{grantedTo}}_{i}$; if $({\mathit{pendingReq}}_{i}\ne \varnothing )\{$$({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{i}\right)$; ${\mathit{grantedTo}}_{i}:={\mathit{grantedTo}}_{i}\cup \left\{({\mathit{ts}}_{h},{P}_{h})\right\}$; send $\langle \mathsf{Grant}\rangle $ to ${P}_{h}$;} On receipt of a $\langle \mathsf{Preempt},{P}_{j}\rangle $ message:if $({\mathit{state}}_{i}=\mathsf{InCS})\{$${\mathit{nGrants}}_{i}:={\mathit{nGrants}}_{i}-1$; send $\langle \mathsf{Relinquish},{P}_{i}\rangle $ to ${P}_{j}$;} On receipt of a $\langle \mathsf{Relinquish},{P}_{j}\rangle $ message:${\mathit{preemptingNow}}_{i}:=\mathbf{nil}$; Delete $(*,{P}_{j})$ from ${\mathit{grantedTo}}_{i}$, and let $({\mathit{ts}}_{j},{P}_{j})$ be the deleted item; ${\mathit{pendingReq}}_{i}:={\mathit{pendingReq}}_{i}\cup \left\{({\mathit{ts}}_{j},{P}_{j})\right\}$; $({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{i}\right)$; ${\mathit{grantedTo}}_{i}:={\mathit{grantedTo}}_{i}\cup \left\{({\mathit{ts}}_{h},{P}_{h})\right\}$; send $\langle \mathsf{Grant}\rangle $ to ${P}_{h}$; |

#### 3.1. Proof of Correctness

**Lemma**

**1.**

**Proof.**

**Lemma**

**2.**

**Proof.**

- Suppose that ${P}_{j}$ changes into the $\mathsf{InCS}$ and $\mathsf{OutCS}$ states alternately infinitely often. After ${P}_{j}$ receives the $\langle \mathsf{Request},{\mathit{ts}}_{i},{P}_{i}\rangle $ message from ${P}_{i}$, the value of $({\mathit{ts}}_{j},{P}_{j})$ exceeds the timestamp $({\mathit{ts}}_{i},{P}_{i})$ for ${P}_{i}$’s request. Because, by this algorithm, the request with the lowest timestamp is granted preferentially, it is impossible for ${P}_{j}$ to change into the $\mathsf{InCS}$ and $\mathsf{OutCS}$ states alternately infinitely often. Then, ${P}_{j}$ eventually sends a $\langle \mathsf{Grant}\rangle $ message to ${P}_{i}$, and ${P}_{i}$ eventually sends a $\langle \mathsf{Grant}\rangle $ message to itself.
- Suppose that ${P}_{j}$ does not change into the $\mathsf{InCS}$ and $\mathsf{OutCS}$ states alternately infinitely often. Because the timestamp of ${P}_{i}$ is smaller than that of ${P}_{j}$, by assumption, ${P}_{j}$’s permission is preempted, and a $\langle \mathsf{Grant}\rangle $ message is sent from ${P}_{j}$ to ${P}_{i}$. In addition, ${P}_{i}$ sends a $\langle \mathsf{Grant}\rangle $ message to itself.

#### 3.2. Performance Analysis

**Lemma**

**3.**

**Proof.**

**Theorem**

**1.**

## 4. The Generalized Local Complementary Theorem

**Theorem**

**2.**

**Proof.**

## 5. Proposed Algorithm for the Generalized Local CS Problem

#### 5.1. Idea

**Exit-Sequence:**- Exit-Sequence for LMUTIN;
- Exit-Sequence for LMUTEX;

**Entry-Sequence:**- Entry-Sequence for LMUTEX;
- Entry-Sequence for LMUTIN;

- Exit-sequence keeps the safety because invocation of exit-sequence for LMUTIN keeps the safety, and invocation of exit-sequence for LMUTEX trivially keeps the safety.
- Similarly, entry-sequence keeps the safety because invocation of entry-sequence for LMUTEX keeps the safety, and invocation of entry-sequence for LMUTIN trivially keeps the safety.

#### 5.2. Details of LKCS

Algorithm 3 Local variables and macros for process ${P}_{i}$ in algorithm LKCS |

Local Variables:$\mathbf{enum}\phantom{\rule{3.33333pt}{0ex}}\mathit{at}\phantom{\rule{3.33333pt}{0ex}}\{\mathsf{MUTEX},\mathsf{MUTIN}\}$; ${\mathit{state}}_{i}\in \left\{\mathsf{InCS},\mathsf{OutCS}\right\}$, initially ${\mathit{state}}_{i}=\left\{\begin{array}{cc}\mathsf{InCS}\hfill & ({P}_{i}\in \mathcal{CS}\left({C}_{0}\right))\hfill \\ \mathsf{OutCS}\hfill & ({P}_{i}\notin \mathcal{CS}\left({C}_{0}\right))\hfill \end{array}\right.$${\mathit{ts}}_{i}$ : integer, initially 1;${\mathit{nGrants}}_{i}\left[\mathit{at}\right]$: set of processID, initially ∅;${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]$: set of (integer, processID), initially $\left\{(1,{P}_{j}\in {\mathcal{CS}}_{i}\left({C}_{0}\right))\right\}$;${\mathit{pendingReq}}_{i}\left[\mathit{at}\right]$: set of (integer, processID), initially ∅;${\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]$: (integer, processID), initially nil;Local Variable only for a leader ${P}_{LDR}$:${\mathit{candidate}}_{LDR}$: set of (integer, processID), initially ∅;Macros:${L}_{i}\equiv \left\{\begin{array}{cc}{l}_{i}\hfill & \mathit{dist}({P}_{LDR},{P}_{i})>2\hfill \\ {l}_{i}+1\hfill & \mathit{dist}({P}_{LDR},{P}_{i})\le 2\hfill \end{array}\right.$ ${K}_{i}\equiv \left\{\begin{array}{cc}{k}_{i}\hfill & \mathit{dist}({P}_{LDR},{P}_{i})>2\hfill \\ {k}_{i}-1\hfill & \mathit{dist}({P}_{LDR},{P}_{i})\le 2\hfill \end{array}\right.$ ${\mathit{Grant}}_{LDR}\equiv \left\{{P}_{j}\phantom{\rule{3.33333pt}{0ex}}\right|\phantom{\rule{3.33333pt}{0ex}}(*,{P}_{j})\in {\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTIN}\right]\wedge (*,{P}_{j})\in {\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTEX}\right]\}$ ${\mathit{Waiting}}_{LDR}\equiv |{\mathit{pendingReq}}_{LDR}\left[\mathsf{MUTIN}\right]|+|{\mathit{pendingReq}}_{LDR}\left[\mathsf{MUTEX}\right]|+|{\mathit{Grant}}_{LDR}|$ ${\mathit{Cond}}_{i}\equiv (\mathit{at}=\mathsf{MUTEX}\wedge |{\mathit{grantedTo}}_{i}\left[\mathsf{MUTEX}\right]|<{K}_{i})\vee (\mathit{at}=\mathsf{MUTIN}\wedge |{\mathit{grantedTo}}_{i}\left[\mathsf{MUTIN}\right]|<|{N}_{i}|-{L}_{i}+1)$ |

Algorithm 4 Algorithm LKCS: exit-sequence and entry-sequence. |

Exit-Sequence:${\mathit{ts}}_{i}:={\mathit{ts}}_{i}+1$; ${\mathit{nGrants}}_{i}\left[\mathsf{MUTIN}\right]:=\varnothing $; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{Request},\mathsf{MUTIN},{\mathit{ts}}_{i},{P}_{i}\rangle $ to ${P}_{j}$;while $\left(\right|{\mathit{nGrants}}_{i}\left[\mathsf{MUTIN}\right]|<|{N}_{i}|+1)\{$if $({P}_{i}={P}_{LDR}\wedge {\mathit{Waiting}}_{LDR}=|{N}_{LDR}|+1)\{$/* The configuration may be in a deadlock. */ $\mathit{TriggerNomination}\left(\right)$; wait until $({\mathit{Waiting}}_{LDR}<|{N}_{LDR}|+1)$;} } ${\mathit{state}}_{i}:=\mathsf{OutCS}$; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{Release},\mathsf{MUTEX},{P}_{i}\rangle $ to ${P}_{j}$;Entry-Sequence:${\mathit{nGrants}}_{i}\left[\mathsf{MUTEX}\right]:=\varnothing $; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{Request},\mathsf{MUTEX},{\mathit{ts}}_{i},{P}_{i}\rangle $ to ${P}_{j}$;while $\left(\right|{\mathit{nGrants}}_{i}\left[\mathsf{MUTEX}\right]|<|{N}_{i}|+1)\{$if $({P}_{i}={P}_{LDR}\wedge {\mathit{Waiting}}_{LDR}=|{N}_{LDR}|+1)\{$/* The configuration may be in a deadlock. */ $\mathit{TriggerNomination}\left(\right)$; wait until $({\mathit{Waiting}}_{LDR}<|{N}_{LDR}|+1)$;} } ${\mathit{state}}_{i}:=\mathsf{InCS}$; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{Release},\mathsf{MUTIN},{P}_{i}\rangle $ to ${P}_{j}$; |

Algorithm 5 Algorithm LKCS: message handlers (1). |

On receipt of a $\langle \mathsf{Request},\mathit{at},{\mathit{ts}}_{j},{P}_{j}\rangle $ message:${\mathit{pendingReq}}_{i}\left[\mathit{at}\right]:={\mathit{pendingReq}}_{i}\left[\mathit{at}\right]\cup \left\{({\mathit{ts}}_{j},{P}_{j})\right\}$; if $\left({\mathit{Cond}}_{i}\right)\{$$({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{i}\left[\mathit{at}\right]\right)$; ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]:={\mathit{grantedTo}}_{i}\left[\mathit{at}\right]\cup \left\{({\mathit{ts}}_{h},{P}_{h})\right\}$; send $\langle \mathsf{Grant},\mathit{at},{P}_{i}\rangle $ to ${P}_{h}$;} else if $({\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]=\mathbf{nil})\{$$({\mathit{ts}}_{h},{P}_{h}):=max\left({\mathit{grantedTo}}_{i}\left[\mathit{at}\right]\right)$; if $(({\mathit{ts}}_{j},{P}_{j})<({\mathit{ts}}_{h},{P}_{h}))\{$${\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]:=({\mathit{ts}}_{h},{P}_{h})$; send $\langle \mathsf{Preempt},\mathit{at},{P}_{i}\rangle $ to ${P}_{h}$;} } On receipt of a $\langle \mathsf{Grant},\mathit{at},{P}_{j}\rangle $ message:if $({P}_{j}\notin {\mathit{nGrants}}_{i}\left[\mathit{at}\right])\{$${\mathit{nGrants}}_{i}\left[\mathit{at}\right]:={\mathit{nGrants}}_{i}\left[\mathit{at}\right]\cup \left\{{P}_{j}\right\}$; } On receipt of a $\langle \mathsf{Release},\mathit{at},{P}_{j}\rangle $ message:if $((*,{P}_{j})={\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]){\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]:=\mathbf{nil}$;Delete $(*,{P}_{j})$ from ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]$; if $({\mathit{pendingReq}}_{i}\left[\mathit{at}\right]\ne \varnothing )\{$$({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{i}\left[\mathit{at}\right]\right)$; ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]:={\mathit{grantedTo}}_{i}\left[\mathit{at}\right]\cup \left\{({\mathit{ts}}_{h},{P}_{h})\right\}$; send $\langle \mathsf{Grant},\mathit{at},{P}_{i}\rangle $ to ${P}_{h}$;} On receipt of a $\langle \mathsf{Preempt},\mathit{at},{P}_{j}\rangle $ message:if $((\mathit{at}=\mathsf{MUTEX}\wedge {\mathit{state}}_{i}=\mathsf{OutCS})\vee (\mathit{at}=\mathsf{MUTIN}\wedge {\mathit{state}}_{i}=\mathsf{InCS}))\{$Delete ${P}_{j}$ from ${\mathit{nGrants}}_{i}\left[\mathit{at}\right]$; send $\langle \mathsf{Relinquish},\mathit{at},{P}_{i}\rangle $ to ${P}_{j}$;} On receipt of a $\langle \mathsf{Relinquish},\mathit{at},{P}_{j}\rangle $ message:${\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]:=\mathbf{nil}$; Delete $(*,{P}_{j})$ from ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]$, and let $({\mathit{ts}}_{j},{P}_{j})$ be the deleted item; ${\mathit{pendingReq}}_{i}\left[\mathit{at}\right]:={\mathit{pendingReq}}_{i}\left[\mathit{at}\right]\cup \left\{({\mathit{ts}}_{j},{P}_{j})\right\}$; $({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{i}\left[\mathit{at}\right]\right)$; ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]:={\mathit{grantedTo}}_{i}\left[\mathit{at}\right]\cup \left\{({\mathit{ts}}_{h},{P}_{h})\right\}$; send $\langle \mathsf{Grant},\mathit{at},{P}_{i}\rangle $ to ${P}_{h}$; |

- If the system is in a deadlock, ${P}_{q}$ is definitely involved in the deadlock. Giving special grants by the sidetrack resolves the deadlock.
- If the system is not in a deadlock, ${P}_{q}$ is not be involved in the deadlock. Furthermore, in this case, LKCS gives special grants by the sidetrack. This is because exact deadlock avoidance mechanisms require global information collection, and they incur large message complexity.

Algorithm 6 Algorithm LKCS: function $\mathit{TriggerNomination}\left(\right)$ for the leader ${P}_{LDR}$. |

$\mathit{TriggerNomination}\left(\right)\{$ $\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}\left(\right|{\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTIN}\right]|=|{N}_{LDR}|-{L}_{LDR}\left)\right\{$ $\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}({\mathit{pendingReq}}_{LDR}\left[\mathsf{MUTEX}\right]\ne \varnothing )\{$ $({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left({\mathit{pendingReq}}_{LDR}\left[\mathsf{MUTEX}\right]\right)$; $\left\}\phantom{\rule{3.33333pt}{0ex}}\mathbf{else}\right\{$ for-each ${P}_{j}\in {\mathit{Grant}}_{LDR}\{$$\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}(({\mathit{ts}}_{j},{P}_{j})\in {\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTIN}\right]\wedge ({\mathit{ts}}_{j},{P}_{j})\in {\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTEX}\right])\{$ /* ${P}_{j}$ may be waiting for grant messages to enter. */ ${\mathit{candidate}}_{LDR}:={\mathit{candidate}}_{LDR}\cup \left\{({\mathit{ts}}_{j},{P}_{j})\right\}$; } } $({\mathit{ts}}_{h},{P}_{h}):=min\left({\mathit{candidate}}_{LDR}\right)$; ${\mathit{candidate}}_{LDR}:=\varnothing $; } send $\langle \mathsf{Trigger},\mathsf{MUTEX},{\mathit{ts}}_{h}\rangle $ to ${P}_{h}$;$\left\}\phantom{\rule{3.33333pt}{0ex}}\mathbf{else}\phantom{\rule{3.33333pt}{0ex}}\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}\right(|{\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTEX}\right]|={K}_{LDR}-1\left)\right\{$ $\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}({\mathit{pendingReq}}_{LDR}\left[\mathsf{MUTIN}\right]\ne \varnothing )\{$ $({\mathit{ts}}_{h},{P}_{h}):=deleteMin\left(pendingRe{q}_{LDR}\left[\mathsf{MUTIN}\right]\right)$; $\left\}\phantom{\rule{3.33333pt}{0ex}}\mathbf{else}\right\{$ for-each ${P}_{j}\in {\mathit{Grant}}_{LDR}\{$$\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}(({\mathit{ts}}_{j}+1,{P}_{j})\in {\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTIN}\right]\wedge ({\mathit{ts}}_{j},{P}_{j})\in {\mathit{grantedTo}}_{LDR}\left[\mathsf{MUTEX}\right])\{$ /* ${P}_{j}$ may be waiting for grant messages to exit. */ ${\mathit{candidate}}_{LDR}:={\mathit{candidate}}_{LDR}\cup \left\{({\mathit{ts}}_{j},{P}_{j})\right\}$; } } $({\mathit{ts}}_{h},{P}_{h}):=min\left({\mathit{candidate}}_{LDR}\right)$; ${\mathit{candidate}}_{LDR}:=\varnothing $; } send $\langle \mathsf{Trigger},\mathsf{MUTIN},{\mathit{ts}}_{h}\rangle $ to ${P}_{h}$;} } |

Algorithm 7 Algorithm LKCS: message handlers (2). |

On receipt of a $\langle \mathsf{Trigger},\mathit{at},\mathit{ts}\rangle $ message:$\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}({\mathit{state}}_{i}=\mathsf{InCS}\wedge \mathit{at}=\mathsf{MUTIN}\wedge \mathit{ts}={\mathit{ts}}_{i}\wedge |{\mathit{nGrants}}_{i}\left[\mathsf{MUTIN}\right]|<|{N}_{i}|+1)\{$ /* ${P}_{i}$ is waiting for grant messages to exit. */ ${\mathit{nGrants}}_{i}\left[\mathsf{MUTIN}\right]:=\varnothing $; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{RequestByTrigger},\mathsf{MUTIN},{P}_{i}\rangle $ to ${P}_{j}$;/* Request message as a trigger. */ $\left\}\phantom{\rule{3.33333pt}{0ex}}\mathbf{else}\phantom{\rule{3.33333pt}{0ex}}\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}\right({\mathit{state}}_{i}=\mathsf{OutCS}\wedge \mathit{at}=\mathsf{MUTEX}\wedge \mathit{ts}={\mathit{ts}}_{i}\wedge |{\mathit{nGrants}}_{i}\left[\mathsf{MUTEX}\right]|<|{N}_{i}|+1)\{$ /* ${P}_{i}$ is waiting for grant messages to enter. */ ${\mathit{nGrants}}_{i}\left[\mathsf{MUTEX}\right]:=\varnothing $; for-each ${P}_{j}\in ({N}_{i}\cup \left\{{P}_{i}\right\})$ send $\langle \mathsf{RequestByTrigger},\mathsf{MUTEX},{P}_{i}\rangle $ to ${P}_{j}$;/* Request message as a trigger. */ } On receipt of a $\langle \mathsf{RequestByTrigger},\mathit{at},{P}_{j}\rangle $ message:Delete $(*,{P}_{j})$ from ${\mathit{pendingReq}}_{i}\left[\mathit{at}\right]$; Delete $(*,{P}_{j})$ from ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]$; ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]:={\mathit{grantedTo}}_{i}\left[\mathit{at}\right]\cup \left\{(0,{P}_{j})\right\}$; send $\langle \mathsf{Grant},\mathit{at},{P}_{i}\rangle $ to ${P}_{j}$; |

- ${\mathit{state}}_{i}$: The current state of ${P}_{i}$: $\mathsf{InCS}$ or $\mathsf{OutCS}$.
- ${\mathit{ts}}_{i}$: The current value of the logical clock [19].
- ${\mathit{nGrants}}_{i}\left[\mathit{at}\right]$: A set of process ids from which ${P}_{i}$ obtains grants for exiting/entering to the CS.
- ${\mathit{grantedTo}}_{i}\left[\mathit{at}\right]$: A set of timestamps $({\mathit{ts}}_{j},{P}_{j})$ for the requests to ${P}_{j}$’s exiting/entering to the CS that ${P}_{i}$ has been granted but that ${P}_{j}$ has not yet released.
- ${\mathit{pendingReq}}_{i}\left[\mathit{at}\right]$: A set of timestamps $({\mathit{ts}}_{j},{P}_{j})$ for the requests to ${P}_{j}$’s exiting/entering to the CS that are pending.
- ${\mathit{preemptingNow}}_{i}\left[\mathit{at}\right]$: A timestamp $({\mathit{ts}}_{j},{P}_{j})$ of a request such that ${P}_{i}$ preempts a permission for ${P}_{j}$’s exiting/entering to the CS if the preemption is in progress.

#### 5.3. Proof of Correctness

**Lemma**

**4.**

**Proof.**

- Suppose that $|{\mathcal{CS}}_{i}\left(C\right)|<{l}_{i}$, that is $|\overline{{\mathcal{CS}}_{i}\left(C\right)}|>|{N}_{i}|+1-{l}_{i}$. Because $|{\mathcal{CS}}_{i}\left({C}_{0}\right)|\ge {l}_{i}$, consider a process ${P}_{j}\in {N}_{i}\cup \left\{{P}_{i}\right\}$, which became the $\mathsf{OutCS}$ state by the $\left(\right|{N}_{i}|+2-{l}_{i})$-th lowest timestamp among processes in $\overline{{\mathcal{CS}}_{i}\left(C\right)}$. Then, ${P}_{j}$ obtains permission to be the $\mathsf{OutCS}$ state from each process in ${N}_{j}\cup \left\{{P}_{j}\right\}$. This implies that ${P}_{i}$ receives a permission request $\langle \mathsf{Request},\mathsf{MUTIN},{\mathit{ts}}_{j},{P}_{j}\rangle $ from ${P}_{j}$ and that ${P}_{i}$ sends a permission $\langle \mathsf{Grant},\mathsf{MUTIN},{P}_{i}\rangle $ to ${P}_{j}$. Because ${P}_{i}$ grants at most $|{N}_{i}|+1-{l}_{i}$ permissions to exit the CS at each time by the condition ${\mathit{Cond}}_{i}$, ${P}_{j}$ cannot obtain a permission from ${P}_{i}$; this is a contradiction.
- Suppose that $|{\mathcal{CS}}_{i}\left(C\right)|>{k}_{i}$. Because $|{\mathcal{CS}}_{i}\left({C}_{0}\right)|\le {k}_{i}$, consider a process ${P}_{j}\in {N}_{i}\cup \left\{{P}_{i}\right\}$ that became the $\mathsf{InCS}$ state by the $({k}_{i}+1)$-th lowest timestamp among processes in ${\mathcal{CS}}_{i}\left(C\right)$. Then, ${P}_{j}$ obtains permission to be the $\mathsf{InCS}$ state from each process in ${N}_{j}\cup \left\{{P}_{j}\right\}$. This implies that ${P}_{i}$ receives a permission request $\langle \mathsf{Request},\mathsf{MUTEX},{\mathit{ts}}_{j},{P}_{j}\rangle $ from ${P}_{j}$ and that ${P}_{i}$ sends a permission $\langle \mathsf{Grant},\mathsf{MUTEX},{P}_{i}\rangle $ to ${P}_{j}$. Because ${P}_{i}$ grants at most ${k}_{i}$ permissions to enter the CS at each time by the condition ${\mathit{Cond}}_{i}$, ${P}_{j}$ cannot obtain a permission from ${P}_{i}$; this is a contradiction.

**Lemma**

**5.**

**Proof.**

#### 5.4. Performance Analysis

**Lemma**

**6.**

**Proof.**

- For ${P}_{i}$’s exiting the CS, ${P}_{i}$ sends a $\langle \mathsf{Request},\mathsf{MUTIN},{\mathit{ts}}_{i},{P}_{i}\rangle $ message to each process in ${N}_{i}\cup \left\{{P}_{i}\right\}$; each process ${P}_{j}$ in ${N}_{i}\cup \left\{{P}_{i}\right\}$ sends a $\langle \mathsf{Grant},\mathsf{MUTIN},{P}_{j}\rangle $ message to ${P}_{i}$; then ${P}_{i}$ sends a $\langle \mathsf{Release},\mathsf{MUTEX},{P}_{i}\rangle $ message to each process in ${N}_{i}\cup \left\{{P}_{i}\right\}$. Thus, $3\left(\right|{N}_{i}|+1)$ messages are exchanged for ${P}_{i}$’s exiting the CS.
- For ${P}_{i}$’s entering the CS, ${P}_{i}$ sends a $\langle \mathsf{Request},\mathsf{MUTEX},{\mathit{ts}}_{i},{P}_{i}\rangle $ message to each process in ${N}_{i}\cup \left\{{P}_{i}\right\}$; each process ${P}_{j}$ in ${N}_{i}\cup \left\{{P}_{i}\right\}$ sends a $\langle \mathsf{Grant},\mathsf{MUTEX},{P}_{j}\rangle $ message to ${P}_{i}$; then ${P}_{i}$ sends a $\langle \mathsf{Release},\mathsf{MUTIN},{P}_{i}\rangle $ message to each process in ${N}_{i}\cup \left\{{P}_{i}\right\}$. Thus, $3\left(\right|{N}_{i}|+1)$ messages are exchanged for ${P}_{i}$’s entering the CS.

- For ${P}_{i}$’s exiting the CS, ${P}_{i}$ sends a $\langle \mathsf{Request},\mathsf{MUTIN},{\mathit{ts}}_{i},{P}_{i}\rangle $ message to each process ${P}_{j}$ in ${N}_{i}\cup \left\{{P}_{i}\right\}$. Then, ${P}_{j}$ sends a $\langle \mathsf{Preempt},\mathsf{MUTIN},{P}_{j}\rangle $ message to the process ${P}_{m}$ to which ${P}_{j}$ sends a $\langle \mathsf{Grant},\mathsf{MUTIN},{P}_{j}\rangle $ message; ${P}_{m}$ sends a $\langle \mathsf{Relinquish},\mathsf{MUTIN},{P}_{m}\rangle $ message back to ${P}_{j}$; and ${P}_{j}$ sends a $\langle \mathsf{Grant},\mathsf{MUTIN},{P}_{j}\rangle $ message to ${P}_{i}$. After ${P}_{i}$ exits the CS, ${P}_{i}$ sends a $\langle \mathsf{Release},\mathsf{MUTEX},{P}_{i}\rangle $ message to each process ${P}_{j}$ in ${N}_{i}\cup \left\{{P}_{i}\right\}$. Then, ${P}_{j}$ sends a $\langle \mathsf{Grant},\mathsf{MUTEX},{P}_{j}\rangle $ message to some process with the highest priority in ${\mathit{pendingReq}}_{j}\left[\mathsf{MUTEX}\right]$. Thus, $6\left(\right|{N}_{i}|+1)$ messages are exchanged.
- For ${P}_{i}$’s entering the CS, ${P}_{i}$ sends a $\langle \mathsf{Request},\mathsf{MUTEX},{\mathit{ts}}_{i},{P}_{i}\rangle $ message to each process ${P}_{j}$ in ${N}_{i}\cup \left\{{P}_{i}\right\}$. Then, ${P}_{j}$ sends a $\langle \mathsf{Preempt},\mathsf{MUTEX},{P}_{j}\rangle $ message to the process ${P}_{m}$ to which ${P}_{j}$ sends a $\langle \mathsf{Grant},\mathsf{MUTEX},{P}_{j}\rangle $ message; ${P}_{m}$ sends a $\langle \mathsf{Relinquish},\mathsf{MUTEX},{P}_{m}\rangle $ message back to ${P}_{j}$; and ${P}_{j}$ sends a $\langle \mathsf{Grant},\mathsf{MUTEX},{P}_{j}\rangle $ message to ${P}_{i}$. After ${P}_{i}$ enters the CS, ${P}_{i}$ sends a $\langle \mathsf{Release},\mathsf{MUTIN},{P}_{i}\rangle $ message to each process ${P}_{j}$ in ${N}_{i}\cup \left\{{P}_{i}\right\}$. Then, ${P}_{j}$ sends a $\langle \mathsf{Grant},\mathsf{MUTIN},{P}_{j}\rangle $ message to some process with the highest priority in ${\mathit{pendingReq}}_{j}\left[\mathsf{MUTIN}\right]$. Thus, $6\left(\right|{N}_{i}|+1)$ messages are exchanged.

**Theorem**

**3.**

## 6. Conclusions

## Acknowledgments

## Author Contributions

## Conflicts of Interest

## References

- Dijkstra, E.W. Solution of a problem in concurrent programming control. Commun. ACM
**1965**, 8, 569. [Google Scholar] [CrossRef] - Saxena, P.C.; Rai, J. A survey of permission-based distributed mutual exclusion algorithms. Comput. Stand. Interfaces
**2003**, 25, 159–181. [Google Scholar] [CrossRef] - Yadav, N.; Yadav, S.; Mandiratta, S. A review of various mutual exclusion algorithms in distributed environment. Int. J. Comput. Appl.
**2015**, 129, 11–16. [Google Scholar] [CrossRef] - Kakugawa, H.; Fujita, S.; Yamashita, M.; Ae, T. Availability of k-coterie. IEEE Trans. Comput.
**1993**, 42, 553–558. [Google Scholar] [CrossRef] - Bulgannawar, S.; Vaidya, N.H. A distributed k-mutual exclusion algorithm. In Proceedings of the 15th International Conference on Distributed Computing Systems, Vancouver, BC, Canada, 30 May–2 June 1995; pp. 153–160. [Google Scholar]
- Chang, Y.I.; Chen, B.H. A generalized grid quorum strategy for k-mutual exclusion in distributed systems. Inf. Process. Lett.
**2001**, 80, 205–212. [Google Scholar] [CrossRef] - Abraham, U.; Dolev, S.; Herman, T.; Koll, I. Self-stabilizing l-exclusion. Theor. Comput. Sci.
**2001**, 266, 653–692. [Google Scholar] [CrossRef] - Chaudhuri, P.; Edward, T. An algorithm for k-mutual exclusion in decentralized systems. Comput. Commun.
**2008**, 31, 3223–3235. [Google Scholar] [CrossRef] - Reddy, V.A.; Mittal, P.; Gupta, I. Fair k mutual exclusion algorithm for peer to peer systems. In Proceedings of the 28th International Conference on Distributed Computing Systems, Beijing, China, 17–20 June 2008. [Google Scholar]
- Hoogerwoord, R.R. An implementation of mutual inclusion. Inf. Process. Lett.
**1986**, 23, 77–80. [Google Scholar] [CrossRef] - Kakugawa, H. Mutual inclusion in asynchronous message-passing distributed systems. J. Parallel Distrib. Comput.
**2015**, 77, 95–104. [Google Scholar] [CrossRef] - Kakugawa, H. On the family of critical section problems. Inf. Process. Lett.
**2015**, 115, 28–32. [Google Scholar] [CrossRef] - Lynch, N.A. Fast allocation of nearby resources in a distributed system. In Proceedings of the 12th Annual ACM Symposium on Theory of Computing, Los Angeles, CA, USA, 28–30 April 1980; pp. 70–81. [Google Scholar]
- Attiya, H.; Kogan, A.; Welch, J.L. Efficient and robust local mutual exclusion in mobile ad hoc networks. IEEE Trans. Mobile Comput.
**2010**, 9, 361–375. [Google Scholar] [CrossRef] - Awerbuch, B.; Saks, M. A dining philosophers algorithm with polynomial response time. In Proceedings of the 31th Annual Symposium on Foundations of Computer Science, St. Louis, MO, USA, 22–24 October 1990; Volume 1, pp. 65–74. [Google Scholar]
- Chandy, M.; Misra, J. The drinking philosophers problem. ACM Trans. Program. Lang. Syst.
**1984**, 6, 632–646. [Google Scholar] [CrossRef] - Beauquier, J.; Datta, A.K.; Gradinariu, M.; Magniette, F. Self-stabilizing local mutual exclusion and daemon refinement. In Proceedings of the International Symposium on Distributed Computing, Toledo, Spain, 4–6 October 2000; pp. 223–237. [Google Scholar]
- Khanna, A.; Singh, A.K.; Swaroop, A. A leader-based k-local mutual exclusion algorithm using token for MANETs. J. Inf. Sci. Eng.
**2014**, 30, 1303–1319. [Google Scholar] - Lamport, L. Time, clocks, and the ordering of events in a distributed system. Commun. ACM
**1978**, 21, 558–565. [Google Scholar] [CrossRef] - Maekawa, M. A $\sqrt{N}$ algorithm for mutual exclusion in decentralized systems. ACM Trans. Comput. Syst.
**1985**, 3, 145–159. [Google Scholar] [CrossRef] - Sanders, B.A. The information structure of mutual exclusion algorithm. ACM Trans. Comput. Syst.
**1987**, 5, 284–299. [Google Scholar] [CrossRef]

© 2017 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Kamei, S.; Kakugawa, H. An Asynchronous Message-Passing Distributed Algorithm for the Generalized Local Critical Section Problem. *Algorithms* **2017**, *10*, 38.
https://doi.org/10.3390/a10020038

**AMA Style**

Kamei S, Kakugawa H. An Asynchronous Message-Passing Distributed Algorithm for the Generalized Local Critical Section Problem. *Algorithms*. 2017; 10(2):38.
https://doi.org/10.3390/a10020038

**Chicago/Turabian Style**

Kamei, Sayaka, and Hirotsugu Kakugawa. 2017. "An Asynchronous Message-Passing Distributed Algorithm for the Generalized Local Critical Section Problem" *Algorithms* 10, no. 2: 38.
https://doi.org/10.3390/a10020038