Module 6: Process Synchronization
Module 6: Process Synchronization
Operating System Concepts with Java 8th Edition 6.1 Silberschatz, Galvin and Gagne 2009
Module 6: Process Synchronization
Background
The Critical-Section Problem
Petersons Solution
Synchronization Hardware
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Atomic Transactions
Operating System Concepts with Java 8th Edition 6.2 Silberschatz, Galvin and Gagne 2009
Objectives
Operating System Concepts with Java 8th Edition 6.3 Silberschatz, Galvin and Gagne 2009
Background
Operating System Concepts with Java 8th Edition 6.4 Silberschatz, Galvin and Gagne 2009
Producer
Operating System Concepts with Java 8th Edition 6.5 Silberschatz, Galvin and Gagne 2009
Consumer
while (count == 0)
; // do nothing
Operating System Concepts with Java 8th Edition 6.6 Silberschatz, Galvin and Gagne 2009
Race Condition
register1 = count
register1 = register1 + 1
count = register1
count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
Consider this execution interleaving with count = 5 initially:
T0: producer execute register1 = count {register1 = 5}
T1: producer execute register1 = register1 + 1 {register1 = 6}
T2: consumer execute register2 = count {register2 = 5}
T3: consumer execute register2 = register2 - 1 {register2 = 4}
T4: producer execute count = register1 {count = 6 }
T5: consumer execute count = register2 {count = 4}
Operating System Concepts with Java 8th Edition 6.7 Silberschatz, Galvin and Gagne 2009
Solution to Critical-Section Problem
Operating System Concepts with Java 8th Edition 6.8 Silberschatz, Galvin and Gagne 2009
Structure of a Typical Process
Operating System Concepts with Java 8th Edition 6.9 Silberschatz, Galvin and Gagne 2009
Petersons Solution
Two process solution
Assume that the LOAD and STORE instructions are atomic; that is,
cannot be interrupted.
The variable turn indicates whose turn it is to enter the critical section.
The flag array is used to indicate if a process is ready to enter the critical
section. flag[i] = true implies that process Pi is ready!
Operating System Concepts with Java 8th Edition 6.10 Silberschatz, Galvin and Gagne 2009
Algorithm for Process Pi
while (true) {
flag[i] = true;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = false;
remainder section
}
Operating System Concepts with Java 8th Edition 6.11 Silberschatz, Galvin and Gagne 2009
Solution to Critical-Section Problem
Using Locks
while (true) {
acquire lock
critical section
release lock
remainder section
}
Operating System Concepts with Java 8th Edition 6.12 Silberschatz, Galvin and Gagne 2009
Synchronization Hardware
Many systems provide hardware support for critical section code
Operating System Concepts with Java 8th Edition 6.13 Silberschatz, Galvin and Gagne 2009
Data Structure for Hardware Solutions
Operating System Concepts with Java 8th Edition 6.14 Silberschatz, Galvin and Gagne 2009
Solution using GetAndSet Instruction
Operating System Concepts with Java 8th Edition 6.15 Silberschatz, Galvin and Gagne 2009
Solution using Swap Instruction
Operating System Concepts with Java 8th Edition 6.16 Silberschatz, Galvin and Gagne 2009
Semaphore
Synchronization tool that does not require busy waiting
Operating System Concepts with Java 8th Edition 6.17 Silberschatz, Galvin and Gagne 2009
Semaphore as General Synchronization Tool
Operating System Concepts with Java 8th Edition 6.18 Silberschatz, Galvin and Gagne 2009
Java Example Using Semaphores
Operating System Concepts with Java 8th Edition 6.19 Silberschatz, Galvin and Gagne 2009
Java Example Using Semaphores
Operating System Concepts with Java 8th Edition 6.20 Silberschatz, Galvin and Gagne 2009
Semaphore Implementation
Operating System Concepts with Java 8th Edition 6.21 Silberschatz, Galvin and Gagne 2009
Semaphore Implementation with
no Busy waiting
Two operations:
block place the process invoking the operation on the
appropriate waiting queue.
wakeup remove one of processes in the waiting queue and
place it in the ready queue.
Operating System Concepts with Java 8th Edition 6.22 Silberschatz, Galvin and Gagne 2009
Semaphore Implementation with
no Busy waiting (Cont.)
Implementation of acquire():
Implementation of release():
Operating System Concepts with Java 8th Edition 6.23 Silberschatz, Galvin and Gagne 2009
Deadlock and Starvation
Deadlock two or more processes are waiting indefinitely for an event
that can be caused by only one of the waiting processes
Operating System Concepts with Java 8th Edition 6.24 Silberschatz, Galvin and Gagne 2009
Classical Problems of Synchronization
Bounded-Buffer Problem
Dining-Philosophers Problem
Operating System Concepts with Java 8th Edition 6.25 Silberschatz, Galvin and Gagne 2009
Bounded-Buffer Problem
Operating System Concepts with Java 8th Edition 6.26 Silberschatz, Galvin and Gagne 2009
Bounded-Buffer Problem
Operating System Concepts with Java 8th Edition 6.27 Silberschatz, Galvin and Gagne 2009
Bounded-Buffer insert()
Operating System Concepts with Java 8th Edition 6.28 Silberschatz, Galvin and Gagne 2009
Bounded-buffer remove()
Operating System Concepts with Java 8th Edition 6.29 Silberschatz, Galvin and Gagne 2009
Bounded-buffer producer
Operating System Concepts with Java 8th Edition 6.30 Silberschatz, Galvin and Gagne 2009
Bounded-buffer consumer
Operating System Concepts with Java 8th Edition 6.31 Silberschatz, Galvin and Gagne 2009
Bounded-buffer factory
Operating System Concepts with Java 8th Edition 6.32 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem
Problem allow multiple readers to read at the same time. Only one
single writer can access the shared data at the same time
Shared Data
Data set
Semaphore mutex initialized to 1
Semaphore db initialized to 1
Integer readerCount initialized to 0
Operating System Concepts with Java 8th Edition 6.33 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem
Operating System Concepts with Java 8th Edition 6.34 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem (Cont.)
Operating System Concepts with Java 8th Edition 6.35 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem (Cont.)
Operating System Concepts with Java 8th Edition 6.36 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem (Cont.)
The database
Operating System Concepts with Java 8th Edition 6.37 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem (Cont.)
Reader methods
Operating System Concepts with Java 8th Edition 6.38 Silberschatz, Galvin and Gagne 2009
Readers-Writers Problem (Cont.)
Writer methods
Operating System Concepts with Java 8th Edition 6.39 Silberschatz, Galvin and Gagne 2009
Dining-Philosophers Problem
Shared data
Bowl of rice (data set)
Semaphore chopStick [5] initialized to 1
Operating System Concepts with Java 8th Edition 6.40 Silberschatz, Galvin and Gagne 2009
Dining-Philosophers Problem (Cont.)
Operating System Concepts with Java 8th Edition 6.41 Silberschatz, Galvin and Gagne 2009
Problems with Semaphores
Operating System Concepts with Java 8th Edition 6.42 Silberschatz, Galvin and Gagne 2009
Monitors
Operating System Concepts with Java 8th Edition 6.43 Silberschatz, Galvin and Gagne 2009
Syntax of a Monitor
Operating System Concepts with Java 8th Edition 6.44 Silberschatz, Galvin and Gagne 2009
Schematic view of a Monitor
Operating System Concepts with Java 8th Edition 6.45 Silberschatz, Galvin and Gagne 2009
Condition Variables
Condition x, y;
Operating System Concepts with Java 8th Edition 6.46 Silberschatz, Galvin and Gagne 2009
Monitor with Condition Variables
Operating System Concepts with Java 8th Edition 6.47 Silberschatz, Galvin and Gagne 2009
Solution to Dining Philosophers
Operating System Concepts with Java 8th Edition 6.48 Silberschatz, Galvin and Gagne 2009
Solution to Dining Philosophers (Cont.)
dp.takeForks (i)
EAT
dp.returnForks (i)
Operating System Concepts with Java 8th Edition 6.49 Silberschatz, Galvin and Gagne 2009
Java Synchronization
Threads waiting to acquire the object lock are placed in the entry set
for the object lock.
Operating System Concepts with Java 8th Edition 6.50 Silberschatz, Galvin and Gagne 2009
Java Synchronization
Operating System Concepts with Java 8th Edition 6.51 Silberschatz, Galvin and Gagne 2009
Java Synchronization
Operating System Concepts with Java 8th Edition 6.52 Silberschatz, Galvin and Gagne 2009
Java Synchronization wait/notify()
When a thread invokes wait():
Operating System Concepts with Java 8th Edition 6.53 Silberschatz, Galvin and Gagne 2009
Java Synchronization
Operating System Concepts with Java 8th Edition 6.54 Silberschatz, Galvin and Gagne 2009
Java Synchronization wait/notify
Operating System Concepts with Java 8th Edition 6.55 Silberschatz, Galvin and Gagne 2009
Java Synchronization wait/notify
Operating System Concepts with Java 8th Edition 6.56 Silberschatz, Galvin and Gagne 2009
Java Synchronization - Bounded Buffer
Operating System Concepts with Java 8th Edition 6.57 Silberschatz, Galvin and Gagne 2009
Java Synchronization
The call to notify() selects an aribitrary thread from the wait set. It
is possible the selected thread is in fact not waiting upon the
condition for which it was notified.
The call notifyAll() selects all threads in the wait set and moves
them to the entry set.
Operating System Concepts with Java 8th Edition 6.58 Silberschatz, Galvin and Gagne 2009
Java Synchronization
notify() may
not notify the
correct thread!
Operating System Concepts with Java 8th Edition 6.59 Silberschatz, Galvin and Gagne 2009
Java Synchronization - Readers-Writers
Operating System Concepts with Java 8th Edition 6.60 Silberschatz, Galvin and Gagne 2009
Java Synchronization - Readers-Writers
Operating System Concepts with Java 8th Edition 6.61 Silberschatz, Galvin and Gagne 2009
Java Synchronization - Readers-Writers
Operating System Concepts with Java 8th Edition 6.62 Silberschatz, Galvin and Gagne 2009
Java Synchronization
Operating System Concepts with Java 8th Edition 6.63 Silberschatz, Galvin and Gagne 2009
Java Synchronization
Operating System Concepts with Java 8th Edition 6.64 Silberschatz, Galvin and Gagne 2009
Concurrency Features in Java 5
Operating System Concepts with Java 8th Edition 6.65 Silberschatz, Galvin and Gagne 2009
Concurrency Features in Java 5
Reentrant Locks
Operating System Concepts with Java 8th Edition 6.66 Silberschatz, Galvin and Gagne 2009
Concurrency Features in Java 5
Semaphores
Operating System Concepts with Java 8th Edition 6.67 Silberschatz, Galvin and Gagne 2009
Concurrency Features in Java 5
Operating System Concepts with Java 8th Edition 6.68 Silberschatz, Galvin and Gagne 2009
Concurrency Features in Java 5
doWork() method with condition variables
Operating System Concepts with Java 8th Edition 6.69 Silberschatz, Galvin and Gagne 2009
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads
Operating System Concepts with Java 8th Edition 6.70 Silberschatz, Galvin and Gagne 2009
Solaris Synchronization
Operating System Concepts with Java 8th Edition 6.71 Silberschatz, Galvin and Gagne 2009
Windows XP Synchronization
Operating System Concepts with Java 8th Edition 6.72 Silberschatz, Galvin and Gagne 2009
Linux Synchronization
Linux:
Prior to kernel Version 2.6, disables interrupts to implement short
critical sections
Version 2.6 and later, fully preemptive
Linux provides:
semaphores
spin locks
Operating System Concepts with Java 8th Edition 6.73 Silberschatz, Galvin and Gagne 2009
Pthreads Synchronization
It provides:
mutex locks
condition variables
Operating System Concepts with Java 8th Edition 6.74 Silberschatz, Galvin and Gagne 2009
Atomic Transactions
System Model
Log-based Recovery
Checkpoints
Concurrent Atomic Transactions
Operating System Concepts with Java 8th Edition 6.75 Silberschatz, Galvin and Gagne 2009
Transactional Memory
With
Operating System Concepts with Java 8th Edition 6.76 Silberschatz, Galvin and Gagne 2009
System Model
Assures that operations happen as a single logical unit of work, in
its entirety, or not at all
Operating System Concepts with Java 8th Edition 6.77 Silberschatz, Galvin and Gagne 2009
Types of Storage Media
Operating System Concepts with Java 8th Edition 6.78 Silberschatz, Galvin and Gagne 2009
Log-Based Recovery
Log entry must reach stable storage before operation on data occurs
Operating System Concepts with Java 8th Edition 6.79 Silberschatz, Galvin and Gagne 2009
Log-Based Recovery Algorithm
Using the log, system can handle any volatile memory errors
Undo(Ti) restores value of all data updated by Ti
Redo(Ti) sets values of all data in transaction Ti to new values
Operating System Concepts with Java 8th Edition 6.80 Silberschatz, Galvin and Gagne 2009
Checkpoints
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 <checkpoint> to the log on stable storage
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
Operating System Concepts with Java 8th Edition 6.81 Silberschatz, Galvin and Gagne 2009
Concurrent Transactions
Operating System Concepts with Java 8th Edition 6.82 Silberschatz, Galvin and Gagne 2009
Serializability
Operating System Concepts with Java 8th Edition 6.83 Silberschatz, Galvin and Gagne 2009
Schedule 1: T0 then T1
Operating System Concepts with Java 8th Edition 6.84 Silberschatz, Galvin and Gagne 2009
Nonserial Schedule
Operating System Concepts with Java 8th Edition 6.85 Silberschatz, Galvin and Gagne 2009
Schedule 2: Concurrent Serializable Schedule
Operating System Concepts with Java 8th Edition 6.86 Silberschatz, Galvin and Gagne 2009
Locking Protocol
Locks
Shared Ti has shared-mode lock (S) on item Q, Ti can read Q
but not write Q
Exclusive Ti has exclusive-mode lock (X) on Q, Ti can read and
write Q
Operating System Concepts with Java 8th Edition 6.87 Silberschatz, Galvin and Gagne 2009
Two-phase Locking Protocol
Operating System Concepts with Java 8th Edition 6.88 Silberschatz, Galvin and Gagne 2009
Timestamp-based Protocols
Operating System Concepts with Java 8th Edition 6.89 Silberschatz, Galvin and Gagne 2009
Timestamp-based Protocol Implementation
Operating System Concepts with Java 8th Edition 6.90 Silberschatz, Galvin and Gagne 2009
Timestamp-ordering Protocol
Operating System Concepts with Java 8th Edition 6.91 Silberschatz, Galvin and Gagne 2009
Schedule Possible Under Timestamp Protocol
Operating System Concepts with Java 8th Edition 6.92 Silberschatz, Galvin and Gagne 2009
End of Chapter 6
Operating System Concepts with Java 8th Edition 6.93 Silberschatz, Galvin and Gagne 2009