lecture #5 - mit.edu file1. programs shouldn’t be able to refer to (and corrupt) each others’...
TRANSCRIPT
![Page 1: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/1.jpg)
6.033 Spring 2019Lecture #5
• Threads • Condition Variables • Preemption
6.033 | spring 2019 | [email protected]
![Page 2: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/2.jpg)
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
assume one program per CPU
(virtualize communication links)
in order to enforce modularity + build an effective operating system
operating systems enforce modularity on a single machine using virtualization
6.033 | spring 2019 | [email protected]
![Page 3: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/3.jpg)
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)
in order to enforce modularity + build an effective operating system
operating systems enforce modularity on a single machine using virtualization
6.033 | spring 2019 | [email protected]
![Page 4: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/4.jpg)
thread: a virtual processor
thread API: suspend():
resume(): restore state from memory
save state of current thread to memory
6.033 | spring 2019 | [email protected]
![Page 5: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/5.jpg)
send(bb,message):acquire(bb.lock)whilebb.in-bb.out==N:release(bb.lock)acquire(bb.lock)bb.buf[bb.inmodN]<-messagebb.in<-bb.in+1release(bb.lock)return
6.033 | spring 2019 | [email protected]
![Page 6: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/6.jpg)
send(bb,message):acquire(bb.lock)whilebb.in-bb.out==N:release(bb.lock)yield()acquire(bb.lock)bb.buf[bb.inmodN]<-messagebb.in<-bb.in+1release(bb.lock)return
6.033 | spring 2019 | [email protected]
![Page 7: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/7.jpg)
yield()://Suspendtherunningthread//Chooseanewthreadtorun//Resumethenewthread
6.033 | spring 2019 | [email protected]
![Page 8: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/8.jpg)
yield():acquire(t_lock)
//Suspendtherunningthread//Chooseanewthreadtorun//Resumethenewthreadrelease(t_lock)
6.033 | spring 2019 | [email protected]
![Page 9: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/9.jpg)
yield():acquire(t_lock)
id=idofcurrentthreadthreads[id].state=RUNNABLEthreads[id].sp=SPthreads[id].ptr=PTR
//Chooseanewthreadtorun//Resumethenewthread
release(t_lock)
Suspend current thread
6.033 | spring 2019 | [email protected]
![Page 10: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/10.jpg)
yield():acquire(t_lock)
id=cpus[CPU].threadthreads[id].state=RUNNABLEthreads[id].sp=SPthreads[id].ptr=PTR
//Chooseanewthreadtorun//Resumethenewthread
release(t_lock)
Suspend current thread
6.033 | spring 2019 | [email protected]
![Page 11: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/11.jpg)
yield():acquire(t_lock)
id=cpus[CPU].threadthreads[id].state=RUNNABLEthreads[id].sp=SPthreads[id].ptr=PTR
do:id=(id+1)modNwhilethreads[id].state!=RUNNABLE
//Resumethenewthreadrelease(t_lock)
Suspend current thread
Choose new thread
6.033 | spring 2019 | [email protected]
![Page 12: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/12.jpg)
yield():acquire(t_lock)
id=cpus[CPU].threadthreads[id].state=RUNNABLEthreads[id].sp=SPthreads[id].ptr=PTR
do:id=(id+1)modNwhilethreads[id].state!=RUNNABLE
SP=threads[id].spPTR=threads[id].ptrthreads[id].state=RUNNINGcpus[CPU].thread=id
release(t_lock)
Suspend current thread
Choose new thread
Resume new thread
6.033 | spring 2019 | [email protected]
![Page 13: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/13.jpg)
send(bb,message):acquire(bb.lock)whilebb.in-bb.out==N:release(bb.lock)yield()acquire(bb.lock)bb.buf[bb.inmodN]<-messagebb.in<-bb.in+1release(bb.lock)return
6.033 | spring 2019 | [email protected]
![Page 14: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/14.jpg)
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
6.033 | spring 2019 | [email protected]
![Page 15: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/15.jpg)
(threads in receive() will wait on bb.not_empty and
notify of bb.not_full)
send(bb,message):acquire(bb.lock)whilebb.in-bb.out==N:release(bb.lock)wait(bb.not_full)acquire(bb.lock)bb.buf[bb.inmodN]<-messagebb.in<-bb.in+1release(bb.lock)notify(bb.not_empty)return
problem: lost notify6.033 | spring 2019 | [email protected]
![Page 16: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/16.jpg)
condition variable API: wait(cv,lock):
notify(cv): notify waiting threads of cv
yield processor, release lock, wait to be notified of cv
6.033 | spring 2019 | [email protected]
![Page 17: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/17.jpg)
send(bb,message):acquire(bb.lock)whilebb.in-bb.out==N:wait(bb.not_full,bb.lock)bb.buf[bb.inmodN]<-messagebb.in<-bb.in+1release(bb.lock)notify(bb.not_empty)return
6.033 | spring 2019 | [email protected]
![Page 19: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/19.jpg)
wait(cv,lock):acquire(t_lock)release(lock)
release(t_lock)acquire(lock)
6.033 | spring 2019 | [email protected]
![Page 20: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/20.jpg)
wait(cv,lock):acquire(t_lock)release(lock)id=cpus[CPU].threadthreads[id].cv=cvthreads[id].state=WAITING
release(t_lock)acquire(lock)
6.033 | spring 2019 | [email protected]
![Page 21: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/21.jpg)
wait(cv,lock):acquire(t_lock)release(lock)id=cpus[CPU].threadthreads[id].cv=cvthreads[id].state=WAITINGyield_wait()release(t_lock)acquire(lock)
will be different than yield()
6.033 | spring 2019 | [email protected]
![Page 22: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/22.jpg)
wait(cv,lock):acquire(t_lock)release(lock)id=cpus[CPU].threadthreads[id].cv=cvthreads[id].state=WAITINGyield_wait()release(t_lock)acquire(lock)
will be different than yield()
notify(cv):acquire(t_lock)forid=0toN-1:ifthreads[id].cv==cv&&threads[id].state==WAITING:threads[id].state=RUNNABLErelease(t_lock)
6.033 | spring 2019 | [email protected]
![Page 23: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/23.jpg)
yield_wait()://calledbywait()acquire(t_lock)
id=cpus[CPU].threadthreads[id].state=RUNNABLEthreads[id].sp=SPthreads[id].ptr=PTR
do:id=(id+1)modNwhilethreads[id].state!=RUNNABLE
SP=threads[id].spPTR=threads[id].ptrthreads[id].state=RUNNINGcpus[CPU].thread=id
release(t_lock)
problem: wait() holds t_lock6.033 | spring 2019 | [email protected]
![Page 24: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/24.jpg)
yield_wait()://calledbywait()
id=cpus[CPU].threadthreads[id].state=RUNNABLEthreads[id].sp=SPthreads[id].ptr=PTR
do:id=(id+1)modNwhilethreads[id].state!=RUNNABLE
SP=threads[id].spPTR=threads[id].ptrthreads[id].state=RUNNINGcpus[CPU].thread=id
problem: current thread’s state shouldn’t be RUNNABLE
6.033 | spring 2019 | [email protected]
![Page 25: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/25.jpg)
yield_wait()://calledbywait()
id=cpus[CPU].threadthreads[id].sp=SPthreads[id].ptr=PTR
do:id=(id+1)modNwhilethreads[id].state!=RUNNABLE
SP=threads[id].spPTR=threads[id].ptrthreads[id].state=RUNNINGcpus[CPU].thread=id
problem: deadlock (wait() holds t_lock)
6.033 | spring 2019 | [email protected]
![Page 26: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/26.jpg)
yield_wait()://calledbywait()
id=cpus[CPU].threadthreads[id].sp=SPthreads[id].ptr=PTR
do:id=(id+1)modNrelease(t_lock)acquire(t_lock)whilethreads[id].state!=RUNNABLE
SP=threads[id].spPTR=threads[id].ptrthreads[id].state=RUNNINGcpus[CPU].thread=id
problem: stack corruption
6.033 | spring 2019 | [email protected]
![Page 27: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/27.jpg)
yield_wait()://calledbywait()
id=cpus[CPU].threadthreads[id].sp=SPthreads[id].ptr=PTRSP=cpus[CPU].stack
do:id=(id+1)modNrelease(t_lock)acquire(t_lock)whilethreads[id].state!=RUNNABLE
SP=threads[id].spPTR=threads[id].ptrthreads[id].state=RUNNINGcpus[CPU].thread=id
6.033 | spring 2019 | [email protected]
![Page 28: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/28.jpg)
timer_interrupt():pushPCpushregistersyield()popregisterspopPC
problem: what if timer interrupt occurs while running yield() or yield_wait()?
preemption: forcibly interrupt threads
6.033 | spring 2019 | [email protected]
![Page 29: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/29.jpg)
timer_interrupt():pushPCpushregistersyield()popregisterspopPC
preemption: forcibly interrupt threads
solution: hardware mechanism to disable interrupts
6.033 | spring 2019 | [email protected]
![Page 30: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/30.jpg)
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)
in order to enforce modularity + build an effective operating system
operating systems enforce modularity on a single machine using virtualization
6.033 | spring 2019 | [email protected]
![Page 31: Lecture #5 - mit.edu file1. 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](https://reader031.vdocuments.site/reader031/viewer/2022041316/5e16203f642ea3016054e30e/html5/thumbnails/31.jpg)
• 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.
6.033 | spring 2019 | [email protected]