COMP310 Lecture 8 Semaphores and Monitors - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

COMP310 Lecture 8 Semaphores and Monitors

Description:

signalC() might not have an effect. 9. Condition Variables and ... waiting to enter may obtain exclusive access. synchronized method1()? synchronized method2 ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 28
Provided by: mcsV
Category:

less

Transcript and Presenter's Notes

Title: COMP310 Lecture 8 Semaphores and Monitors


1
COMP310 Lecture 8Semaphores and Monitors
Ben-Ari Chapter 7
  • Monitors
  • Java Monitors
  • Project and assignment
  • Readers/Writers next lecture

2
Monitors
  • A high level synchronisation mechanism, related
    to objects in OO languages.
  • Hoare, 1974.
  • Used to encapsulate synchronisation logic.

3
Monitors
  • A monitor has some
  • private variables, like an object
  • public operations (a lot like methods)?
  • these guarantee exclusive access
  • condition variables
  • Allow processes to block and wait, or to signal.
    Somewhat like semaphores.

4
Monitors
Can't be accessed outside monitor code
monitor RWint x 0, y 0 cond c1, c2
op anOp ... op anotherOp... op
thirdOp... op fourthOp...
Condition Variables
Only one process ever executing an operation
5
Monitors
E1 E2 E3
Op1
p has exclusive access
Op2
cond c1 W1,W2
cond c2 W3,W4
E1,E2,E3 are processes waiting to enter (i.e,
obtain exclusive access)?
6
Processes and Monitors
E1 E2 E3
Op1
p finishes its operation
Op2
cond c1 W1,W2
cond c2 W3,W4
There are no guarantees about which of
E1,E2,E3 can now enter the monitor. (Starvation
possible here.)?
7
Condition Variables
  • A condition variable has an associated wait queue
    and two operations.
  • waitC(cv)
  • process enqueues itself and blocks
  • when it becomes blocked, it releases exclusive
    access
  • signalC(cv)?
  • unblocks a waiting proc. if there is one
  • has no effect if queue is empty

8
Condition Variables and Blocking Semaphores
  • A process doesn't necessarily block when calling
    wait() on a semaphore
  • waitC() always causes the process to block
  • signal() always has an effect on the semaphore
  • signalC() might not have an effect

9
Condition Variables and Blocking Semaphores
  • For weak semaphores, signal() unblocks an
    arbitrary process
  • signalC() always unblocks the process at the head
    of the queue
  • For semaphores, a process unblocked by
    signal()can begin execution immediately
  • For a condition variable, this depends on the
    signalling policy.

10
Processes and Monitors
E1 E2 E3
Op1
p has exclusive access
Op2
cond c1 W1,W2
cond c2 W3,W4
11
Processes and Monitors
E1 E2 E3
Op1
p has signalled c1
Op2
cond c1 W1,W2
cond c2 W3,W4
W1,W2 are waiting in the order shown.
12
Signalling Policies
  • So which processes can get exclusive access now?
  • The most important policies are
  • Waiting process gets exclusive access (signal and
    wait).
  • The signalling process retains exclusive access
    (signal and continue).

13
Signal and Wait (EltSltW)?
E1 E2 E3
Op1
W1 has exclusive access
Signal Q p is blocked
Op2
cond c1 W1,W2
cond c2 W3,W4
Now, p is in a queue containing signalled
processes. Runs before any other process enters.
14
Signal and Continue (EltWltS)?
E1 E2 E3
Op1
p has exclusive access
Op2
cond c1 W1,W2
cond c2 W3,W4
The signalled process can run later.
15
Evaluating Signalling Policies
  • Signal-and-wait guarantees that monitor state has
    not changed between signal of a condition and
    resumption of woken thread
  • Signal-and-continue enables signalling thread to
    change state
  • May need to recheck condition
  • Doesn't normally make a difference

16
Monitors and Java
  • Entering a synchronized corresponds to entering a
    monitor.
  • All monitor operations guarantee mutual
    exclusion.
  • In Java, you must explicitly declare synchronized
    methods.

17
Monitors and Java
  • Monitors are not condition variables, they
    contain condition variables
  • In Java, all objects behave like condition
    variables in that they have o.wait() and
    o.notify() operations.
  • Condition variables prevent starvation by using a
    wait queue.
  • In Java, threads block in a wait set, so there
    are no guarantees about the order in which they
    will be unblocked.

18
Monitors and Java
  • Processes waiting at condition variables, can
    only be unblocked one at a time.
  • In Java, you can call notifyAll() to unblock all
    waiting threads.
  • Several condition variables can be contained
    within one monitor.
  • In Java, only the object itself functions as a
    condition variable for that object.

19
Java Monitors
synchronized method1()?
E1 E2 E3
synchronized method2()?
p has exclusive access
waitset W1,W2
Order not guaranteed.
20
Java Monitors
synchronized method1()?
E1 E2 E3
synchronized method2()?
p calls this.notify()?
waitset W1,W2
Order not guaranteed.
21
Java Monitors (EWltS)?
synchronized method1()?
E1 E2 E3
synchronized method2()?
p has exclusive access
waitset W1,W2
One of W1,W2 has been unblocked
22
Java Monitors (EWltS)?
synchronized method1()?
E1 E2 E3
synchronized method2()?
p finishes its sync block
waitset W1,W2
Either the unblocked process or a process waiting
to enter may obtain exclusive access.
23
Semaphores With Monitors
monitor Semint value kcond notZero op
wait if (value0)? waitC(notZero)
value--
op signal value signalC(notZero)?
24
Semaphores With Monitors
public synchronized void semWait()? throws
InterruptedException if (value
0)? this.wait() value-- public
synchronized void semSignal()? throws
InterruptedException value this.notify()

25
Semaphores With Monitors
public synchronized void semWait()? throws
InterruptedException while (value
0)? this.wait() value--
26
Project One
  • Allocating Arbitrary Resources
  • Files
  • Forks
  • Communication channels etc
  • Processes request sets of named resources
  • Deadlock will occur if resources acquired in bad
    orders.
  • Need to analyse acquisition orders, and implement
    two of them in Java

27
Project One
  • Implement a test/driver program
  • 30 mark for implementation
  • Good, clean well commented code
  • 70 for report
  • Analysis of acquisition techniques
  • Design and documentation of your program
  • Design of experiments that explore behaviour and
    performance of strategies
  • Analysis of experimental results
Write a Comment
User Comments (0)
About PowerShow.com