Download presentation
Presentation is loading. Please wait.
Published byLorin Russell Modified over 9 years ago
1
CS 5150 Software Engineering Lecture 14 Program Design 1
2
CS 5150 2 Administrivia Quizzes finally done (next one will be much faster) TAs reading milestone 2 reports Teammate feedback due this evening Presentations... Quiz 2...
3
CS 5150 3 General Presentation Feedback Client and scheduling drama Cables, computers Intro Title slide Slide numbers Note taking Projecting code Technical language Posture
4
CS 5150 4 General Project Feedback Code reviews Regression testing
5
CS 5150 5 SE in the News 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 http://www.google.com/patents/US702802 3 Invitation to open source business model webinar
6
CS 5150 6 Quiz 2
7
7 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
8
8 Why Program Concurrently? Real-world interaction Isolation Concurrent design patterns Parallel performance
9
9 Why Program Concurrently? Real-world interaction Isolation Concurrent design patterns Parallel performance
10
10 Why Program Concurrently? Real-world interaction Isolation Concurrent design patterns Parallel performance
11
11 Why Program Concurrently? Real-world interaction Isolation Concurrent design patterns Parallel performance
12
12 Parsing, Batch Approach batch_parse( pile_of_characters ) pile_of_tokens = tokenize( pile_of_characters ) syntax_tree = parse( pile_of_tokens )
13
13 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 )
14
14 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 ) tokenize_driver( pile_of_characters ) declare parser_state while(... ) small_pile_of_tokens = parse_a_little( parser_state, small_pile_of_tokens )
15
15 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 )
16
16 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 )
17
17 Why Program Concurrently? Real-world interaction Isolation Concurrent design patterns Parallel performance
18
18 How to Program Concurrently?
19
19 Units of Concurrency
20
20 Units of Concurrency
21
21 Units of Concurrency
22
22 Units of Concurrency
23
23 The Many Names of Cooperative Concurrency light-weight thread fiber green thread task generator event handler cooperative thread coroutine continuation goroutine iterator protothread eventlet greenlet
24
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 )
25
25 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 )
26
26 To Every Unit of Concurrency a Purpose
27
27 Why Threads are Awful - - Ye Olde Home Town Bank
28
28 Data Race make_deposit( account_id id, number amount ) { account = lookup_account( id ); account.balance += amount; } make_deposit(42, 100) make_deposit(42, 200)
29
29 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 ); }
30
30 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) transfer(42, 83, 200)
31
31 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(42, 73, 100) transfer(73, 42, 100)
32
32 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 ); }
33
33 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
34
34 Abstraction Violators in Sequential Programming goto exceptions global state dynamic dispatch higher-order functions
35
35 Abstraction Tools in Concurrent Programming Isolation Transactions
36
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
37
37 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 ) 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 )
38
38 Implementation of Isolation and Transactions
39
39 Cooperative Threads Easy to implement robust transactions Cons: No parallelism Starvation Programming language support is sparse
40
40 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
41
41 I Want Smokin’ Performance on Multi- cores! 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
42
42 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.
43
43 MVC for Web Applications
44
44 MVC Sequence Diagram
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.