Docsity

Synchronization Primitives: Locks, Messages, and Transactions, Study notes of Computer Science

Partial preview of the text

Download Synchronization Primitives: Locks, Messages, and Transactions and more Study notes Computer Science in PDF only on Docsity!

Synchronization Primitives

Locks synchronized (lock) { balance

amt;

Messages

thread

server

channel-put

deposit-ch

amt

Transactions

atomic { balance += amt;

Transactions

atomic

marks a set of actions to appear to happen

instantaneously to all other processesInstead of stopping other processes, let everyonerun until non-instantaneous state is detected

This potential problem is called a

conflict

Hide the problem by discarding/rewinding changesand trying again later

This is called an

abort

If there was no problem, then make the changespermanent

This is called a

commit

Transactions

Process 1 atomic {

a++; b++; c++; }

Process 2 atomic

d++; b++; f++; }

One process may have to retry its transaction

Transactions

Process 1 atomic {

a++; b++; c++; }

Process 2 atomic

d++; e =

b;

f++; }

Depends on transaction implementation

Multiple Data

Messages

(and multiple managers)

define

a-server

sync ( handle-evt

a-request-ch ...

define

b-server

sync ( handle-evt

b-request-ch ...

handle-evt

a+b-request-ch ...

a-request-ch

Multiple Data

Transactions

(no problem)

atomic {

a.op(b);b.op(a); }

Transactions can fix deadlock and priority inversion

Waiting

Messages^ ...^ (

sync ( if

empty?

queue

never-evt ( channel-put-ev

dequeue-ch (

first

queue

channel-get

dequeue-ch

Waiting

Transactions

atomic {

if (q.isEmpty())

retry; result = q.dequeue(); }

retry

means “try again when something changes”

Implementing Transactions

Pessimistic

implementation:

  • Watch for conflicts during transaction

abort early to avoid wasted work

Optimistic

implementation:

  • Check for conflicts just before commit

lower overall overhead

Issues with Transactions

Transactions only work with actions that areundoable or immediate — which does not includeI/O If a transaction is too long:• Read/write logs grow large• The transaction may be constantly interruptedTracking reads and writes to detect conflicts canincur significant overhead