Parallel Programming 0024 Week 06 Thomas Gross Spring Semester 2010 Apr 15, 2010.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
Introduction to Proofs
Operating Systems Part III: Process Management (Process Synchronization)
PROOF BY CONTRADICTION
CS 267: Automated Verification Lecture 8: Automata Theoretic Model Checking Instructor: Tevfik Bultan.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
May 23, 2002Serguei A. Mokhov, 1 Synchronization COMP346/ Operating Systems Tutorial 3 Revision 1.2 October 7, 2003.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 4: Mutual Exclusion.
Mutual Exclusion.
Chapter 3 The Critical Section Problem
1 Operating Systems, 112 Practical Session 5, Synchronization 1.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
02/11/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Strong Induction, WOP and Invariant Method Leo Cheung.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
02/14/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
Introduction to Proofs
Lecture 3.1: Mathematical Induction CS 250, Discrete Structures, Fall 2014 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag.
MATH 224 – Discrete Mathematics
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Chapter 5 Existence and Proof by contradiction
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION OPERATING SYSTEMS.
Solutions to Second 4330/6310 Quiz Spring First question Which of the following statements are true or false (2 points) and why? (3 points)
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
1 Computability CS 101E Spring 2007 Michele Co. 2 So Far In Class We’ve seen the following programming constructs –if, if-else, if-else-if –switch –for.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Today’s Agenda  HW #2 Out  Replay Semaphore and Lock  Advanced Locking Advanced Topics in Software Engineering 1.
CIS 842: Specification and Verification of Reactive Systems Lecture INTRO-Examples: Simple BIR-Lite Examples Copyright 2004, Matt Dwyer, John Hatcliff,
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
Soundness of Types Ensuring that a type system is not broken.
Section 1.7. Definitions A theorem is a statement that can be shown to be true using: definitions other theorems axioms (statements which are given as.
5-5 Indirect Proof. Indirect Reasoning: all possibilities are considered and then all but one are proved false. The remaining possibility must be true.
EXAMPLE 3 Write an indirect proof Write an indirect proof that an odd number is not divisible by 4. GIVEN : x is an odd number. PROVE : x is not divisible.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Section 1.7. Section Summary Mathematical Proofs Forms of Theorems Direct Proofs Indirect Proofs Proof of the Contrapositive Proof by Contradiction.
CSE322 PUMPING LEMMA FOR REGULAR SETS AND ITS APPLICATIONS
Background on the need for Synchronization
Concurrency.
CSC Multiprocessor Programming, Spring, 2011
Lecture 9: Process Synchronization
The Foundations: Logic and Proofs
The Critical-Section Problem
Critical section problem
Practical Session 5, Synchronization
Copyright © 2014 Pearson Education, Inc.
Dr. Halimah Alshehri MATH 151 Dr. Halimah Alshehri Dr. Halimah Alshehri.
Operating Systems {week 10}
Presentation transcript:

Parallel Programming 0024 Week 06 Thomas Gross Spring Semester 2010 Apr 15, 2010

Classroom exercise Consider this program (fragment) [PingPong] for thread A (myid == 0) and thread B (myid == 1) // thread A public void run() { while (true) { A1: non_critical section A2: while ( !(signal.turn == 0)){} A3: critical_section A4: signal.turn = 1; }

Class room exercise, continued // thread B public void run() { while (true) { B1: non_critical section B2: while ( !(signal.turn == 1)){} B3: critical_section B4: signal.turn = 0; }

Your task (now!) Show that these threads will never be both in their critical section at the same time. You should prove this property in a manner that’s similar to the proof given in class.

Some thoughts on how to proceed We introduced already labels for statements and produced two distinct versions for thread A and thread B. Now you should formulate the invariant.

Invariant(s) (i) at(A3) -> turn == 0 (ii) at(B3) -> turn== 1 (iii) not [at(A3) AND at(B3)] m

Proof strategy Proof by induction on the execution sequence. Base case: does (i) hold at the start of the execution of the program (threads at A1 and B1) Induction step: Assume that (i) holds. Will execution of an additional step invalidate (i)?

Proof (i) at(A1): condition (i) is false => do not care about signal at(A2): condition (i) is false => do not care about signal at(A3): condition (i) is true => turn == 0, follows from the fact that turn was 0 at(A2) AND the transition from A2->A3 did not change value of turn at(A4): condition (i) is false ==> do not care about turn Now, we consider: at(B1) : no change to turn at(B2) : no change to turn at(B3) : no change to turn at(B4) : changes turn to 0 => Invariant 1 is true

Proof (ii) Same way (please do it if you had trouble with proof of i)

Proof (iii) Induction start trivial. Proof of induction step by contradiction. Assume thread A entered CS (A3) at time t1 Assume thread B entered CS (B3) at time t2, where t2 = t1 + delta --> CONTRADICTION: since we are in A3 signal MUST be 0 (cannot be 0 and 1 at the same time) Assume thread B entered CS (B3) at time t1 Assume thread A entered CS (A3) at time t2, where t2 = t1 + delta --> CONTRADICTION: since we are in B3 signal MUST be 1 (cannot be 0 and 1 at the same time)

Classroom exercise (based on 3 rd variation) class Turn { // 0 : wants to enter exclusive section // 1 : does not want to enter... private volatile int flag = 1; void request() { flag = 0;} void free() { flag = 1; } int read() { return flag; } }

Worker class Worker implements Runnable { private int myid; private Turn mysignal; private Turn othersignal; Worker(int id, Turn t0, Turn t1) { myid = id; mysignal = t0; othersignal = t1; }

Worker public void run() { while (true) { mysignal.request(); while (true) { if (othersignal.read() == 1) break; } // critical section mysignal.free(); }

Master class Synch3b { public static void main(String[] args) { Turn gate0 = new Turn(); Turn gate1 = new Turn(); Thread t1 = new Thread(new Worker(0,gate0, gate1)); Thread t2 = new Thread(new Worker(1,gate1, gate0)); t1.start(); t2.start(); }

Worker public void run() { while (true) { mysignal.request(); while (true) { if (othersignal.read() == 1) break; } // critical section mysignal.free(); }

Worker 0 public void run() { while (true) { A1: A2: s0.request(); A3: while (true) { if (s1.read() == 1) break; } A4: // critical section A5: s0.free(); }

Worker 1 public void run() { while (true) { B1: B2: s1.request(); B3: while (true) { if (s0.read() == 1) break; } B4: // critical section B5: s1.free(); }

Mutual exclusion Show that this solution provides mutual exclusion.

Invariants (i) s0.flag == 0 equivalent to (at(A3) V at(A4) V at(A5)) (ii) s1.flag == 0 equivalent to (at(B3) V at(B4) V at(B5)) (iii) not (at(A4) at(B4)) V

Induction Show with induction that (i), (ii), and (iii) hold. At the start, s0.flag==1 and at(A1) – ok Induction step: assume (i) is true. Consider all possible moves A1  A2 A2  A3 A3  A3 A3  A4 A4  A5 A5  A1 Let’s look at them one by one:

Induction step A1  A2 : no effect on (i) – ok A2  A3 : (i) holds (s0.flag == 0 and at(A3)) – ok A3  A3 : (i) holds, no change to s0.flag, at(A3) – ok A3  A4 : (i) holds, no change to s0.flag, at(A4) – ok A4  A5 : (i) holds, no change to s0.flag, at(A5) – ok A5  A1 : (i) holds, s0.flag == 1 and at(A1) – ok Note that the “– ok“ is based on the observation that no action by Thread Worker 1 will have any effect on s0.flag So (i) holds.

Your turn Show that (ii) holds as well. Sorry if you think this is trivial. You’re right.

Proving (iii) Recall (iii) not (at(A4) at(B4)) Use … induction. At the start, at(A1) and at(B1), so (iii) holds. Induction step: assume (iii) holds and consider possible transitions. Assume at(A4) and consider B3  B4 (while Worker0 remains at A4!) no other transition is relevant or possible But since s0.flag==0 (because of (i)), a transition B3  B4 is not possible, so (iii) remains true. V

… Same argument applies if we start with the assumption at(B4). So no transition will violate (iii). Of course this sketch of a proof depends on the fact that no action by Worker0 (Worker1) will modify any of the state of Worker1 (Worker0).

Any Questions?