1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Symmetric Multiprocessors: Synchronization and Sequential Consistency.
Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Mutual Exclusion.
Multiprocessor OS The functional capabilities often required in an OS for a multiprogrammed computer include the resource allocation and management schemes,
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Transaction Processing IS698 Min Song. 2 What is a Transaction?  When an event in the real world changes the state of the enterprise, a transaction is.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
1 Concurrency: Deadlock and Starvation Chapter 6.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Transaction Lectured by, Jesmin Akhter, Assistant professor, IIT, JU.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
Games Development 2 Concurrent Programming CO3301 Week 9.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
Kernel Locking Techniques by Robert Love presented by Scott Price.
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
Fall 2008Programming Development Techniques 1 Topic 16 Assignment and Local State Section 3.1.
Bank Account Environment Model Examples Prof. Tony White April 2010.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you allowed to talk about the midterm on piazza or in public yet? A)Yes B)No 2.
Linux Kernel Development Chapter 8. Kernel Synchronization Introduction Geum-Seo Koo Fri. Operating System Lab.
Operational Semantics of Scheme
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Multithreading / Concurrency
Background on the need for Synchronization
“Language Mechanism for Synchronization”
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
143a discussion session week 3
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Symmetric Multiprocessors: Synchronization and Sequential Consistency
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Concurrency: Mutual Exclusion and Process Synchronization
Kernel Synchronization I
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Threads and Multithreading
Presentation transcript:

1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?

2/25 Concurrency and asynchronous computing Object oriented approaches lose “referential transparency” Referential transparency means equal expressions can be substituted for one another without changing the value of the expression

3/25 Example of referential transparency (define (make-adder n) (lambda (x) (+ x n)) (define D1 (make-adder 4)) (define D2 (make-adder 4)) Are D1 and D2 the same? Different procedural objects But can replace any expression with D1 by same expression with D2 and get same value – so YES

4/25 Example of loss of transparency (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! Balance (- balance amount)) balance) “Insufficient funds”)) (define (deposit amount) (set! Balance (+ balance amount))) (define (dispatch m) (cond ((eq? M ‘withdraw) withdraw) ((eq? M ‘deposit) deposit) ((eq? M ‘balance) balance) (else (error “unknown request” m)))) dispatch) (define peter (make-account 100)) (define paul (make-account 100)) Are these the same?

5/25 The role of time in evaluation (d1 5) ;Value: 9 (d1 5) ;Value: 9 Order of evaluation doesn’t matter ((peter ‘deposit) 5) ;Value: 105 ((peter ‘deposit) 5) ;Value: 110 Order of evaluation does matter

6/25 Role of concurrency and time Behavior of objects with state depends on sequence of events that precede it. Objects don’t change one at a time; they act concurrently. Computation could take advantage of this by letting processes run at the same time But this raises issues of controlling interactions

7/25 Why is time an issue? (define peter (make-account 100)) (define paul peter) ((peter ‘withdraw) 10) ((paul ‘withdraw) 25) Peter Bank Paul Peter Bank Paul

8/25 Peter Bank Paul 100 Access balance (100) New value 100 – 10 = 90 New value 100 – 25 = 75 Set balance 90 Set balance Why is time an issue? (define (withdraw amount) (if (>= balance amount) (begin (set! Balance (- balance amount)) balance) “Insufficient funds”)) ((peter ‘withdraw) 10) ((paul ‘withdraw) 25) T I M E

9/25 Correct behavior of concurrent programs REQUIRE That no two operations that change any shared state variables can occur at the same time That a concurrent system produces the same result as if the processes had run sequentially in some order –Does not require the processes to run sequentially, only to produce results as if they had run sequentially –There may be more than one “correct” result as a consequence!

10/25 Parallel execution (define x 10) (define p3 (lambda () (set! X (* x x)))) (define p4 (lambda () (set! X (+ x 1)))) (parallel-execute p3 p4) P1: a: lookup first x in p3 b: lookup second x in p3 c: assign product of a and b to x P2: d: lookup x in p4 e: assign sum of d and 1 to x a b c d e a d b e c a b d c e d a b e c a d b c e a d e b c d a b c e d a e b c a b d e c d e a b c

11/25 Parallel execution (define x 10) (define p3 (lambda () (set! X (* x x)))) (define p4 (lambda () (set! X (+ x 1)))) (parallel-execute p3 p4) P1: a: lookup first x in p3 b: lookup second x in p3 c: assign product of a and b to x P2: d: lookup x in p4 e: assign sum of d and 1 to x a b c d e a d b e c a b d c e d a b e c a d b c e a d e b c d a b c e d a e b c a b d e c d e a b c

12/25 Parallel execution (define x 10) (define p3 (lambda () (set! X (* x x)))) (define p4 (lambda () (set! X (+ x 1)))) (parallel-execute p3 p4) P1: a: lookup first x in p3 b: lookup second x in p3 c: assign product of a and b to x P2: d: lookup x in p4 e: assign sum of d and 1 to x a b c d e a d b e c a b d c e d a b e c a d b c e a d e b c d a b c e d a e b c a b d e c d e a b c

