Concepts of Concurrent Computation



Similar documents
CS Fall 2008 Homework 2 Solution Due September 23, 11:59PM

Synchronization. Todd C. Mowry CS 740 November 24, Topics. Locks Barriers

How To Make A Correct Multiprocess Program Execute Correctly On A Multiprocedor

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Lecture 9 verifying temporal logic

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

The Bakery Algorithm: Yet Another. Specication and Verication. Dean Rosenzweig z. Abstract. In a meeting at Schloss Dagstuhl in June 1993, Uri Abraham

Bounded Cost Algorithms for Multivalued Consensus Using Binary Consensus Instances

14.1 Rent-or-buy problem

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

Monitors, Java, Threads and Processes

CS 103X: Discrete Structures Homework Assignment 3 Solutions

First-class User Level Threads

Mutual Exclusion using Monitors

Outline of this lecture G52CON: Concepts of Concurrency

Concurrent Programming

Distributed Databases

Tasks Schedule Analysis in RTAI/Linux-GPL

Today s Agenda. Automata and Logic. Quiz 4 Temporal Logic. Introduction Buchi Automata Linear Time Logic Summary

In This Lecture. More Concurrency. Deadlocks. Precedence/Wait-For Graphs. Example. Example

Temporal Logics. Computation Tree Logic

Comp 204: Computer Systems and Their Implementation. Lecture 12: Scheduling Algorithms cont d

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

An Implementation Of Multiprocessor Linux

A Survey of Parallel Processing in Linux

Fundamentals of Software Engineering

Software Engineering using Formal Methods

Road Map. Scheduling. Types of Scheduling. Scheduling. CPU Scheduling. Job Scheduling. Dickinson College Computer Science 354 Spring 2010.

Overview of Presentation. (Greek to English dictionary) Different systems have different goals. What should CPU scheduling optimize?

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

Paper 3F6: Software Engineering and Systems Distributed Systems Design Solutions to Examples Paper 3

Introduction to SPIN. Acknowledgments. Parts of the slides are based on an earlier lecture by Radu Iosif, Verimag. Ralf Huuck. Features PROMELA/SPIN

(Pessimistic) Timestamp Ordering. Rules for read and write Operations. Pessimistic Timestamp Ordering. Write Operations and Timestamps

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

The Course.

SYSTEM ecos Embedded Configurable Operating System

Chapter 6, The Operating System Machine Level

File Management. Chapter 12

Textbook and References

Introduction. What is an Operating System?

Victor Shoup Avi Rubin. Abstract

Concurrency Control. Module 6, Lectures 1 and 2

Loop Invariants and Binary Search

Formal verification of contracts for synchronous software components using NuSMV

Conallel Data Structures: A Practical Paper


Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Transactional Support for SDN Control Planes "

Monitors (Deprecated)

OS OBJECTIVE QUESTIONS

Introduction to Promela and SPIN. LACL, Université Paris 12

CS4410 Final Exam : Solution set. while(ticket[k] && (ticket[k],k) < (ticket[i],i))

W4118 Operating Systems. Instructor: Junfeng Yang

CS11 Java. Fall Lecture 7

Database Replication Techniques: a Three Parameter Classification

Model Checking based Software Verification

Implementing AUTOSAR Scheduling and Resource Management on an Embedded SMT Processor

U.C. Berkeley CS276: Cryptography Handout 0.1 Luca Trevisan January, Notes on Algebra

Parallel Computing in Python: multiprocessing. Konrad HINSEN Centre de Biophysique Moléculaire (Orléans) and Synchrotron Soleil (St Aubin)

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Predictive modeling for software transactional memory

Operating Systems 4 th Class

Why? A central concept in Computer Science. Algorithms are ubiquitous.

1 Definition of a Turing machine

Database Replication with Oracle 11g and MS SQL Server 2008

PERFORMANCE TESTING CONCURRENT ACCESS ISSUE AND POSSIBLE SOLUTIONS A CLASSIC CASE OF PRODUCER-CONSUMER

