CIS 540 Principles of Embedded Computation Spring 2016 Instructor: Rajeev Alur

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Synchronization and Deadlocks
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Program correctness The State-transition model A global state S  s 0 x s 1 x … x s m {s k = local state of process k} S0  S1  S2  … Each state transition.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Concurrent Programming Problems OS Spring Concurrency pros and cons Concurrency is good for users –One of the reasons for multiprogramming Working.
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutual Exclusion.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Critical Section chapter3.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
CPSC 668Set 6: Mutual Exclusion in Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
Concurrency.
CPSC 668Set 6: Mutual Exclusion in Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
Concurrency in Distributed Systems: Mutual exclusion.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Instructor: Rajeev Alur
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
6.852: Distributed Algorithms Spring, 2008 Class 13.
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch CSCE
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Program correctness The State-transition model The set of global states = so x s1 x … x sm {sk is the set of local states of process k} S0 ---> S1 --->
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Program correctness The State-transition model A global states S  s 0 x s 1 x … x s m {s k = set of local states of process k} S0  S1  S2  Each state.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Synchronicity Introduction to Operating Systems: Module 5.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
CIS 720 Lecture 5. Techniques to avoid interference Disjoint variables –If the write set of each process is disjoint from the read and write set of other.
1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
CPSC 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Fall 2011 Prof. Jennifer Welch CSCE 668 Set 6: Mutual Exclusion in Shared Memory 1.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Introduction to distributed systems description relation to practice variables and communication primitives instructions states, actions and programs synchrony.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Designing Parallel Algorithms (Synchronization)
Grades.
Semaphores Chapter 6.
CS333 Intro to Operating Systems
CIS 720 Lecture 5.
CIS 720 Lecture 5.
Presentation transcript:

CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur

Asynchronous Execution What can happen in a single step of this asynchronous model P?  P1 synchronizes with the environment to accept input on in  P2 synchronizes with the environment to send output on out  P1 performs some internal computation (one of its internal tasks)  P2 performs some internal computation (one of its internal tasks)  P1 produces output on channel x, followed by its consumption by P2  P2 produces output on channel y, followed by its consumption by P1 in x P1 out P2 y P CIS 540 Spring 2016; Lecture Feb 22

Shared Memory Programs AtomicReg nat x := 0 Process P1 nat y1:=0 y1 := x x := y1 +1 Process P2 nat y2:=0 y2 := x x := y2 +1 Declaration of shared variables + Code for each process Key restriction: Each statement of a process either changes local variables, reads a single shared var, or writes a single shared var Execution model: execute one step of one of the processes Can be formalized as asynchronous processes CIS 540 Spring 2016; Lecture Feb 22

Shared Memory Processes  Processes P1 and P2 communicate by reading/writing shared variables  Each shared variable can be modeled as an asynchronous process  State of each such process is the value of corresponding variable  In implementation, shared memory can be a separate subsystem  Read and write channel between each process and each shared variable  To write x, P1 synchronizes with x on “x.write1” channel  To read y, P2 synchronizes with y on “y.read2” channel x.write1 P1 xy P2 y.write1 y.read2 y.write2 x.read2 x.write2 y.read1 x.read1 CIS 540 Spring 2016; Lecture Feb 22

Atomic Registers  By definition of our asynchronous model, each step of above is either internal to P1 or P2, or involves exactly one synchronization: either read or write of one shared variable by one of the processes  Atomic register: Basic primitives are read and write  To “increment” such a register, a process first needs to read and then write back incremented value  But these two are separate steps, and register value can be changed in between by another process x.write1 P1 xy P2 y.write1 y.read2 y.write2 x.read2 x.write2 y.read1 x.read1 CIS 540 Spring 2016; Lecture Feb 22

Data Races AtomicReg nat x := 0 Process P1 nat y1:=0 R1: y1 := x W1: x := y1 +1 Process P2 nat y2:=0 R2: y2 := x W2: x := y2 +1 What are the possible values of x after all steps are executed? x can be 1 or 2 Possible executions: R1, R2, W1, W2 R1, W1, R2, W2 R1, R2, W2, W1 R2, R1, W1, W2, R2, W2, R1, W1 R2, R1, W2, W1 Data race: Concurrent accesses to shared object where the result depends on order of execution, Should be avoided!! CIS 540 Spring 2016; Lecture Feb 22

Puzzle AtomicReg nat x := 1 What possible values can the shared register x take? Process P1 nat u1,v1 u1 := x v1 := x x := u1 + v1 Process P2 nat u2,v2 u2 := x v2 := x x := u2 + v2 CIS 540 Spring 2016; Lecture Feb 22

Mutual Exclusion Problem  Critical Section: Part of code that an asynchronous process should execute without interference from others  Critical section can include code to update shared objects/database  Mutual Exclusion Problem: Design code to be executed before entering critical section by each process  Coordination using shared atomic registers  No assumption about how long a process stays in critical section  A process may want to enter critical section repeatedly Process P1 Entry Code Critical Section To be designed Process P2 Entry Code Critical Section CIS 540 Spring 2016; Lecture Feb 22

