Critical Regions

67
1 Critical Regions Although semaphores provide a convenient and effective mechanism for process synchronization, their incorrect use can still result in timing errors that are difficult to detect. Examples: § 7.6 signal(mutex); critical section wait(mutex); Several processes may be executing in their critical section simultaneously. wait(mutex); critical section wait(mutex); A deadlock will occur. A process omits the wait(mutex), or the signal(mutex), or both Either mutual exclusion is violated or a deadlock will occur.

description

Critical Regions. § 7.6. Although semaphores provide a convenient and effective mechanism for process synchronization, their incorrect use can still result in timing errors that are difficult to detect. Examples:. A process omits the wait(mutex) , or the signal(mutex) , or both - PowerPoint PPT Presentation

Transcript of Critical Regions

Page 1: Critical Regions

1

Critical Regions• Although semaphores provide a convenient and

effective mechanism for process synchronization, their incorrect use can still result in timing errors that are difficult to detect.

• Examples:

§ 7.6

signal(mutex);critical sectionwait(mutex);

Several processes may be executing in their critical section simultaneously.

signal(mutex);critical sectionwait(mutex);

Several processes may be executing in their critical section simultaneously.

wait(mutex);critical sectionwait(mutex);

A deadlock will occur.

wait(mutex);critical sectionwait(mutex);

A deadlock will occur.

A process omits the wait(mutex), or the signal(mutex), or both

Either mutual exclusion is violated or a deadlock will occur.

A process omits the wait(mutex), or the signal(mutex), or both

Either mutual exclusion is violated or a deadlock will occur.

Page 2: Critical Regions

2

Critical Regions• Although semaphores provide a convenient and

effective mechanism for process synchronization, their incorrect use can still result in timing errors that are difficult to detect.

• Examples:

§ 7.6

signal(mutex);critical sectionwait(mutex);

Several processes may be executing in their critical section simultaneously.

signal(mutex);critical sectionwait(mutex);

Several processes may be executing in their critical section simultaneously.

wait(mutex);critical sectionwait(mutex);

A deadlock will occur.

wait(mutex);critical sectionwait(mutex);

A deadlock will occur.

A process omits the wait(mutex), or the signal(mutex), or both

Either mutual exclusion is violated or a deadlock will occur.

A process omits the wait(mutex), or the signal(mutex), or both

Either mutual exclusion is violated or a deadlock will occur.

Multiple Choices Question:( ) What kind of problem can happen if

more than one thread work on a semaphore in the following sequence? signal(mutex);

criticalSection(); wait(mutex);

(a) starvation (b) deadlock (c) blocking (d) not synchronizing (e) violate mutual exclusion

Answer: e

Page 3: Critical Regions

3

Critical Region

• High-level synchronization construct• A shared variable v of type T, is declared as:

v:shared T;• Variable v accessed only inside statement

region v when (B) S;where B is a boolean expression.

• While statement S is being executed, no other process can access variable v.

Page 4: Critical Regions

4

Critical Region

• Regions referring to the same shared variable exclude each other in time.

• When a process tries to execute the region statement, the Boolean expression B is evaluated. If B is true, statement S is executed. If it is false, the process is delayed until B becomes true and no other process is in the region associated with v.

Page 5: Critical Regions

5

Critical Region

• Example: two statementsregion v when (true) S1;region v when (true) S2;

are executed concurrently in distinct sequential processes, the result will be equivalent to the sequential execution “S1 followed by S2” or “S2 followed by S1.”

Page 6: Critical Regions

6

Critical Region

• The critical-region construct guards against certain simple errors associated with the semaphore solution to the critical-section problem that may be made by a programmer.

• However, it does not necessarily eliminate all synchronization errors; rather, it reduces their number.

• Can be used to solve certain general synchronization problems.

Page 7: Critical Regions

7

Example – bounded buffer

• Shared data:struct buffer {item pool[n];int count, in, out;}

• Producer process inserts nextp into the shared buffer

region buffer when( count < n) {pool[in] = nextp;in = (in+1) % n;count++;

}

Page 8: Critical Regions

8

Example – bounded buffer

• Consumer process removes an item from the shared buffer and puts it in nextc

region buffer when (count > 0) {nextc = pool[out];

out = (out+1) % n;count--;}

Page 9: Critical Regions

9

Implement the conditional critical region (Skip)

Page 10: Critical Regions

10

• High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes.

• A monitor presents a set of programmer-defined operations that are provided mutual exclusion within the monitor.

