-
ContentsContents1.1. IntroductionIntroduction2.2.
ComputerComputer--System StructuresSystem Structures3.3.
OperatingOperating--System StructuresSystem Structures4.4.
Processes Processes 5.5. Threads Threads 6.6. CPU Scheduling CPU
Scheduling 7.7. Process Synchronization Process Synchronization
8.8. DeadlocksDeadlocks9.9. Memory ManagementMemory
Management10.10.Virtual MemoryVirtual Memory11.11.File SystemsFile
Systems
Chapter 7 Chapter 7 Process Process
SynchronizationSynchronization
-
Process SynchronizationProcess Synchronization
Why Synchronization?Why Synchronization?To ensure data
consistency for concurrent To ensure data consistency for
concurrent access to shared data!access to shared data!
Contents: Contents: Various mechanisms to ensure the orderly
Various mechanisms to ensure the orderly execution of cooperating
processesexecution of cooperating processes
Process SynchronizationProcess Synchronization
A ConsumerA Consumer--Producer ExampleProducer Example
Consumer:while (1) {
while (counter == 0);
nextc = buffer[out];out = (out +1) %
BUFFER_SIZE;counter--;consume an item in nextc;
}
Producerwhile (1) {
while (counter == BUFFER_SIZE);
produce an item in nextp;….buffer[in] = nextp;in = (in+1) %
BUFFER_SIZE;counter++;
}
-
Process SynchronizationProcess Synchronization
counter++ counter++ vsvs countercounter——r1 = counterr1 =
counter r2 = counterr2 = counterr1 = r1 + 1r1 = r1 + 1 r2 = r2 r2 =
r2 -- 11counter = r1counter = r1 counter = r2counter = r2Initially,
let Initially, let countercounter = 5.= 5.
1.1. P: r1 = P: r1 = countercounter2.2. P: r1 = r1 + 1P: r1 = r1
+ 13.3. C: r2 = C: r2 = countercounter4.4. C: r2 = r2 C: r2 = r2 ––
115.5. P: P: countercounter = r1= r16.6. C: C: countercounter = r2=
r2
A Race Condition!
Process SynchronizationProcess Synchronization
A Race Condition:A Race Condition:A situation where the outcome
of the A situation where the outcome of the execution depends on
the particular execution depends on the particular order of process
scheduling.order of process scheduling.
The CriticalThe Critical--Section Problem:Section Problem:Design
a protocol that processes can Design a protocol that processes can
use to cooperate.use to cooperate.
Each process has a segment of code, Each process has a segment
of code, called a called a critical sectioncritical section, whose
, whose execution must be execution must be mutually
exclusivemutually exclusive..
-
Process SynchronizationProcess Synchronization
A General Structure for the A General Structure for the
CriticalCritical--Section ProblemSection Problem
permission request
exit notification
entry section;
critical section;
exit section;
remainder section;} while (1);
do {
Three RequirementsThree Requirements1.1. Mutual ExclusionMutual
Exclusion
a.a. Only one process can be in its critical Only one process
can be in its critical section.section.
2.2. ProgressProgressa.a. Only processes not in their remainder
section Only processes not in their remainder section
can decide which will enter its critical section.can decide
which will enter its critical section.b.b. The selection cannot be
postponed indefinitely.The selection cannot be postponed
indefinitely.
3.3. Bounded WaitingBounded Waitinga.a. A waiting process only
waits for a A waiting process only waits for a
bounded number of processes to enter bounded number of processes
to enter their critical sections. their critical sections.
The Critical-Section Problem
-
NotationNotationProcesses Pi and Processes Pi and PjPj, where
j=1, where j=1--i;i;
AssumptionAssumptionEvery basic machineEvery basic
machine--language instruction language instruction is atomic.is
atomic.
The Critical-Section Problem –A Two-Process Solution
NotationNotationProcesses Pi and Processes Pi and PjPj, where
j=1, where j=1--i;i;
AssumptionAssumptionEvery basic machineEvery basic
machine--language instruction is language instruction is atomic.
atomic.
Algorithm 1Algorithm 1Idea: Remember which Idea: Remember which
process is allowed to enter process is allowed to enter its
critical section, That is, its critical section, That is, process i
can enter its process i can enter its critical section if turn =
i.critical section if turn = i.
The Critical-Section Problem –A Two-Process Solution
while (turn != i) ;
critical section
turn=j;
remainder section} while (1);
do {
-
Algorithm 1 fails the progress requirement:Algorithm 1 fails the
progress requirement:
P0
P1
Time
Time
suspend or quit!turn=0 exit
turn=1
exit
turn=0 blocked on P1’s entry section
The Critical-Section Problem –A Two-Process Solution
Algorithm 2Algorithm 2Idea: Remember the state of Idea: Remember
the state of each process.each process.flag[i]==true flag[i]==true
Pi is ready to Pi is ready to enter its critical section.enter its
critical section.Algorithm 2 fails the progress Algorithm 2 fails
the progress requirement when requirement when
flag[0]==flag[1]==true; flag[0]==flag[1]==true;
the exact timing of the two the exact timing of the two
processes?processes?
The Critical-Section Problem –A Two-Process Solution
flag[i]=true;
while (flag[ j ]) ;
critical sectionflag[ i ]=false;remainder section
} while (1);
do {
Initially, flag[0]=flag[1]=false
* The switching of “flag[i]=true” and “while (flag[j]);”.
-
Algorithm 3Algorithm 3Idea: Combine the Idea: Combine the ideas
of Algorithms ideas of Algorithms 1 and 21 and 2When (flag[i]
&& When (flag[i] && turn=i), turn=i), PjPj must
wait.must wait.Initially, Initially, flag[0]=flag[1]=false,
flag[0]=flag[1]=false, and turn = 0 or 1and turn = 0 or 1
The Critical-Section Problem –A Two-Process Solution
flag[ i ]=true;
turn=j;
while (flag[ j ] && turn==j) ;critical sectionflag[ i
]=false;remainder section
} while (1);
do {
Properties of Algorithm 3 Properties of Algorithm 3 Mutual
ExclusionMutual Exclusion
The eventual value of The eventual value of turnturn determines
determines which process enters the critical section.which process
enters the critical section.
ProgressProgressA process can only be stuck in the while A
process can only be stuck in the while loop, and the process which
can keep it loop, and the process which can keep it waiting must be
in its critical sections.waiting must be in its critical
sections.
Bounded WaitingBounded WaitingEach process wait at most one
entry by the Each process wait at most one entry by the other
process.other process.
The Critical-Section Problem –A Two-Process Solution
-
Bakery AlgorithmBakery AlgorithmOriginally designed for
distributed Originally designed for distributed
systemssystemsProcesses which are ready to Processes which are
ready to enter their critical section must enter their critical
section must take a number and wait till the take a number and wait
till the number becomes the lowest. number becomes the lowest.
intint number[i]: Pinumber[i]: Pi’’s number if it is s number if it
is nonzero.nonzero.booleanboolean choosing[i]: Pi is taking
choosing[i]: Pi is taking a number.a number.
The Critical-Section Problem –A Multiple-Process Solution
• An observation: If Pi is in its critical section, and Pk (k !=
i) has already chosen its number[k], then (number[i],i) <
(number[k],k).
choosing[i]=true;
number[i]=max(number[0], …number[n-1])+1;
choosing[i]=false;
for (j=0; j < n; j++)
while choosing[j] ;
while (number[j] != 0 && (number[j],j)
-
Synchronization HardwareSynchronization Hardware
Motivation: Motivation: Hardware features make programming
Hardware features make programming easier and improve system
efficiency.easier and improve system efficiency.
Approach:Approach:Disable Interrupt Disable Interrupt No
PreemptionNo Preemption
Infeasible in multiprocessor environment Infeasible in
multiprocessor environment where message passing is used.where
message passing is used.Potential impacts on interruptPotential
impacts on interrupt--driven system driven system
clocks.clocks.
Atomic Hardware InstructionsAtomic Hardware
InstructionsTestTest--andand--set, Swap, etc.set, Swap, etc.
booleanboolean TestAndSet(booleanTestAndSet(boolean &target)
{&target) {booleanboolean rvrv = target;=
target;target=true;target=true;return return rvrv;;
}}
while (TestAndSet(lock)) ;
critical section
lock=false;remainder section
} while (1);
do {
Synchronization HardwareSynchronization Hardware
-
2003/9/92003/9/9講到這裡講到這裡
void void Swap(booleanSwap(boolean &a, &a,
booleanboolean &b) {&b) {booleanboolean temp = a;temp =
a;a=b;a=b;b=temp;b=temp;
}}key=true;while (key == true)
Swap(lock, key);critical section
lock=false;remainder section
} while (1);
do {
Synchronization HardwareSynchronization Hardware
-
waiting[i]=true;key=true;while (waiting[i] && key)
key=TestAndSet(lock);waiting[i]=false;critical section;j= (i+1)
% n;while(j != i) && (not waiting[j])
j= (j+1) % n;If (j=i) lock=false;else waiting[j]=false;
remainder section
} while (1);
do {
Synchronization HardwareSynchronization HardwareMutual
Exclusion
Pass if key == F or waiting[i] == F
ProgressExit process
sends a process in.Bounded Waiting
Wait at most n-1 times
Atomic TestAndSet is hard to implement in a multiprocessor
environment.
SemaphoresSemaphores
Motivation: Motivation: A highA high--level solution for more
level solution for more complex problems.complex problems.
SemaphoreSemaphoreA variable A variable S S only accessible by
only accessible by two atomic operations:two atomic operations:
signal(S) { /* V */S++;
}
wait(S) { /* P */while (S
-
Semaphores Semaphores –– Usages Usages
Critical SectionsCritical Sections
do {wait(mutex);
critical sectionsignal(mutex);
remainder section} while (1);
Precedence Enforcement
P1:S1;signal(synch);
P2:wait(synch);S2;
SemaphoresSemaphores
ImplementationImplementationSpinlockSpinlock –– A BusyA
Busy--Waiting Waiting SemaphoreSemaphore
““while (S
-
SemaphoresSemaphores
Semaphores with Block Waiting Semaphores with Block Waiting
typedef struct {
int value;struct process *L;
} semaphore ;
void signal(semaphore S);S.value++; if (S.value
-
Deadlocks and StarvationDeadlocks and Starvation
DeadlockDeadlockA set of processes is in a A set of processes is
in a deadlockdeadlock state state when every process in the set is
waiting for when every process in the set is waiting for an event
that can be caused only by another an event that can be caused only
by another process in the set.process in the set.
Starvation (or Indefinite Blocking)Starvation (or Indefinite
Blocking)E.g., a LIFO queueE.g., a LIFO queue
P0: wait(S); P1: wait(Q);wait(Q); wait(S);
… …signal(S); signal(Q);signal(Q); signal(S);
Binary SemaphoreBinary Semaphore
Binary Semaphores versus Binary Semaphores versus Counting
SemaphoresCounting Semaphores
The value ranges from 0 to 1The value ranges from 0 to 1easy
implementation!easy implementation!
wait(S)wait(S1); /* protect C */C--;if (C < 0) {
signal(S1);wait(S2);
}signal(S1);
signal(S)wait(S1);C++;if (C
-
Classical Synchronization Classical Synchronization Problems
Problems –– The Bounded The Bounded
BufferBufferProducer:Producer:
do {do {produce an item in produce an item in
nextpnextp;;…………..wait(empty); /* control buffer availability
*/wait(empty); /* control buffer availability
*/wait(mutexwait(mutex); /* mutual exclusion */); /* mutual
exclusion */…………add add nextpnextp to buffer;to
buffer;signal(mutexsignal(mutex););signal(full); /* increase item
counts */signal(full); /* increase item counts */
} while (1);} while (1);
Initialized to nInitialized to 1
Initialized to 0
Classical Synchronization Classical Synchronization Problems
Problems –– The Bounded The Bounded
BufferBufferConsumer:Consumer:
do {do {wait(full); /* control buffer availability */wait(full);
/* control buffer availability */wait(mutexwait(mutex); /* mutual
exclusion */); /* mutual exclusion */…………..remove an item from
buffer to remove an item from buffer to
nextpnextp;;…………signal(mutexsignal(mutex););signal(empty); /*
increase item counts */signal(empty); /* increase item counts
*/consume consume nextpnextp;;
} while (1);} while (1);
Initialized to n
Initialized to 1Initialized to 0
-
Classical Synchronization Classical Synchronization Problems
Problems –– Readers and Readers and
WritersWritersThe Basic Assumption:The Basic Assumption:
Readers: shared locksReaders: shared locksWriters: exclusive
locksWriters: exclusive locks
The first readerThe first reader--writers problemwriters
problemNo readers will be kept waiting unless a writer No readers
will be kept waiting unless a writer has already obtained
permission to use the has already obtained permission to use the
shared object shared object potential hazard to writers!potential
hazard to writers!
The second readerThe second reader--writers problem:writers
problem:Once a writer is ready, it performs its write Once a writer
is ready, it performs its write asapasap! ! potential hazard to
readers!potential hazard to readers!
Classical Synchronization Classical Synchronization Problems
Problems –– Readers and Readers and
WritersWriterssemaphore semaphore wrtwrt, , mutexmutex; ;
(initialized to 1);(initialized to 1);intint
readcountreadcount=0;=0;Writer:Writer:
wait(wrtwait(wrt););…………writing is performedwriting is
performed…………signal(wrtsignal(wrt))
Reader:wait(mutex);readcount++; if (readcount == 1)
wait(wrt);signal(mutex);…… reading ……wait(mutex);readcount--; if
(readcount== 0)
signal(wrt);signal(mutex);
First R/WSolution
Queueingmechanism
Which is awaken?
-
Classical Synchronization Classical Synchronization Problems
Problems –– DiningDining--Philosophers Philosophers
Each philosopher must pick up one Each philosopher must pick up
one chopstick beside him/her at a timechopstick beside him/her at a
timeWhen two chopsticks are picked up, the When two chopsticks are
picked up, the philosopher can eat.philosopher can eat.
thinking hungry
deadeating
13/05/200313/05/2003
-
Classical Synchronization Classical Synchronization Problems
Problems –– DiningDining--PhilosophersPhilosophers
semaphore chopstick[5];semaphore chopstick[5];do {do {
wait(chopstick[i]);wait(chopstick[i]);wait(chopstick[(i + 1) % 5
]);wait(chopstick[(i + 1) % 5 ]);…… eat eat
……signal(chopstick[i]);signal(chopstick[i]);signal(chopstick[(i+1)
% 5]);signal(chopstick[(i+1) % 5]);……think think ……
} while (1);} while (1);
Classical Synchronization Classical Synchronization Problems
Problems –– DiningDining--
PhilosophersPhilosophersDeadlock or Starvation?!Deadlock or
Starvation?!Solutions to Deadlocks:Solutions to Deadlocks:
At most four philosophers appear.At most four philosophers
appear.Pick up two chopsticks Pick up two chopsticks
““simultaneouslysimultaneously””..Order their behaviors, e.g., odds
pick up Order their behaviors, e.g., odds pick up their right one
first, and evens pick up their right one first, and evens pick up
their left one first.their left one first.
Solutions to Starvation:Solutions to Starvation:No philosopher
will starve to death.No philosopher will starve to death.
A deadlock could happen??A deadlock could happen??
-
Critical RegionsCritical Regions
Motivation:Motivation:Various programming errors in using
Various programming errors in using lowlow--level constructs,e.g.,
level constructs,e.g., semaphoressemaphores
Interchange the order of wait and signal Interchange the order
of wait and signal operationsoperationsMiss some waits or
signalsMiss some waits or signalsReplace waits with signalsReplace
waits with signalsetcetc
The needs of highThe needs of high--level language level
language constructs to reduce the possibility constructs to reduce
the possibility
Critical RegionsCritical Regions
Region v when B do S;Region v when B do S;Variable v Variable v
–– shared among processes shared among processes and only
accessible in the regionand only accessible in the region
structstruct buffer {buffer {item pool[n];item pool[n];intint
count, in, out;count, in, out;
};};
B B –– condition condition count < 0count < 0
S S –– statementsstatements
Example: Mutual Exclusionregion v when (true) S1;region v when
(true) S2;
-
Critical Regions Critical Regions
––ConsumerConsumer--ProducerProducer
Producer:Producer:region buffer when region buffer when (count
< n) {(count < n) {
pool[in] = pool[in] = nextpnextp;;in = (in + 1) % n;in = (in +
1) % n;count++;count++;
}}
Consumer:Consumer:region buffer when region buffer when (count
> 0) {(count > 0) {
nextcnextc = pool[out];= pool[out];out = (out + 1) % n;out =
(out + 1) % n;countcount----;;
}}
struct buffer {item pool[n];int count, in, out;
};
Critical Regions Critical Regions ––Implementation by
SemaphoresImplementation by Semaphores
Region Region xx when B do S;when B do
S;wait(mutexwait(mutex););
while (!B) {while (!B) {/* fail B *//* fail B
*/firstfirst--count++;count++;if (secondif (second--count > 0)
count > 0) /* try other processes waiting on second/* try other
processes waiting on second--delay */delay */
signal(secondsignal(second--delay);delay);else else
signal(mutexsignal(mutex););/* block itself on first/* block itself
on first--delay */delay */
wait(firstwait(first--delay);delay);
/* to protect the region */semaphore mutex; /* to (re-)test B
*/semaphore first-delay;int first-count=0;/* to retest B
*/semaphore second-delay; int second-count=0;
-
Critical Regions Critical Regions ––Implementation by
Implementation by
SemaphoresSemaphoresfirstfirst--countcount----;;secondsecond--count++;count++;if
(firstif (first--count > 0)count > 0)
signal(firstsignal(first--delay);delay);else signal(secondelse
signal(second--delay);delay);
/* block itself on first/* block itself on first--delay */delay
*/wait(secondwait(second--delay);delay);secondsecond--countcount----;;
}}S;S;if (firstif (first--count > 0)count > 0)
signal(firstsignal(first--delay);delay);else if (secondelse if
(second--count > 0)count > 0)
signal(secondsignal(second--delay);delay);else else
signal(mutexsignal(mutex););
MonitorMonitor
ComponentsComponentsVariables Variables –– monitor state monitor
state ProceduresProcedures
Only access local variables or formal Only access local
variables or formal parametersparameters
Condition variablesCondition variablesTailorTailor--made
syncmade syncx.wait() or x.signalx.wait() or x.signal
monitor name {variable declarationvoid proc1(…) {} …void
procn(…) {}
}
………procedures
initializationcode
shared data
x
entry queue
queue for x
-
MonitorMonitor
Semantics of signal & waitSemantics of signal &
waitx.signal() resumes one suspended process. If x.signal() resumes
one suspended process. If there is none, no effect is imposed.there
is none, no effect is imposed.PP x.signal() a suspended process
x.signal() a suspended process QQ
PP either waits until either waits until QQ leaves the monitor
or waits leaves the monitor or waits for another conditionfor
another conditionQQ either waits until either waits until PP leaves
the monitor, or waits leaves the monitor, or waits for another
condition.for another condition.
Monitor Monitor –– DiningDining--PhilosophersPhilosophersmonitor
monitor dpdp {{
enumenum {thinking, hungry, eating} state[5];{thinking, hungry,
eating} state[5];condition self[5]; condition self[5]; void void
pickup(intpickup(int i) {i) {
stat[i]=hungry;stat[i]=hungry;test(i);test(i);if (stat[i] !=
eating) self[i].wait;if (stat[i] != eating) self[i].wait;
}}void void putdown(intputdown(int i) {i) {
stat[i] = thinking;stat[i] = thinking;test((i+4) % 5);
test((i+4) % 5); test((i + 1) % 5);test((i + 1) % 5);
}}
Pi:dp.pickup(i);… eat …dp.putdown(i);
-
Monitor Monitor –– DiningDining--PhilosophersPhilosophers
void void test(inttest(int i) {i) {if (stat[(i+4) % 5]) != if
(stat[(i+4) % 5]) != eating &&eating &&
stat[i] == hungry &&stat[i] == hungry
&&state[(i+1) % 5] != eating) {state[(i+1) % 5] != eating)
{
stat[i] = eating; stat[i] = eating;
self[i].signal();self[i].signal();
}}}}void init() {void init() {
for (for (intint i=0; i < 5; i++)i=0; i < 5; i++)state[i]
= thinking;state[i] = thinking;
}}
No deadlock!But starvation could occur!
Monitor Monitor –– Implementation by Implementation by
SemaphoresSemaphores
SemaphoresSemaphoresmutexmutex –– to protect the monitorto
protect the monitornextnext –– being initialized to zero, on which
being initialized to zero, on which processes may suspend
themselvesprocesses may suspend themselves
nextcountnextcount
For each external function For each external function
FFwait(mutexwait(mutex););……body of F;body of F;……if (nextif
(next--count > 0)count > 0)
signal(next);signal(next);else else
signal(mutexsignal(mutex););
-
Monitor Monitor –– Implementation by Implementation by
SemaphoresSemaphores
For every condition For every condition xxA semaphore A
semaphore xx--semsemAn integer variable An integer variable
xx--countcountImplementation of x.wait() and x.signal
:Implementation of x.wait() and x.signal :
x.wait()x-count++;if (next-count > 0)
signal(next);else signal(mutex);wait(x-sem);x-count--;
x.signalif (x-count > 0) {
next-count++; signal(x-sem);wait(next);next-count--;
}
* x.wait() and x.signal() are invoked within a monitor.
MonitorMonitor
ProcessProcess--Resumption OrderResumption OrderQueuing
mechanisms for a monitor and its Queuing mechanisms for a monitor
and its condition variables.condition variables.A solution:A
solution:
x.wait(cx.wait(c););where the expression where the expression cc
is evaluated to is evaluated to determine its processdetermine its
process’’s resumption order.s resumption order.
R.acquire(t);R.acquire(t);……
access the resource;access the resource;R.release;R.release;
monitor ResAllc {boolean busy;condition x;void acquire(int time)
{
if (busy)x.wait(time);
busy=true;}…}
-
MonitorMonitor
Concerns:Concerns:Processes may access resources without
Processes may access resources without consulting the
monitor.consulting the monitor.Processes may never release
resources.Processes may never release resources.Processes may
release resources which they Processes may release resources which
they never requested.never requested.Process may even request
resources twice.Process may even request resources twice.
MonitorMonitor
Remark: Whether the monitor is correctly Remark: Whether the
monitor is correctly used?used?=> Requirements for correct
computations=> Requirements for correct computations
Processes always make their calls on the monitor Processes
always make their calls on the monitor in correct order.in correct
order.No uncooperative process can access resource No uncooperative
process can access resource directly without using the access
protocols.directly without using the access protocols.
Note: Scheduling behavior should consult Note: Scheduling
behavior should consult the builtthe built--in monitor scheduling
algorithm if in monitor scheduling algorithm if resource access RPC
are built inside the resource access RPC are built inside the
monitor.monitor.
-
OS Synchronization OS Synchronization –– Solaris Solaris 22
Semaphores and Condition VariablesSemaphores and Condition
VariablesAdaptive Adaptive MutexMutex
SpinSpin--locking if the locklocking if the lock--holding thread
is holding thread is running; otherwise, blocking is used.running;
otherwise, blocking is used.
ReadersReaders--Writers LocksWriters LocksExpensive in
implementations.Expensive in implementations.
Turnstile Turnstile A queue structure containing threads A queue
structure containing threads blocked on a lock.blocked on a
lock.Priority inversion Priority inversion priority inheritance
priority inheritance protocol for kernel threadsprotocol for kernel
threads
OS Synchronization OS Synchronization –– Windows Windows
20002000
General MechanismGeneral MechanismSpinSpin--locking for short
code segments in a locking for short code segments in a
multiprocessor platform.multiprocessor platform.Interrupt disabling
when access to global Interrupt disabling when access to global
variables is done in a variables is done in a
uniprocessoruniprocessorplatform.platform.
Dispatcher ObjectDispatcher ObjectState: signaled or nonState:
signaled or non--signaledsignaledMutexMutex –– select one process
from its waiting select one process from its waiting queue to the
ready queue.queue to the ready queue.Events Events –– select all
processes waiting for select all processes waiting for the
event.the event.
-
Atomic TransactionsAtomic Transactions
Why Atomic Transactions?Why Atomic Transactions?Critical
sections ensure mutual exclusion in Critical sections ensure mutual
exclusion in data sharing, but the relationship between data
sharing, but the relationship between critical sections might also
be meaningful!critical sections might also be meaningful!Atomic
TransactionsAtomic Transactions
Operating systems can be viewed as Operating systems can be
viewed as manipulators of data!manipulators of data!
Atomic Transactions Atomic Transactions ––System ModelSystem
Model
Transaction Transaction –– a logical unit of a logical unit of
computationcomputation
A sequence of read and write operations A sequence of read and
write operations followed by a commit or an abort.followed by a
commit or an abort.
Beyond Beyond ““critical sectionscritical sections””1.
Atomicity: All or Nothing1. Atomicity: All or Nothing
An aborted transaction must be An aborted transaction must be
rolled backrolled back..The effect of a committed transaction must
persist The effect of a committed transaction must persist and be
imposed as a logical unit of operations.and be imposed as a logical
unit of operations.
-
Atomic Transactions Atomic Transactions –– System System
ModelModel
2. 2. SerializabilitySerializability: : The order of transaction
executions must be The order of transaction executions must be
equivalent to a serial schedule. equivalent to a serial
schedule.
T0 T1R(A)W(A)
R(A)W(A)
R(B)W(B)
R(B)W(B)
Two operations Oi & Oj conflict if1. Access the same
object2. One of them is write
Atomic Transactions Atomic Transactions –– System System
ModelModel
Conflict Conflict SerializableSerializable::SS is conflict is
conflict serializableserializable if if SS can be transformed can
be transformed into a serial schedule by swapping into a serial
schedule by swapping
nonconflictingnonconflictingoperations.operations.
T0 T1R(A)W(A)
R(A)W(A)
R(B)W(B)
R(B)W(B)
T0 T1R(A)W(A)R(B)W(B)
R(A)W(A)R(B)W(B)
-
Atomic Transactions Atomic Transactions ––Concurrency
ControlConcurrency Control
Locking ProtocolsLocking ProtocolsLock modes (A general
approach!)Lock modes (A general approach!)
1. Shared1. Shared--Mode: Mode: ““ReadsReads””..2. Exclusive2.
Exclusive--Mode: Mode: ““ReadsReads”” & &
““WritesWrites““
General RuleGeneral RuleA transaction must receive a lock of an
A transaction must receive a lock of an appropriate mode of an
object before it appropriate mode of an object before it accesses
the object. The lock may not accesses the object. The lock may not
be released until the last access of the be released until the last
access of the object is done.object is done.
Atomic Transactions Atomic Transactions ––Concurrency
ControlConcurrency Control
LockRequest Locked?
Request compatible with the
current lock?
Lock is granted WAIT
Yes
Yes
No
No
-
Atomic Transactions Atomic Transactions ––Concurrency
ControlConcurrency Control
When to release locks w/o violating When to release locks w/o
violating serializabilityserializability
TwoTwo--Phase Locking Protocol (2PL) Phase Locking Protocol
(2PL) ––Not DeadlockNot Deadlock--FreeFree
How to improve 2PL?How to improve 2PL?Semantics, Order of Data,
Access Semantics, Order of Data, Access Pattern, etc.Pattern,
etc.
GrowingPhase
ShrinkingPhase
serializableschedules
2PL schedules
R0(A) W0(A) R1(A) R1(B) R0(B) W0(B)
Atomic Transactions Atomic Transactions ––Concurrency
ControlConcurrency Control
TimestampTimestamp--Based ProtocolsBased ProtocolsA time stamp
for each transaction TS(TA time stamp for each transaction
TS(Tii))
Determine transactionsDetermine transactions’’ order in a
schedule in order in a schedule in advance!advance!
A General Approach:A General Approach:TS(TTS(Tii) ) –– System
Clock or Logical CounterSystem Clock or Logical Counter
Unique?Unique?
Scheduling Scheme Scheduling Scheme ––
deadlockdeadlock--freefree))(()( )( iQWT TTSMaxQtimestampW i
−=−
))(()( )( iQRT TTSMaxQtimestampR i −=−
-
Atomic Transactions Atomic Transactions ––Concurrency
ControlConcurrency Control
R(Q) requested by TR(Q) requested by Tii check TS(Tcheck TS(Tii)
!) !
W(Q) requested by TW(Q) requested by Tii check TS(Tcheck TS(Tii)
!) !
Rejected transactions are rolled back Rejected transactions are
rolled back and restarted with a new time stamp.and restarted with
a new time stamp.
Time
W-timestamp(Q)
Rejected Granted
Time
R-timestamp(Q)Rejected Granted
Time
W-timestamp(Q)Rejected Granted
FailureFailure Recovery Recovery –– A Way to Achieve A Way to
Achieve AtomicityAtomicity
Failures of Volatile and Nonvolatile Failures of Volatile and
Nonvolatile Storages!Storages!
Volatile Storage: Memory and CacheVolatile Storage: Memory and
CacheNonvolatile Storage: Disks, Magnetic Tape, Nonvolatile
Storage: Disks, Magnetic Tape, etc.etc.Stable Storage: Storage
which never fail.Stable Storage: Storage which never fail.
LogLog--Based RecoveryBased RecoveryWriteWrite--Ahead
LoggingAhead Logging
Log RecordsLog Records< Ti starts >< Ti starts ><
Ti commits >< Ti commits >
-
Two Basic Recovery Procedures:Two Basic Recovery Procedures:
undo(Ti): restore data updated by Tiundo(Ti): restore data
updated by Tiredo(Ti): reset data updated by Tiredo(Ti): reset data
updated by Ti
Operations must be idempotent!Operations must be
idempotent!Recover the system when a failure Recover the system
when a failure occurs:occurs:
““RedoRedo”” committed transactions, and committed transactions,
and ““undoundo”” aborted transactions.aborted transactions.
Failure RecoveryFailure Recovery
Timerestart crash
Failure RecoveryFailure Recovery
Why Why CheckpointingCheckpointing??The needs to scan and rerun
all log The needs to scan and rerun all log entries to redo
committed entries to redo committed transactions.transactions.
CheckPointCheckPointOutput all log records, Output DB, and
Output all log records, Output DB, and Write to stable
storage!Write to stable storage!Commit: A Force Write
ProcedureCommit: A Force Write Procedure
Timecrashcheckpoint