Semaphore vs. Monitor - PowerPoint PPT Presentation

1 / 3
About This Presentation
Title:

Semaphore vs. Monitor

Description:

... thread, the caller yields the monitor (Hoare type) or continues (Mesa Type). Only one of the caller or the released thread can continue, but not both. ... – PowerPoint PPT presentation

Number of Views:574
Avg rating:3.0/5.0
Slides: 4
Provided by: csBg
Category:

less

Transcript and Presenter's Notes

Title: Semaphore vs. Monitor


1
Semaphore vs. Monitor
Condition Variables Semaphores
Can only be used in monitors Can be used anywhere in a program.
Wait() always blocks the caller. Down() does not always block the caller (i.e., when the semaphore counter is greater than zero).
Signal() either releases a blocked thread, if there is one, or the signal is lost as if it never happens. Up() either releases a blocked thread, if there is one, or increases the semaphore counter
If Signal() releases a blocked thread, the caller yields the monitor (Hoare type) or continues (Mesa Type). Only one of the caller or the released thread can continue, but not both. If Up() releases a blocked thread, the caller and the released thread both continue.
While the programmer is the one to arrange for
mutual exclusion with Semaphores, it is up to the
compiler to implement the mutual exclusion on
monitors entries.
2
Monitor reader_writer int numberOfReaders
0 boolean busy FALSE condition okToRead,
okToWrite public startRead if(busy
(okToRead.queue)) okToRead.wait numberOfReader
s numberOfReaders 1 okToRead.signal
finishRead numberOfReaders
numberOfReaders - 1 if(numberOfReaders 0)
okToWrite.signal startWrite
if((numberOfReaders ! 0) busy)
okToWrite.wait busy TRUE
finishWrite busy FALSE if(okToWrite.
queue) okToWrite.signal else okToRead.signal
  • //Semaphore
  • typedef int semaphoresemaphore mutex
    1semaphore db 1int rc 0
  • void startRead()
  • down(mutex) rc rc 1 if(rc
    1) down(db) up(mutex)
  • Void finishRead()
  • down(mutex) rc rc - 1 if(rc
    0) up(db) up(mutex)
  • Void startWrite()
  • down(db)
  • Void finishWrite()
  • up(db)

(1)
(2)
3
  • Examine the similarity between semaphores in
    program_1 and monitors in program_2.
  • The two programs dont behave the same behavior.
    Think what it takes to implement program_2 with
    semaphores.
Write a Comment
User Comments (0)
About PowerShow.com