• A monitor type consists of declarations of variables whose values define the state of an instance of the type, as well as the bodies of procedures or functions that implement operations on the type.

Monitors § 7.7

Page 11: Critical Regions

11

monitor monitor-name{

shared variable declarationsprocedure body P1 (…) {

. . .}procedure body P2 (…) {

. . .} procedure body Pn (…) {

. . .} {

initialization code}

}

Monitor Syntax

Page 12: Critical Regions

12

Condition Variables

• Encapsulation: limits access to the local variables by only the local procedures.

• The monitor construct prohibits concurrent access to all procedures defined within the monitor.

• Only one process may be active within the monitor at a time.

• Synchronization is built into the monitor type, the programmer does not need to code it explicitly.

• Special operations wait and signal can be invoked on the variables of type condition. condition x, y;

• A process that invokes x.wait is suspended until another process invokes x.signal

Page 13: Critical Regions

13

Condition Variables

• Encapsulation: limits access to the local variables by only the local procedures.

• The monitor construct prohibits concurrent access to all procedures defined within the monitor.

• Only one process may be active within the monitor at a time.

• Synchronization is built into the monitor type, the programmer does not need to code it explicitly.

• Special operations wait and signal can be invoked on the variables of type condition. condition x, y;

• A process that invokes x.wait is suspended until another process invokes x.signal

True-False Question:

( ) Although there maybe several processes inside the monitor at the same time, there can only be one process with the state of active at a time.

Answer: ○

Page 14: Critical Regions

14

Schematic View of a Monitor

Page 15: Critical Regions

15

Condition Variables

• Condition variable can only be used with the operations wait and signal.– The operation

x.wait();means that the process invoking this operation is suspended until another process invokes

x.signal();– The x.signal operation resumes exactly one

suspended process. If no process is suspended, then the signal operation has no effect.

Contrast this operation with the signal operation

associated with semaphores, which always affects the state of the semaphore.

Page 16: Critical Regions

16

Monitor with condition variables

Page 17: Critical Regions

17

Two Possibilities

• When x.signal() operation is invoked by a process P, there is a suspended process Q associated with condition x.1. P either waits until Q leaves the monitor, or waits

for another condition.

2. Q either waits until P leaves the monitor, or waits for another condition.

• Concurrent C: when process P executes the signal operation, process Q is immediately resumed.

Advocated by Hoare

More reasonable

since P was already executing in the

monitor.

“logical” condition for

which Q was waiting may no longer hold by

the time Q is resumed.

Page 18: Critical Regions

18

Solution to Dining Philosophers

monitor dp {

enum {thinking, hungry, eating} state[5];condition self[5];void pickup(int i) // following slidesvoid putdown(int i) // following slidesvoid test(int i) // following slidesvoid init() {

for (int i = 0; i < 5; i++)state[i] = thinking;

}}

philosopher i can delay herself when she is hungry, but is unable to obtain the chopsticks she

needs.

Page 19: Critical Regions

19

pickUp() Procedure

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 neighborstest((i+4) % 5);test((i+1) % 5);

}

Each philosopher, before starting to eat,

must invoke the operation pickup().

May result in the suspension of the

philosopher thread.

Page 20: Critical Regions

20

test() Procedure

void test(int i) {if ( (state[(i + 4) % 5] != eating) && (state[i] == hungry) && (state[(i + 1) % 5] != eating)) {

state[i] = eating;self[i].signal();

}}

Release self[i] so that the thread can proceed.

Philosopher i can set the variable state[i] = eating only if her two neighbors

are not eating.

Page 21: Critical Regions

21

Solution to Dining Philosophers

• Philosopher i must invoke the operations pickup and putdown in the following sequence:

dp.pickUp(i);...eat...

dp.putDown(i);

• This solution ensures that no two neighbors are eating simultaneously, and no deadlocks will occur.

• However, it is possible for a philosopher to starve to death.

Page 22: Critical Regions

22

Implement monitorusing semaphore

• Variables semaphore mutex; // (initially = 1)semaphore next; // (initially = 0)int next_count = 0;

• Each external procedure F will be replaced bywait(mutex); … body of F; …if (next_count > 0)

signal(next)else

signal(mutex);

• Mutual exclusion within a monitor is ensured.

Page 23: Critical Regions

23

Implement monitorusing semaphore

• For each condition variable x, we have:semaphore x_sem; // (initially = 0)int x_count = 0;

• The operation x.wait can be implemented as:

