UVA CS 655 - Lecture 22: Abstractions for Concurrency

Unformatted text preview:

PowerPoint PresentationMenuLast TimeAbstractionsAlgol 68: Collateral ClausesAlgol 68: SemaphoresSemaphore ExampleSemaphore Example, cont.What can go wrong?MonitorsMonitor ExampleMonitor Example, cont.Java SynchronizationSynchronized ExampleLindaDesign by Taking AwayBasic IdeaTuplesTuple Space OperationsMeaning of takeOperational SemanticsShared AssignmentSemaphoreDistributed EbayFactorialFinishing FactorialConcurrent Finishing FactorialSorting in LindaSummaryChargeDavid Evanshttp://www.cs.virginia.edu/~evansCS655: Programming LanguagesUniversity of VirginiaComputer ScienceLecture 22: Abstractions for ConcurrencyWhen you have a world-wide tuple space, you’ll be ableto tune it in from any computer anywhere – or from any quasi-computer: any cell phone, any TV, any toaster. David Gelernter’s introduction to JavaSpaces Principles, Patterns, and Practice.17 April 2001 CS 655: Lecture 21 2Menu•Form going around–Signup for Project Presentations–Vote for Next Lecture (no cheating!)•Abstractions for Concurrency–Algol 68–Monitors–Linda and JavaSpaces17 April 2001 CS 655: Lecture 21 3Last Time•Concurrent programming is programming with partial ordering on time•A concurrent programming language gives programmers mechanisms for expressing that partial order•We can express many partial orders using the thread control primitives fork and join and locking primitives protect, acquire and release.17 April 2001 CS 655: Lecture 21 4Abstractions•Programming at that low level would be a pain – are there better abstractions?–Hundreds of attempts...we’ll see a few today.•Issues–Thread creation –Thread synchronization–Resource contentionforkjoinprotect, acquire, release17 April 2001 CS 655: Lecture 21 5Algol 68: Collateral Clauses•Collateral Clausesstmt0; (stmt1, stmt2) stmt3;Defines a partial order:stmt0stmt1 stmt2stmt317 April 2001 CS 655: Lecture 21 6Algol 68: Semaphores•Dijkstra, “Cooperating Sequential Processes”•type semaup – increments down – decrements (must > 0 before)17 April 2001 CS 655: Lecture 21 7Semaphore Examplebegin sema mutex := level 1;proc producer while not finished do down mutex ... insert item up mutex od;17 April 2001 CS 655: Lecture 21 8Semaphore Example, cont.proc consumer while not finished do down mutex ... remove item up mutex od;par (producer, consumer) // start them in parallel17 April 2001 CS 655: Lecture 21 9What can go wrong?•Programmer neglects to up semaphore•Programmer neglects to down semaphore before accessing shared resource•Programmer spends all her time worrying about up and down instead of the algorithm17 April 2001 CS 655: Lecture 21 10Monitors•Concurrent Pascal [Hansen 74], Modula [Wirth 77], Mesa [Lampson80]•Integrated data abstraction and resource synchronization•Routines that use a shared resource grouped in a monitor, accesses only allowed through exported procedures•Conditions can control when threads may execute exported procedures17 April 2001 CS 655: Lecture 21 11Monitor Examplemonitor boundedbuffer buffer: array 0..N-1 of int;count: 0..N; nonempty, nonfull: condition; procedure append (x: int) if count = N then nonfull.wait; buffer[count] := x; count := count + 1; nonempty.signalend appendExample adapted from [Hansen93]17 April 2001 CS 655: Lecture 21 12Monitor Example, cont.procedure remove () returns portion if count = 0 then nonempty.wait; x := ... nonfull.signalend remove;17 April 2001 CS 655: Lecture 21 13Java Synchronization•synchronized method qualifier–Once a synchronized method begins execution, it will complete before any other thread enters a method of the same object–Run-time must associate a lock with every object•Is this enough to implement a semaphore?•Is this better/worse than monitors?17 April 2001 CS 655: Lecture 21 14Synchronized Exampleclass ProducerConsumer { private int x = 1; synchronized void produce () { x = x + 1; } synchronized void consume () { x = x – 1; }}How could we require x stay positive?17 April 2001 CS 655: Lecture 21 15Linda•Program Concurrency by using uncoupled processes with shared data space•Add concurrency into a sequential language by adding:–Simple operators–Runtime kernel (language-independent)–Preprocessor (or compiler)17 April 2001 CS 655: Lecture 21 16Design by Taking Away•Backus: von Neumann bottleneck results from having a store–Remove the store  Functional Languages•Gelernter: distributed programming is hard because of inter-process scheduling and communication due to order of mutation–We don’t have to remove the store, just mutation–Remove mutation  read-and-remove only store  tuple spaces17 April 2001 CS 655: Lecture 21 17Basic Idea•Have a shared space (“tuple space”) –Processes can add, read, and take away values from this space•Bag of processes, each looks for work it can do by matching values in the tuple space•Get load balancing, synchronization, messaging, etc. for free!17 April 2001 CS 655: Lecture 21 18TuplesConventional MemoryLinda/JavaSpacesUnit Bit Logical Tuple(23, “test”, false)Access Using Address (variable) Selection of valuesOperations read, write read, add, removeJavaSpaces: read, write, takeimmutable17 April 2001 CS 655: Lecture 21 19Tuple Space Operations•out (t) – add tuple t to tuple space•take (s)  t – returns and removes tuple t matching template s•read (s)  t – same as in, except doesn’t remove t.•Operations are atomic (even if space is distributed)17 April 2001 CS 655: Lecture 21 20Meaning of taketake (“f”, int n)take (“f”, 23)take (“t”, bool b, int n)take (string s, int n)take (“cookie”)(“f”, 23)(“f”, 17)(“t”, 25)(“t”, true)(“t”, false)Tuple Space17 April 2001 CS 655: Lecture 21 21Operational Semantics•Extend configurations with a tuple space (just a bag of tuples)•Transition rule for out:–Just add an entry to the tuple space•Transition rule for take:–If there is a match (ignoring binding):•Remove it from the tuple space•Advance the thread–Similar to join last time – it just waits if there is no match17 April 2001 CS 655: Lecture 21 22Shared AssignmentLoc := Expressiontake (“Loc”, formal loc_value);out (“Loc”, Expression);e.g.:x := x + 1; take (“x”, formal x_value)out (“x”, x_value + 1);17 April 2001 CS 655: Lecture 21 23Semaphore•Create (int n, String resource)for (i = 0; i < n; i++) out


View Full Document

UVA CS 655 - Lecture 22: Abstractions for Concurrency

Download Lecture 22: Abstractions for Concurrency
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 22: Abstractions for Concurrency and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 22: Abstractions for Concurrency 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?