Embedded Software Architecture

CPU Scheduling. CPU Scheduling

1 An application in BPC: a Web-Server

Brewer s Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services

Last Class: Semaphores

Chapter 1 FUNDAMENTALS OF OPERATING SYSTEM

System model. Section 7.1. Operating Systems: Deadlock p. 1/15

Concurrency Control. Chapter 17. Comp 521 Files and Databases Fall

Facing the Challenges for Real-Time Software Development on Multi-Cores

About the inverse football pool problem for 9 games 1

Chapter 6 Concurrent Programming

A Systematic Approach. to Parallel Program Verication. Tadao TAKAOKA. Department of Computer Science. Ibaraki University. Hitachi, Ibaraki 316, JAPAN

6.2 Permutations continued

Synchronization in. Distributed Systems. Cooperation and Coordination in. Distributed Systems. Kinds of Synchronization.

Analysis of Algorithms, I

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Linux Scheduler. Linux Scheduler

Pronto: High Availability for Standard Off-the-shelf Databases

Lightweight Locking for Main Memory Database Systems

Using semantic properties for real time scheduling

CPU Scheduling 101. The CPU scheduler makes a sequence of moves that determines the interleaving of threads.

Chapter 12: Multiprocessor Architectures. Lesson 01: Performance characteristics of Multiprocessor Architectures and Speedup

Signature-Free Asynchronous Binary Byzantine Consensus with t < n/3, O(n 2 ) Messages, and O(1) Expected Time

Formal Verification of Software

Efficient and Practical Non-Blocking Data Structures

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors

Testing LTL Formula Translation into Büchi Automata

OPERATING SYSTEMS DEADLOCKS

Shared Address Space Computing: Programming

Formal Verification by Model Checking

Introduction to Software Verification

arxiv: v1 [math.pr] 5 Dec 2011

Chapter 7: Deadlocks!

Transcription:

Chair of Software Engineering Concepts of Concurrent Computation Bertrand Meyer Sebastian Nanz Lecture 3: Synchronization Algorithms

Today's lecture In this lecture you will learn about: the mutual exclusion problem, a common framework for evaluating solutions to the problem of exclusive resource access solutions to the mutual exclusion problem (Peterson's algorithm, the Bakery algorithm) and their properties ways of proving properties for concurrent programs 2

Chair of Software Engineering The mutual exclusion problem

Mutual exclusion As discussed in the last lecture, race conditions can corrupt the result of a concurrent computation if processes are not properly synchronized We want to develop techniques for ensuring mutual exclusion Mutual exclusion: a form of synchronization to avoid the simultaneous use of a shared resource To identify the program parts that need attention, we introduce the notion of a critical section Critical section: part of a program that accesses a shared resource. 4

The mutual exclusion problem (1) We assume to have n processes of the following form: while true loop entry protocol critical section exit protocol non-critical section end Design the entry and exit protocols to ensure: Mutual exclusion: At any time, at most one process may be in its critical section Freedom from deadlock: If two or more processes are trying to enter their critical sections, one of them will eventually succeed Freedom from starvation: If a process is trying to enter its critical section, it will eventually succeed 5

The mutual exclusion problem (2) while true loop entry protocol critical section exit protocol non-critical section end Further important conditions: Processes can communicate with each other only via atomic read and write operations If a process enters its critical section, it will eventually exit from it A process may loop forever or terminate while being in its non-critical section The memory locations accessed by the protocols may not be accessed outside of them 6

Locks Synchronization mechanisms based on the ideas of entryand exit-protocols are called locks They can typically be implemented as a pair of functions: lock do entry protocol end unlock do exit protocol end 7

Busy waiting We will use the following statement in pseudo code await b which is equivalent to while not b loop end This type of waiting is called busy waiting or "spinning" Busy waiting is inefficient on multitasking systems Busy waiting makes sense if waiting times are typically so short that a context switch would be more expensive Therefore spin locks (locks using busy waiting) are often used in operating system kernels 8

