Lesson 2
Lesson 2
Outline
Processes
Threads
Basic synchronization
Bake-off
Processes vs. Threads…
Both useful for parallel programming & concurrency
Hide latency
Maximize CPU utilization
Handle multiple, asynchronous events
But: different programming styles, performance characteristics, and more
Processes
Process: execution context (PC, registers) + address space, files, etc.
Basic unit of execution
Process API
UNIX:
fork() – create copy of current process
Different return value
Copy-on-write
exec() – replace process w/ executable
Windows:
CreateProcess (…)
10 arguments
Translation Lookaside Buffer
TLB: fast, fully associative memory
Stores page numbers (key), frame (value) in which they are stored
Copy-on-write: protect pages, copy on first write
1
Processes Example
Communication
Processes:
Input = state before fork()
Output = return value
argument to exit()
But: how can processes communicate during execution?
IPC
signals
Processes can send & receive ints associated with particular signal numbers
Process must set up signal handlers
Best for rare events (SIGSEGV)
Not terribly useful for parallel or concurrent programming
Pipes
Communication channels – easy & fast
Just like UNIX command line
ls | wc -l
Pipe example
sockets
- Explicit message passing
+ Can distribute processes anywhere
2
mmap (common and aws0m hack)
All processes map same file into fixed memory location
Objects in region shared across processes
Use process-level synchronization
Much more expensive than threads…
Threads
Processes - everything in distinct address space
Threads API
UNIX (POSIX):
pthread_create() – start separate thread executing function
pthread_join() – wait for thread to complete
Windows:
CreateThread (…)
only 6 arguments!
Threads example
3
Communication
In threads, everything shared except: stacks, registers & thread-specific data
Old way:
pthread_setspecific
pthread_getspecific
New way: __thread
static __thread int x;
Easier in Java…
Updates of shared state must be synchronized
Bake-off
Processes or threads?
Performance
Flexibility / Ease-of-use
Robustness
Scheduling
4
Threads
Communicate through memory – must be on same machine
- Require thread-safe code
Robustness
Processes – far more robust
Processes isolated from other processes
Process dies – no effect
Apache 1.x
Threads:
If one thread crashes (e.g., derefs NULL), whole process terminates
Then there’s the stack size problem
Apache 2.x…