Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015!...
Transcript of Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015!...
![Page 1: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/1.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
6.033 Spring 2015!Lecture #6
• Threads • Condition Variables • Preemption
![Page 2: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/2.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
Enforcing Modularity via Virtualization
in order to enforce modularity + build an effective operating system
today’s goal: use threads to allow multiple programs to share a CPU
1. programs shouldn’t be able to refer to (and corrupt) each others’ memory
2. programs should be able to communicate
3. programs should be able to share a CPU without one program halting the progress of the others
virtual memory
bounded buffers
threads(virtualize processors)
(virtualize communication links)
![Page 3: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/3.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
thread: a virtual processor
thread API: !!suspend():!!
!!resume(): restore state from memory
save state of current thread to memory
![Page 4: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/4.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!send(bb,!message):!!!!!acquire(bb.lock)!!!!!while!True:!!!!!!!if!bb.in!–!bb.out!<!N:!!!!!!!!!bb.buf[bb.in!mod!N]!<@!message!!!!!!!!!bb.in!<@!bb.in!+!1!!!!!!!!!release(bb.lock)!!!!!!!!!return
![Page 5: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/5.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!send(bb,!message):!!!!!acquire(bb.lock)!!!!!while!True:!!!!!!!if!bb.in!–!bb.out!<!N:!!!!!!!!!bb.buf[bb.in!mod!N]!<@!message!!!!!!!!!bb.in!<@!bb.in!+!1!!!!!!!!!release(bb.lock)!!!!!!!!!return!!!!!!!release(bb.lock)!!!!!!!yield()!!!!!!!acquire(bb.lock)
![Page 6: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/6.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield():!!!!!//!Suspend!the!running!thread!!!!!//!Choose!a!new!thread!to!run!!!!!//!Resume!the!new!thread!!!
![Page 7: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/7.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield():!!!!!acquire(t_lock)!!!!!!//!Suspend!the!running!thread!!!!!//!Choose!a!new!thread!to!run!!!!!//!Resume!the!new!thread!!!!!!!!!!!release(t_lock)
![Page 8: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/8.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield():!!!!!acquire(t_lock)!!!!!!id!=!id#of#current#thread!!!!!threads[id].state!=!RUNNABLE!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!//!Choose!a!new!thread!to!run!!!!!//!Resume!the!new!thread!!!!!!!release(t_lock)
Suspend current thread
![Page 9: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/9.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield():!!!!!acquire(t_lock)!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].state!=!RUNNABLE!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!//!Choose!a!new!thread!to!run!!!!!//!Resume!the!new!thread!!!!!!!release(t_lock)
Suspend current thread
![Page 10: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/10.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield():!!!!!acquire(t_lock)!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].state!=!RUNNABLE!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!//!Resume!the!new!thread!!!!!!!!!release(t_lock)
Suspend current thread
Choose new thread
![Page 11: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/11.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield():!!!!!acquire(t_lock)!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].state!=!RUNNABLE!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!SP!=!threads[id].sp!!!!!PTR!=!threads[id].ptr!!!!!threads[id].state!=!RUNNING!!!!!cpus[CPU].thread!=!id!!!!!!release(t_lock)
Suspend current thread
Choose new thread
Resume new thread
![Page 12: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/12.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!send(bb,!message):!!!!!acquire(bb.lock)!!!!!while!True:!!!!!!!if!bb.in!–!bb.out!<!N:!!!!!!!!!bb.buf[bb.in!mod!N]!<@!message!!!!!!!!!bb.in!<@!bb.in!+!1!!!!!!!!!release(bb.lock)!!!!!!!!!return!!!!!!!release(bb.lock)!!!!!!!yield()!!!!!!!acquire(bb.lock)
![Page 13: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/13.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
condition variables: let threads wait for events, and get notified when they occur
condition variable API: !!!wait(cv):!!
!!notify(cv): notify waiting threads of cv
yield processor and wait to be notified of cv
![Page 14: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/14.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
problem: lost notify
!!send(bb,!message):!!!!!acquire(bb.lock)!!!!!while!True:!!!!!!!if!bb.in!–!bb.out!<!N:!!!!!!!!!bb.buf[bb.in!mod!N]!<@!message!!!!!!!!!bb.in!<@!bb.in!+!1!!!!!!!!!release(bb.lock)!!!!!!!!!notify(bb.not_empty)!!!!!!!!!return!!!!!!!release(bb.lock)!!!!!!!wait(bb.not_full)!!!!!!!acquire(bb.lock)
(threads in receive() will wait on bb.not_empty and
notify of bb.not_full)
![Page 15: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/15.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
condition variable API: !!!wait(cv,lock):!!
!!!!!notify(cv): notify waiting threads of cv
yield processor, release lock, wait to be notified of cv
![Page 16: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/16.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!send(bb,!message):!!!!!acquire(bb.lock)!!!!!while!True:!!!!!!!if!bb.in!–!bb.out!<!N:!!!!!!!!!bb.buf[bb.in!mod!N]!<@!message!!!!!!!!!bb.in!<@!bb.in!+!1!!!!!!!!!release(bb.lock)!!!!!!!!!notify(bb.not_empty)!!!!!!!!!return!!!!!!!wait(bb.not_full,!bb.lock)
![Page 17: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/17.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!wait(cv,!lock):!!!!!acquire(t_lock)!!!!!release(lock)!!!!!threads[id].cv!=!cv!!!!!threads[id].state!=!WAITING!!!!!yield_wait()!!!!!release(t_lock)!!!!!acquire(lock)
will be different than yield()
!!notify(cv):!!!!!acquire(t_lock)!!!!!for!i!=!0!to!N@1:!!!!!!!if!threads[id].cv!==!cv!&&!!!!!!!!!!threads[id].state!==!WAITING:!!!!!!!!!threads[id].state!=!RUNNABLE!!!!!release(t_lock)
![Page 18: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/18.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield_wait():!//=called=by=wait()!!!!!acquire(t_lock)!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].state!=!RUNNABLE!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!SP!=!threads[id].sp!!!!!PTR!=!threads[id].ptr!!!!!threads[id].state!=!RUNNING!!!!!cpus[CPU].thread!=!id!!!!!!release(t_lock)
problem: wait() holds t_lock
![Page 19: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/19.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield_wait():!//=called=by=wait()!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].state!=!RUNNABLE!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!SP!=!threads[id].sp!!!!!PTR!=!threads[id].ptr!!!!!threads[id].state!=!RUNNING!!!!!cpus[CPU].thread!=!id
problem: current thread’s state shouldn’t be RUNNABLE
![Page 20: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/20.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield_wait():!//=called=by=wait()!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!SP!=!threads[id].sp!!!!!PTR!=!threads[id].ptr!!!!!threads[id].state!=!RUNNING!!!!!cpus[CPU].thread!=!id
problem: deadlock (wait() holds t_lock)
![Page 21: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/21.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield_wait():!//=called=by=wait()!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!!!release(t_lock)!!!!!!!acquire(t_lock)!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!SP!=!threads[id].sp!!!!!PTR!=!threads[id].ptr!!!!!threads[id].state!=!RUNNING!!!!!cpus[CPU].thread!=!id
problem: stack corruption
![Page 22: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/22.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!yield_wait():!//=called=by=wait()!!!!!!id!=!cpus[CPU].thread!!!!!threads[id].sp!=!SP!!!!!threads[id].ptr!=!PTR!!!!!SP!=!cpus[CPU].stack!!!!!!do:!!!!!!!id!=!(id!+!1)!mod!N!!!!!!!release(t_lock)!!!!!!!acquire(t_lock)!!!!!while!threads[id].state!!=!RUNNABLE!!!!!!SP!=!threads[id].sp!!!!!PTR!=!threads[id].ptr!!!!!threads[id].state!=!RUNNING!!!!!cpus[CPU].thread!=!id
![Page 23: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/23.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!timer_interrupt():!!!!!!push!PC!!!!!push!registers!!!!!yield()!!!!!pop!registers!!!!!pop!PC
problem: what if timer interrupt occurs while CPU is running yield() or yield_wait()?
preemption: forcibly interrupt threads
![Page 24: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/24.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
!!timer_interrupt():!!!!!!push!PC!!!!!push!registers!!!!!yield()!!!!!pop!registers!!!!!pop!PC
preemption: forcibly interrupt threads
solution: hardware mechanism to disable interrupts
![Page 25: Lecture #6web.mit.edu/6.033/2015/ · Katrina LaCurts | lacurts@mit | 6.033 2015 6.033 Spring 2015! Lecture #6 • Threads • Condition Variables • Preemption](https://reader034.fdocuments.in/reader034/viewer/2022042304/5ecf750f6dae822b9756edb9/html5/thumbnails/25.jpg)
Katrina LaCurts | lacurts@mit | 6.033 2015
• Threads Virtualize a processor so that we can share it among programs. yield() allows the kernel to suspend the current thread and resume another.
• Condition Variables Provide a more efficient API for threads, where they wait for an event and are notified when it occurs. wait() requires a new version of yield(), yield_wait().
• Preemption Forces a thread to be interrupted so that we don’t have to rely on programmers correctly using yield(). Requires a special interrupt and hardware support to disable other interrupts.