Chapter 6: Process Synchronization: Silberschatz, Galvin and Gagne ©2009 Operating System Concepts - 8 Edition
Chapter 6: Process Synchronization: Silberschatz, Galvin and Gagne ©2009 Operating System Concepts - 8 Edition
Synchronization
6.2
Objectives
To introduce the critical-section problem, whose solutions can be used to
problem
to ensure atomicity
6.3
Background
Concurrent access to shared data may result in data
inconsistency
6.4
Race Condition
6.5
6.6
Petersons Solution
Two process solution
Assume that the LOAD and STORE instructions are atomic; that is,
cannot be interrupted.
int turn;
Boolean flag[2]
section.
6.7
6.8
Semaphore
Less complicated
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
6.9
// initialized to 1
do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
6.10
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal ()
Note that applications may spend lots of time in critical sections and
6.11
Two operations:
6.12
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
6.13
P0
P1
wait (S);
wait (Q);
wait (Q);
wait (S);
.
signal (S);
signal (Q);
signal (Q);
signal (S);
6.14
6.15
Bounded-Buffer Problem
N buffers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value N.
6.16
6.17
6.18
Readers-Writers Problem
A data set is shared among a number of concurrent processes
Readers only read the data set; they do not perform any
updates
one single writer can access the shared data at the same time
Shared Data
Data set
6.19
do {
wait (wrt) ;
//
writing is performed
signal (wrt) ;
} while (TRUE);
6.20
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);
6.21
Dining-Philosophers Problem
Shared data
6.22
6.23
6.24
Monitors
procedure Pn () {}
Initialization code ( .) { }
}
}
6.25
6.26
Condition Variables
condition x, y;
Two operations on a condition variable:
6.27
6.28
Variables
wait(mutex);
body of F;
if (next_count > 0)
signal(next)
else
signal(mutex);
6.29
Monitor Implementation
6.30
Monitor Implementation
The operation x.signal can be implemented as:
if (x-count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
6.31
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads
6.32
Solaris Synchronization
Implements a variety of locks to support multitasking, multithreading
Uses adaptive mutexes for efficiency when protecting data from short code
segments
6.33
Windows XP Synchronization
Uses interrupt masks to protect access to global resources on uniprocessor
systems
semaphores
6.34
Linux Synchronization
Linux:
Linux provides:
semaphores
spin locks
6.35
Pthreads Synchronization
Pthreads API is OS-independent
It provides:
mutex locks
condition variables
read-write locks
spin locks
6.36
Atomic Transactions
System Model
Log-based Recovery
Checkpoints
Concurrent Atomic Transactions
6.37
System Model
Assures that operations happen as a single logical unit of work, in its
6.38
crashes
6.39
Log-Based Recovery
Record to stable storage information about all modifications by a transaction
Most common is write-ahead logging
Log on stable storage, each log record describes single transaction write
operation, including
Transaction name
Old value
New value
occurs
6.40
6.41
Checkpoints
Checkpoint scheme:
1.
2.
3.
Now recovery only includes Ti, such that Ti started executing before the
most recent checkpoint, and all transactions after Ti All other transactions
already on stable storage
6.42
Concurrent Transactions
Must be equivalent to serial execution serializability
Could perform all transactions in critical section
6.43
Serializability
Consider two data items A and B
Consider Transactions T0 and T1
Execute T0, T1 atomically
Execution sequence called schedule
Atomically executed transaction order called serial schedule
For N transactions, there are N! valid serial schedules
6.44
Schedule 1: T0 then T1
6.45
Nonserial Schedule
Nonserial schedule allows overlapped execute
dont conflict
S is conflict serializable
6.46
6.47
Locking Protocol
Ensure serializability by associating lock with each data item
Locks
Shared Ti has shared-mode lock (S) on item Q, Ti can read Q but not
write Q
6.48
6.49
Timestamp-based Protocols
Select order among transactions in advance timestamp-ordering
Transaction Ti associated with timestamp TS(Ti) before Ti starts
6.50
If TS(Ti) W-timestamp(Q)
6.51
Timestamp-ordering Protocol
Suppose Ti executes write(Q)
6.52
6.53
End of Chapter 6