Chapter 6 Process Synchronization: Part 2. Problems with Semaphores Correct use of semaphore operations may not be easy: –Suppose semaphore variable called.

Slides:



Advertisements
Similar presentations
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
Advertisements

Process Synchronization CS 3100 Process Synchronizatiion1.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6: Process Synchronization.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Critical Regions § 7.6 Although semaphores provide a convenient and effective mechanism for process synchronization, their incorrect use can still result.
6: Process Synchonization II 1 PROCESS SYNCHRONIZATION II THE BOUNDED BUFFER ( PRODUCER / CONSUMER ) PROBLEM: This is the same producer / consumer problem.
Modified from Silberschatz, Galvin and Gagne & Stallings Lecture 12 Chapter 6: Process Synchronization (cont)
Massively Distributed Database Systems - Transaction management Spring 2014 Ki-Joune Li Pusan National University.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Solution to Dining Philosophers. Each philosopher I invokes the operations pickup() and putdown() in the following sequence: dp.pickup(i) EAT dp.putdown(i)
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Concurrency Control Lectured by, Jesmin Akhter, Assistant professor, IIT, JU.
Chapter 15 Concurrency Control Yonsei University 1 st Semester, 2015 Sanghyun Park.
Concurrency Control in Database Operating Systems.
UNIT III: Concurrency Process SynchronizationSynchronization The Critical-Section problemCritical-Section problem Peterson’s Solution Synchronization HardwareHardware.
Operating System Concepts 7th Edition Abraham SilBerschatz Peter Baer Galvin Greg Gagne Prerequisite: CSE212.
Synchronization. Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization Adapted to COP4610 by Robert van Engelen.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization Background The Critical-Section.
7c.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Module 7c: Atomicity Atomic Transactions Log-based Recovery Checkpoints Concurrent.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Outline n Background.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Process Synchronization Monitors Simulation of Monitors Checkpoint and Recovery Monitors Simulation of Monitors Checkpoint and Recovery Topics:
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Synchronization. 6.2Operating System Principles Module 6: Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization.
Concurrency Control Introduction Lock-Based Protocols
1 Concurrency control lock-base protocols timestamp-based protocols validation-based protocols Ioan Despi.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 6 Operating Systems.
Chapter 6: Process Synchronization. 6.2 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2CSCI 380 – Operating Systems Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Timestamp-based Concurrency Control
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Deadlock and Starvation
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization – Part 3
Deadlock and Starvation
Advanced Operating Systems - Fall 2009 Lecture 8 – Wednesday February 4, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours: M,
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Module 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization
Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Presentation transcript:

Chapter 6 Process Synchronization: Part 2

Problems with Semaphores Correct use of semaphore operations may not be easy: –Suppose semaphore variable called mutex is intialized to 1 What happens in the following? –signal (mutex) … CS … wait (mutex) –wait (mutex) … CS … wait (mutex) Using wait and signal in a wrong sequence  synchronization problems Omitting wait or signal  synchronization problems

Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time A programmer does not need to manually write the entire synchronization code

Monitors monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } … }

Schematic view of a Monitor

Condition Variables Condition variable –Synchronization mechanism working with a monitor Two operations on a condition variable: –condition x, y; //declare condition variables –x.wait (): a process that invokes the operation is suspended. –x.signal (): resumes one of the processes (if any) that invoked x.wait ()

Monitor with Condition Variables

Solution to Dining Philosophers monitor DP { enum {THINKING, HUNGRY, EATING} state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); }

Solution to Dining Philosophers (cont) void test (int i) { if (state[i] == HUNGRY && state[(i + 4) % 5] != EATING && state[(i + 1) % 5] != EATING) { state[i] = EATING ; self[i].signal () ; } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } } // end of monitor

Solution to Dining Philosophers (cont) Each philosopher i invokes the operations pickup() and putdown() in the following sequence: DP.pickup (i) EAT DP.putdown (i)