x_count++;if (next_count > 0)

signal(next);else

signal(mutex);wait(x_sem);x_count--;

Page 24: Critical Regions

24

Implement monitorusing semaphore

• The operation x.signal can be implemented as:

if (x_count > 0) {next_count++;signal(x_sem);wait(next);next_count--;

}

Page 25: Critical Regions

25

Process-resumption order

• If several processes are suspended on condition x, and an x.signal operation is executed by some process, then how to determine which of the suspended processes should be resumed next?

• Except FCFS, the conditional-wait construct can be used:x.wait(c)

where c is an integer expression that is evaluated when the wait operation is executed.

• The value of c, called priority number, is then stored with the name of the process that is suspended. When x.signal is executed, the process with the smallest associated priority number is resumed next.

Page 26: Critical Regions

26

Example

• Monitor controlling the allocation of a single resource among competing processes.

• monitor ResourceAllocation {boolean busy;condition x;void acquire(int time) {

if (busy)x.wait(time);

busy = true;}void release() {

busy = false;x.signal();

}void init() {

busy = false;}

}

Each process, when requesting an allocation of its resources, specifies the maximum time it plans to

use the resource.

The monitor allocates the resource to that process that has the

shortest time-allocation request.

Page 27: Critical Regions

27

Example

• A process that needs to access the resource must following the sequence:

R.acquire(t);...access the resource;...

R.release();

• Unfortunately, the monitor concept cannot guarantee that the sequence will be followed.

access resource without permission

process not releasing resource

process releasing not requested resource

process request same resource before release it

Page 28: Critical Regions

28

Access-control problem

• Check two conditions to establish correctness of system: – User processes must always make their calls on the

monitor in a correct sequence.– Must ensure that an uncooperative process does not

ignore the mutual-exclusion gateway provided by the monitor, and try to access the shared resource directly, without using the access protocols.

These checkings are not reasonable

for large or dynamic system.

Can be solved only by additional mechanisms.

(chap18)

Page 29: Critical Regions

29

OS Synchronization

Solaris 2:• Implements a variety of locks to support

multitasking, multithreading (including real-time threads), and multiprocessing.

• Synchronization in Solaris 2 Provides:- adaptive mutex- condition variables- semaphores- reader-writer locks

§ 7.8

Page 30: Critical Regions

30

Adaptive Mutex

• An adaptive mutex protects access to every critical data item. It starts as a standard semaphore implemented as a spinlock.

• If the data are locked (in use), the adaptive mutex does one of two things:– If the lock is held by a thread that is currently running

on another CPU, the thread spins while waiting for the lock to become available.

– If the thread holding the lock is not currently in run state, the thread blocks and go to sleep until the lock being released.

The thread holding the data is likely to

end soon.Put to sleep for avoiding the spinning when the lock

will not be freed reasonably quickly.

Page 31: Critical Regions

31

Adaptive Mutex

• An adaptive mutex protects access to every critical data item. It starts as a standard semaphore implemented as a spinlock.

• If the data are locked (in use), the adaptive mutex does one of two things:– If the lock is held by a thread that is currently running

on another CPU, the thread spins while waiting for the lock to become available.

– If the thread holding the lock is not currently in run state, the thread blocks and go to sleep until the lock being released.

Put to sleep for avoiding the spinning when the lcok

will not be freed reasonably quickly.

Multiple Choices Question:( ) Different operations may be adopted by the

adaptive mutex mechanism when a thread is requesting a locked data. The decision is made by the status of(a) located memories (b) relative CPU speed (c) the thread holding the lock (d) the type of monitor entries

Answer: C

Page 32: Critical Regions

32

Adaptive Mutex

• Adaptive mutex only protect those data that are accessed by short code segments where a lock will be held for less than a few hundred instructions.

• For longer code segments, condition variables and semaphores are used.– If the desired lock is already held, the thread issues a

wait and sleep.

– The cost of putting a thread to sleep and waking it is less than the cost of wasting several hundred instructions waiting in a spinlock.

If the code segment is longer than that, spin

waiting will be exceedingly inefficient.

Page 33: Critical Regions

33

Readers-Writers Lock

• The readers-writers locks are used to protect data that are accessed frequently, but usually only in a read-only manner.

• In these circumstances, readers-writers locks are more efficient than semaphores.

• Expensive to implement, so again they are used on only long sections of code.

THINKTHINK

Page 34: Critical Regions

34

OS Synchronization

Windows 2000:• Uses interrupt masks to protect access to global resources

on uniprocessor systems.

• Uses spinlocks on multiprocessor systems.

• Also provides dispatcher objects which may act as either mutexes and semaphores.

• Dispatcher objects may also provide events. An event acts much like a condition variable.

Page 35: Critical Regions

35

Atomic Transactions

• Need to make sure that a CS forms a single logical unit of work that either is performed in its entirety or is not performed at all.

• A collection of instructions (or operations) that performs a single logical function is called a transaction.

• A major issue in processing transactions is the preservation of atomicity despite the possibility of failures within the computer system.

§ 7.9

Page 36: Critical Regions

36

Commit & Abort

• From our point of view, a transaction is simply a sequence of read and write operations, terminated by commit or abort operation.– commit: signifies that the transaction has

terminated successfully.– abort: the transaction had to cease its normal

execution due to some logical error.

Page 37: Critical Regions

37

Roll Back

• An aborted transaction must have no effect on the state of the data that it has already modified, so that the atomicity property is ensured.

• Thus, the state of the data accessed by an aborted transaction must be restored to what it was just before the transaction started executing --- rolled back.

Page 38: Critical Regions

38

Device Properties (Skip)

• To determine how the system should ensure atomicity, we need first to identify the properties of devices used for storing the various data accessed by the transactions.

1. Volatile StorageInformation residing involatile storage does not usually survive system crashed.

2. Nonvolatile StorageInformation residing involatile storage usually survive system crashed.

3. Stable StorageInformation residing is never lost.

Page 39: Critical Regions

39

Mechanisms for ensuringTransaction Atomicity

• Log-Based Recovery

• Checkpoints

• Concurrent Atomic Transactions

Page 40: Critical Regions

40

Log-Based Recovery• Record informatin describing all the modifications

made by the transaction to the various data it accessed.

• Write-ahead logging:Each log record describes a single operation of a transaction write.Fields: – Transaction Name– Data Item Name– Old Value– New Value

§ 7.9.2

Page 41: Critical Regions

41

Log-Based Recovery

• Before a transaction Ti starts its execution, record <Ti starts> is written to the log.

• During its execution, any write operation by Ti is preceded by the writing of the appropriate new record to the log.

• When Ti commits, the record < Ti commits> is written to the log.

Page 42: Critical Regions

42

Log-Based Recovery

• Performance penalty ... two physical writes are required for every logical write requested.

• More storage is needed: for the data and the log.

• The recovery algorithm uses two procedures:– undo(Ti)

– redo(Ti)

Page 43: Critical Regions

43

Log-Based Recovery

• If a transaction Ti aborts → restore the state of the data that it has updated by executing undo(Ti).

• If a system failure occurs, must consulting the log to determine the proper operation:– The log contains < Ti starts> record, but does not

contain < Ti commits> record → Transaction Ti needs to be undone.

– The log contains both the < Ti starts> and the < Ti commits> records → Transaction Ti needs to be redone.

– Drawbacks ...

Page 44: Critical Regions

44

Log-Based Recovery

• Drawbacks:1. The search process takes time.

2. Most of the transactions need to be redone already updated the data. Redoing the modificaiton takes longer.

• To reduce the overhead ... use “Checkpoints.”

Page 45: Critical Regions

45

Checkpoints

• In addition to the write-ahead log, the system periodically performs checkpoints that require:– Output all log records currently residing in

main memory onto stable storage.– Output all modified data residing in main

memory to the stable storage.– Output a log record <checkpoint> onto stable

storage.

§ 7.9.3

allows the system to streamline its

recovery procedure

Page 46: Critical Regions

46

Checkpoints

• After failure occur, the recovery routine examines the log to determine the most recent transaction Ti that started before the most recent chekckpoint.

• The redo and undo need to be applied to only Ti and all Tj that started execution after Ti:

– For all Tk in T such that record <Tk commits> appears in

the log, execute redo(Tk).

– For all Tk in T that have no <Tk commits> record in the

log, execute undo(Tk).

T

Page 47: Critical Regions

47

Concurrent Atomic Transactions

• Serializability can be maintained by simply executing each transaction within a CS ... too restrictive.

• We can allow transactions to overlap their execution, while maintaining serializability... concurrency-control algorithms

Page 48: Critical Regions

48

Serial Schedule

• Schedule 1:

• A schedule where each transaction is executed atomically is called a serial schedule.

• For a set of n transactions, there exist n! different valid serial schedules.

