Download Parallel and Concurrent Programming: Processes and Threads | CMPSCI 691 and more Study notes Computer Science in PDF only on Docsity!
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
Parallel & Concurrent Programming:
Processes & Threads
Emery Berger
CMPSCI 691W - Spring 2006
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 2
Outline
Processes
Threads
Basic synchronization
Bake-off
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 3
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
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 4
Processes
Process:
execution
context (PC,
registers) +
address space,
files, etc.
Basic unit of
execution
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 7
Processes Example
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 8
Communication
Processes:
Input = state before fork()
Output = return value
argument to exit()
But: how can processes communicate
during execution?
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 9
IPC
signals
Send & receive ints
Not terribly useful for parallel or
concurrent programming
pipes
Communication channels – easy & fast
Just like UNIX command line
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 10
Pipe example
int main() {
int pfds[2];
pipe(pfds);
if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
execlp("ls", "ls", NULL);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp("wc", "wc", "-l", NULL);
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 13
Threads API
UNIX (POSIX):
pthread_create() – start separate
thread executing function
pthread_join() – wait for thread to
complete
Windows:
CreateThread (…)
only 6 arguments!
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 14
Threads example
#include <pthread.h>
void * run (void * d) {
int q = ((int) d);
int v = 0;
for (int i = 0; i < q; i++) {
v = v + expensiveComputation(i);
return (void *) v;
main() {
pthread_t t1, t2;
int r1, r2;
pthread_create (&t1, run, 100);
pthread_create (&t2, run, 100);
pthread_wait (&t1, (void *) &r1);
pthread_wait (&t2, (void *) &r2);
printf (“r1 = %d, r2 = %d\n”, r1, r2);
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 15
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
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 16
Basic synchronization
Mutual exclusion locks
Only one thread in critical section
pthread_mutex_lock (&l);
update data; /* critical section */
pthread_mutex_unlock (&l);
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 19
Scheduling
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 20
Context Switch Cost
Threads – much cheaper
Stash registers, PC (“IP”), stack pointer
Processes:
Same as threads plus –
Process context
TLB shootdown
ÖProcess switches more expensive, or
require long quanta
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 21
Flexibility / Ease-of-use
Processes – more flexible
+ Easy to spawn remotely
+ Can communicate via sockets = can be
distributed across cluster / Internet
- Requires explicit communication or risky
hackery
Threads
Communicate through memory – must
be on same machine
- Require thread-safe code
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 22
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…