EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides...
Transcript of EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides...
![Page 1: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/1.jpg)
EECS 482Introduction to Operating
Systems
Winter 2019
Baris Kasikci
Slides by: Harsha V. Madhyastha
![Page 2: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/2.jpg)
Recap● How to handle non-running threads?
• Save private state in TCB to resume execution later
● How to switch between threads?• Transfer control from current thread to OS• Save state of current thread and load state of next
thread
Feb 1, 2019
![Page 3: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/3.jpg)
Creating a new thread● What state should a new thread be put into?
● Recall: When a thread is paused, its state is put in ready queue
● Implication:• When creating a thread, we need to construct its
TCB as if it had been running and got paused
Feb 1, 2019
![Page 4: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/4.jpg)
Creating a new thread● Steps
• Allocate and initialize new thread control block• Allocate and initialize new stack
» In Project 2, this is done via » makecontext() (no need for getcontext())
• Add thread control block to ready queue
● Note: Unix fork() is related but different• fork() creates a new process (new thread + new
address space)Feb 1, 2019
![Page 5: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/5.jpg)
How to use new thread● Creating a thread is like an asynchronous
procedure callparent
call return
parentcreate
child works
parent works
Synchronous
Asynchronous
Feb 1, 2019
![Page 6: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/6.jpg)
Synchronizing with child
● What if parent wants to work for a while, then wait for child to finish?
parentcreate
child works
parent works parent continues
Feb 1, 2019
![Page 7: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/7.jpg)
Synchronizing with childparent()
create child threadprint “parent works”…print “parent continues”…
child()…print “child is done”
Desired outputparent workschild is doneparent continues
OR
child is doneparent worksparent continues
When would this work?
Feb 1, 2019
![Page 8: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/8.jpg)
Synchronizing with childparent()
create child threadprint “parent works”…yield()print “parent continues”…
child()…print “child is done”
Desired outputparent workschild is doneparent continues
OR
child is doneparent worksparent continues
Does this work?
Feb 1, 2019
![Page 9: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/9.jpg)
Synchronizing with joinparent()
create child threadprint “parent works”…childThread.join()print “parent continues”…
child()…print “child is done”
Desired outputparent workschild is doneparent continues
OR
child is doneparent worksparent continues
Feb 1, 2019
![Page 10: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/10.jpg)
High-level synchronization● Raise the level of abstraction to make life easier
for programmers
Operating System
Hardware
Applications
Atomic operations(load/store, interrupt enable/
disable, test&set)
Concurrent programs
High-level synchronizationprimitives
(lock, monitor, semaphore)
Feb 1, 2019
![Page 11: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/11.jpg)
Implementing high-level synchronization primitives● Data structures used must be thread-safe
● Cannot use high-level synchronization primitives• Need to use atomic operations provided by hardware
Feb 1, 2019
![Page 12: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/12.jpg)
Atomicity on uniprocessor● Potential approach if single CPU:
• Prevent context switches during an operation by preventing events that cause context switches
● Example: Disable interrupts to ensure atomicitydisable interruptsif (no milk) {
buy milk}enable interrupts
● Problems?
Feb 1, 2019
![Page 13: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/13.jpg)
Lock implementation #1lock() {
disable interruptswhile (status != FREE) {
enable interruptsdisable interrupts
}status = BUSYenable interrupts
}
unlock() {disable interruptsstatus = FREEenable interrupts
}
Feb 1, 2019
![Page 14: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/14.jpg)
Need for other atomic primitives● On uniprocessor, disabling interrupts prevents
current thread from being switched out● But this doesn’t work on a multiprocessor
• Other processors are still running threads• Not acceptable to stop all other CPUs from executing
● Could use atomic load/store• Example: “Too much milk” solution #3
Feb 1, 2019
![Page 15: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/15.jpg)
Atomic Read-Modify-Write: Test-And-Set● Semantics of test-and-set are to atomically write
1 to a memory location and return old value
● In Project 2, exchange in std::atomic
test_and_set (X) {old = X;X = 1;return old;
}
Feb 1, 2019
![Page 16: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/16.jpg)
Lock implementation #2// status=0 means lock is freelock() {
while (test_and_set(status) == 1) {}
}
unlock() {status = 0
}
● test_and_set is atomic, so only one thread will see transition from 0 to 1
Feb 1, 2019
![Page 17: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/17.jpg)
Busy waiting● Problem with lock implementations #1 and #2
• Waiting thread uses lots of CPU time just checking for lock to become free
• Better for thread to sleep and let other threads run
● Solution: Integrate lock implementation with thread dispatch à have lock manipulate thread queues
• Waiting thread gives up CPU, so other threads can run• Someone wakes up thread when lock is free
Feb 1, 2019
![Page 18: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/18.jpg)
Lock implementation #3lock() {
disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}enable interrupts
}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
Feb 1, 2019
![Page 19: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/19.jpg)
Lock implementation #3lock() {
disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}enable interrupts
}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
Feb 1, 2019
![Page 20: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/20.jpg)
Lock implementation #3lock() {
disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}enable interrupts
}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
Feb 1, 2019
![Page 21: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/21.jpg)
Lock implementation #3lock() {
disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}enable interrupts
}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
Feb 1, 2019
![Page 22: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/22.jpg)
Lock implementation #3
Wait_queue_lock
Ready_queue
CPU
TL
TL
Feb 1, 2019
lock() {disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
enable interrupts
![Page 23: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/23.jpg)
Lock implementation #3
Wait_queue_lock
Ready_queue
CPU
TL
TX
TL
Feb 1, 2019
lock() {disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
enable interrupts
![Page 24: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/24.jpg)
Lock implementation #3
Wait_queue_lock
Ready_queue
CPU
TL
TX TL
Feb 1, 2019
lock() {disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
enable interrupts
![Page 25: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/25.jpg)
Lock implementation #3
Wait_queue_lock
Ready_queue
CPU
TL
TX
TL
Feb 1, 2019
lock() {disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
enable interrupts
![Page 26: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/26.jpg)
Lock implementation #3
Wait_queue_lock
Ready_queue
CPU
TL
TU
TL
Feb 1, 2019
lock() {disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
enable interrupts
![Page 27: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/27.jpg)
Lock implementation #3
Wait_queue_lock
Ready_queue
CPU
TL
TX
TL
Feb 1, 2019
lock() {disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
enable interrupts
![Page 28: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/28.jpg)
Lock implementation #3lock() {
disable interruptsif (status == FREE) {
status = BUSY} else {
add thread to queue of threads waiting for lockswitch to next ready thread
}enable interrupts
}unlock() {
disable interruptsstatus = FREEif (any thread is waiting for this lock) {
move waiting thread to ready queuestatus = BUSY
}enable interrupts
}
Feb 1, 2019
![Page 29: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/~manosk/eecs482/handouts/... · Slides by: Harsha V. Madhyastha. Recap ... • Save private state in TCB to resume execution](https://reader034.fdocuments.in/reader034/viewer/2022052101/603bb6984b92594df500308f/html5/thumbnails/29.jpg)
Interrupt enable/disable pattern
● Adding thread to lock wait queue + switching must be atomic● Thread must leave interrupts disabled when calling switch
● What can lock() assume about the state of interrupts after switch returns?
● How does lock() wake up from switch?
● Switch invariant• All threads promise to have interrupts disabled when calling switch• All threads assume interrupts are disabled when returning from switch
Feb 1, 2019