1 Synchronization via Transactions. 2 Concurrency Quiz If two threads execute this program concurrently, how many different final values of X are there?

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
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 ©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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Mutual Exclusion: Primitives and Implementation Considerations.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Transaction Processing Lecture ACID 2 phase commit.
1 Transaction Management Database recovery Concurrency control.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Java Thread and Memory Model
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Transactions. Transaction: Informal Definition A transaction is a piece of code that accesses a shared database such that each transaction accesses shared.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
Transaction Processing Concepts Muheet Ahmed Butt.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Transactions.
Transaction Management
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Atomic Operations in Hardware
Atomic Operations in Hardware
Concurrency: Mutual Exclusion and Synchronization
Background and Motivation
Database Security Transactions
Implementing Mutual Exclusion
Chapter 6: Process Synchronization
Implementing Mutual Exclusion
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
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

1 Synchronization via Transactions

2 Concurrency Quiz If two threads execute this program concurrently, how many different final values of X are there? Initially, X == 0. void increment() { int temp = X; temp = temp + 1; X = temp; } void increment() { int temp = X; temp = temp + 1; X = temp; } Thread 1Thread 2 Answer: A.0 B.1 C.2 D.More than 2

3 Schedules/Interleavings Model of concurrent execution Interleave statements from each thread into a single thread If any interleaving yields incorrect results, some synchronization is needed tmp1 = X; tmp1 = tmp1 + 1; X = tmp1; tmp2 = X; tmp2 = tmp2 + 1; X = tmp2; Thread 1 Thread 2 tmp1 = X; tmp2 = X; tmp2 = tmp2 + 1; tmp1 = tmp1 + 1; X = tmp1; X = tmp2; If X==0 initially, X == 1 at the end. WRONG result!

4 Locks fix this with Mutual Exclusion Is mutual exclusion really what we want? Don’t we just want the correct result? Some interleavings may give the correct result. Why can’t we keep these? void increment() { lock.acquire(); int temp = X; temp = temp + 1; X = temp; lock.release(); }

5 Providing atomicity and isolation directly Critical regions need atomicity and isolation Definition: An atomic operation’s effects either all happen or none happen.  Money transfer either debits one acct and credits the other, or no money is transferred Definition: An isolated operation is not affected by concurrent operations.  Partial results are not visible  This allows isolated operations to be put in a single, global order

6 Providing atomicity and isolation directly Implementing atomicity and isolation  Changes to memory are buffered (isolation)  Other processors see old values (isolation)  If something goes wrong (e.g., exception), system rolls back state to start of critical section (atomicity)  When critical region ends, changes become visible all at once (atomicity) Hardware  Processor support for buffering and committing values Software  Runtime system buffers and commits values

7 Transactions Transaction begin (xbegin)  Start of critical region Transaction end (xend)  End of critical region xbegin/xend can be implicit with atomic{} Transaction restart (or abort)  User decides to abort transaction  In Java throwing an exception aborts the transaction atomic { acctA -= 100; acctB += 100; } Transaction to transfer $100 from acctA to acctB.

8 Atomicity and Isolation AcctA starts with $150 Different blocks to update balance  Overnight batch process to read/process/write accounts  Debit $100  Telephone transaction to read/process/write quickly  Debit $90 Isolation guarantees that phone update is not lost  It is allowed by atomicity  In fact, both transactions (in either order) should result in overdraft  AcctA = -$40

