1 / 15

Synchronization (other solutions …)

Synchronization (other solutions …). Announcements. Assignment 2 is graded Project 1 is due today. Critical Section Problem. Problem: Design a protocol for processes to cooperate, such that only one process is in its critical section How to make multiple instructions seem like one?. CS 1.

sdiana
Download Presentation

Synchronization (other solutions …)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Synchronization(other solutions …)

  2. Announcements • Assignment 2 is graded • Project 1 is due today

  3. Critical Section Problem • Problem: Design a protocol for processes to cooperate, such that only one process is in its critical section • How to make multiple instructions seem like one? CS1 Process 1 Process 2 CS2 Time  Processes progress with non-zero speed, no assumption on clock speed Used extensively in operating systems: Queues, shared variables, interrupt handlers, etc.

  4. Solution Structure Shared vars: Initialization: Process: . . . . . . Entry Section Critical Section Exit Section Added to solve the CS problem

  5. Solution Requirements • Mutual Exclusion • Only one process can be in the critical section at any time • Progress • Decision on who enters CS cannot be indefinitely postponed • No deadlock • Bounded Waiting • Bound on #times others can enter CS, while I am waiting • No livelock • Also efficient (no extra resources), fair, simple, …

  6. Process 0 turn = 0; while(turn != 0); Critical Section turn = 1; Solution 1 (Two Processes) Shared: int turn; Initialize: turn = 0; Process 1 turn = 1; while(turn != 1); Critical Section turn = 0; Problem: Does not satisfy progress! Assumes strict alternation

  7. Process 0 flag[0] = true; while(flag[1]); Critical Section flag[0] = false; Solution 1 (Two Processes) Shared: bool flag[2]; Initialize: flag[0] = false; flag[1] = false; Process 1 flag[1] = true; while(flag[0]); Critical Section flag[1] = false; Problem: Does not satisfy progress! Timing dependent What if you swap statement in the entry section?

  8. Process i flag[i] = true; turn = j; while(flag[j] && turn == j); Critical Section flag[i] = false; Peterson’s Algorithm Shared: bool flag[2]; int turn; Initialize: flag[0] = false; flag[1] = false; turn = 0; Works for atomic load/stores! Special problem for multiprocessors

  9. Solution 2: Disable Interrupts • Disable interrupts while process is in critical section • 2 approaches: • Scheduler checks if program counter is in critical section • Do not interrupt if it is • Pro: Fast Con: needs compiler support • Process disables interrupts before entering critical section • Enables on exit • Pro: Easy Con: when infinite loop in CS • Cons • Response time • Multiprocessors  Critical section should be small! Process Disable interrupts Critical Section Enable interrupts Entry Section Exit Section

  10. Solution 3: Hardware Primitives • Modern hardware provides better atomic instructions: • test_and_set • swap • compare_and_swap • load_linked/store_conditional • fetch_and_add • Entire function is atomic • Possible by locking bus for both load and store void swap(bool *a, bool *b) { bool temp = *a; *a = *b; *b = temp; } bool test_and_set(bool *target) { bool temp = *target; *target = TRUE; return temp; }

  11. Process i While(test_and_set(&lock)); Critical Section lock = false; Solution 3: Hardware Primitives Share: int lock; Initialize: lock = false; Problem: Does not satisfy bounded waiting (see book for correct solution)

  12. Hardware Primitives • Pros: • Simple primitive • Easy to program critical sections • Cons • Busy waiting for entire duration of critical section! • Also called spinlocks

  13. Implementing Semaphores • P() and V() should be executed atomically • Disable interrupts, or • Busy waiting solutions • Move busy wait from entry of CS to P() and V() • P() and V() code is small

  14. Semaphores Atomicity typedef struct semaphore { int lock; int value: ProcessList L; } Semaphore; void P(Semaphore *S) { while(test_and_set(&S->lock) == 1) /* do nothing */; S->value = S->value - 1; if (S.value < 0) { add this process to S.L; atomic_clear_and block(&S->lock); } else atomicclear(&S->lock); } void V(S) { while(test_and_set(&S->lock) == 1) /* do nothing */; S->value = S->value + 1; if (S->value <= 0) { remove a process P from S.L; wakeup P } atomicclear(&S->lock); }

  15. A Real Race Condition • Customer to sales executive: “ This is the second time I have written to you, and I don't blame you for not answering me, because I sounded crazy, but it is a fact that as a tradition in our family we have ice-cream for dessert after dinner each night. …”

More Related