Lec25-Bakery Algorithm

# Tokenownidownid 3 enter critical section

• Notes
• 16

This preview shows pages 6–10. Sign up to view the full content.

(token[OwnID],OwnID)); //3. Enter Critical section critical_section(); //4. Leave Critical Section token[OwnID] = 0; } Why multiple waits? Because, have to wait for multiple processes

This preview has intentionally blurred sections. Sign up to view the full version.

7 Receive a token Receive a token Initially token[0] .. token[n-1] are set to zero Process i chooses token[i] as max (token[0],token[1],...,token[n-1]) + 1 ; Let n = 5; Let the order of execution be P0,P3,P4,P1,P2,P3,P4… P0 gets token[0] = max(0,0,0,0,0) + 1 = 0+1=1 P3 gets token[3] = max(1,0,0,0,0) + 1 = 1+1=2 P4 gets token[4] = max(1,0,0,2,0) + 1 = 2+1=3 P1 gets token[1] = max(1,0,0,2,3) + 1 = 3+1=4 P2 gets token[2] = max(1,4,0,2,3) + 1 = 4+1=5 P3 gets token[3] = max(1,4,5,2,3) + 1 = 5+1=6 P4 gets token[4] = max(1,4,5,6,3) + 1 = 6+1=7
8 Wait for turn Wait for turn Pi waits until it has the lowest token of all the processes waiting to enter the critical section. Bakery Algorithm does not guarantee that two processes do not receive the same token In case of a tie, the process with the lowest ID is served first. for (OthersID = 0;OthersID < n ; OthersID++) while (token[OthersID]!=0 && (token[OthersID],OthersID)< (token[OwnID],OwnID)); (a,b) < (c,d) = TRUE if a < c or if both a = c and b < d token[OwnID] = 0 => Process is not trying to enter the critical section

This preview has intentionally blurred sections. Sign up to view the full version.

9 while (TRUE) { //1. Receive a token token[OwnID]= max(token[0],token[1],..,token[n-1])+1; //2. Wait for turn for (OthersID = 0;OthersID<n;OthersID++) while (token[OthersID]!=0 &&(token[OthersID],OthersID)< (token[OwnID],OwnID)); while (TRUE) { //1. Receive a token token[OwnID]= max(token[0],token[1],..,token[n-1])+1; //2. Wait for turn for (OthersID = 0;OthersID<n;OthersID++) while (token[OthersID]!=0 &&(token[OthersID],OthersID)< (token[OwnID],OwnID)); token[0] = 1 Timeout
This is the end of the preview. Sign up to access the rest of the document.
• Fall '09
• TSL

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern