Вы находитесь на странице: 1из 25

Bakery

Bar-Ilan Shmuel Goldklang 2007

Bakery
Bakery algorithms provides a solution for Mutual exclusion. It suit for N process. And it uses read/write memory type

Algorithm
Initially Number [i] = 0 Choosing [i] = false, for i,0 <= i= < n-1 <Entry> 1: Choosing [i] := true 2: Number [i] := MAX (number) + 1 3: Choosing [i] := false 4: for j := 0 to n -1 (!= i) do 5: WAIT UNTIL Choosing [j] = false 6: wait until Number[j] = 0 or (Number[j],j > Number[i],i]) <Critical Section> <Exit> 7: Number [i] := 0 <Remainder>

Question #1- a
Is there a situation that two (or more) process will have the same ticket number?

Question #1- a
Is there a situation that two (or more) process will have the same ticket number? Yes, L#2 isnt atomic operation
2: Number [i] := MAX (number) + 1

So it two (or more) process running execute the same line at the same time, resloving the same value.

Question #1- b
So if two process have the same ticket value. How does the algorithm prevent from them to enter entering the critical section?

Question #1- b
So if two process have the same ticket value. How does the algorithm prevent from them to enter entering the critical section? In L#6 there is a special operator to take care of this
6:wait until Number[j] = 0 or (Number[j],j > Number[i],i]) 1: bool operator> (tic1, pid1, tic2, pid2) 2: { 3: return (tic1<tic2) | ((tic1=tic2) & (pid1<pid2) 4: }

Question #2
# 1 2 3 4 Enter process

Question #2
# 0 1 2 3 4 Enter process

Question #2
# 0 1 0 2 0 3 0 4 0 Enter process

Question #2
# 0 1 * 1 0 2 0 3 0 * 4 0 Enter process

Question #2
# 0 1 * 1 0 1 2 0 0 3 0 0 * 4 0 1 1 Enter process

Question #2
# 0 1 2 * 1 0 1 * 2 0 0 3 0 0 * * 4 0 1 1 Enter process

Question #2
# 0 1 2 * 1 0 1 0 * 2 0 0 2 3 0 0 0 * * 4 0 1 1 1 4 Enter process

Question #2
# 0 1 2 3 * * 1 0 1 0 * * 2 0 0 2 * 3 0 0 0 * * 4 0 1 1 1 4 Enter process

Question #2
# 0 1 2 3 * * 1 0 1 0 3 * * 2 0 0 2 2 * 3 0 0 0 3 * * 4 0 1 1 0 1 4 2 Enter process

Question #2
# 0 1 2 3 4 * * * 1 0 1 0 3 * * 2 0 0 2 2 * * 3 0 0 0 3 * * * 4 0 1 1 0 1 4 2 Enter process

Question #2
# 0 1 2 3 4 * * * 1 0 1 0 3 3 * * 2 0 0 2 2 0 * * 3 0 0 0 3 3 * * * 4 0 1 1 0 4 1 4 2 1 Enter process

Question #2
# 0 1 2 3 4 5 * * * 1 0 1 0 3 3 * * 2 0 0 2 2 0 * * * 3 0 0 0 3 3 * * * * 4 0 1 1 0 4 1 4 2 1 Enter process

Question #2
# 0 1 2 3 4 5 * * * 1 0 1 0 3 3 0 * * 2 0 0 2 2 0 0 * * * 3 0 0 0 3 3 3 * * * * 4 0 1 1 0 4 4 1 4 2 1 3 Enter process

Question #2
# 0 1 2 3 4 5 6 * * * 1 0 1 0 3 3 0 * * 2 0 0 2 2 0 0 * * * 3 0 0 0 3 3 3 * * * * * 4 0 1 1 0 4 4 1 4 2 1 3 Enter process

Question #2
# 0 1 2 3 4 5 6 * * * 1 0 1 0 3 3 0 0 * * 2 0 0 2 2 0 0 0 * * * 3 0 0 0 3 3 3 0 * * * * * 4 0 1 1 0 4 4 4 1 4 2 1 3 4 Enter process

Question #2
# 0 1 2 3 4 5 6 7 * * * 1 0 1 0 3 3 0 0 * * 2 0 0 2 2 0 0 0 * * * 3 0 0 0 3 3 3 0 * * * * * 4 0 1 1 0 4 4 4 1 4 2 1 3 4 Enter process

Question #2
# 0 1 2 3 4 5 6 7 * * * 1 0 1 0 3 3 0 0 0 * * 2 0 0 2 2 0 0 0 0 * * * 3 0 0 0 3 3 3 0 0 * * * * * 4 0 1 1 0 4 4 4 0 1 4 2 1 3 4 Enter process

Question #3
What changes need to be done in order to allow up to K process to enter the critical section?
Initia counter = 0 <Entry> 1: wait until counter < K 2: inc counter <Critical Section> <Exit> 4: dec counter <Reminder>
Line 1-2 are Mutual exclusion using bakery algorithm.

Вам также может понравиться