process synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/course...
TRANSCRIPT
![Page 1: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/1.jpg)
Process Synchronization
Mehdi KargahiSchool of ECE
University of TehranSpring 2008
![Page 2: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/2.jpg)
M. Kargahi (School of ECE)
Producer-Consumer (Bounded Buffer)
� Producer
� Consumer
![Page 3: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/3.jpg)
M. Kargahi (School of ECE)
Race Condition
� Producer
� Consumer
![Page 4: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/4.jpg)
M. Kargahi (School of ECE)
Critical Sections
� Structure of a Typical Process
� Requirements for Solving critical-section problem:� Mutual exclusion
� Progress
� Bounded waiting
![Page 5: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/5.jpg)
M. Kargahi (School of ECE)
Two-Process Solutions
� Algorithm 1� Shared: int turn;
Pi:
while (TRUE) {
while (turn!=i);
critical-section
turn = 1-i;
remainder-section
}
Mutual exclusion yes
Progress no
Bounded waiting yes
![Page 6: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/6.jpg)
M. Kargahi (School of ECE)
Two-Process Solutions
� Algorithm 2� Shared: boolean flag[2];
Pi:
flag[i] = FALSE;
do {
flag[i] = TRUE;
while (flag[j]);
critical-section
flag[i] = FALSE;
remainder-section
} while (TRUE);
Mutual exclusion yes
Progress no
Bounded waiting yes
![Page 7: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/7.jpg)
M. Kargahi (School of ECE)
Peterson’s Solution
![Page 8: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/8.jpg)
M. Kargahi (School of ECE)
Eisenberg & McGuire Solution
![Page 9: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/9.jpg)
Eisenberg & McGuire Solution
![Page 10: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/10.jpg)
M. Kargahi (School of ECE)
Synchronization Hardware
� Atomic instructions: TestAndSet
![Page 11: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/11.jpg)
M. Kargahi (School of ECE)
Synchronization Hardware
� Atomic instructions: Swap
![Page 12: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/12.jpg)
M. Kargahi (School of ECE)
Synchronization Hardware
� Spin lock� Methods that use busy waiting
� n-process solution with bounded-waiting
![Page 13: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/13.jpg)
M. Kargahi (School of ECE)
Synchronization Hardware
![Page 14: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/14.jpg)
M. Kargahi (School of ECE)
Semaphores
� A semaphore S is an integer variable� Two atomic operations
![Page 15: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/15.jpg)
M. Kargahi (School of ECE)
Semaphore Usage
� Critical-section
� Process synchronization
� Problems: Spin-lock & bounded-waiting
![Page 16: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/16.jpg)
M. Kargahi (School of ECE)
A Better Definition
![Page 17: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/17.jpg)
M. Kargahi (School of ECE)
Semantics
� The value of semaphore specifies the number of processes waiting on that semaphore
� It is assumed that wait and signal are executed atomically, while this is not simply valid
� Method 1: disabling interrupts to prevent interleaving of wait and signal operations on a semaphore� In multiprocessor systems, interrupts on all processors
should be disabled resulting in quite low performance
� Method 2: using one of the previous critical-section methods (busy waiting)� More proper for MP systems� Wait and signal have a few instructions resulting in short
critical sections� Busy waiting is not problematic
![Page 18: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/18.jpg)
M. Kargahi (School of ECE)
Deadlocks
� What is the difference between deadlock and starvation?
![Page 19: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/19.jpg)
M. Kargahi (School of ECE)
Classical Problems of Synchronization
� The bounded-buffer problem
Producer Consumer
![Page 20: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/20.jpg)
M. Kargahi (School of ECE)
The Readers-Writers Problem
Writer Reader
![Page 21: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/21.jpg)
M. Kargahi (School of ECE)
The Dining Philosophers Problem
![Page 22: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/22.jpg)
M. Kargahi (School of ECE)
The Dining Philosophers Problem
� Problem?� Three solutions !!� Is a deadlock-free solution also starvation-free?
![Page 23: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/23.jpg)
M. Kargahi (School of ECE)
Monitors
� Problems with semaphores (programming faults):
![Page 24: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/24.jpg)
M. Kargahi (School of ECE)
Syntax of a Monitor
![Page 25: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/25.jpg)
M. Kargahi (School of ECE)
A Monitor Scheme
![Page 26: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/26.jpg)
M. Kargahi (School of ECE)
Monitor with Condition Variables
![Page 27: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/27.jpg)
M. Kargahi (School of ECE)
When x.signal() is Invoked by P
� Two possibilities exist:
![Page 28: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/28.jpg)
M. Kargahi (School of ECE)
Dining-Philosophers Solution using Monitors
![Page 29: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/29.jpg)
M. Kargahi (School of ECE)
Dining-Philosophers Solution using Monitors
![Page 30: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/30.jpg)
M. Kargahi (School of ECE)
Implementing a Monitor using Semaphores
� External procedure F is replaced by
x.wait() x.signal()
![Page 31: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/31.jpg)
M. Kargahi (School of ECE)
Resuming Processes within a Monitor
� x.wait(c), where c is called a priority number
![Page 32: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/32.jpg)
M. Kargahi (School of ECE)
Similar Mechanisms
� Down: wait
� Up: signal
![Page 33: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/33.jpg)
M. Kargahi (School of ECE)
Problems with Monitors
![Page 34: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/34.jpg)
M. Kargahi (School of ECE)
Atomic Transactions
� Transaction� A collection of instructions that performs a single
logical function
� A transaction may commit or abort� When a transaction is aborted, the state of the data
accessed by it is restored to what it was before executing that transaction, i.e., it is rolled back
� The execution of a transaction should be assumed as atomic
![Page 35: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/35.jpg)
M. Kargahi (School of ECE)
Different Types of Storage
� Volatile storage
� Non-Volatile storage
� Stable storage
![Page 36: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/36.jpg)
M. Kargahi (School of ECE)
Log-Based Recovery
� Log is stored on the stable storage
� Write-ahead log� Every log precedesa single write operation by
� Transaction name
� Data item name
� Old value
� New value
� Special log records� <Ti starts>
� <Ti commits>
![Page 37: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/37.jpg)
M. Kargahi (School of ECE)
Recovery Algorithm
� Undo(Ti): restores the value of all data updates by Ti to the old values
� Redo(Ti): sets the value of all data updates by Ti to the new values
� If Ti aborts, we need to execute undo(Ti)
� After a system failure�
�
![Page 38: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/38.jpg)
M. Kargahi (School of ECE)
Concurrent Atomic Transactions
� Serializability
� Restrictive
� Serial vs. non-serial schedules
Serial Non-serial but serializable
� Conflicting operations: Oi and Oj are conflicting if they access the same data item and at least one of them is a write operation
![Page 39: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/39.jpg)
M. Kargahi (School of ECE)
Concurrent Atomic Transactions
� A conflict serializable schedule
� If a schedule S can be transformed to a serial schedule S’ by a series of swaps of non-conflicting operations
![Page 40: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/40.jpg)
M. Kargahi (School of ECE)
Locking Protocol
� Shared vs. exclusive lock
� Two-phase locking (2PL) protocol�
�
� Properties� Ensures conflict Serializability
� It is not deadlock-free
� There may be conflict-serializable schedules that cannot be obtained by 2PL
� Additional information on transactions may improve 2PL
![Page 41: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/41.jpg)
M. Kargahi (School of ECE)
Timestamp-Based Protocols
� Each transaction Ti is assigned a timestamp in an ascending order of time by the system
� Each data item Q has two timestamps
![Page 42: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/42.jpg)
M. Kargahi (School of ECE)
Timestamp-Based Protocols
� If transaction Ti issues read(Q)
� If transaction Ti issues write(Q)
� Each transaction that is rolled is restarted with a new timestamp
![Page 43: Process Synchronization - ce.sharif.irce.sharif.ir/courses/86-87/2/ce424/resources/root/Course Slides/6... · Process synchronization Problems : Spin-lock & bounded-waiting. M. Kargahi](https://reader035.vdocuments.net/reader035/viewer/2022062414/5ec879b17dc0d5242f66e5ca/html5/thumbnails/43.jpg)
M. Kargahi (School of ECE)
Timestamp-Based Protocols
� Properties� Deadlock-free: no transaction ever waits
� Not stronger than 2PL and vice versa