Threads
11/15/16
Threads 11/15/16 CS31 teaches you How a computer runs a program. - - PowerPoint PPT Presentation
Threads 11/15/16 CS31 teaches you How a computer runs a program. How the hardware performs computations How the compiler translates your code How the operating system connects hardware and software The implications for you
11/15/16
software
Transistors (*10^3) Clock Speed (MHZ) Power (W) ILP (IPC)
area) doubles roughly every two years.
we make use of it?
CPU cores on the chip.
advantage of multiple cores.
multiple cores.
memory/resources.
information to give us parallelism within a process?
and threads.
process attributes (e.g., open files).
a process (PC, SP, registers),
Text Data Stack 1 Thread 1 PC1 SP1 Process 1 OS Heap This is the picture we’ve been using all along: A process with a single thread, which has execution state (registers) and a stack.
Thread 1 PC1 SP1 Thread 2 PC2 SP2 Process 1 Text Data Stack 1 OS Heap Stack 2 We can add a thread to the
memory (VAS) with other threads. New thread gets private registers, local stack.
Thread 1 PC1 SP1 Thread 2 Thread 3 PC2 SP2 PC3 SP3 Process 1 Text Data Stack 1 OS Heap Stack 2 Stack 3 A third thread added. Note: they’re all executing the same program (shared instructions in text), though they may be at different points in the code.
11
tid0 tid1 … tidm . . .
per-thread stacks
Process:
+ Sharing is easy + Sharing is cheap
no data copy from one Pi’s address space to another Pj’s address space
+ Thread create faster than process + OS can schedule on multiple CPUs
+ Parallelism
(when cooperating Pis are on different computers)
12
. . .
Every Process has 1 thread of execution
An example threaded program’s execution:
1. Main thread often initializes shared state 2. Then spawns multiple threads 3. Set of threads execute concurrently to perform some task 4. Main thread may do a join, to wait for
5. Main thread may do some final sequential processing (like write results to a file)
13
(Unlike processes which form a tree hierarchy)
pwd sh ls ls T1 Process hierarchy
Threads associated with a process
T2 T0 T4 T3 shared code, data bash
14
Single Core Processor
Simulate by time slicing
Multi-Core Processor
True concurrency
Time Thread A Thread B Thread C Thread A Thread B Thread C Run on multiple cores
executing simultaneously, and potentially interacting with each other.
Separating threads and processes makes it easier to support parallel applications:
creating new processes (less state to store, initialize).
process (threads share page tables, access same memory).
thread
another thread’s stack
another thread’s stack
… function C function D … function A function B Shared Heap int *x; Z Thread 1’s stack Thread 2’s stack Thread 2 can dereference x to access Z. Function B returns…
another thread’s stack
… function C function D … function A function B Shared Heap int *x; Thread 1’s stack Thread 2’s stack Thread 2 can dereference x to access Z. Z Shared data on heap!
CPUs/cores that process in parallel
One core: Four cores:
many factors:
communication, diminishing returns.
23
static int x; int foo(int *p) { int y; y = 3; y = *p; *p = 7; x += y; } Heap: Stack:
0x0
Globals: Instructions:
max
Tid j Tid i
If threads i and j both execute function foo code: Q1: which variables do they each get own copy of? which do they share? Q2: which stmts can affect values seen by the other thread?
Shared Virtual Address Space:
foo:
24
static int x; int foo(int *p) { int y; y = 3; y = *p; *p = 7; x += y; } Each tid gets its
x is in global memory and is shared by every thread Heap: Stack:
0x0
Globals: Instructions:
max
p is parameter, each tid gets its own copy
could point an int storage location: on the stack, or in global mem, or on the heap,
stack frame
x:10 y:3 p:- y:3 p:-
Tid j Tid i
single core run.
same process