If processes Pi and Pj receive the same number if i j then P is served first

If processes pi and pj receive the same number if i j

This preview shows page 21 - 33 out of 58 pages.

° If processes Pi and Pj receive the same number, ± if i <= j, then P is served first; else Pj is served first. ° The numbering scheme always generates numbers in increasing order of enumeration; i.e. 1,2,3,3,3,3,4,4,5,5
Image of page 21
Bakery Algorithm (cont.) ° Notation - ± Lexicographic order(ticket#, process id#) ° ( a , b ) < ( c , d ) if ( a < c ) or if (( a = c ) and ( b < d )) ° max( a 0 ,…. a n -1 ) is a number, k , such that k >= a i for i = 0,…, n -1 ° Shared Data var choosing : array [0.. n -1] of boolean ;(initialized to false ) number : array [0.. n -1] of integer ; (initialized to 0)
Image of page 22
Bakery Algorithm (cont.) repeat choosing [ i ] := true ; number [ i ] := max( number [0], number [1],…, number [ n -1]) +1; choosing [ i ] := false ; for j := 0 to n -1 do begin while choosing [j] do no-op ; while number [ j ] <> 0 and ( number [ j ] ,j ) < ( number [ i ] ,i ) do no-op; end ; critical section number [ i ]:= 0; remainder section until false;
Image of page 23
Hardware Solutions for Synchronization ° Mutual exclusion solutions presented depend on memory hardware having read/write cycle. ± If multiple reads/writes could occur to the same memory location at the same time, this would not work. ± Processors with caches but no cache coherency cannot use the solutions ° In general, it is impossible to build mutual exclusion without a primitive that provides some form of mutual exclusion. ° How can this be done in the hardware???
Image of page 24
Synchronization Hardware ° Test and modify the content of a word atomically - Test-and-set instruction function Test-and-Set ( var target : boolean ): boolean ; begin Test-and-Set := target ; target := true ; end ; ° Similarly “ SWAP ” instruction
Image of page 25
Mutual Exclusion with Test-and-Set ° Shared data: var lock: boolean (initially false) ° Process Pi repeat while Test-and-Set ( lock ) do no-op; critical section lock := false; remainder section until false;
Image of page 26
Bounded Waiting Mutual Exclusion with Test-and-Set var j : 0.. n -1; key : boolean ; repeat waiting [ i ] := true ; key := true ; while waiting [i] and key do key := Test-and-Set(lock) ; waiting [ i ] := false ; critical section j := j + 1 mod n; while ( j <> i ) and ( not waiting [ j ]) do j := j + 1 mod n ; if j = i then lock := false ; else waiting [ j ] := false ; remainder section until false ;
Image of page 27
Semaphore ° Semaphore S - integer variable ± used to represent number of abstract resources ° Can only be accessed via two indivisible (atomic) operations wait ( S ): while S <= 0 do no-op S := S -1; signal (S): S := S +1; ± P or wait used to acquire a resource, decrements count ± V or signal releases a resource and increments count ± If P is performed on a count <= 0, process must wait for V or the release of a resource.
Image of page 28
Example: Critical Section for n Processes ± Shared variables var mutex : semaphore initially mutex = 1 ± Process P i repeat wait ( mutex ) ; critical section signal ( mutex ) ; remainder section until false
Image of page 29
Semaphore as a General Synchronization Tool ° Execute B in P j only after A execute in P i ° Use semaphore flag initialized to 0 ° Code: P P i j . . . . . . A wait ( flag ) signal ( flag ) B
Image of page 30
Problem... ° Busy Waiting, uses CPU that others could use. This type of semaphore is called a spinlock . ± OK for short times since it prevents a context switch. ° For longer runtimes, need to modify P and V so that processes can block and resume.
Image of page 31
Semaphore Implementation ° Define a semaphore as a record type semaphore = record value : integer ; L : list of processes ; end ; ° Assume two simple operations ° block suspends the process that invokes it.
Image of page 32
Image of page 33

You've reached the end of your free preview.

Want to read all 58 pages?

  • One '20
  • producer, monitor, Semaphore

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors