05-thread
05-thread
Fall 2014
Threads
Myungjin Lee
[email protected]
1
What’s “in” a process?
2
The Big Picture
3
Concurrency/Parallelism
4
What’s needed?
5
How could we achieve this?
6
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
7
Threads and processes
8
• Threads are concurrent executions sharing an address
space (and some OS resources)
• Address spaces provide isolation
– If you can’t name it, you can’t read or write it
• Hence, communicating between processes is expensive
– Must go through the OS to move data from one address space to
another
• Because threads are in the same address space,
communication is simple/cheap
– Just update a shared variable!
9
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
10
(old) Process address space
0xFFFFFFFF
stack
(dynamic allocated mem)
SP
static data
(data segment)
code PC
(text segment)
0x00000000
11
(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)
13
Terminology
14
“Where do threads come from, Mommy?”
15
Kernel threads
Mach, NT,
Chorus,
address
Linux, …
space
os kernel
thread CPU
(thread create, destroy,
signal, wait, etc.)
16
Kernel threads
17
“Where do threads come from, Mommy?” (2)
18
User-level threads
user-level
thread library
address
space
os kernel
thread CPU
19
User-level threads: what the kernel sees
address
space
os kernel
thread CPU
20
User-level threads: the full story
user-level
thread library
kernel threads
os kernel
thread CPU
(kernel thread create, destroy,
signal, wait, etc.)
21
User-level threads
22
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?
23
User-level thread implementation
24
Thread interface
25
Thread context switch
26
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()?
27
What if a thread tries to do I/O?
28
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.)
29
What if the kernel preempts a thread
holding a lock?
• Other threads will be unable to enter the critical section and
will block (stall)
30
Addressing these problems
31
Summary
32
The design space
older
MS/DOS UNIXes
thread
Java Mach, NT,
Chorus,
Linux, …
many threads/process many threads/process
one process many processes
33