13/25 Serializing access to shared state Serialization: Processes will execute concurrently, but there will be distinguished sets of procedures such that only one execution of a procedure in each serialized set is permitted to happen at a time. If some procedure in the set is being executed, then a process that attempts to execute any procedure in the set will be forced to wait until the first execution has finished. Use serialization to control access to shared variables.

14/25 Serializers to “mark” critical regions We can mark regions of code that cannot overlap execution in time. This adds an additional constraint to the partial ordering imposed by the separate processes. Assume make-serializer takes a procedure as input and returns a serialized procedure that behaves like the original procedure, expect that if some other procedure in the same serialized set is underway, this procedure must wait for that process’ completion before beginning.

15/25 Serialized execution (define x 10) (define mark-red (make-serializer)) (define p5 (mark-red (lambda () (set! X (* x x))))) (define p6 (mark-red (lambda () (set! X (+ x 1))))) (parallel-execute p5 p6) P1: a: lookup first x in p5 b: lookup second x in p5 c: assign product of a and b to x P2: d: lookup x in p6 e: assign sum of d and 1 to x a b c d e a d b e c a b d c e d a b e c a d b c e a d e b c d a b c e d a e b c a b d e c d e a b c

16/25 Serializing access to a shared state variable (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! Balance (- balance amount)) balance) “Insufficient funds”)) (define (deposit amount) (set! Balance (+ balance amount))) (let ((protected (make-serializer))) (define (dispatch m) (cond ((eq? M ‘withdraw) (protected withdraw)) ((eq? M ‘deposit) (protected deposit)) ((eq? M ‘balance) balance) (else (error “unknown request” m)))) dispatch)) (define peter (make-account 100)) (define paul peter)

17/25 Multiple shared resources Swapping money between accounts (define (exchange account1 account2) (let ((difference (- (account1 ‘balance) (account2 ‘balance)))) ((account1 ‘withdraw) difference) ((account2 ‘deposit) difference))) A1 = 300 A2 = 100 A3 = 200 (exchange a1 a2) & (exchange a2 a3) 1.Difference a1 & a2 = d1 2.Withdraw d1 from a1 3.Deposit d1 to a2 4.Difference a1 & a3 = d2 5.Withdraw d2 from a1 6.Deposit d2 to a3 1  d1 =  d2 =  a1 =  a3 =  a1 = 0 3  a2 = 300

18/25 Locking out access to shared state variables (define (make-account-with-serializer balance) (define (withdraw amount) (if (>= balance amount) (begin (set! Balance (- balance amount)) balance) “Insufficient funds”)) (define (deposit amount) (set! Balance (+ balance amount))) (let ((balance-serializer (make-serializer))) (define (dispatch m) (cond ((eq? M ‘withdraw) withdraw) ((eq? M ‘deposit) deposit) ((eq? M ‘balance) balance) ((eq? M ‘serializer) balance-serializer) (else (error “unknown request” m)))) dispatch))

19/25 Serialized access to shared variables (define (deposit account amount) (let ((s (account ‘serializer)) (d (account ‘deposit))) ((s d) amount))) (define (serialized-exchange acct1 acct2) (let ((serializer1 (acct1 ‘serializer)) (serializer2 (acct2 ‘serializer))) ((serializer1 (serializer2 exchange)) acct1 acct2)))

20/25 Deadlocks Suppose Peter attempts to exchange a1 with a2 And Paul attempts to exchange a2 with a1 Imagine that Peter gets the serializer for a1 at the same time that Paul gets the serializer for a2. Now Peter is stalled waiting for the serializer from a2, but Paul is holding it. And Paul is similarly waiting for the serializer from a1, but Peter is holding it. This “deadly embrace” is called a deadlock.

21/25 Implementing serializers We can implement serializers using a primitive synchronization method, called a mutex. A mutex acts like a semaphore flag: Once one process has acquired the mutex (or run the flag up the flagpole), no other process can acquire the mutex until it has been released (or the flag has been run down the flagpole). Thus only one of the procedures produced by the serializer can be running at any given time. All others have to wait for the mutex to be released so that they can acquire it and block out competing processes.

22/25 A simple Scheme Mutex (define (make-serializer) (let ((mutex (make-mutex))) (lambda (p) (define (serialized-p. Args) (mutex ‘acquire) (let ((val (apply p args))) (mutex ‘release) val)) serialized-p)))

23/25 A simple Scheme Mutex (define (make-mutex) (let ((cell (list #f))) (define (the-mutex m) (cond ((eq? M ‘acquire) (if (test-and-set! Cell) (the-mutex ‘acquire))) ((eq? M ‘release) (clear! Cell)))) the-mutex)) (define (clear! Cell) (set-car! Cell #f))

24/25 Implementing test-and-set! (define (test-and-set! Cell) (if (car cell) #t (begin (set-car! Cell #t) #f))) This operation must be performed atomically, e.g. directly in the hardware!!

25/25 Concurrency and time in large systems Can enable parallel processes by judiciously controlling access to shared variables In essence this defines a notion of atomic actions, which must be initiated and completed before other actions may proceed. Careful programming leads to inefficient processing, while ensuring correct behavior Ultimately concurrent processing inherently requires careful attention to communication between processes.