topic 20
play

Topic 20 If they interact with each other, as when sharing a - PDF document

Concurrency In the real world, many processes act concurrently Topic 20 If they interact with each other, as when sharing a resource, strange things can happen Concurrency Section 3.4 Fall 2008 Programming Development 1 Fall 2008


  1. Concurrency • In the real world, many processes act concurrently Topic 20 • If they interact with each other, as when sharing a resource, strange things can happen Concurrency Section 3.4 Fall 2008 Programming Development 1 Fall 2008 Programming Development 2 Techniques Techniques Interaction of two withdraw Simplified bank account processes example • Simplified withdraw procedure • What happens if we have multiple people with a shared account? ; withdraw an amount from the account ; balance • Each process does a read of balance and a set of (define (withdraw amount) balance to a new value (set! balance (- balance amount))) • There are six ways to interleave these events • Withdraw process must read balance, then do a • Some of these sequences of events make sense and computation, then set balance to new value some don't Fall 2008 Programming Development 3 Fall 2008 Programming Development 4 Techniques Techniques Consider two withdrawals Sequence 1 • balance = 100 initially process 1 process 2 • process 1 = (withdraw 20) • process 2 = (withdraw 30) reads balance = 100 sets balance = 70 reads balance = 70 sets balance = 50 Fall 2008 Programming Development 5 Fall 2008 Programming Development 6 Techniques Techniques 1

  2. Sequence 2 Sequence 3 process 1 process 2 process 1 process 2 reads balance = 100 reads balance = 100 reads balance = 100 reads balance = 100 sets balance = 70 sets balance = 80 sets balance = 80 sets balance = 70 ??? ??? Fall 2008 Programming Development 7 Fall 2008 Programming Development 8 Techniques Techniques Sequence 4 Sequence 5 process 1 process 2 process 1 process 2 reads balance = 100 reads balance = 100 reads balance = 100 reads balance = 100 sets balance = 70 sets balance = 80 sets balance = 80 sets balance = 70 ??? ??? Fall 2008 Programming Development 9 Fall 2008 Programming Development 10 Techniques Techniques Interactions can be more Sequence 6 complicated process 1 process 2 • Let x = 10 • Let process 1 = (lambda () (set! x (* x x))) reads balance = 100 • Let process 2 = (lambda () (set! x (+ x 1))) sets balance = 80 • Process 1 must do two reads of x reads balance = 80 • Process 2 might change x between reads by process 1 sets balance = 50 • Five different final values for x are possible Fall 2008 Programming Development 11 Fall 2008 Programming Development 12 Techniques Techniques 2

  3. Notation Five different results • R1(n) means process 1 reads x, obtains n x = 10 x = 10 x = 10 x = 10 x = 10 • R2(n) means process 2 reads x, obtains n R1(10) R2(10) R2(10) R1(10) R1(10) • S1(n) means process 1 sets x to n R1(10) S2(11) R1(10) R1(10) R1(10) • S2(n) means process 2 sets x to n S1(100) R1(11) S2(11) R2(10) R2(10) • Process 1 does two reads, then a set R2(100) R1(11) R1(11) S1(100) S2(11) • Process 2 does one read, then a set S2(101) S1(121) S1(110) S2(11) S1(100) x = 101 x = 121 x = 110 x = 11 x = 100 Fall 2008 Programming Development 13 Fall 2008 Programming Development 14 Techniques Techniques Concurrent processing in The only way DrScheme The only way to prevent unwanted interactions is to serialize certain processes, that is, they have to occur • Must use Pretty-Big language sequentially rather than in parallel • Procedures to be run concurrently must have no arguments • Each procedure is run in a separate thread Fall 2008 Programming Development 15 Fall 2008 Programming Development 16 Techniques Techniques Parallel-execute Serialization • Issue – several processes may share (and be able to ; cause parallel execution of args change) a common state variable. Need some way to (define (parallel-execute . args) isolate changes so that only one process can (map thread args)) access/change the data at a time. • Map creates a thread for each procedure • They start running immediately • There are several ways to force procedures to run sequentially – generally, allow us to interleave • Example: programs but constrain interleaving ; executes the two procedures in parallel (parallel-execute (lambda () (set! x (* x x))) • Generally – have the process “acquire a flag” – while (lambda () (set! x (+ x 1)))) that process has the flag, no other process can run until that flag is released. Fall 2008 Programming Development 17 Fall 2008 Programming Development 18 Techniques Techniques 3

  4. Making a Serializer What is required? • Use a primitive synchronization mechanism called a • To implement a mutex, need a mechanism for testing mutex. it and setting it that can not be interrupted… • Mutex’s can be acquired and released. • A mutex is a mutable object (e.g., a 1 element list) that can hold the value of true or false. • When the value is false, it is available for being acquired. • When the value is true, it is unavailable and the process that wants it must wait… Fall 2008 Programming Development 19 Fall 2008 Programming Development 20 Techniques Techniques Test-and-set! Test-and-set! behavior • Conceptually behaves like (define cell1 (list #f)) (car cell1) --> #f ; if cell is already #t then return #t otherwise ; set it to #t and return #f (test-and-set! cell1) --> #f (define (test-and-set! cell) (car cell1) --> #t (if (car cell) (test-and-set! cell1) --> #t #t (begin (set-car! cell #t) #f))) What do we do with a flag like this? Use it for mutual exclusion purposes. Share • For this to work, it must not be interruptable flag among several processes – let them run only when they get the flag. • Exists as a single machine instruction on some Specific example of a semaphore. machines Fall 2008 Programming Development 21 Fall 2008 Programming Development 22 Techniques Techniques Mutual exclusion flag (mutex) Make-mutex ; provides a cell that can be used for • A boolean flag that is controlled by one process at a ; mutual exclusion purposes among processes time (because it is manipulated by test-and-set!) (define (make-mutex) (let ((cell (list #f))) • Accepts 'acquire and 'release messages (define (the-mutex m) (cond ((eq? m 'acquire) • A set of processes (that must be mutually exclusive) (if (test-and-set! cell) will share the same flag. Grab the flag before they (the-mutex ‘acquire))) ;retry start, and release it when they are done. ((eq? m 'release) (clear! cell)))) • In this way, only one process at a time can the-mutex)) manipulate the variable. (define (clear! Cell) (set-car! Cell #f)) Fall 2008 Programming Development 23 Fall 2008 Programming Development 24 Techniques Techniques 4

  5. Make-serializer Serializers ; make a serializer by creating a mutex ; to be shared by the mutually exclusive • A serializer is a higher-order procedure that converts ; procedures other procedures into ones that can only run one at a time (define (make-serializer) (let ((mutex (make-mutex))) (lambda (p) • All the procedures that are processed by the same (define (serialized-p . args) serializer are run in sequence without interleaving (mutex 'acquire) (let ((value (apply p args))) (mutex 'release) value)) serialized-p))) Fall 2008 Programming Development 25 Fall 2008 Programming Development 26 Techniques Techniques Protecting bank accounts Serialization guaranteed ; create a bank account that is protected and ; allows parallel execution (define s (make-serializer)) (define (make-account balance) (define (withdraw amount) (parallel-execute (if (>= balance amount) (s (lambda () (set! x (* x x)))) (s (lambda () (set! x (+ x 1))))) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) Fall 2008 Programming Development 27 Fall 2008 Programming Development 28 Techniques Techniques continued Works for simple transactions • Muliple processes withdrawing from and depositing to (let ((s (make-serializer))) the same account are serialized (define (dispatch m) • Multiple accounts can still be withdrawn from and (cond ((eq? m 'withdraw) (s withdraw)) deposited to concurrently ((eq? m 'deposit) (s deposit)) • Problems can still arise if two or more resources are ((eq? m 'balance) balance) involved in a transaction (else (error "unknown msg-MAKE-ACCOUNT" m)))) dispatch)) Fall 2008 Programming Development 29 Fall 2008 Programming Development 30 Techniques Techniques 5

Recommend


More recommend