You are on page 1of 16

Threads and Concurrency 1

Threads and Concurrency

key concepts
threads, concurrent execution, timesharing, context switch, interrupts, preemption

reading
Three Easy Pieces: Chapter 26 (Concurrency and Threads)

CS350 Operating Systems Fall 2018

Threads and Concurrency 2

What is a Thread? needs own stack cutof registers


• Threads provide a way for programmers to express concurrency in a program.
• A normal sequential program consists of a single thread of execution.

• In threaded concurrent programs there are multiple threads of execution, all


occuring at the same time.
boot thread
Andhis c symbol I hors
R threadtest menu
cscope test d
goto thread
c
ttl toown
threadtest

if paramnot used void rarrame

Semaphore global war fora thread States communication

CS350 Operating Systems Fall 2018

thread fork create 1 thread for OS


fo seg of
commands
of fifteen P
wtf
void passparumsfor function pointer too
matter
furetw loudthread
need
prutcher
Threads and Concurrency 3

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

Threads and Concurrency 4

OS/161’s Thread Interface

• create a new thread:


int thread_fork(
const char *name, // name of new thread
struct proc *proc, // thread’s process
void (*func) // new thread’s function
(void *, unsigned long), signature of function
void *data1, // function’s first param
unsigned long data2 // function’s second param
);
• terminate the calling thread:
about
Yield void thread_exit(void);
doit returnfrom athread
µ • volutarily yield execution:
isn't done yet
tell OS that itanother
we void thread_yield(void);
but maybe thread can run for
www.wrs
goes See kern/include/thread.h a bit
wow
wypeds CS350 Operating Systems Fall 2018
Threads and Concurrency 5

Why Threads? threadeach


core
each on
running
• Reason #1: parallelism exposed by threads enables parallel execution if the
underlying hardware supports it.
– programs can run faster
• Reason #2: parallelism exposed by threads enables better processor utilization
– if one thread has to block, another may be able to run

ifeventis snowline access hardrie networkcall


i waiting win block cpu

CS350 Operating Systems Fall 2018

Threads and Concurrency 6

Review: Sequential Program Execution


var
globalsemaphore
Cie

stack code memory


data

CPU register contents


SP PC

The Fetch/Execute Cycle


1. fetch instruction PC points to

2. decode and execute instruction


3. advance PC

CS350 Operating Systems Fall 2018


Threads and Concurrency 7

MIPS Registers

num name use


p num name use
0 z0 always zero
I 24-25 t8-t9 temps (caller-save)
1 at assembler reserved 26-27 k0-k1 kernel temps
2 v0 return val/syscall # 28 gp global pointer
3 v1 return value 29 sp stack pointer
4-7 a0-a3 subroutine args 30 s8/fp frame ptr (callee-save)
8-15 t0-t7 temps (caller-save) 31 ra return addr (for jal)
16-23 s0-s7 saved (callee-save)

mPspnehnies it youneedto save


save sanctum these waves as earlier
See kern/arch/mips/include/kern/regdefs.h

CS350 Operating Systems Fall 2018

Threads and Concurrency 8

Review: The Stack

FuncA() {
stack frame(s)
...
FuncB();
...
}

FuncA FuncB() {
FuncB ...
FuncC();
FuncC ...
}
stack growth

CS350 Operating Systems Fall 2018


Threads and Concurrency 9

Concurrent Program Execution (Two Threads)

SP PC
Thread 2 CPU register contents

code memory
T1 T2 data
stack stack

Thread 1 CPU register contents


SP PC

each threadheeds own set ofregisters


Conceptually, each thread executes sequentially using its private register con-
tents and stack.

CS350 Operating Systems Fall 2018

Threads and Concurrency 10

isoform
Implementing Concurrent Threads
hasoursetof
registers

• Option 1: multiple processors, multiple cores, hardware multithreading per core


– P processors, C cores per processor, M multhreading degree per core ⇒
P CM threads can execute simultaneously
– separate register set for each running thread, to hold its execution context
• Option 2: timesharing givenCPU multiplethreads running
onCPU
– multiple threads take turns on the same hardware
– rapidly switch from thread to thread so that all make progress

Ksfeels likemultiplethreadsrunning
at same time
In practice, both techniques can be combined.

CS350 Operating Systems Fall 2018


Threads and Concurrency 11

Timesharing and Context Switches

• When timesharing, the switch from one thread to another is called a context
switch

• What happens during a context switch:


1. decide which thread will run next (scheduling)
2. save register contents of current thread
Tstack
tack
curative savedpreviously
3. load register contents of next thread

• Thread context must be saved/restored carefully, since thread execution


continuously changes the context

CS350 Operating Systems Fall 2018

Threads and Concurrency 12

Context Switch on the MIPS (1 of 2)

/* 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. */

/* Allocate stack space for saving 10 registers. 10*4 = 40 */

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)

/* Store the old stack pointer in the old thread */


sw sp, 0(a0)
CS350 Operating Systems Fall 2018
Threads and Concurrency 13

Context Switch on the MIPS (2 of 2)

/* Get the new stack pointer from the new thread */


lw sp, 0(a1)
nop /* delay slot for load */

/* Now, restore the registers */


lw s0, 0(sp)
lw s1, 4(sp)
lw s2, 8(sp)
lw s3, 12(sp)
lw s4, 16(sp)
lw s5, 20(sp)
lw s6, 24(sp)
lw s8, 28(sp)
lw gp, 32(sp)
lw ra, 36(sp)
nop /* delay slot for load */