Mutual Exclusion Problem  Safety Requirement: Both processes should not be in critical section simultaneously (can be formalized using invariants)  Absence of deadlocks: If a process is trying to enter, then some process should be able to enter Process P1 Entry Code Critical Section To be designed Process P2 Entry Code Critical Section CIS 540 Spring 2016; Lecture Feb 22

Mutual Exclusion: First Attempt AtomicReg bool flag1 := 0; flag2 := 0 Process P1 Idle flag1 := 1 Try flag2=0 ? Crit else flag1 := 0 Process P2 Idle flag2 := 1 Try flag1=0 ? Crit else flag2 := 0 Is this correct? CIS 540 Spring 2016; Lecture Feb 22

Peterson’s Mutual Exclusion Protocol AtomicReg bool flag1 := 0; flag2 := 0; {1,2} turn Process P1 Idle flag1 := 1 Try1 turn=2 ? Crit else flag1 := 0 turn := 1 Try2 Try3 flag2=1? else Process P2 Idle flag2 := 1 Try1 turn=1 ? Crit else flag2 := 0 turn := 2 Try2 Try3 flag1=1? else CIS 540 Spring 2016; Lecture Feb 22

Test&Set Register  Beyond atomic registers:  In one (atomic) step, can do more than just read or write  Stronger synchronization primitives  Test&Set Register: Holds a Booleans value  Reset operation: Changes the value to 0  Test&Set operation: Returns the old value and changes value to 1  If two processes are competing to execute Test&Set on a register with value 0, one will get back 0 and other will get back 1  Modern processors support strong “atomic” operations  Compare-and-swap  Load-linked-store-conditional  Implementation is expensive (compared to read/write operations)! CIS 540 Spring 2016; Lecture Feb 22

Mutual Exclusion using Test&Set Register Test&SetReg free:= 0 Process P1 IdleTry t&s(free)=0 ? Crit else reset(free) Is this correct? Process P2 Idle Try t&s(free)=0 ? Crit else reset(free) CIS 540 Spring 2016; Lecture Feb 22

Another Look at Asynchronous Execution Model nat x:=0; y:=0 A x : x := x+1 A y : y :=y+1  Tasks A x and A y execute in an arbitrary order  Motivation: If we establish that all possible executions of this asynchronous design satisfy some requirement, then this will hold in every implementation of this  Are the following realistic executions? (0,0) - A x -> (1,0) - A x -> (2,0) - A x -> (3,0) … - A x -> (105,0) - A x -> … (0,0) - A x -> (1,0) - A x -> (2,0) - A y -> (2,1) - A y -> (2,2) … - A y -> (2,105) - A y ->…  Does the system satisfy the following requirement: “In every execution, values of both x and y eventually exceed 10” CIS 540 Spring 2016; Lecture Feb 22

Fairness Assumption nat x:=0; y:=0 A x : x := x+1 A y : y :=y+1  Fairness assumption for a task  Assumption about the underlying platform/scheduler  Informally, an infinite execution is unfair for a task if the task does not get a chance to execute  Unfair to A y : (0,0) - A x -> (1,0) - A x -> (2,0) - A x -> (3,0) … - A x -> (105,0) …  Unfair to A x : (0,0) - A x -> (1,0) - A x -> (2,0) - A y -> (2,1) - A y -> (2,2) … (2,105)…  Fairness assumptions restrict the set of “possible” executions to realistic ones without putting concrete bounds on relative speeds CIS 540 Spring 2016; Lecture Feb 22

Formalizing Fairness  Definition 1: An infinite execution is fair to a task A, if the task A is executed repeatedly (i.e. infinitely often) during this execution  Is this fair to task B y of P2? (0,0) - B x -> (1,0) - B x -> (2,0) - B x -> (3,0) - B x -> … (105,0) - B x -> …  After first step, the task B y is not enabled, and thus, cannot be executed. So this execution is a possible execution, and so should be considered fair  Definition 2: An infinite execution is fair to a task A, if repeatedly, either task A is executed or is disabled nat x:=0; y:=0 A x : x := x+1 A y : y :=y+1 Process P1 nat x:=0; y:=0 B x : x := x+1 B y : x=0  y :=y+1 Process P2 CIS 540 Spring 2016; Lecture Feb 22

Weak vs Strong Fairness  Is this fair to task A y of P3? (0,0) - A x -> (1,0) - A x -> (2,0) - A x -> (3,0) … - A x -> (105,0) - A x -> …  Task A y is not continuously enabled, and thus, this is fair according to definition 2 (called weak fairness)  Definition 3 (Strong fairness): An infinite execution is fair to a task A, if task A is either executed repeatedly, or disabled from a certain step onwards  Above execution is weakly-fair to task A y, but not strongly-fair nat x:=0; y:=0 A x : x := x+1 A y : even(x)  y :=y+1 Process P3 CIS 540 Spring 2016; Lecture Feb 22