How to implement a monitor? Use semaphores Supported by a programming language, e.g., Java monitors Public class simpleClass { Public synchronized void safeMethod() { /* Implementation of safeMethod() */ } SimpleClass sc = new SimpleClass(); Invoking sc.safeMethod() requires to own the lock on the object instance sc

Example: Pthreads Synchronization Pthread: POSIX thread –Portable Operating System Interface for uniX: IEEE API standard (IEEE 1003) Pthreads API is OS-independent It provides: –mutex –semaphore –condition variables –read-write locks –spin locks

Atomic Transactions System Model Log-based Recovery Checkpoints Concurrent Atomic Transactions

System Model Assures that operations happen as a single logical unit of work, in its entirety or not at all Related to field of database systems Challenge is assuring atomicity despite computer system failures Transaction –collection of instructions or operations that performs single logical function concerned with changes to stable storage, e.g., disk –Transaction is series of read and write operations –Terminated by commit (transaction successful) or abort (transaction failed) operation –Aborted transaction must be rolled back to undo any changes it performed

Log-Based Recovery Record information about all modifications by a transaction to stable storage Most common is write-ahead logging –Each log record describes a write operation: Transaction name Data item name Old value New value – written to log when transaction T i starts – written when T i commits Log entry must reach stable storage before operation on data occurs

Log-Based Recovery Algorithm Using the log, system can handle any volatile memory errors –Undo(T i ) restores value of all data updated by T i –Redo(T i ) sets values of all data in transaction T i to new values Undo(T i ) and redo(T i ) must be idempotent –Multiple executions must have the same result as one execution If system fails, restore state of all updated data via log –If log contains without, undo(T i ) –If log contains and, redo(T i )

Checkpoints Log could become long –Recovery could take long as a result Checkpoints shorten log and recovery time. Checkpoint scheme: 1.Output all log records currently in volatile storage to stable storage 2.Output all modified data from volatile to stable storage 3.Output a log record to the log on stable storage Upon recovery, scan the log backwards to find the first checkpoint –If appears before the checkpoint, any updates by performed Ti is written to the stable storage due to the checkpointing –Only redo or undo the transactions that started after the checkpoint  Make recovery faster!

Concurrent Transactions and Serializability To improve throughput & response time, execute transactions concurrently Serializability: Correctness criterion –Concurrent execution of transactions must be equal to one serial execution order

Serial Schedule 1: T 0 then T 1

Schedule 2: Concurrent Serializable Schedule

Locking Protocol Ensure serializability by associating lock with each data item –Follow locking protocol for access control Locks –Shared – T i has shared-mode lock (S) on item Q, T i can read Q but not write Q –Exclusive – Ti has exclusive-mode lock (X) on Q, T i can read and write Q Require every transaction on item Q acquire appropriate lock If lock already held, a new conflicting request have to wait

Two-Phase Locking (2PL) Protocol Generally ensures conflict serializability Each transaction issues lock and unlock requests in two phases –Growing phase: obtaining locks –Shrinking: releasing locks Does not prevent deadlock

Timestamp-based Protocols Timestamp-ordering Transaction T i associated with timestamp TS(T i ) before T k starts –TS(T i ) < TS(T k ) if Ti entered system before T k –TS can be generated from system clock or as logical counter incremented at each entry of transaction Timestamps determine serializability order –If TS(T i ) < TS(T k ), system must ensure that a produced schedule is equivalent to a serial schedule where T i appears before T k

Timestamp-based Protocol Implementation Data item Q gets two timestamps –W-timestamp(Q) – largest timestamp of any transaction that executed write(Q) successfully –R-timestamp(Q) – largest timestamp of successful read(Q) –Updated whenever read(Q) or write(Q) executed Timestamp-ordering protocol assures any conflicting read and write executed in timestamp order Suppose Ti executes read(Q) –If TS(T i ) < W-timestamp(Q), Ti needs to read value of Q that was already overwritten read operation rejected and T i rolled back –If TS(T i ) ≥ W-timestamp(Q) read executed, R-timestamp(Q) set to max(R- timestamp(Q), TS(T i ))

Timestamp-ordering Protocol Suppose Ti executes write(Q) –If TS(T i ) < R-timestamp(Q), value Q produced by T i was needed previously and T i assumed it would never be produced Write operation rejected, T i rolled back –If TS(T i ) < W-timestamp(Q), T i attempting to write obsolete value of Q Write operation rejected and T i rolled back –Otherwise, write executed Any rolled back transaction T i is assigned new timestamp and restarted Timestamp ordering ensures conflict serializability and freedom from deadlock

Schedule Possible Under Timestamp Protocol Timestamp of T2 < timestamp of T3 OK in timestamp ordering, but not allowed in 2PL

End of Chapter 6