/* and return. */
j ra
stack
addi sp, sp, 40 /* in delay slot */ reclaimspaceon
go back to caller ofthread
.end switchframe_switch

CS350 Operating Systems Fall 2018

Threads and Concurrency 14

What Causes Context Switches?

• the running thread calls thread yield voluntary contextswith


– running thread voluntarily allows other threads to run
• the running thread calls thread exit
– running thread is terminated

• the running thread blocks, via a call to wchan sleep


– more on this later . . .

runningforsome time now wait


• the running thread is preempted
anotherthread
– running thread involuntarily stops running to
giveCPUto thread
roti
prioritizing
c
preventstarvation fairness forspeed

CS350 Operating Systems Fall 2018

FL
Threads and Concurrency 15

OS/161 Thread Stack after Voluntary Context Switch (thread yield())

stack frame(s)

stack growth

thread_yield()
stack frame

thread_switch
stack frame

saved thread context


(switchframe)

registers
stonevalues of
CS350 Operating Systems Fall 2018

Threads and Concurrency 16

Thread States

thread fromrunning
preemption
ifOSstops
involuntary
or thread_yield()

ready running

dispatch

got resource or event need resource or event


(wchan_wakeone/all()) (wchan_sleep())

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

• without preemption, a running thread could potentially run forever, without


yielding, blocking, or exiting

• preemption means forcing a running thread to stop running, so that another


thread can have a chance
• to implement preemption, the thread library must have a means of “getting
control” (causing thread library code to be executed) even though the running
thread has not called a thread library function
I
• this is normally accomplished using interrupts

Feelserviced
hit needsto
be

CS350 Operating Systems Fall 2018

Threads and Concurrency 18

Review: Interrupts

e SarePc • an interrupt is an event that occurs during the execution of a program

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

procuring CS350 Operating Systems Fall 2018

jmawfu
con
eye
Threads and Concurrency 19

OS/161 Thread Stack after in Interrupt

stack frame(s)

whsfafeoppeogd.me

stack growth

trap frame

interrupt handling
stack frame(s)
equgtjEves

CS350 Operating Systems Fall 2018

Threads and Concurrency 20

Preemptive Scheduling

• A preemptive scheduler imposes a limit, called the scheduling quantum on how

good long a thread can run before being preempted.


time
tountertotmok
• The quantum is an upper bound on the amount of time that a thread can run. It
may block or yield before its quantum has expired.
minute
w • Periodic timer interrupts allow running time to be tracked.
went • If a thread has run too long, the timer interrupt handler preempts the thread by
North calling thread yield. Car.hn ycontext switch

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.

CS350 Operating Systems Fall 2018


Threads and Concurrency 21

OS/161 Thread Stack after Preemption

stack frame(s)

µwrt stack growth

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

Threads and Concurrency 22

Two-Thread Example (Part 1)

Thread 1 Stack Thread 2 Stack


Cody
program program

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.

CS350 Operating Systems Fall 2018


Threads and Concurrency 23

Two-Thread Example (Part 2)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

INTERRUPT!
trap frame thread_yield

thread_switch
interrupt handler

switch frame

A time interrupt occurs! Interrupt handler runs.

CS350 Operating Systems Fall 2018

Threads and Concurrency 24

Two-Thread Example (Part 3)

Thread 1 Stack T Thread 2 Stack

program program
stack frame(s) stack frame(s)
Ft

trap frame thread_yield

thread_switch
interrupt handler

thread_yield switch frame

Interrupt handler decides Thread 1 quantum has expired.

CS350 Operating Systems Fall 2018


Threads and Concurrency 25

Two-Thread Example (Part 4)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

trap frame thread_yield

thread_switch
interrupt handler

thread_yield switch frame

thread_switch

to
switch frame

Scheduler chooses Thread 2 to run. Context switch.

CS350 Operating Systems Fall 2018

Threads and Concurrency gain 26

Two-Thread Example (Part 5)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

trap frame thread_yield

interrupt handler

thread_yield
thread_switch

switch frame

Thread 2 context is restored.

CS350 Operating Systems Fall 2018


Threads and Concurrency 27

Two-Thread Example (Part 6)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

trap frame

interrupt handler

thread_yield
thread_switch

switch frame

thread yield finishes, Thread 2 program resumes.

CS350 Operating Systems Fall 2018

Threads and Concurrency 28

Two-Thread Example (Part 7)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

trap frame thread_yield

i l
thread_switch
interrupt handler

thread_yield switch frame

e thread_switch

switch frame

o
Later, Thread 2 yields again. Scheduler chooses Thread 1.

CS350 Operating Systems Fall 2018


Threads and Concurrency 29

Two-Thread Example (Part 8)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

t
I e trap frame thread_yield

thread_switch
interrupt handler

switch frame

Thread 1 context is restored, interrupt handler resumes.

CS350 Operating Systems Fall 2018

Threads and Concurrency 30

Two-Thread Example (Part 9)

Thread 1 Stack Thread 2 Stack

program program
stack frame(s) stack frame(s)

thread_yield

thread_switch

switch frame

Interrupt handler restores state from trap frame and returns.

CS350 Operating Systems Fall 2018


ofCece as N Cts c
i 1
21
c
4iteration
ofloop
part
first
9
2 CN D c t Ctc 15
Nets
i III
z

lastiteration is special
b c dont need last R

You might also like