concurrency
concurrency
Rob Miller
Fall 2007
Memory
Network
Shared memory
Message Passing
¾ Analogy: two computers in a network, communicating only by network
connections
Cash
machines A B C D
deposit $256 withdraw $65 deposit $127 get balance
to account 1 from account 2 to account 1 of account 1
Cash
machines A B C D
waiting
iti ffor llockk
waiting for lock
one lock per account one lock for the whole bank
A B C D
deposit
p $256 withdraw $65 deposit
p $127 g balance
get
to account 1 from account 2 to account 1 of account 1
get bal
queue for
dep$127
A
Account 1
dep$256 wdrw $65
Al
Alice B b
Bob
withdraw
withdraw
get-balance get-balance
if balance > $75,
$75 if balance > $50,
$50
withdraw $75 withdraw $50
OK OVERDRAWN
Account
bal: $100 get-
balance
streams
socket socket
JAVA? open,ask,chg
open,ask,chg
open,ask,chg
¾ Every
E object
bj bbelongs
l to exactly
l one process (actually
( ll JJava enforces
f this)
hi )
¾ Modules in different processes communicate using streams
© Robert Miller 2007
Error Stream
Processes actually have three streams by default
¾ Standard input, standard output, and standard error (System.err in Java)
QuoteApp
Q A start
Eclipse
QuoteApp process Console process
std output
Black text
AsyncQuoter
start StreamGobbler
Red text
open,
open std error
AAPL debugging msgs,
ask,
chg error msgs
QuoterProc
QuoterProc process
¾ QuoteApp
Q A redirects
di subprocess’s
b ’ standardd d error to its
i own std
d error
¾ This is useful, because it lets you see error messages from the subprocess
(like uncaught exceptions) in the Eclipse console view
© Robert Miller 2007
Deadlock
Not just helpful for debugging
¾ You must consume the standard error stream from a subprocess, even if
yyou don’t care what it says
y
¾ Failing to do this can lead to deadlock
• Streams have finite buffers – when the buffer is full, the writer has to
wait (“block”)
( block ) until a reader starts to empty it
• So if subprocess fills up its error stream, and main process doesn’t
empty it, then the subprocess will eventually block
• Meanwhile the main process eventually has to wait for replies back
from the subprocess – so both processes are waiting for each other
• Deadlock!