Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.

Similar presentations


Presentation on theme: "1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org."— Presentation transcript:

1 1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org

2 2 6 th Edition §Skip 1.4, 1.5, 1.6 §Skip 2.6 §Skip 3.6, 3.7, 3.8 l (3.5 will be done later) §Skip 4.5, 4.6 §Skip 6.4, 6.5

3 3 §Producer / Consumer §Critical Section problem

4 4 The Critical-Section Problem §n processes all competing to use some shared data §Each process has a code segment, called critical section, in which the shared data is accessed. §Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.

5 5 §Race condition: The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last.

6 6 Solution to Critical-Section Problem 1.Mutual Exclusion. 2.Progress. 3.Bounded Waiting.

7 7 Algorithm 1 turn is initialized to 0 (or 1)

8 8 §Algorithm 1 §Strict Alternation §Other problems with Algorithm 1?...

9 9 Algorithm 2 §Shared variables l boolean flag[2]; initially flag [0] = flag [1] = false. l flag [i] = true  P i ready to enter its critical section

10 10 Algorithm 2 § i, j two processes j=1-i §Process P i do { flag[i] := true; while (flag[j]) ; critical section flag [i] = false; remainder section } while (1);

11 11 Algorithm 2 §Process P 0 do { flag[0] := true; while (flag[1]) ; critical section flag [0] = false; remainder section } while (1); §Process P 1 do { flag[1] := true; while (flag[0]) ; critical section flag [1] = false; remainder section } while (1);

12 12 Algorithm 2 §Problems?

13 13 Algorithm 3 §Process P i do { flag [i]:= true; turn = j; while (flag [j] and turn = j) ; critical section flag [i] = false; remainder section } while (1);

14 14 Algorithm 3 §Process P 0 do { flag [0]:= true; turn = 1; while (flag [1] and turn = 1) ; critical section flag [0] = false; remainder section } while (1); §Process P 1 do { flag [1]:= true; turn = 0; while (flag [0] and turn = 0) ; critical section flag [1] = false; remainder section } while (1);

15 15 Algorithm 3 §Meets all three requirements; solves the critical-section problem for two processes.

16 16 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

17 17 Bakery Algorithm §Notation <  lexicographical order (ticket #, process id #) l (a,b) < c,d) if a < c or if a = c and b < d l max (a 0,…, 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 0 respectively

18 18 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]) ; while ((number[j] != 0) && (number[j],j) < (number[i],i) ) ; } critical section number[i] = 0; //exit section remainder section } while (1);

19 19 Synchronization Hardware §One way … disable interrupts §problems?

20 20 Synchronization Hardware §One way … disable interrupts §What problems can occur if we use the following implementation of a lock? Lock::Acquire { disable_interrupts(); } (critical section) Lock::Release { enable_interrupts(); }

21 21 TestAndndSet Instruction boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }

22 22 Mutual Exclusion with Test-and-Set §Shared data: boolean lock = false; Process P i do { while (TestAndSet(lock)) ; critical section lock = false; remainder section } What if two processes try to execute TestAndSet?

23 23 Synchronization Hardware Any problems with test and set?

24 24 §Can a context switch occur if a process is executing in its critical section?

25 25 Synchronization Hardware Do yourself §Swap §Mutual exclusion with test and set

26 26 §Drawback of all of the synchronization solutions seen so far?

27 27 BUSY WAITING A process is waiting for an event to occur and it does so by executing instructions

28 28 §Busy Waiting §Priority inversion problem §A lock that uses busy waiting is called a spinlock §Synchronization tool that allow a process to block instead of wasting CPU time, when the process is not allowed to enter its critical region.


Download ppt "1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org."

Similar presentations


Ads by Google