43
Process Synchronization Mehdi Kargahi School of ECE University of Tehran Spring 2008

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

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M. Kargahi (School of ECE)

Timestamp-Based Protocols

� Properties� Deadlock-free: no transaction ever waits

� Not stronger than 2PL and vice versa