简体   繁体   中英

Lamport's bakery algorithm

    do { 
     choosing[i] = true; 
   number[i] = max(number[0], number[1], …, number [n – 1])+1; 
     choosing[i] = false; 
     for (j = 0; j < n; j++) { 
     while (choosing[j]); // espera que j obtenha um bilhete 
     while ((number[j]!= 0) && (number[j],j)<(number[i],i))); 
     } 
     critical section 
     number[i] = 0; 
     remainder section 
    } while (1); 

I have a doubt about this algorithm , supposedly when your number is zero you wont be able to enter the critical section.

But the way the loop works if the condition inside the loop is true you will get stuck in said loop.

Meaning non zero numbers would be the ones not reaching the critical condition right?

This is kinda confusing for me , i would appreciate your help

Greetins John.

Have a look at the original paper:

http://research.microsoft.com/en-us/um/people/lamport/pubs/bakery.pdf

(source: http://research.microsoft.com/en-us/um/people/lamport/pubs/pubs.html#bakery )

Assertion 2 implies that at most one processor can be in its critical section at any time. Assertions 1 and 2 prove that processors enter their critical sections on a first-come-first-served basis. Hence, an individual processor cannot be blocked unless the entire system is deadlocked. Assertion 3 implies that the system can only be deadlocked by a processor halting in its critical section, or by an unbounded sequence of processor failures and re-entries.

It's not possible for your number ie number[i] to be zero when you reach the critical section, because you are the only one writing to it, and because you set it before reaching the loop while ((number[j]!= 0) && (number[j],j)<(number[i],i))); section.

It is also not possible for you to get stuck in this waiting loop (L2) by definition, since the problem assumes that a thread cannot crash in the critical section. Therefore once all the other threads you are waiting on exit the critical section and set their number[j] to zero, it is your turn to enter the critical section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM