Monitors, Condition Variables, and Readers-Writers - PowerPoint PPT Presentation

About This Presentation
Title:

Monitors, Condition Variables, and Readers-Writers

Description:

Title: PowerPoint Presentation Last modified by: Xin Yuan Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show (4:3) Other titles – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 60
Provided by: fsu49
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Monitors, Condition Variables, and Readers-Writers


1
Monitors, Condition Variables, and Readers-Writers
2
Motivation for Monitors
  • Semaphores are a big step from the low-level
    loads and stores
  • However, semaphores are used for both mutual
    exclusion and synchronization
  • The idea of monitors is to separate these two
    concerns

3
Monitor Defined
  • Monitor a lock condition variables
  • Lock provides mutual exclusion to shared data
  • Condition variable provides a queue for waiting
    threads inside a critical section
  • Enabling synchronization between processes.

4
Locks
  • A lock provides two operations
  • LockAcquire()
  • Wait until the lock is free, then grab it
  • LockRelease()
  • Unlock, wake up anyone waiting to Acquire
  • A lock is initially free

5
More on Locks
  • Always acquire a lock before accessing a shared
    data structure
  • Always release the lock after finishing with the
    shared data structure

6
An Example of Using Locks
  • AddToQueue()
  • Lock.Acquire()
  • // put 1 item to the queue
  • Lock.Release()
  • RemoveFromQueue()
  • Lock.Acquire()
  • // if something on the queue
  • // remove 1 item form the queue
  • Lock.Release()
  • return item

7
Condition Variables
  • We need additional mechanisms to wait inside
    locked regions
  • However, holding the lock while waiting prevents
    other threads from entering the locked region
  • Condition variables make it possible to sleep
    inside a critical section
  • Atomically release the lock go to sleep

8
Condition Variables
  • Each condition variable
  • Consists of a queue of threads
  • Provides three operations
  • Wait()
  • Atomically release the lock and go to sleep
  • Reacquire lock on return
  • Signal()
  • Wake up one waiting thread, if any
  • Broadcast()
  • Wake up all waiting threads

9
Condition Variables
  • Note
  • The three operations can only be used inside
    locked regions

10
An Example of Using Condition Variables
  • AddToQueue()
  • lock.Acquire()
  • // put 1 item to the queue
  • condition.Signal(lock)
  • lock.Release()
  • RemoveFromQueue()
  • lock.Acquire()
  • while nothing on queue
  • condition.Wait(lock)
  • lock.Release()
  • return item

11
Hoare vs. Mesa Monitors
  • Hoare Monitors (used in most textbooks)
  • Signal() transfers the CPU directly to a waiting
    thread

12
Mesa vs. Hoare Monitors
  • Mesa Monitors (used in most real operating
    systems)
  • Signal() only puts a waiting thread on the
    schedulers ready queue
  • By the time the waken thread gets the CPU, the
    waiting condition may no longer be true and needs
    to be retested

13
Readers-Writers Problem
  • Commonly seen in database applications
  • Readers never modify the database
  • Writers read and modify the database
  • We want one writer at a time, but many readers at
    the same time

14
Constraints
  • A reader should wait when a writer is accessing
    or waiting for the database
  • Condition okToRead
  • A write should wait when there is a reader or
    writer accessing the database
  • Condition okToWrite
  • A reader or a writer should wait when someone is
    modifying global states
  • Lock lock

15
Basic Structure of the Solution
  • Reader
  • Wait until no writers
  • Access database
  • Wake up waiting writers
  • Writer
  • Wait until no active readers or writers
  • Access database
  • Wake up waiting readers or writers

16
Developing the Solution
  • Global states
  • activeReaders 0
  • activeWriters 0
  • waitingReaders 0
  • waitingWriters 0
  • Condition okToRead NULL
  • Condition okToWrite NULL
  • Lock lock FREE

17
Put in Locks and Some Global States
  • Reader()
  • lock.Acquire()
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • lock.Release()

18
Add the Wait Condition for Reader
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • lock.Release()

19
Add the Wait Condition for Writer
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • lock.Release()

20
Add the Signal Condition for Reader
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • lock.Release()

21
Add the Signal Condition for Writer
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

22
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

23
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

24
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

25
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

26
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

27
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

28
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

29
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

30
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

31
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

32
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

33
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

34
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

35
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

36
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

37
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

38
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

39
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

40
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

41
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

42
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

43
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

44
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

45
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

46
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

47
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

48
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

49
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

50
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

51
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

52
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

53
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

54
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

55
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

56
Code Demonstration
  • Reader()
  • lock.Acquire()
  • while (activeWriters gt 0
  • waitingWriters gt 0)
  • waitingReaders
  • okToRead.Wait(lock)
  • --waitingReaders
  • activeReaders
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeReaders
  • if (activeReaders 0
  • waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • lock.Release()
  • Writer()
  • lock.Acquire()
  • while (activeWriters gt 0
  • activeReaders gt 0)
  • waitingWriters
  • okToWrite.Wait(lock)
  • --waitingWriters
  • activeWriters
  • lock.Release()
  • // access database
  • lock.Acquire()
  • --activeWriters
  • if (waitingWriters gt 0)
  • okToWrite.Signal(lock)
  • else if (waitingReaders gt 0)
  • okToRead.Broadcast(lock)
  • lock.Release()

57
Semaphores vs. Monitors
  • Can we implement monitors with semaphores?
  • Wait()
  • P(s)
  • Signal()
  • V(s)

58
Semaphores vs. Monitors
  • Not quite
  • Condition variables only work inside a lock
  • Using semaphores inside a lock may deadlock

59
Semaphores vs. Monitors
  • Condition variables have no history, but
    semaphores have
  • Signal() with an empty queue does nothing
  • A subsequent Wait() will wait
  • V() increments semaphore
  • A subsequent P() will not wait
Write a Comment
User Comments (0)
About PowerShow.com