Towards a solution The mutual exclusion problem is quite tricky: in the 1960's many incorrect solutions were published We will work along a series of failing attempts until establishing a solution We will start with trying to find a solution for only two processes 9

Solution attempt I First idea: use two variables enter1 and enter2; if enteri is true, it means that process P i intends to enter the critical section enter1 := false enter2 := false P1 1 2 3 4 5 while true loop await not enter2 enter1 := true critical section enter1 := false non-critical section end P2 1 2 3 4 5 while true loop await not enter1 enter2 := true critical section enter2 := false non-critical section end 10

Solution attempt I is incorrect The solution attempt fails to ensure mutual exclusion The two processes can end up in their critical sections at the same time, as demonstrated by the following execution sequence P2 1 await not enter1 P1 1 await not enter2 P1 2 enter1 := true P2 2 enter2 := true P2 3 critical section P1 3 critical section 11

Solution attempt II When analyzing the failure, we see that we set the variable enteri only after the await statement, which is guarding the critical section Second idea: switch these statements around enter1 := false enter2 := false P1 P2 1 2 3 4 5 while true loop enter1 := true await not enter2 critical section enter1 := false non-critical section end 1 2 3 4 5 while true loop enter2 := true await not enter1 critical section enter2 := false non-critical section end 12

Solution attempt II is incorrect The solution provides mutual exclusion However, the processes can deadlock: P1 1 enter1 := true P2 1 enter2 := true P2 2 await not enter1 P1 2 await not enter2 13

Solution attempt III Third idea: let's try something new, namely a single variable turn that has value i if it's P i 's turn to enter the critical section turn := 1 or turn := 2 P1 1 2 3 4 while true loop await turn = 1 critical section turn := 2 non-critical section end P2 1 2 3 4 while true loop await turn = 2 critical section turn := 1 non-critical section end 14

Proving correctness of solution attempt III Solution attempt III looks good to us, let's try to prove it correct Draw the related transition system; states are labeled with triples (i, j, k): program pointer values P1 i and P2 j, and value of the variable turn = k. 15

Proving correctness of solution attempt III Solution attempt III satisfies mutual exclusion Proof. Mutual exclusion expressed as LTL formula: G (P1 2 P2 2) Easy to see that this formula holds, as there are no states of the form (2, 2, k). Solution attempt III is deadlock-free Proof. Deadlock-freedom expressed as LTL formula: G ((P1 1 P2 1) -> F (P1 2 P2 2)) We have to examine the states (1, 1, 1) and (1, 1, 2); in both cases, one of the processes is enabled to enter its critical section. 16

Another setback Let's check starvation-freedom Expressed as LTL formula: for i = 1, 2 G (P i 1 -> F (P i 2)) Recall: processes may terminate in non-critical section A problematic case is (1, 4, 2): variable turn = 2, P1 trying to enter critical section (although not its turn), P2 in noncritical section If P2 terminates, turn will never be set to 1: P1 will starve 17

Chair of Software Engineering Peterson's algorithm

Peterson's algorithm (for two processes) Peterson s algorithm combines the ideas of solution attempts II and III If both processes have set their enter-flag to true, then the value of turn decides who may enter the critical section enter1 := false enter2 := false turn := 1 or turn := 2 P1 P2 1 2 3 4 5 6 while true loop enter1 := true turn := 2 await not enter2 or turn = 1 critical section enter1 := false non-critical section end 1 2 3 4 5 6 while true loop enter2 := true turn := 1 await not enter1 or turn = 2 critical section enter2 := false non-critical section end 19

Peterson's algorithm: mutual exclusion Peterson s algorithm satisfies mutual exclusion Proof. Assume that both P1 and P2 are in their critical section and that P1 entered before P2 When P1 entered the critical section we have enter1 = true, and P2 must thus have seen turn = 2 upon entering its critical section P2 could not have executed line 2 after P1 entered, as this sets turn = 1 and would have excluded P2, as P1 does not change turn while being in the critical section However, P2 could not have executed line 2 before P1 entered either because then P1 would have seen enter2 = true and turn = 1, although P2 should have seen turn = 2 Contradiction 20

