Professional Documents
Culture Documents
key concepts
threads, concurrent execution, timesharing, context switch, interrupts, preemption
reading
Three Easy Pieces: Chapter 26 (Concurrency and Threads)
what
run OS/161 Threaded Concurrency Examples box of
signature Ota
f stack
• Key ideas from the examples:
an
– A thread can create new threads using thread fork
every
threadhas
enericto – New theads start execution in a function specified as a parameter to
our stack
run any
thread fork
staekfranes
functionble – The original thread (which called thread fork and the new thread
Cpwvatuvay
unpaintto (which is created by the call to thread fork) proceed concurrently, as
structureum two simultaneous sequential threads of execution. originalthreadlboot
stillruns
– All threads share access to the program’s global variables and heap. output
depends o
then – Each thread’s function activations are private to that thread.
scheduler
T stackframes unexpected
t
ttl
CS350 Operating Systems Fall 2018
MIPS Registers
FuncA() {
stack frame(s)
...
FuncB();
...
}
FuncA FuncB() {
FuncB ...
FuncC();
FuncC ...
}
stack growth
SP PC
Thread 2 CPU register contents
code memory
T1 T2 data
stack stack
isoform
Implementing Concurrent Threads
hasoursetof
registers
Ksfeels likemultiplethreadsrunning
at same time
In practice, both techniques can be combined.
• When timesharing, the switch from one thread to another is called a context
switch
/* See kern/arch/mips/thread/switch.S */
lookin
stuck on whereregisters are saved
switchframe_switch:
/* a0: address of switchframe pointer of old thread. */
/* a1: address of switchframe pointer of new thread. */
spaceT
addi sp, sp, -40
gobytes of
sw ra, 36(sp) /* Save the registers */
sw gp, 32(sp)
sw s8, 28(sp)
sw s6, 24(sp)
sw s5, 20(sp)
sw s4, 16(sp)
sw s3, 12(sp)
sw s2, 8(sp)
sw s1, 4(sp)
sw s0, 0(sp)
/* and return. */
j ra
stack
addi sp, sp, 40 /* in delay slot */ reclaimspaceon
go back to caller ofthread
.end switchframe_switch
FL
Threads and Concurrency 15
stack frame(s)
stack growth
thread_yield()
stack frame
thread_switch
stack frame
registers
stonevalues of
CS350 Operating Systems Fall 2018
Thread States
thread fromrunning
preemption
ifOSstops
involuntary
or thread_yield()
ready running
dispatch
otherthreadto
blocked
caneitherof bait channel
the tendons
running: currently executing cueingCPW
blocked ready
ready: ready to execute
ble waitforscheduler
blocked: waiting for something, so not ready to execute. toreschedule and
alsonot sureabout
currentthreads
running blockedrunning
CS350 Operating Systems Fall 2018
Threads and Concurrency 17
Preemption
Feelserviced
hit needsto
be
Review: Interrupts
aturnoff
• interrupts are caused by system devices (hardware), e.g., a timer, a disk
interrupt controller, a network interface
afumPffffhaidETrupts
• when an interrupt occurs, the hardware automatically transfers control to a fixed
3 transfer
contralto
location in memory
fixedtomtit
• at that memory location, the thread library places a procedure called an
interrupt handler
cmoutnenswvkhhamd
isknownand havetimeto San
yg.ua
• the interrupt handler normally:
then bio context switch
I
1. create a trap frame to record thread context at the time of the interrupt btgewhmdfimump.rs
e
trapframe 2. determines which device caused the interrupt and performs device-specific
tempdatais not
which saved
2 cheek
processing
denial 3. restores the saved thread context from the trap frame and resumes execution
demerspeake of the thread
jmawfu
con
eye
Threads and Concurrency 19
stack frame(s)
whsfafeoppeogd.me
stack growth
trap frame
interrupt handling
stack frame(s)
equgtjEves
Preemptive Scheduling
d
• The preempted thread changes state from running to ready, and it is placed on
the ready queue.
irrupted
OS/161 threads use preemptive round-robin scheduling.
stack frame(s)
trap frame
interrupt handling
stack frame(s) functions toseeAretedevice called
thread_yield() interrupt 1 whattodo
M
stack frame
thread_switch() fourth Quantum call yield
stack frame
2 callsSukh dotheswitch
awww
quorums t
gas saved thread context
(switchframe) 6
sales
It CS350
letotherthread continue
Operating Systems Fall 2018
pf
stack frame(s) stack frame(s)
thread_yield
thread_switch
switch frame
so 8 gpsp
i
mafor
Threadswitch
Thread 1 is running, thread two had previously yielded voluntarily.
program program
stack frame(s) stack frame(s)
INTERRUPT!
trap frame thread_yield
thread_switch
interrupt handler
switch frame
program program
stack frame(s) stack frame(s)
Ft
thread_switch
interrupt handler
program program
stack frame(s) stack frame(s)
thread_switch
interrupt handler
thread_switch
to
switch frame
program program
stack frame(s) stack frame(s)
interrupt handler
thread_yield
thread_switch
switch frame
program program
stack frame(s) stack frame(s)
trap frame
interrupt handler
thread_yield
thread_switch
switch frame
program program
stack frame(s) stack frame(s)
i l
thread_switch
interrupt handler
e thread_switch
switch frame
o
Later, Thread 2 yields again. Scheduler chooses Thread 1.
program program
stack frame(s) stack frame(s)
t
I e trap frame thread_yield
thread_switch
interrupt handler
switch frame
program program
stack frame(s) stack frame(s)
thread_yield
thread_switch
switch frame
lastiteration is special
b c dont need last R