Lec25-Bakery Algorithm

# Token1 1 f t 3 enter critical section criticalsection

• Notes
• 16

This preview shows 9 out of 14 pages.

token[1] = 1 F && T //3. Enter Critical section critical_section(); T && F //3. Enter Critical section critical_section();

Subscribe to view the full document.

10 Bakery Algorithm Bakery Algorithm while (TRUE) { //1. Receive a token choosing[OwnID] = true; token[OwnID]= max(token[0],token[1],..,token[n-1])+1; choosing[OwnID] = false; //2. Wait for turn for (OthersID = 0;OthersID<n;OthersID++) while(choosing[OthersID]); while (token[OthersID]!=0 &&(token[OthersID],OthersID)< (token[OwnID],OwnID)); //3. Enter Critical section critical_section(); //4. Leave Critical Section token[OwnID] = 0; }
11 Bakery Algorithm Bakery Algorithm The reason for choosing is to prevent the second while loop being entered when process P OthersID is setting its token[OthersID] . choosing[OthersID] is true if P OthersID is choosing a token. If a process P OthersID is choosing a token when Pi tries to look at it, Pi waits until P OthersID has done so before looking

Subscribe to view the full document.

12 Hardware Solutions Hardware Solutions Disabling/Enabling Interrupts Fetch Next Instruction Execute Instruction Check for Interrupt: Process Interrupt Start Halt Decode Instruction No Interrupt Checking No Context Switching This solution is dangerous It will not work if we have multiple CPUs Since, the Disable Interrupt instruction will execute on only one CPU Rest of the CPUs, will continue checking the interrupts
13 Software Lock Software Lock Process 1 1.while (FLAG == FALSE); 2.FLAG = FALSE; 4.FLAG = TRUE; 5.noncritical_section(); Process 2 1.while (FLAG == FALSE); 2.

Subscribe to view the full document.

You've reached the end of this preview.
• 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