Peterson's algorithm: starvation-freedom Peterson s algorithm is starvation-free Proof. Assume P1 is forced to wait in the entry protocol forever P2 can eventually do only one of three actions: 1. Be in its non-critical section: then enter2 is false, thus allowing P1 to enter. 2. Wait forever in its entry protocol: impossible because turn cannot be both 1 and 2 3. Repeatedly cycle through its code: then P2 will set turn to 1 at some point and never change it back 21

Peterson's algorithm for n processes Up until now, we have only seen a solution to the mutual exclusion problem for two processes; the problem is however posed for n processes Peterson's algorithm has a direct generalization enter[1] := 0;...; enter[n] := 0 turn[1] := 0;...; turn[n 1] := 0 P i 1 2 3 4 5 6 7 for j = 1 to n 1 do enter[i] := j turn[j] := i await (for all k!= i : enter[k] < j) or turn[j]!= i end critical section enter[i] := 0 non-critical section 22

Peterson's algorithm for n processes Every process has to go through n 1 stages to reach the critical section: variable j indicates the stage enter[i]: stage the process P i is currently in turn[j]: which process entered stage j last Waiting: P i waits if there are still processes at higher stages, or if there are processes at the same stage unless P i is no longer the last process to have entered this stage Idea for mutual exclusion proof: at most n j processes can have passed stage j => at most n (n - 1) = 1 processes can be in the critical section max. n processes max. n-1 processes... 1 2... max. 3 Stage: n 2 max. 2 CS n - 1 23

Chair of Software Engineering The Bakery algorithm

Fairness again Freedom from starvation still allows that processes may enter their critical sections before a certain, already waiting process is allowed access We study an algorithm that has very strong fairness guarantees 25

Bounded waiting The following definitions help analyze the fairness with respect to process waiting in mutual exclusion algorithms Bounded waiting: If a process is trying to enter its critical section, then there is a bound on the number of times any other process can enter its critical section before the given process does so. r-bounded waiting: If a process tries to enter its critical section then it will be able to enter before any other process is able to enter its critical section r + 1 times. This means: bounded waiting = there exists an r such that the waiting is r-bounded First-come-first-served: 0-bounded waiting 26

Relating the definitions starvation-freedom deadlock-freedom starvation-freedom bounded waiting bounded waiting starvation-freedom bounded waiting + deadlock-freedom starvation-freedom deadlock-freedom If two or more processes are trying to enter their critical sections, one of them will eventually succeed. starvation-freedom If a process is trying to enter its critical section, it will eventually succeed. bounded waiting If a process is trying to enter its critical section, then there is a bound on the number of times any other process can enter its critical section before the given process does so. 27

Peterson's algorithm: no bounded waiting Assume a scenario with three competing processes P1 2 enter[1] := 1 P2 2 enter[2] := 1 P2 3 turn[1] := 2 P3 2 enter[3] := 1 P3 3 turn[1] := 3 turn[1]!= 2: P2 can proceed P2... enters + leaves critical section P2 2 enter[2] := 1 P2 3 turn[1] := 2 turn[1]!= 3: P3 can proceed P3... enters + leaves critical section... P3 can unblock P2 etc. P2 and P3 can overtake P1 unboundedly often Still P1 is not starved as it eventually (fairness) executes turn[1] := 1 and can proceed into the critical section 28

The Bakery algorithm: first attempt Idea: ticket systems for customers, at any turn the customer with the lowest number will be served number[i]: ticket number drawn by a process P i Waiting: until P i has the lowest number currently drawn number[1] := 0;...; number[n] := 0 P i 1 2 3 4 5 6 number[i] := 1 + max(number[1],..., number[n]) for all j!= i do await number[j] = 0 or number[i] < number[j] end critical section number[i] := 0 non-critical section Where is the problem? 29

Problem with the first attempt Line 1 may not be executed atomically Hence two processes may get the same ticket number Then a deadlock can happen in line 3, as none of the processes' ticket numbers is less than the other 30

