cse 513 i ntroduction to operating systems class 3 i
play

CSE 513 I ntroduction to Operating Systems Class 3 - I - PowerPoint PPT Presentation

CSE 513 I ntroduction to Operating Systems Class 3 - I nterprocesses Communication & Synchronization Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and Science University 1 Race conditions What is a race condition?


  1. CSE 513 I ntroduction to Operating Systems Class 3 - I nterprocesses Communication & Synchronization Jonathan Walpole Dept. of Comp. Sci. and Eng. Oregon Health and Science University 1

  2. Race conditions What is a race condition? � � t wo or more processes have an inconsist ent view of a shared memory region (I .e., a variable) Why do race conditions occur? � � values of memory locat ions replicat ed in regist ers during execut ion � cont ext swit ches at arbit rary t imes during execut ion � processes can see “st ale” memory values in regist ers What solutions can we apply? � � prevent cont ext swit ches by prevent ing int errupt s? � make processes coordinat e wit h each ot her t o ensure mut ual exclusion in accessing “crit ical sect ions” of code 2

  3. Counter increment race condition I ncrementing a counter (load, increment, store) Context switch can occur af ter load and bef ore increment! 3

  4. Mutual exclusion conditions No two processes simultaneously in critical region � No assumptions made about speeds or numbers of CPUs � No process running outside its critical region may block � another process No process must wait f orever to enter its critical region � 4

  5. Critical regions with mutual exclusion Mutual exclusion using critical regions 5

  6. How can we implement mutual exclusion? � What about using a binary “lock” variable in memory and having processes check it and set it bef ore entry to critical regions? � Many computers have some limited hardware support f or setting locks � “At omic” Test and Set Lock inst r uct ion � “At omic” compar e and swap oper at ion � Solves the problem of � Expr essing int ent ion t o ent er C.S. � Act ually set t ing a lock t o pr event concur r ent access 6

  7. Test and Set Lock � Test- and- set does two things atomically: � Test a lock (whose value is r et ur ned) � Set t he lock 1. extract value lock = {TRUE , FALSE} Test_var 2. Set TRUE � Lock obtained when the return value is FALSE � I f TRUE, someone already had the lock (and still has it) 7

  8. Test and Set Lock FALSE Lock 8

  9. Test and Set Lock P1 FALSE Lock 9

  10. Test and Set Lock FALSE = Lock Available!! P1 FALSE Lock 10

  11. Test and Set Lock P1 FALSE TRUE Lock 11

  12. Test and Set Lock P1 FALSE TRUE Lock 12

  13. Test and Set Lock P1 P2 P3 TRUE TRUE TRUE TRUE TRUE P4 TRUE TRUE Lock 13

  14. Test and Set Lock P1 P2 P3 TRUE TRUE TRUE TRUE TRUE P4 TRUE TRUE Lock 14

  15. Test and Set Lock P1 P2 P3 TRUE TRUE TRUE TRUE TRUE P4 TRUE TRUE Lock 15

  16. Test and Set Lock P1 P2 P3 TRUE TRUE TRUE TRUE TRUE P4 FALSE TRUE Lock 16

  17. Test and Set Lock P1 P2 P3 FALSE TRUE P4 FALSE Lock 17

  18. Test and Set Lock P1 P2 P3 FALSE TRUE P4 FALSE Lock 18

  19. Test and Set Lock P1 P2 P3 TRUE TRUE TRUE P4 TRUE TRUE Lock 19

  20. Critical section entry code with TSL I J 1 repeat 1 repeat 2 while(TSL(lock)) while(TSL(lock)) • 3 no-op; 3 no-op; 4 critical section 4 critical section 5 Lock = FALSE; 5 Lock = FALSE; 6 remainder section 6 remainder section 7 until FALSE 7 until FALSE Guaranteed that only one process returns with FALSE when a lock is returned to the system and others are waiting to act on the lock 20

  21. Generalized primitives f or critical sections Thus f ar, the solutions have used busy waiting � � a process consumes CP U resources t o evaluat e when a lock becomes f ree � on a single CP U syst em busy wait ing prevent s t he lock holder f rom running, complet ing t he crit ical sect ion and releasing t he lock! � it would be bet t er t o block inst ead of busy wait (on a single CP U syst em) Blocking synchronization primitives � � sleep – allows a process t o sleep on a condit ion � wakeup – allows a process t o signal anot her process t hat a condit ion it was wait ing on is t rue � but how can t hese be implement ed? 21

  22. Blocking synchronization primitives Sleep and wakeup are system calls � � OS can implement t hem by managing a dat a st ruct ure t hat records who is blocked and on what condit ion � but how can t he OS access t hese dat a st ruct ures at omically? Concurrency in the OS: context switches and interrupts � � t he OS can arrange not t o perf orm a cont ext swit ch while manipulat ing it s dat a st ruct ures f or sleep and wakeup � but what about int errupt s? � what if int errupt handlers manipulat e t he sleep and wakeup dat a st ruct ures? What if t hey need synchronizat ion? � how can t he OS synchronize access t o it s own crit ical sect ions? 22

  23. Disabling interrupts Disabling interrupts in the OS vs disabling interrupts in � user processes � why not allow user processes t o disable int errupt s? � is it ok t o disable int errupt s in t he OS? � what precaut ions should you t ake? 23

  24. Generic synchronization problems 24

  25. Producer/ Consumer with busy waiting process producer{ process consumer{ while(1){ while(1){ //produce char c while (count==0) while (count==n) no_op; no_op; c = buf[OutP]; buf[InP] = c; OutP = OutP + 1 mod n InP = InP + 1 mod n count--; count++; //consume char } } } } n-1 0 Global variables: 2 char buf[n] int InP, OutP; // [0-n-1] int count 3 … 25

  26. Problems with busy waiting solution Producer and consumer can’t run at the same time � Count variable can be corrupted if context switch occurs � at the wrong time Bugs dif f icult to track down � 26

  27. Producer/ Consumer with blocking process consumer{ process producer{ while(1){ while(1){ • • //produce char c while (count==0) • • if (count==n) sleep(empty); • • sleep(full); c = buf[OutP]; • • OutP = OutP + 1 mod n buf[InP] = c; • • InP = InP + 1 mod n count--; • • count++; if (count == n-1) • • if (count == 1) wakeup(full); • • wakeup(empty); //consume char • • } } • • } } n-1 1 Global variables: char buf[n] 2 int InP, OutP; // [0-n-1] int count 3 … 27

  28. Problems with the blocking solution Count variable can be corrupted � I ncrements or decrements may be lost � Both processes may sleep f orever � Buf f er contents may be over- written � Code that manipulates count must be made a critical � section and protected using mutual exclusion Sleep and wakeup must be implemented as system calls � OS must use synchronization mechanisms (TSL or � interrupt disabling) in its implementation of sleep and wake- up … I .e., the critical sections of OS code that manipulate sleep/ wakeup data structures must be protected using mutual exclusion 28

  29. Semaphores � An abstract data type that can be used f or condition synchronization and mutual exclusion � I nteger variable with two operations: � down (sema_var ) decr ement sema_var by 1, if possible if not possible, “wait ” unt il possible � up (sema_var ) incr ement sema_var by 1 � Both up() and down() are assumed to be atomic � made t o be at omic by OS implement at ion 29

  30. Semaphores � There are multiple names f or semaphores � Down(S), wait (S), P(S) � Up(S), signal(S), V(S) � Semaphore implementations � Binar y semaphor es (mut ex) • support mutual exclusion (lock either set or f ree) � Count ing semaphor es • support multiple values f or more sophisticated coordination and controlled concurrent access among processes 30

  31. Using Semaphores f or Mutex semaphore mutex = 1 1 repeat 1 repeat 2 down(mutex); 2 down(mutex); 3 critical section 3 critical section 4 up(mutex); 4 up(mutex); 5 remainder section 5 remainder section 6 until FALSE 6 until FALSE 31

  32. Using Semaphores f or Mutex semaphore mutex = 0 1 repeat 1 repeat 2 down(mutex); 2 down(mutex); 3 critical section 3 critical section 4 up(mutex); 4 up(mutex); 5 remainder section 5 remainder section 6 until FALSE 6 until FALSE 32

  33. Using Semaphores f or Mutex semaphore mutex = 0 1 repeat 1 repeat 2 down(mutex); 2 down(mutex); 3 critical section 3 critical section 4 up(mutex); 4 up(mutex); 5 remainder section 5 remainder section 6 until FALSE 6 until FALSE 33

  34. Using Semaphores f or Mutex semaphore mutex = 1 1 repeat 1 repeat 2 down(mutex); 2 down(mutex); 3 critical section 3 critical section 4 up(mutex); 4 up(mutex); 5 remainder section 5 remainder section 6 until FALSE 6 until FALSE 34

  35. Using Semaphores f or Mutex semaphore mutex = 1 Check again to see if it can be decremented 1 repeat 1 repeat 2 down(mutex); 2 down(mutex); 3 critical section 3 critical section 4 up(mutex); 4 up(mutex); 5 remainder section 5 remainder section 6 until FALSE 6 until FALSE 35

  36. I n class exercise… � I mplement producer consumer solution: 36

  37. Counting semaphores in producer/ consumer Global variables semaphore full_buffs = 0; semaphore empty_buffs = n; char buff[n]; int InP, OutP; process producer{ process consumer{ while(1){ while(1){ • • //produce char c • down(full_buffs); • down(empty_buffs); c = buf[OutP]; • • buf[InP] = c; OutP = OutP + 1 mod n • • InP = InP + 1 mod n up(empty_buffs); • • up(full_buffs); • //consume char • } } • • } } 37

Recommend


More recommend