Question

In: Computer Science

mutual exclusion is Lamport’s bakery

A software approach to mutual exclusion is Lamport’s bakery algorithm [LAMP74], so called because it is based on the practice in bakeries and other shops in which every customer receives a numbered ticket on arrival, allowing each to be served in turn. The algorithm is as follows:

boolean choosing[n];

int number[n];

while (true) {

choosing[i] = true;

number[i] = 1 + getmax(number[], n);

choosing[i] = false;

for (int j = 0; j < n; j++){

while (choosing[j]) { };

while ((number[j] != 0) && (number[j],j) < (number[i],i)) { };

}

/* critical section */;

number [i] = 0;

/* remainder */;

}

The arrays choosing and number are initialized to false and 0, respectively. The ith element of each array may be read and written by process i but only read by other processes. The notation (a, b) < (c, d) is defined as:

(a < c) or (a = c and b < d)

a. Describe the algorithm in words.

b. Show that this algorithm avoids deadlock.

c. Show that it enforces mutual exclusion.

Solutions

Expert Solution

a.      

When a process wishes to enter its critical section, it is assigned a ticket number. The ticket number assigned is calculated by adding one to the largest of the ticket numbers currently held by the processes waiting to enter their critical section and the process already in its critical section. The process with the smallest ticket number has the highest precedence for entering its critical section. In case more than one process receives the same ticket number, the process with the smallest numerical name enters its critical section. When a process exits its critical section, it resets its ticket number to zero.

 

b.      

If each process is assigned a unique process number, then there is a unique, strict ordering of processes at all times. Therefore, deadlock cannot occur. 

 

c.      

To demonstrate mutual exclusion, we first need to prove the following lemma: if Pi is in its critical section, and Pk has calculated its number[k] and is attempting to enter its critical section, then the following relationship holds: 

 

( number[i], i ) < ( number[k], k )

 

 

To prove the lemma, define the following times:

 

Tw1 Pi reads choosing[k] for the last time, for j = k, in its first wait, so we have choosing[k] = false at Tw1.

Tw2 Pi begins its final execution, for j = k, of the second while loop. We therefore have Tw1 < Tw2.

Tk1 Pk enters the beginning of the repeat loop.

Tk2 Pk finishes calculating number[k].

Tk3 Pk sets choosing[k] to false. We have Tk1 < Tk2 < Tk3.

 

 

Since at Tw1, choosing[k] = false, we have either Tw1 < Tk1 or Tk3 < Tw1. In the first case, we have number[i] < number[k], since Pi was assigned its number prior to Pk; this satisfies the condition of the lemma.

 

 

In the second case, we have Tk2 < Tk3 < Tw1 < Tw2, and therefore Tk2 < Tw2. This means that at Tw2, Pi has read the current value of number[k]. Moreover, as Tw2 is the moment at which the final execution of the second while for j = k takes place, we have (number[i], i) < ( number[k], k), which completes the proof of the lemma.

 

 

It is now easy to show the mutual exclusion is enforced. Assume that Pi is in its critical section and Pk is attempting to enter its critical section. Pk will be unable to enter its critical section, as it will find number[i] ≠ 0 and (number[i], i) < (number[k], k).


It is now easy to show the mutual exclusion is enforced. Assume that Pi is in its critical section and Pk is attempting to enter its critical section.

Related Solutions

There are three ways to provide mutual exclusion in a system. Explain how mutual exclusion can...
There are three ways to provide mutual exclusion in a system. Explain how mutual exclusion can be guaranteed using hardware approach and software approach.
software approach to mutual exclusion
Consider the following program which provides a software approach to mutual exclusion:Integer array control [1: N]; integer kWhere 1 ≤ k ≤ N, and each element of “control” is either 0, 1, Or 2. All elements of “control” are initially zero; the initial valueof k is immaterial.The program of the ith process (1 ≤ i ≤ N) isbegin integer j;L0: control [i] := l;LI: for j:=k step l until N, l step l until k dobeginif j = i then...
Consider the four distributed mutual exclusion algorithms Permission Based ---#1: Centralized Mutual Exclusion ---#2: Decentralized Mutex...
Consider the four distributed mutual exclusion algorithms Permission Based ---#1: Centralized Mutual Exclusion ---#2: Decentralized Mutex Algorithm ---#3: Distributed Mutual Exclusion #4: A Token Ring Algorithm Discuss their relative fault tolerance – basically the effect of processor crashes on the performance of the algorithm. You don’t have to come up with a 1-2-3-4 ordering.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
What are the disadvantages of disabling interrupts as a strategy for achieving mutual exclusion?
What are the disadvantages of disabling interrupts as a strategy for achieving mutual exclusion?
Please prove the correctness of the following extended compare_and_wait program in terms of mutual exclusion, progress,...
Please prove the correctness of the following extended compare_and_wait program in terms of mutual exclusion, progress, and bounded waiting. please show it in the in terms of mutual exclusion, progress, and bounded waiting.aspects thankyou while (true) { waiting[i] = true; key = 1; while (waiting[i] && key == 1) key = compare_and_swap(&lock,0,1); waiting[i] = false;    // critical section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j + 1) % n; if...
Modify the included program to implement the strict alternation solution to achieve mutual exclusion. It does...
Modify the included program to implement the strict alternation solution to achieve mutual exclusion. It does not matter whether Thread 0 goes first or Thread 1, but it is important that the thread strictly alternate. PROGRAM: #include <iostream> #include <pthread.h> #include <stdlib.h> int count; int turn = 0; // Shared variable used to implement strict alternation void* myFunction(void* arg) {    int actual_arg = *((int*) arg);       for(unsigned int i = 0; i < 10; ++i) {    //...
Question Four Explain Peterson’s solution for critical-section problem and show that mutual exclusion is preserved with...
Question Four Explain Peterson’s solution for critical-section problem and show that mutual exclusion is preserved with Peterson’s solution. (Assume there are only two processes P0 and P1)
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock;...
Single Lane Bridge Problem : Java Threads Given : //Use ReentrantLock for mutual exclusion import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class bridge { private final ReentrantLock myLock = new ReentrantLock(); public bridge(){ } public String cross(){ myLock.lock(); try { return "crossing the bridge"; } finally { myLock.unlock(); } } } public class lane extends Thread { int cars; bridge Bridge; public lane(int Cars1, bridge Bridge1) { cars = Cars1; Bridge = Bridge1; } public void run(){ for(int x=0;x //Important clue :...
When a special machine instruction is used to provide mutual exclusion in the fashion of Figure 5.2, there is no control over how
When a special machine instruction is used to provide mutual exclusion in the fashion of Figure 5.2, there is no control over how long a process must wait before being granted access to its critical section. Devise an algorithm that uses the compare & swap instruction but that guarantees that any process waiting to enter its critical section will do so within n −1 turns, where n is the number of processes that may require access to the critical section...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT