77fe681e83f8b6916d9da213de50269c.ppt
- Количество слайдов: 24
Operating Systems CMPSCI 377 Lecture 7: Synchronization Emery Berger University of Massachusetts, Amherst UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
Last Time: Scheduling Algorithms n FCFS n n Round-robin n n Shortest job first Multilevel Feedback Queues n n Use quantum & preemption to alternate jobs SJF n n First-Come, First-Served Round robin on each priority queue Lottery Scheduling n n Jobs get tickets Scheduler randomly picks winner UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 2
This Time: Synchronization n Threads must communicate to ensure consistency n n Else: race condition (non-deterministic result) Synchronization operations How to write concurrent code How to implement synch. operations UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 3
Synchronization – Motivation n “The too much milk problem” n Model of need to synchronize activities UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 4
Synchronization Terminology Mutual exclusion (“mutex”) – prevents multiple threads from entering Critical section – code only one thread can execute at a time Lock – mechanism for mutual exclusion n Lock on entering critical section, accessing shared data Unlock when complete Wait if locked UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 5
Solving the Too Much Milk Problem n Correctness properties n n n Only one person buys milk n Safety: “nothing bad happens” Someone buys milk if you need to n Progress: “something good eventually happens” First: use atomic loads & stores as building blocks n n n “Leave a note” (lock) “Remove a note” (unlock) “Don’t buy milk if there’s a note” (wait) UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 6
Too Much Milk: Solution 1 thread A thread B if (no milk && no note) leave note buy milk remove note n too Does this work? much milk UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 7
Too Much Milk: Solution 2 Idea: use labeled notes thread A thread B leave note A if (no note B) if (no milk) buy milk remove note A leave note B if (no note A) if (no milk) buy milk remove note B oops – no milk UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 8
Too Much Milk: Solution 3 Idea: wait for the right note thread A thread B leave note A while (note B) do nothing if (no milk) buy milk remove note A leave note B if (no note A) if (no milk) buy milk remove note B n Must try all possibilities to verify UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 9
Too Much Milk: Solution 3 Possibility 1: A first, then B thread A thread B leave note A while (note B) do nothing if (no milk) buy milk remove note A leave note B if (no note A) if (no milk) buy milk remove note B n OK UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 10
Too Much Milk: Solution 3 Possibility 2: B first, then A thread B leave note A while (note B) do nothing if (no milk) buy milk remove note A leave note B if (no note A) if (no milk) buy milk remove note B n OK UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 11
Too Much Milk: Solution 3 Possibility 3: Interleaved – A waits & buys thread A thread B leave note A while (note B) do nothing if (no milk) buy milk remove note A leave note B if (no note A) if (no milk) buy milk remove note B n OK UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 12
Too Much Milk: Solution 3 Possibility 4: Interleaved – A waits, B buys thread A thread B leave note A while (note B) do nothing if (no milk) buy milk remove note A leave note B if (no note A) if (no milk) buy milk remove note B n OK UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 13
Too Much Milk: Solution 3: “Thread A waits for B, otherwise buys” n Correct – preserves desired properties n n n Safety: we only buy one milk Progress: we always buy milk But… UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 14
Problems with this Solution n Complicated n n Asymmetrical n n n Threads A & B are different Adding more threads = different code for each thread Poor utilization n n Difficult to convince ourselves that it works Busy waiting – consumes CPU resources, no useful work Possibly non-portable n Relies on atomicity of loads & stores UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 15
Language Support n n Synchronization complicated Better way – provide language-level support n n n Higher-level approach Hide gory details in runtime system Increasingly high-level approaches: n n n Locks, Atomic Operations Semaphores – generalized locks Monitors – tie shared data to synchronization UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 16
Locks n Provide mutual exclusion to shared data via two atomic routines n n n Lock: : Acquire – wait for lock, then take it Lock: : Release – unlock, wake up waiters Rules: n n n Acquire lock before accessing shared data Release lock afterwards Lock – initially released UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 17
Too Much Milk: Locks thread A thread B Lock. acquire() if (no milk) buy milk Lock. release() n Clean, symmetric - but how do we implement it? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 18
Implementing Locks n n Requires hardware support (in general) Can build on atomic operations: n n Load/Store Disable interrupts n n Uniprocessors only Test & Set, Compare & Swap UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 19
Disabling Interrupts n Prevent scheduler from switching threads in middle of critical sections n n Ignores quantum expiration (timer interrupt) No handling I/O operations n n (Don’t make I/O calls in critical section!) Why not implement as system call? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 20
Implementing Locks: Disabling Interrupts Class Lock { private int value; private Queue q; Lock () { value = 0; q = empty; } public void release () { disable interrupts; if (q not empty) { thread t = q. pop(); put t on ready queue; } else { value = FREE; } enable interrupts; } public void acquire () { disable interrupts; if (value == BUSY) { add this thread to q; sleep(); } else { value = BUSY; } enable interrupts; } } UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 21
Example: Locks via Disabling Interrupts UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 22
Summary n n n Communication between threads: via shared variables Critical sections = regions of code that modify or access shared variables Must be protected by synchronization primitives that ensure mutual exclusion n n Loads & stores: tricky, error-prone Solution: high-level primitives (e. g. , locks) UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 23
Next Time n More synchronization n n Semaphores Monitors UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 24


