10 - Bakery Algorithm Before entering its critical section,...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Bakery Algorithm Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section. If processes P i and P j receive the same number, if i < j , then P i is served first; else P j is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... Critical section for n processes Bakery Algorithm Notation < lexicographical order (ticket #, process id #) ( a,b ) < ( c,d ) if a < c or if a = c and b < d max ( a ,, a n-1 ) is a number, k , such that k a i for i = 0,, n 1 Shared data boolean choosing[n]; int number[n]; Data structures are initialized to false and respectively 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]) do no-op ; while ((number[j] != 0) && ((number[j],j) < (number[i],i))) do no-op ; } critical section number[i] = 0; remainder section } while (1); Mutual Exclusion: Hardware Support Interrupt Disabling A process runs until it invokes an operating-system service or until it is interrupted Disabling interrupts guarantees mutual exclusion Processor is limited in its ability to interleave programs Multiprocessing disabling interrupts on one processor will not guarantee mutual exclusion Mutual Exclusion: Hardware Support Special Machine Instructions Performed in a single instruction cycle (Atomic) Not subject to interference from other instructions Test and Set Swap Synchronization Hardware Test and modify the content of a word atomically boolean TestAndSet (boolean &target) { boolean rv = target; target = true; return rv; } Mutual Exclusion With Test-and- set Shared data: boolean lock = false; Process P i do { while (TestAndSet(lock)) do no- op; critical section lock = false; remainder section } Synchronization Hardware Atomically swap two variables. void Swap (boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; } Mutual Exclusion With Swap Shared data (initialized to false ): boolean lock; boolean waiting[n]; Process P i do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section } Key is a local boolean variable Mutual Exclusion Machine Instructions Advantages Applicable to any number of processes on either a single processor or multiple processors. It is simple and therefore easy to verify It can be used to support multiple critical sections Mutual Exclusion Machine Instructions Disadvantages Busy-waiting consumes processor time Starvation is possible when a process leaves a critical section and more than one process is waiting. Semaphores (OS Support) Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent Wait and signal operations cannot be interrupted Queue is used to hold processes waiting on the semaphore Semaphores...
View Full Document

Page1 / 71

10 - Bakery Algorithm Before entering its critical section,...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online