• Each serial schedule is correct.

§ 7.9.4.1

T0 T1

read(A)write(A)read(B)write(B) read(A) write(A) read(B) write(B)

Page 49: Critical Regions

49

Nonserial Schedule

• If two transactions are allowed to overlap their execution... nonserial schedule.

• A nonserial schedule does not necessarily incorrect.

Page 50: Critical Regions

50

Nonserial Schedule

• Two consective operation O1 and O2 of Ti and Tj are conflict if they access the same data item and at least one of these operations is a write operation.

• Schedule 2: T0 T1

read(A)write(A) read(A) write(A)read(B)write(B) read(B) write(B)

Page 51: Critical Regions

51

Nonserial Schedule

• Two consective operation O1 and O2 of Ti and Tj are conflict if they access the same data item and at least one of these operations is a write operation.

• Schedule 2: T0 T1

read(A)write(A) read(A) write(A)read(B)write(B) read(B) write(B)

conflict

Page 52: Critical Regions

52

Nonserial Schedule

• Two consective operation O1 and O2 of Ti and Tj are conflict if they access the same data item and at least one of these operations is a write operation.

• Schedule 2: T0 T1

read(A)write(A) read(A) write(A)read(B)write(B) read(B) write(B)

not conflict

Page 53: Critical Regions

53

Nonserial Schedule

• If Oi and Oj are consective and do not conflict, then Oi and Oj can be swapped to produce a new but equivalent schedule.

• Schedule 2: T0 T1

read(A)write(A) read(A) write(A)read(B)write(B) read(B) write(B)

not conflict

Page 54: Critical Regions

54

Nonserial Schedule

• If Oi and Oj are consective and do not conflict, then Oi and Oj can be swapped to produce a new but equivalent schedule.

• Schedule 2: T0 T1

read(A)write(A) read(A)read(B) write(A)write(B) read(B) write(B)

Page 55: Critical Regions

55

Nonserial Schedule

• If Oi and Oj are consective and do not conflict, then Oi and Oj can be swapped to produce a new but equivalent schedule.

• Schedule 2: T0 T1

read(A)write(A)read(B) read(A) write(A)write(B) read(B) write(B)

Page 56: Critical Regions

56

Nonserial Schedule

• If Oi and Oj are consective and do not conflict, then Oi and Oj can be swapped to produce a new but equivalent schedule.

• Schedule 2: T0 T1

read(A)write(A)read(B) read(A)write(B) write(A) read(B) write(B)

Page 57: Critical Regions

57

Nonserial Schedule

• If Oi and Oj are consective and do not conflict, then Oi and Oj can be swapped to produce a new but equivalent schedule.

• Schedule 2:

• This schedule is conflict serializable.

T0 T1

read(A)write(A)read(B)write(B) read(A) write(A) read(B) write(B)

Page 58: Critical Regions

58

Locking Protocol

• Locking protocol governs how locks are acquired and released by transactions.

• Modes in which a data item can be locked:– Shared: If a transaction Ti has obtained a

shared-mode lock (denoted by S) on data item Q, then Ti can read this item, but cannot write Q.

– Exclusive: If a transaction Ti has obtained an exclusive-mode lock (denoted by X) on data item Q, then Ti can both read and write Q.

Similar to the reader-writers algorithm

Page 59: Critical Regions

59

Locking protocol

• It is not always desirable for a transaction to unlock a data item immediately after its last access of that data item, because serializability may bot be ensured.

• One protocol ensures serializability is the two-phase locking protocol: each transaction issue lock and unlock in two phases:– Growing Phase: A transaction may obtain locks, but may not

release any lock.– Shrinking Phase: A transaction may release locks, but may not

obtain any new locks.

• Initially, a transaction is in the growing phase and acquires locks as needed. Once the transaction releases a lock, it enters the shrinking phase, and no more lock requests can be issed.

Ensures conflict serializability. However, it does not ensure freedom

from deadlock.

Page 60: Critical Regions

60

Timestamp-based Protocols

• The serializability order among transactions can be selected in advance:– Unique fixed timestamp TS(Ti) for each Ti

assigned by the system before Ti starts.

– TS(Ti) < TS(Tj) for later transaction Tj.

• Value of the system clock or a logical counter can be used to implement the timestamp.

Page 61: Critical Regions

61

Timestamp-based Protocols

• The timestamps of the transactions determine the serializability order.

