Operating Systems: Threads
Operating Systems: Threads
Threads
Lecture 4
Michael O’Boyle
1
Overview
• Process vs threads
– how related
• Concurrency
– why threads
• Design space of process/threads
– a simple taxonomy
• Kernel threads
– more efficient
• User-level threads
– even faster
2
What’s “in” a process?
3
Thread: Concurrency vs. Parallelism
■ Threads are about concurrency and parallelism
single core T1 T2 T3 T4 T1 T2 T3 T4 T1 …
time
core 1 T1 T3 T1 T3 T1 …
core 2 T2 T4 T2 T4 T2 …
time
Motivation
5
What’s needed?
• In many cases
– Everybody wants to run the same code
– Everybody wants to access the same data
– Everybody has the same privileges
– Everybody uses the same resources (open files, network
connections, etc.)
• But you’d like to have multiple hardware execution states:
– an execution stack and stack pointer (SP)
• traces state of procedure calls made
– the program counter (PC), indicating the next instruction
– a set of general-purpose processor registers and their values
6
How could we achieve this?
7
Can we do better?
• Key idea:
– separate the concept of a process (address space, OS resources)
– … from that of a minimal “thread of control” (execution state: stack,
stack pointer, program counter, registers)
• This execution state is usually called a thread, or
sometimes, a lightweight process
thread
8
Threads and processes
9
Single and Multithreaded Processes
thread thread
11
The design space
Key
older
MS/DOS UNIXes
thread
Java Mach, NT,
Chorus,
Linux, …
many threads per process many threads per process
one process many processes
12
(old) Process address space
0xFFFFFFFF
stack
(dynamic allocated mem)
SP
static data
(data segment)
code PC
(text segment)
0x00000000
13
(new) Address space with threads
thread 1 stack
SP (T1)
0xFFFFFFFF
thread 2 stack
SP (T2)
thread 3 stack
SP (T3)
address space
heap
(dynamic allocated mem)
static data
(data segment)
0x00000000 PC (T2)
code
PC (T1)
(text segment)
PC (T3)
15
Terminology
16
Where do threads come from?
17
Kernel threads
Mach, NT,
Chorus,
address
Linux, …
space
os kernel
thread CPU
(thread create, destroy,
signal, wait, etc.)
18
Kernel threads
19
Cheaper alternative
20
User-level threads
user-level
thread library
address
space
os kernel
thread CPU
Now thread id is unique within the context of a process, not unique system-wide
21
User-level threads: what the kernel sees
address
space
os kernel
thread CPU
22
User-level threads
user-level
thread library
kernel threads
os kernel
thread CPU
(kernel thread create, destroy,
signal, wait, etc.)
One problem: If a user-level thread blocked due to I/O, all other blocked
23
User-level threads
24
OLD Performance example
– Processes
• fork/exit: 251 µs
Why?
– Kernel threads
• pthread_create()/pthread_join(): 94 µs (2.5x faster)
– User-level threads
• pthread_create()/pthread_join: 4.5 µs (another 20x
faster)
Why?
25
User-level thread implementation
26
Thread interface
27
Thread context switch
28
How to keep a user-level thread from
hogging the CPU?
• Strategy 1: force everyone to cooperate
– a thread willingly gives up the CPU by calling yield()
– yield() calls into the scheduler, which context switches to another
ready thread
– what happens if a thread never calls yield()?
29
What if a thread tries to do I/O?
30
Multiple kernel threads “powering”
each address space
user-level
thread library
address
space
kernel threads
os kernel
thread CPU
(kernel thread create, destroy,
signal, wait, etc.)
31
Summary
32