-1

I was reading through some examples online regarding about the critical region, entry protocol and exit protocol, and had a hard time figuring out. http://pages.cs.wisc.edu/~dusseau/Classes/CS537-S01/SampleQuizzes/sol2.html

class BankAccount {

    private int turn = 0;
    private boolean lock = {true, true};
    private int balance;
    private int accountNumber;

    BankAccount(int acct) {
        accountNumber = acct;
        balance = 0;
    }

   // tellerID is either 0 or 1
   public void deposit(int amount, int tellerID) {
        lock[tellerID] = true;
        turn = 1 - tellerID;
        while (lock[1-tellerID] && turn == (1 - tellerID));
        balance += amount;
        lock[tellerID] = false;
    }
}

What's the entry protocol, exit protocol and critical region for this example? and as for the critical region, is it mutual exclusion, starvation or will it result in a deadlock?

Dom
  • 1,687
  • 6
  • 27
  • 37
i-link
  • 75
  • 1
  • 5
  • I was thinking if entry protocol is lock[tellerID] = true; Exit protocol is lock[tellerID] = false; Critical region while (lock[1-tellerID] && turn == (1 - tellerID));? – i-link Feb 27 '14 at 15:24
  • First of all, the link you provided already gives you the answer, secondly if you can't figure out why the example you show can result in a deadlock I'd advise you to read up on concurrency at least a little. Thirdly you copied the example wrong `private boolean lock = {true, true}` should be `private boolean lock[] = {true, true}` – Ceiling Gecko Feb 27 '14 at 15:25
  • 1
    Why don't you improve your question with definitions of all of the terms you asked about, your interpretation of how those definitions apply to the question, what you think the answer is, and perhaps an actual question like "I don't understand what THIS part means, and I cannot find the answer on Google." – Rainbolt Feb 27 '14 at 15:25
  • when you say `and had a hard time figuring out` .. my eyes glaze over a lil bit. ddid you spend 2 days straight? or was it 2 hours ? – Caffeinated Feb 27 '14 at 15:25
  • By the way, there are only two teller ID's, and so there are really only four possible situations that I can think of that could arise here. If you actually wrote them on paper, you would see this question very clearly. I'll give you a hint - the entry protocol is not all on a single line. – Rainbolt Feb 27 '14 at 15:27
  • haha, I just trying to understand why is there turn = 1 - tellerID;? – i-link Feb 27 '14 at 15:30
  • turn = 1 - tellerID is this line stopping the other thread from accessing it? since i think both thread can enter the method – i-link Feb 27 '14 at 15:35
  • No, that is telling the other thread "It is your turn now." – Rainbolt Feb 27 '14 at 16:01

1 Answers1

1

Assume I am teller 1. You are teller 0.

lock[tellerID] = true;

I grab a lock for myself.

turn = 1 - tellerID;

I say "It is your turn now." Turn is set to the ID of the other teller (for now).

while lock[1-tellerID]

I loop while you have a lock.

while turn == (1 - tellerID)

I also loop while it is your turn.

Imagine we both reached the while loop one after another. I have my lock, and you have your lock. We are both waiting for the other to release the lock, or for it to be our turn. It has to either be your turn or my turn, so only one of us falls through the loop. This is obviously mutual exclusion. When you go to make another deposit, you will notify me that it is my turn.

Waiting, grabbing locks, and taking turns are all part of an entry protocol. Releasing locks is part of an exit protocol. Everything in between is, by default, a critical section. Everything outside is, by default, a non-critical section.

Rainbolt
  • 3,542
  • 1
  • 20
  • 44
  • The condition in the loop is an && condition, meaning they must both be true. I get to go as soon as it's my turn, OR as soon as you release a lock. So, if you go first, I wait until you release the lock. If I go first, you wait for me to release the lock. Turn will always be my turn or your turn, so we avoid getting "stuck". – Rainbolt Feb 27 '14 at 16:08
  • So it's mutual exclusion not deadlock, cause I was reading that thread 0 must wait for thread 1 in order to proceed – i-link Feb 27 '14 at 16:10
  • So can i say that the critical region is balance += amount; <- this is the shared data? – i-link Feb 27 '14 at 16:12
  • @i-link The critical region is everything in between the entry and exit protocols. Don't try to determine the critical region by itself... it will only cause you to fail your tests in class. I could say that `I LOVE PRETTY FLOWERS` is the critical region, as long as it falls between the entry and the exit protocols. – Rainbolt Feb 27 '14 at 16:17
  • can i say that while (lock[1-tellerID]) is always true? cause in the first portion of the coding, both lock are set to true? be it in thread 1 or 0. lock[1-0] and lock [1-1], both are true? – i-link Feb 27 '14 at 16:25
  • @i-link No. In the exit protocol, both threads set the lock to false. Therefore, is is not always true. – Rainbolt Feb 27 '14 at 16:28