Modified from Silberschatz, Galvin and Gagne & Stallings Lecture 11 Chapter 6: Process Synchronization (cont)

  • View
    222

  • Download
    2

Embed Size (px)

Text of Modified from Silberschatz, Galvin and Gagne & Stallings Lecture 11 Chapter 6: Process...

  • Slide 1
  • Modified from Silberschatz, Galvin and Gagne & Stallings Lecture 11 Chapter 6: Process Synchronization (cont)
  • Slide 2
  • 2 CS 446/646 Principles of Computer Operating Systems Chapter 6: Process Synchronization Background The Critical-Section Problem Petersons Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions
  • Slide 3
  • 3 CS 446/646 Principles of Computer Operating Systems Concurrency can be viewed at different levels: multiprogramming interaction between multiple processes running on one CPU (pseudo-parallelism) multithreading interaction between multiple threads running in one process multiprocessors interaction between multiple CPUs running multiple processes/threads (real parallelism) multicomputers interaction between multiple computers running distributed processes/threads the principles of concurrency are basically the same in all of these categories Process Interaction & Concurrency Software view Hardware view
  • Slide 4
  • 4 CS 446/646 Principles of Computer Operating Systems o processes unaware of each other they must use shared resources independently, without interfering, and leave them intact for the others P1P1 P2P2 resource o processes indirectly aware of each other they work on common data and build some result together via the data P2P2 P1P1 data o processes directly aware of each other they cooperate by communicating, e.g., exchanging messages P2P2 P1P1 messages Whether processes or threads: three basic interactions Process Interaction & Concurrency
  • Slide 5
  • 5 CS 446/646 Principles of Computer Operating Systems CPU Insignificant race condition in the shopping scenario the outcome depends on the CPU scheduling or interleaving of the threads (separately, each thread always does the same thing) >./multi_shopping grabbing the salad... grabbing the milk... grabbing the apples... grabbing the butter... grabbing the cheese... > A B salad apples milk buttercheese >./multi_shopping grabbing the milk... grabbing the butter... grabbing the salad... grabbing the cheese... grabbing the apples... > A salad apples B milk buttercheese Race Condition
  • Slide 6
  • 6 CS 446/646 Principles of Computer Operating Systems char chin, chout; void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); } A char chin, chout; void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); } B >./echo Hello world! Single-threaded echo Multithreaded echo (lucky) >./echo Hello world! 1 2 3 4 5 6 lucky CPU scheduling Significant race conditions in I/O & variable sharing Race Condition
  • Slide 7
  • 7 CS 446/646 Principles of Computer Operating Systems char chin, chout; void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); } A >./echo Hello world! Single-threaded echo char chin, chout; void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); } B Significant race conditions in I/O & variable sharing 1 5 6 2 3 4 unlucky CPU scheduling Multithreaded echo (unlucky) >./echo Hello world! ee.... Race Condition
  • Slide 8
  • 8 CS 446/646 Principles of Computer Operating Systems void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (...); } B void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (...); } A >./echo Hello world! Single-threaded echo Significant race conditions in I/O & variable sharing 1 5 6 2 3 4 unlucky CPU scheduling Multithreaded echo (unlucky) >./echo Hello world! eH.... changed to local variables Race Condition
  • Slide 9
  • 9 CS 446/646 Principles of Computer Operating Systems Significant race conditions in I/O & variable sharing in this case, replacing the global variables with local variables did not solve the problem we actually had two race conditions here: one race condition in the shared variables and the order of value assignment another race condition in the shared output stream: which thread is going to write to output first this race persisted even after making the variables local to each thread generally, problematic race conditions may occur whenever resources and/or data are shared by processes unaware of each other or processes indirectly aware of each other Race Condition
  • Slide 10
  • 10 CS 446/646 Principles of Computer Operating Systems Producer / Consumer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed }
  • Slide 11
  • 11 CS 446/646 Principles of Computer Operating Systems Race Condition count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Consider this execution interleaving with count = 5 initially: S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}
  • Slide 12
  • 12 CS 446/646 Principles of Computer Operating Systems The indivisible execution blocks are critical regions a critical region is a section of code that may be executed by only one process or thread at a time B A common critical region B A As critical region Bs critical region although it is not necessarily the same region of memory or section of program in both processes but physically different or not, what matters is that these regions cannot be interleaved or executed in parallel (pseudo or real) Critical Regions
  • Slide 13
  • 13 CS 446/646 Principles of Computer Operating Systems enter critical region? exit critical region enter critical region? exit critical region critical regions can be protected from concurrent access by padding them with entrance and exit gates o a thread must try to check in, then it must check out We need mutual exclusion from critical regions void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (...); } BA void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (...); } Critical Regions
  • Slide 14
  • 14 CS 446/646 Principles of Computer Operating Systems Solution to Critical-Section Problem 1.Mutual Exclusion o If process P i is executing in its critical section, o then no other processes can be executing in their critical sections 2.Progress o If no process is executing in its critical section and there exist some processes that wish to enter their critical section, o then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3.Bounded Waiting o A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes
  • Slide 15
  • 15 CS 446/646 Principles of Computer Operating Systems Petersons Solution Two process solution Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables: int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process P i is ready!
  • Slide 16
  • 16 CS 446/646 Principles of Computer Operating Systems Algorithm for Process P i do { flag[i] = TRUE; turn = j; while (flag[j] && turn == j); critical section flag[i] = FALSE; remainder section } while (TRUE);
  • Slide 17
  • 17 CS 446/646 Principles of Computer Operating Systems Synchronization Hardware Uniprocessors could disable interrupts Currently running code would execute without preemption what guarantees that the user process is going to ever exit the critical region? meanwhile, the CPU cannot interleave any other task even unrelated to this race condition Generally too inefficient on multiprocessor systems Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words
  • Slide 18
  • 18 CS 446/646 Principles of Computer Operating Systems Solution to Critical-section Problem Using Locks do { acquire lock critical section release lock remainder section } while (TRUE); Many systems provide hardware support for critical section code
  • Slide 19
  • 19 CS 446/646 Principles of Computer Operating Systems 1. thread A reaches CR and finds the lock at 0 and sets it in one shot, then enters 1.1 even if B comes right behind A, it will find that the lock is already at 1 2. thread A exits CR, then resets lock to 0 3. thread B finds the lock at 0 and sets it to 1 in one shot, just before entering CR cri

Search related