9 Atomicity and Isolation AcctA starts with $150 Different blocks to update balance  Overnight batch process to read/process/write accounts  Debit $100  Telephone transaction to read/process/write quickly  Debit $90 Isolation guarantees that phone update is not lost  This is a lost update atomic{ Read AcctA (150) Decrement AcctA by 100 Write AcctA (50) atomic{ AcctA -= 90 } time

10 AcctA == 200 initially. After these two concurrent transactions AcctA==350. What property does that violate?  A. No property is violated  B. Atomicity  C. Isolation  D. Durability atomic{ AcctA += 150 } atomic{ AcctA -= 90 }

11 Atomicity and Isolation Atomicity is hard because  Programs make many small changes.  Most operations are not atomic, like x++;  System must be able to restore state at start of atomic operation  What about actions like dispensing money or firing missles? Isolation is hard because  More concurrency == more performance  …but system must disallow certain interleavings  System usually does not allow visibility of isolated state (hence the term isolated)  Data structures have multiple invariants that dictate constraints on a consistent update Mutual exclusion provides isolation  Most popular parallel programming technique Parallel programming: how to provide isolation (and possibly atomicity)

12 Concrete Syntax for Transactions The concrete syntax of JDASTM. Transaction tx = new Transaction(id); boolean done = false; while(!done) { try { tx.BeginTransaction(); // party on my data structure! done = tx.CommitTransaction(); } catch(AbortException e) { tx.AbortTransaction(); done = false; }

13 Transaction’s System Bookkeeping Transaction A’s read set is R A  Set of objects (addresses) read by transaction A Transaction B’s write set is W B  Set of objects (addresses) written by transaction B Transaction A’s address set is R A UNION W A  Set of objects (addresses) read or written by transaction A atomic { acctA -= 100; acctB = acctA; } Read: acctA Write: acctA, acctB

14 Transactional Safety Conflict serializability – If one transaction writes data read or written by another transaction, then abort one transaction. Recoverability – No transaction that has read data from an uncommitted transaction may commit. atomic { x++; } atomic { load t0, [x] add t0, 1 store t0, [x] } Safe if abort transaction A or B whenever W A ∩ (R B UNION W B ) ≠ EMPTYSET

15 Safety examples Transaction 0 Transaction 1 atomic { load t0, [x] add t0, 1 store t0, [x] } atomic { load t0, [x] add t0, 1 Read: Write: x Read: Write: x x Conflict: Transaction 1 should restart

16 How Isolation Could Be Violated Dirty reads Non-repeatable reads Lost updates

17 Restarting + I/O = Confusion Transactions can restart!  What kind of output should I expect? Transaction tx = new Transaction(id); boolean done = false; while(!done) { try { tx.BeginTransaction(); … System.out.println(“Deja vu all over again”); done = tx.CommitTransaction(); } catch(AbortException e) { tx.AbortTransaction(); done = false; }

18 Reading Uncommitted State What about transactional data read outside a transaction?  Hardware support: strong isolation for all reads  Software: Uncommitted state is visible In your lab, a lane can go from colored to white when a transaction rolls back  The GUI updating thread reads uncommitted state outside of a transaction Why would we want to read data outside of a transaction?  Performance

19 Transactional Communication Conflict serializability is good for keeping transactions out of each other’s address sets Sometimes transactions must communicate  One transaction produces a memory value  Other transaction consumes the memory value Communication is easy to do with busy waiting  Just read the variable that will change  Transaction will restart when its written by other thread

20 Communicating Transactions Transactions busy-wait for each other.  The variable count is in the read set, so any write to count will restart the transaction CokeMachine::Deposit(){ atomic { while (count == n) ; Add coke to the machine; count++; } CokeMachine::Deposit(){ atomic { while (count == n) ; Add coke to the machine; count++; } CokeMachine::Remove(){ atomic { while (count == 0) ; Remove coke from machine; count--; } CokeMachine::Remove(){ atomic { while (count == 0) ; Remove coke from machine; count--; } Class CokeMachine{ … int count = 0; } Class CokeMachine{ … int count = 0; }

21 Tx Communication Without Busy-Waiting Retry: how to block with transactions  Pause transaction  deschedule this thread  Reschedule whenever another transaction conflicts with this transaction Transactional thread is suspended until another thread modifies data it read  E.g., count variable

22 Retry: Communication Without Busy-Wait Scheduler and runtime cooperate to monitor address sets of transactions that are descheduled CokeMachine::Deposit(){ atomic { if(count == n) {retry; } Add coke to the machine; count++; } CokeMachine::Deposit(){ atomic { if(count == n) {retry; } Add coke to the machine; count++; } CokeMachine::Remove(){ atomic { if(count == 0) { retry; } Remove coke from machine; count--; } CokeMachine::Remove(){ atomic { if(count == 0) { retry; } Remove coke from machine; count--; } Class CokeMachine{ … int count = 0; } Class CokeMachine{ … int count = 0; }

23 Comparing Transactions and Monitors CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.notify(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.notify(); lock  release(); } Which is better? A. Transactions B. Monitors CokeMachine::Deposit(){ atomic { if(count == n) {retry; } Add coke to the machine; count++; } CokeMachine::Deposit(){ atomic { if(count == n) {retry; } Add coke to the machine; count++; } CokeMachine::Remove(){ atomic { if(count == 0) {retry; } Remove coke from machine; count--; } CokeMachine::Remove(){ atomic { if(count == 0) {retry; } Remove coke from machine; count--; }

24 Load linked/Store Conditional Load linked/store conditional.  Idea is to let user load a data item, compute, then store back and if “no one else” (i.e., another processor or an I/O device) has touched that memory location, then allow the store since the read-modify-write was atomic. tmp = r1 = [addr]; // Load linked into r1 do_whatever (some restrictions); // Store conditional from r2 if(tmp == [addr]) then [addr] = r2; r2 = 1; else r2 = 0;  Restrictions on compute: no memory accesses, limited number of instructions, no interrupts or exceptions. Hardware queue locks

25 Load linked/Store Conditional All of these events, if they happen between the load linked and the store conditional will cause the store conditional to fail. EXCEPT which?  A. Breakpoint instruction  B. Branch instruction  C. External write to loaded memory address  D. Return from exception instruction