0% found this document useful (0 votes)
6 views5 pages

Lesson 2

The document discusses the differences between processes and threads in parallel programming, highlighting their respective execution contexts, communication methods, and performance characteristics. It covers process APIs in UNIX and Windows, inter-process communication (IPC) methods, and the advantages and disadvantages of using processes versus threads. The document also emphasizes the cost of context switching, flexibility, ease of use, and robustness of each approach.

Uploaded by

Arjay Balberan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Lesson 2

The document discusses the differences between processes and threads in parallel programming, highlighting their respective execution contexts, communication methods, and performance characteristics. It covers process APIs in UNIX and Windows, inter-process communication (IPC) methods, and the advantages and disadvantages of using processes versus threads. The document also emphasizes the cost of context switching, flexibility, ease of use, and robustness of each approach.

Uploaded by

Arjay Balberan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LESSON 2: Processes & Threads

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 – same address space (& files, sockets, etc.)

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

Context Switch Cost


Threads – much cheaper
 Stash registers, PC (“IP”), stack pointer
Processes – above plus
Process switches more expensive, or require long quanta
 Process context
 TLB shootdown
Flexibility / Ease-of-use
Processes – more flexible
+ Easy to spawn remotely
+ ssh foo.cs.umass.edu “ls -l”
+ Can communicate via sockets = can be distributed across cluster / Internet
- Requires explicit communication or risky hackery

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…

You might also like