f undamen tal structures of computer science i i 15 212
play

F undamen tal Structures of Computer Science I I 15-212-ML - PDF document

F undamen tal Structures of Computer Science I I 15-212-ML F all 1998 CONCURRENCY Concurrency Op erations in a p rogram a re if they b e concurrent c ould executed in pa rallel. Concurrent p rograms a re


  1. F undamen tal Structures of Computer Science I I 15-212-ML F all 1998 CONCURRENCY

  2. Concurrency Op erations in a p rogram a re if they b e concurrent c ould executed in pa rallel. Concurrent p rograms a re inherently nondeterministic . Concurrent p rogramming languages p rovide abstraction mechanisms fo r concurrency , with less overhead than using system-level p ro cesses. 2

  3. F o rcing Sequential Op erations Being fo rced to have sequential op erations can have disatrous e�ects fo r inherently concurrent applications. Example: the Unix \lost connection to remote news xrn server" feature. 3

  4. Natural Places fo r Concurrent Programming Interactive systems such as windo w managers and � GUIs. User interaction can b e complex. A sp readsheet might p rovide an edito r, a windo w fo r � graphical displa ys of data, and even a sp eech interface. Many applications have \deadtime" b et w een input � events when running. If application is output intensive, concurrency can � mak e it resp onsive to input. Distributed systems { each no de has its o wn state � and control �o w. Anything involving a net w o rk. Client-server p roto cols. � 4

  5. Language Supp o rt is essential to writing and maintaining Abstraction soft w a re When it comes to concurrency , most languages do not p rovide useful abstractions Ra w p ro cess creation using fo rk () is p rovided in C 5

  6. Reasoning Ab out Languages In SML there is a clean semantics that aids our reasoning true ` ! ` ! � e , � e , v 1 2 ` if then else ! � e e e , v 1 2 2 With references, reasoning b ecomes mo re complicated let val memo = ref ( fn () => raise Impossible) fun s'() = let val r = s() in memo := ( fn () => r); r end in memo := s'; fn () => (!memo)() end 6

  7. Concurrent Programming is Ha rd Sequential p rograms a re deterministic let val ref x = 0 in x := !x + 1; x := !x + 2; print (!x) end Reasoning required when p rogramming with concurrency is much mo re complicated let val ref x = 0 in k (x := !x + 1) (x := !x + 2); print (!x) end 7

  8. Ingredients fo r Concurrency A mechanism fo r intro ducing sequential of � threads control, o r p ro cesses A w a y fo r p ro cesses to � communicate A mechanism fo r p ro cesses to to � synchronize restrict o rder of execution and limit nondeterminism 8

  9. Flavo rs of Concurrent Language Sha red-memo ry Rely on imp erative features fo r interp ro cess � communication Sepa rate synchronization p rimitives to control � access to sha red state. Example: Java � Message-passing Uni�ed mechanism fo r synchronization and � communication Example: CML � 9

  10. Interference let val x = ref 0 in k (x := !x + 1) (x := !x + 2); print (!x) end can o ccur when t w o p ro cesses a re accessing Interference of co de where assignments a re made critical regions Synchronization p rovides the mechanism fo r avoiding interference, b y allo wing a p ro cess to obtain a lo ck in a critical region. 10

  11. Deadlo ck Consider the follo wing schematic of p ro cesses P and Q: P: acquire A; acquire B; compute; release B; release A; Q: acquire B; acquire A; compute; release A; release B; This can deadlo ck with P holding the lo ck on A and Q holding the lo ck on B. 11

  12. Livelo ck No w consider P and Q de�ned as: P: Q: l: acquire A; l: acquire B; if (B is held) if (A is held) then (release A; goto l) then (release B; goto l) else acquire B else acquire A compute compute release B; release A release A; release B What might happ en here? 12

  13. Threads in Java f public interface Runnable public abstract void run(); g f public class Thread implements Runnable public Thread(); public Thread(String name); ... public void run(); public void start() thro ws IllegalThreadStateException; public �nal void stop(); public �nal void suspend() thro ws SecurityException; public �nal void resume() thro ws SecurityException; ... g 13

  14. Threads in Java (con t) f class PrimeThread extends Thread long minPrime; f PrimeThread(long minPrime) this.minPrime = minPrime; g f public void run() � // compute p rimes minPrime ... g g PrimeThread p = new PrimeThread(101); p.start(); 14

  15. Threads in Java (con t) A metho d acquires a lo ck b efo re it synchronized � executes. If a va riable is ever to b e assigned b y one thread � and used b y another, all accesses to that va riable should b e synchronized . Lo cking is ca rried out using monito rs in the JVM. � Synchronization mak es metho ds and blo cks atomic . � Deadlo cking is not p revented. � 15

  16. Threads in Java (con t) f public class Box p rivate Object boxContents; public synchronized f Object get() Object contents = boxContents; boxContents = null; return contents; g contents) f public synchronized boolean put(Object if (boxContents != null) return false; boxContents = contents; return true; g g 16

  17. Sha red Memo ry Mo del Promotes e�ciency , but not co rrectness � Requires \defensive p rogramming" (p rotect y our � data from interference). P o o r �t with value-o riented p rogramming (ML) � 17

  18. Message-P assing Languages Pro cesses communicate b y sending messages across � channels. The communication ma y either b e (o r � blo cking synchronous) o r (asynchronous) non-blo cking [4 p ossibilities] Mailb o x metapho r: In asynchronous send, once the � letter is in the mailb o x, the sender can p ro ceed with other tasks. T elephone metapho r: In synchronous send, the � sender is tied up until his message is received. � Synchr onous message p assing is e asier to r e ason ab out. 18

  19. Basic CML Primitives: Threads A is a CML p ro cess. Initially , there is a single thread thread but mo re can b e created using spawn : val spawn : (unit -> unit) -> thread id Creates a new thread to evaluate the function. � The numb er of threads is unb ounded. � Since threads a re rep resented b y ML values, their � sto rage can b e recycled b y the ga rbage collecto r. 19

  20. Basic CML Primitives: Channels F o r threads to b e useful, w e need w a ys to communicate b et w een them. F o r communicating values of t yp e 'a , CML p rovides t yp e 'a chan The send and receive op erations a re val recv : 'a chan -> 'a val send : ('a chan * 'a) -> unit Message passing is synchronous 20

  21. Basic CML Primitives: Channels (con t) When a thread executes a o r on a send receive channel, it blo cks until some other thread o�ers the complementa ry communication. The message is then passed from sender to receiver, and the threads continue. Message passing involves b oth communication and synchronization 21

  22. Example: Reference Cells As a simple example of channels, as w ell as client-server p roto cols, w e'll implement the follo wing signature fo r mutable cells: signature CELL = sig t yp e 'a cell val cell : 'a -> 'a cell val get : 'a cell -> 'a val put : 'a cell * 'a -> unit end 22

  23. Example: Reference Cells (con t) structure Cell :> CELL = struct datat yp e 'a request = GET | PUT of 'a datat yp e 'a cell = CELL of f reqCh:'a CML.chan g request CML.chan, replyCh:'a (CELL f reqCh, replyCh g ) fun get = (CML.send (reqCh, GET); CML.recv replyCh) fun (CELL f reqCh, ... g , put x) = CML.send (reqCh, PUT x) ... end 23

  24. Example: Reference Cells (con t) structure Cell :> CELL = struct datat yp e of 'a request = GET | PUT 'a datat yp e of 'a cell = CELL f reqCh:'a CML.chan g request CML.chan, replyCh:'a fun cell x = let val reqCh = CML.channel() val replyCh = CML.channel() fun loop x = ( case (CML.recv reqCh) of GET => (CML.send (replyCh, x); loop x) | (PUT x') => loop x') in ( fn CML.spawn () => loop x); f reqCh replyCh g CELL = reqCh, replyCh = end (CELL f reqCh, replyCh g ) fun get = (CML.send (reqCh, GET); CML.recv replyCh) (CELL f reqCh, ... g , fun put x) = CML.send (reqCh, PUT x) end 24

  25. Example: Reference Cells (con t) This is an example of st yle of concurrent client-server p rogramming. Why is the implementation co rrect? Why can't multiple clients interfere with each other, and receive each other's messages, since they a re communicating on the same channel? 25

Recommend


More recommend