If TS(Ti) < TS(Tj), then the system must ensure that the produced schedule is equivalent to a serial schedule in which transaction Ti appears before transaction Tj.

• Each data item Q has two timestamp value:– W-timestamp(Q): the largest timestamp of any

transaction that executed write(Q) successfully.

– R-timestamp(Q): the largest timestamp of any transaction that executed read(Q) successfully.Updated whenever a new

read(Q) or write(Q) instruction is executed.

Page 62: Critical Regions

62

Timestamp-based Protocols• This protocol ensures that any conflicting read and write operations

are executed in timestamp order. • Suppose Ti issues read(Q):

– If TS(Ti) < W-timestamp(Q) → Ti needs to read a value of Q that was already overwritten. read operation is rejected and Ti is rolled back.

– If TS(Ti) ≥ W-timestamp(Q) → read operation is executed, and R-timestamp(Q) is set to the maximum of R-timestamp(Q) and TS(Ti).

• Suppose Ti issues write(Q):– If TS(Ti) < R-timestamp(Q) → the value of Q that Ti is producing was

needed previously and Ti assumed that this value would never be produced. write operation is rejected, and Ti is rolled back.

– If TS(Ti) < W-timestamp(Q) → Ti is attempting to write an absolete value of Q. write operation is rejected and Ti is rolled back.

– Otherwise, the write operation is executed.• A transaction Ti, that is rolled back by the concurrency-control scheme

as a result of the issuing of either a read or write operation is assigned a new timestamp and is restarted.

Page 63: Critical Regions

63

Timestamp-based Protocols• This protocol ensures that any conflicting read and write operations

are executed in timestamp order. • Suppose Ti issues read(Q):

– If TS(Ti) < W-timestamp(Q) → Ti needs to read a value of Q that was already overwritten. read operation is rejected and Ti is rolled back.

– If TS(Ti) ≥ W-timestamp(Q) → read operation is executed, and R-timestamp(Q) is set to the maximum of R-timestamp(Q) and TS(Ti).

• Suppose Ti issues write(Q):– If TS(Ti) < R-timestamp(Q) → the value of Q that Ti is producing was

needed previously and Ti assumed that this value would never be produced. write operation is rejected, and Ti is rolled back.

– If TS(Ti) < W-timestamp(Q) → Ti is attempting to write an absolete value of Q. write operation is rejected and Ti is rolled back.

– Otherwise, the write operation is executed.• A transaction Ti, that is rolled back by the concurrency-control scheme

as a result of the issuing of either a read or write operation is assigned a new timestamp and is restarted.

T1

read(Q)

W-timestamp(Q)

TS(Ti)

W-timestamp(Q)

Page 64: Critical Regions

64

Timestamp-based Protocols• This protocol ensures that any conflicting read and write operations

are executed in timestamp order. • Suppose Ti issues read(Q):

– If TS(Ti) < W-timestamp(Q) → Ti needs to read a value of Q that was already overwritten. read operation is rejected and Ti is rolled back.

– If TS(Ti) ≥ W-timestamp(Q) → read operation is executed, and R-timestamp(Q) is set to the maximum of R-timestamp(Q) and TS(Ti).

• Suppose Ti issues write(Q):– If TS(Ti) < R-timestamp(Q) → the value of Q that Ti is producing was

needed previously and Ti assumed that this value would never be produced. write operation is rejected, and Ti is rolled back.

– If TS(Ti) < W-timestamp(Q) → Ti is attempting to write an absolete value of Q. write operation is rejected and Ti is rolled back.

– Otherwise, the write operation is executed.• A transaction Ti, that is rolled back by the concurrency-control scheme

as a result of the issuing of either a read or write operation is assigned a new timestamp and is restarted.

T1

write(Q)

R-timestamp(Q)

TS(Ti)

W-timestamp(Q)

Page 65: Critical Regions

65

Example

• Assume a transaction is assigned a timestamp immediately before its first instruction.

• TS(T2) < TS(T3)• Possible under timestamp

protocol• Also can be produced by two-

phase locking protocol.

T2 T3

read(B) read(B) write(B)read(A) read(A) write(A)

Page 66: Critical Regions

66

Timestamp-based Protocols

• Some schedules are possible under two-phase locking protocol but not under the timestamp protocol, and vice versa.

• The timestamp-ordering protocol ensures conflict serializability. It follows from the fact that conflicting operations are processed in timestamp order.

• Ensures freedom from deadlock, because no transaction ever waits.

Page 67: Critical Regions

67

The End