A suggestion for a fix Replace the comparison number[i] < number[j] by (number[i], i) < (number[j], j) The "less than" relation is defined in this case as (a, b) < (c, d) if (a < c) or ((a = c) and (b < d)) Idea: if two ticket numbers turn out to be the same, the process with the lower identifier gets precedence 31

The fix doesn't work Unfortunately, with the fix we no longer have mutual exclusion: P1 and P2 both compute the current maximum as 0 P2 assigns itself ticket number 1 (number[2] := 1) and proceeds into critical section P1 assigns itself ticket number 1 (number[1] := 1) and proceeds into critical section, because (number[1], 1) < (number[2], 2) 32

The bakery algorithm Finally, we indicate with a flag if a process is currently calculating its ticket number number[1] := 0;...; number[n] := 0 choosing[1] := false,..., choosing[n] := false P i 1 2 3 4 5 6 7 8 9 choosing[i] := true number[i] := 1 + max(number[1],..., number[n]) choosing[i] := false for all j!= i do await choosing[j] = false await number[j] = 0 or (number[i], i) < (number[j], j) end critical section number[i] := 0 non-critical section doorway bakery 33

Two lemmas Lemma 1. If processes P i and P k are in the bakery and P i entered the bakery before P k entered the doorway, then number[i] < number[k]. Lemma 2. If process P i is in its critical section and process P k is in the bakery then (number[i], i) < (number[k], k). For P i choosing[k] = false when reading it in line 5 If we have the situation of Lemma 1, we are finished. If P k had left the doorway before P i read number[k], it was reading its current value. Since process P i went on into the critical section, it must have found (number[i], i) < (number[k], k). 34

Correctness of the bakery algorithm The Bakery algorithm satisfies mutual exclusion. Proof. Follows from Lemma 2. The Bakery algorithm is deadlock-free. Proof. Some waiting process P i has the minimum value of (number[i], i) among all the processes in the bakery. This process must eventually complete the for loop and enter the critical section. The Bakery algorithm is first-come-first-served. Proof. Follows from Lemmas 1 and 2. 35

Unbounded ticket numbers Drawback of the Bakery algorithm: values of the ticket numbers can grow unboundedly Assume P1 gets ticket number 1 and proceeds to its critical section. Then process P2 gets ticket number 2, lets P1 exit from its critical section and enters its own critical section. As P1 tries to re-enter its critical section it draws ticket number 3. In this manner two processes could alternatingly draw ticket numbers until the maximum size of an integer on the system is reached. 36

Space bounds for synchronization algorithms Size and number of shared memory locations is an important measure to compare synchronization algorithms For Peterson s algorithm, we count 2n 1 registers (bounded by n), and in the case of the Bakery algorithm 2n registers (unbounded in size) Large overhead: can we do better? One can prove in general a lower bound: mutual exclusion problem for n processes satisfying mutual exclusion and global progress needs to use n shared one-bit registers The bound is tight (Lamport's one bit algorithm) 37

Non-atomic memory access The mutual exclusion problem makes the assumption that memory accesses are executed atomically This might not be a valid assumption on multiprocessor systems, leading to inconsistencies The Bakery algorithm can help here as well: each memory location is only written by a single process, hence conflicting write operations cannot occur 38

Other atomic primitives (1) Having only atomic read and write to implement locks makes efficient implementation difficult Where available, locks can be built from more complex atomic primitives test-and-set (x, value) do temp := *x *x := value result := temp end Note that x in this pseudo-code is treated as a reference 39

Other atomic primitives (2) Using more powerful primitives, concise solutions to the mutual exclusion problem can be obtained: b := false P i 1 2 3 4 await not test-and-set(b, true) critical section b := false non-critical section 40

Other atomic primitives (3) fetch-and-add (x, value) do temp := *x *x := *x + value result := temp end compare-and-swap (x, old, new) do if *x = old then *x := new; result := true else end end result := false 41