Transcript .ppt

CS 5150
Software Engineering
Lecture 14
Program Design 1
Administrivia
•
•
•
•
•
CS 5150
Quizzes finally done (next one will be much
faster)
TAs reading milestone 2 reports
Teammate feedback due this evening
Presentations ...
Quiz 2 ...
2
General Presentation Feedback
•
•
•
•
•
•
•
•
•
CS 5150
Client and scheduling drama
Cables, computers
Intro
Title slide
Slide numbers
Note taking
Projecting code
Technical language
Posture
3
General Project Feedback
•
•
CS 5150
Code reviews
Regression testing
4
SE in the News
•
•
•
•
CS 5150
Rumors of imminent antitrust legal action
against Google by the Federal Trade
Commission
Rackspace advocates for the abolition of
software patents
Linked list patented
•
http://www.google.com/patents/US702802
3
Invitation to open source business model
webinar
5
Quiz 2
CS 5150
6
Concurrent and Parallel
Programming is Hard
• ... but not impossible
• We can dramatically improve on current
standard practices with available
technologies
• There are important benefits to be reaped
by expanding the use of concurrent
programming
7
•
•
•
•
Why Program
Concurrently?
Real-world
interaction
Isolation
Concurrent
design
patterns
Parallel
performance
8
•
•
•
•
Why Program
Concurrently?
Real-world
interaction
Isolation
Concurrent
design
patterns
Parallel
performance
9
•
•
•
•
Why Program
Concurrently?
Real-world
interaction
Isolation
Concurrent
design
patterns
Parallel
performance
10
•
•
•
•
Why Program
Concurrently?
Real-world
interaction
Isolation
Concurrent
design patterns
Parallel
performance
11
•
Parsing, Batch
Approach
batch_parse( pile_of_characters )
pile_of_tokens = tokenize( pile_of_characters )
syntax_tree
= parse( pile_of_tokens )
12
•
•
Incremental; Parser
Drives
batch_parse( pile_of_characters )
pile_of_tokens = tokenize( pile_of_characters )
syntax_tree
= parse( pile_of_tokens )
parse_driver( pile_of_characters )
declare tokenizer_state
while( ... )
small_pile_of_tokens =
tokenize_a_little( tokenizer_state )
<parsing logic>
13
Incremental; Tokenizer
Drives
•
•
•
batch_parse( pile_of_characters )
pile_of_tokens = tokenize( pile_of_characters )
syntax_tree
= parse( pile_of_tokens )
parse_driver( pile_of_characters )
declare tokenizer_state
while( ... )
small_pile_of_tokens =
tokenize_a_little( tokenizer_state )
<parsing logic>
tokenize_driver( pile_of_characters )
declare parser_state
while( ... )
small_pile_of_tokens = <tokenizing logic>
parse_a_little( parser_state, small_pile_of_tokens )
14
Incremental; Symmetric
•
incremental_parse1( pile_of_characters )
declare tokenizer_state
declare parser_state
while( ... )
small_pile_of_tokens =
tokenize_a_little( tokenizer_state )
parse_a_little( parser_state, small_pile_of_tokens )
15
Incremental; Concurrent
•
•
incremental_parse1( pile_of_characters )
declare tokenizer_state
declare parser_state
while( ... )
small_pile_of_tokens =
tokenize_a_little( tokenizer_state )
parse_a_little( parser_state, small_pile_of_tokens )
incremental_parse2( pile_of_characters )
declare token_channel
start( tokenizer, pile_of_characters, token_channel )
start( parser, token_channel )
wait( tokenizer )
wait( parser )
16
•
•
•
•
Why Program
Concurrently?
Real-world
interaction
Isolation
Concurrent
design patterns
Parallel
performance
17
How to Program
Concurrently?
18
Units of Concurrency
19
Units of Concurrency
20
Units of Concurrency
21
Units of Concurrency
22
The Many Names of
Cooperative Concurrency
• coroutine
• continuation
• goroutine
• iterator
• protothread
• eventlet
• greenlet
• light-weight thread
• fiber
• green thread
• task
• generator
• event handler
• cooperative thread
23
Event handler pattern
•
procedure main()
declare event_dispatcher
register_event_handler( event_dispatcher, button_click )
register_event_handler( event_dispatcher, text_entry )
register_event_handler( event_dispatcher, open_file )
run_event_loop( event_dispatcher )
24
Event handler pattern
•
•
procedure main()
declare event_dispatcher
register_event_handler( event_dispatcher, button_click )
register_event_handler( event_dispatcher, text_entry )
register_event_handler( event_dispatcher, open_file )
run_event_loop( event_dispatcher )
procedure run_event_loop( dispatcher )
while( true )
event = wait_for_event()
invoke_handler( dispatcher, event )
25
To Every Unit of Concurrency a
Purpose
26
Why Threads are Awful -Ye Olde Home Town
Bank
27
Data Race
•
make_deposit( account_id id, number amount )
{
account = lookup_account( id );
account.balance += amount;
}
make_deposit(42, 200)
make_deposit(42, 100)
28
Prevent Races with Mutual Exclusion
Locks
•
make_deposit( account_id id, number amount )
{
account = lookup_account( id );
acquire( account.lock );
account.balance += amount;
release( account.lock );
}
29
Fine-grained Locking Can
Cause Atomicity Violations
•
transfer( account_id id1, account_id id2,
number amount ) {
if( check_withdrawal( id1, amount ) ) {
withdraw( id1, amount );
make_deposit( id2, amount );
}
}
transfer(42, 75, 100)
30
transfer(42, 83, 200)
Coarse-grained Locks Can Cause
Deadlock
•
transfer( account_id id1, account_id id2,
number amount ) {
account1 = lookup_account( id1 );
account2 = lookup_account( id2 );
acquire( account1.lock );
acquire( account2.lock );
...
release( account2.lock );
release( account1.lock );
}
transfer(73, 42, 100)
transfer(42, 73, 100)
31
Lock Ordering to the
Rescue?
•
transfer( account_id id1, account_id id2,
number amount ) {
if( id1 < id2 )
acquire( account1.lock );
acquire( account2.lock );
else
acquire( account2.lock );
acquire( account1.lock );
...
if( id1 < id2 )
release( account2.lock );
release( account1.lock );
else
release( account1.lock );
release( account2.lock );
}
32
Back to Basics:
Abstraction
• Abstractions allow us to design larger
pieces of software from smaller pieces
without keeping all the details in our heads
• Sequential programming abstractions:
• structured control flow
• procedures
• modules
33
Abstraction Violators in
Sequential Programming
• goto
• exceptions
• global state
• dynamic dispatch
• higher-order functions
34
Abstraction Tools in
Concurrent Programming
• Isolation
• Transactions
35
Transactions
•
transfer( account_id id1, account_id id2,
number amount )
begin_transaction
if( check_withdrawal( id1, amount ) )
withdraw( id1, amount )
make_deposit( id2, amount )
end_transaction
36
Transactions
•
•
transfer( account_id id1, account_id id2,
number amount )
begin_transaction
if( check_withdrawal( id1, amount ) )
withdraw( id1, amount )
make_deposit( id2, amount )
end_transaction
transfer( account_id id1, account_id id2,
number amount )
transaction
if( check_withdrawal( id1, amount ) )
withdraw( id1, amount )
make_deposit( id2, amount )
37
Implementation of
Isolation and Transactions
38
Cooperative Threads
• Easy to implement robust transactions
• Cons:
• No parallelism
• Starvation
• Programming language support is sparse
39
Testing Multithreaded Programs is
Hard
• Exponential interleavings
• Reproducibility is hard
• Instrumentation disrupts the program
(heisenbugs)
• The only hope for large programs: reduce
the number of opportunities threads have to
affect each other
40
I Want Smokin’
Performance on Multicores!
• Domain-specific tools
• Let someone else worry about the
parallelism
• Coarse-grained units of work
• Extremely hard to get satisfying
performance benefits with fine-grained units
of work
41
Another Concurrent
DesignPattern: Model/View
Controller
•
•
•
Controller: Receives instrument readings from
the aircraft and sends controls signals to the
aircraft.
Model: Translates data received from and sent to
the aircraft, and instructions from the user into a
model of flight performance. Uses domain
knowledge about the aircraft and flight.
View: Displays information about the aircraft to
the user and transmits instructions to the model.
42
MVC for Web
Applications
43
MVC Sequence
Diagram
44