+ All Categories
Home > Documents > L19: Monitors (I) - UPMbabel.upm.es/teaching/concurrencia/material/slides/G4F1M/19.pdf · 1 L19:...

L19: Monitors (I) - UPMbabel.upm.es/teaching/concurrencia/material/slides/G4F1M/19.pdf · 1 L19:...

Date post: 07-Nov-2018
Category:
Upload: nguyennga
View: 218 times
Download: 0 times
Share this document with a friend
41
L19: Monitors (I) Este texto se distribuye bajo los t´ erminos de la Creative Commons License esar S´ anchez Grado en Ingenier´ ıa Inform´ atica Grado en Matem´ aticas e Inform´ atica Universidad Polit´ ecnica de Madrid Wed, 22-April-2015 Under construction! Do not print
Transcript

1

L19: Monitors (I)

Este texto se distribuye bajo los terminos de la Creative Commons License

Cesar Sanchez

Grado en Ingenierıa InformaticaGrado en Matematicas e Informatica

Universidad Politecnica de Madrid

Wed, 22-April-2015

Underco

nstructi

on! Donot prin

t

2

HW9: Multibuffer con Monitores

Homework:HW1: Creacion de threads en JavaHW2: Provocar una condicion de carreraHW3: Garanatizar la exclusion mutua con espera activaHW4: Garantizar la exlusion mutua con semaforosHW5: Almacen de un dato con semaforosHW6: Almacen de varios datos con semaforosHW7: Especificacion de un recurso compartidoHW8: Multibuffer con metodos synchronizedHW9: Multibuffer con “Monitores”

Fecha de Cierre:Lunes 27-Abril-2015 23:59

Entrega online:http://lml.ls.fi.upm.es/~entrega

3

HW9: Multibuffer con Monitores

3

HW9: Multibuffer con Monitores

Use cclib.Do not use Java locks&conditions!

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

prod1

prod2

prod3

cons1

cons2

cons3

example: producer consumer

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

prod1

prod2

prod3

cons1

cons2

cons3

example: producer consumer

ready queue

wait set

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

prod1

prod2

prod3

cons1

cons2

cons3

example: producer consumer

= F

alm(7)

alm(2)

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod2

cons1

cons2

cons3prod1

prod3

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod2

cons1

cons2

cons3prod1

prod3

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

cons1

prod2 cons2

cons3prod1

prod3

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod2 cons2

cons3

prod3 cons1

prod1

= T

= 7

CPRE for prod3 is false!

prod2

prod1

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod2

prod1

prod2

prod1

cons2

cons3

= T

= 7

prod3cons1

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod2

prod1

prod2

prod1

cons2

cons3

prod3

= T

= 7cons1

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

cons2

cons3

cons1

prod3wait-set

prod2

prod1

prod2

prod1

recheck CPRE

= F

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod2

prod1

prod2

prod1

cons2

cons3

cons1

prod3

= F

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

example: producer consumer

prod3

prod2

prod1

prod2

prod1

cons2

cons3

cons1

4

Conditional Synchronization (with synchronize/notify())

hayDato?

Almacen1

getput(x)

d

prod1

prod2

prod3

cons1

cons2

cons3

example: producer consumer

= T

= 3

5

Problems with synchronize/notify()

hayDato?

Almacen1

getput(x)

d

prod1

prod2

prod3

cons1

cons2

cons3

= T

= 3

notify() does not guarantee which thread to wake-up

notifyAll() wakes up all threads

The ready queue is not (necessarily) FIFO PROBLEMS:InefficientStarvation

6

Monitors

GOAL: Implement resources with Monitors

6

Monitors

GOAL: Implement resources with Monitors

One wait-set

Non-deterministic

Java synchronized

6

Monitors

GOAL: Implement resources with Monitors

One wait-set Several (associated) wait-sets

Non-deterministic FIFO queues

Java synchronized Monitors

7

Monitors (1. Mutual exclusion)

To import the monitors from cclib:

import es.upm.babel.cclib.Monitor;

7

Monitors (1. Mutual exclusion)

To create a monitor:

// data members:

Monitor mutex;

// constructor:

mutex = new Monitor();

Monitors are objects.

To import the monitors from cclib:

import es.upm.babel.cclib.Monitor;

7

Monitors (1. Mutual exclusion)

To create a monitor:

// data members:

Monitor mutex;

// constructor:

mutex = new Monitor();

Monitors are objects.

To import the monitors from cclib:

import es.upm.babel.cclib.Monitor;

To use the monitor:

public Producto extraer() {

mutex.enter();

//...

mutex.leave();

return result;

}

7

Monitors (1. Mutual exclusion)

To create a monitor:

// data members:

Monitor mutex;

// constructor:

mutex = new Monitor();

Monitors are objects.

To import the monitors from cclib:

import es.upm.babel.cclib.Monitor;

To use the monitor:

public Producto extraer() {

mutex.enter();

//...

mutex.leave();

return result;

}

hayDato?

Almacen1

getput(x)

d

= T

= 3

7

Monitors (1. Mutual exclusion)

To create a monitor:

// data members:

Monitor mutex;

// constructor:

mutex = new Monitor();

Monitors are objects.

To import the monitors from cclib:

import es.upm.babel.cclib.Monitor;

To use the monitor:

public Producto extraer() {

mutex.enter();

//...

mutex.leave();

return result;

}

hayDato?

Almacen1

getput(x)

d

= T

= 3 mutex

ready queue (FIFO)

8

Monitors (2. Conditional Synchronization)

Conditions are also objects!

8

Monitors (2. Conditional Synchronization)

Conditions are also objects!

Each Condition has its own FIFO wait-set.

8

Monitors (2. Conditional Synchronization)

To create a condition:

// data members:

Monitor.Cond cAlmacenar;

Monitor.Cond cExtraer;

// constructor:

cAlmacenar = mutex.newCond();

cExtraer = mutex.newCond();

Conditions are also objects!

To use the condition:

//...

cExtraer.await();

//...

cExtraer.signal();

//...

Each Condition has its own FIFO wait-set.

8

Monitors (2. Conditional Synchronization)

To create a condition:

// data members:

Monitor.Cond cAlmacenar;

Monitor.Cond cExtraer;

// constructor:

cAlmacenar = mutex.newCond();

cExtraer = mutex.newCond();

Conditions are also objects!

To use the condition:

//...

cExtraer.await();

//...

cExtraer.signal();

//...

Each Condition has its own FIFO wait-set.

hayDato?

Almacen1

getput(x)

d

= T

= 3 mutex

8

Monitors (2. Conditional Synchronization)

To create a condition:

// data members:

Monitor.Cond cAlmacenar;

Monitor.Cond cExtraer;

// constructor:

cAlmacenar = mutex.newCond();

cExtraer = mutex.newCond();

Conditions are also objects!

To use the condition:

//...

cExtraer.await();

//...

cExtraer.signal();

//...

Each Condition has its own FIFO wait-set.

hayDato?

Almacen1

getput(x)

d

= T

= 3 mutex

cExtraer (FIFO)

cAlmacenar (FIFO)

9

Signaling Policies

There are different posible guarantees aboutwhich thread continues running after a signal()

9

Signaling Policies

Signal & Continue (SC) : The process who signal keep the mutual exclusionand the signaled will be awaken but need to acquire the mutual exclusionbefore going.

There are different posible guarantees aboutwhich thread continues running after a signal()

9

Signaling Policies

Signal & Continue (SC) : The process who signal keep the mutual exclusionand the signaled will be awaken but need to acquire the mutual exclusionbefore going.

Signal & Wait (SW) : The signaler is blocked and must wait for mutualexclusion to continue and the signaled thread is directly awaken and canstart continue its operations.

There are different posible guarantees aboutwhich thread continues running after a signal()

9

Signaling Policies

Signal & Continue (SC) : The process who signal keep the mutual exclusionand the signaled will be awaken but need to acquire the mutual exclusionbefore going.

Signal & Wait (SW) : The signaler is blocked and must wait for mutualexclusion to continue and the signaled thread is directly awaken and canstart continue its operations.

Signal & Urgent Wait (SU) : Like SW but the signaler thread has theguarantee than it would go just after the signaled thread

There are different posible guarantees aboutwhich thread continues running after a signal()

9

Signaling Policies

Signal & Continue (SC) : The process who signal keep the mutual exclusionand the signaled will be awaken but need to acquire the mutual exclusionbefore going.

Signal & Wait (SW) : The signaler is blocked and must wait for mutualexclusion to continue and the signaled thread is directly awaken and canstart continue its operations.

Signal & Urgent Wait (SU) : Like SW but the signaler thread has theguarantee than it would go just after the signaled thread

Signal & Exit (SX) : The signaler exits from the method directly after thesignal and the signaled thread can start directly.

There are different posible guarantees aboutwhich thread continues running after a signal()

9

Signaling Policies

Signal & Continue (SC) : The process who signal keep the mutual exclusionand the signaled will be awaken but need to acquire the mutual exclusionbefore going.

Signal & Wait (SW) : The signaler is blocked and must wait for mutualexclusion to continue and the signaled thread is directly awaken and canstart continue its operations.

Signal & Urgent Wait (SU) : Like SW but the signaler thread has theguarantee than it would go just after the signaled thread

Signal & Exit (SX) : The signaler exits from the method directly after thesignal and the signaled thread can start directly.

There are different posible guarantees aboutwhich thread continues running after a signal()

cclib: SC + signaled thread becomes the first in the ready queue

10

Monitors: summary

Syntax:

class Monitor

enter();

leave();

newCond();

class Monitor.Cond

await();

signal();

10

Monitors: summary

Syntax:

class Monitor

enter();

leave();

newCond();

class Monitor.Cond

await();

signal();

Semantics:• enter(): grabs the monitor lock• leave(): returns the monitor lock• newCond(): creates a condition associated with the monitor• await(): puts thread to sleep in the wait-set of the condition and frees

the lock of the monitor• signal(): awakes the oldest thread in the wait-set and puts it first in

the ready queue

11

Almacen1Monitor.java: almacenar()

public void almacenar(Producto producto) {

// access protocol

mutex.enter();

// CPRE:

if (hayDato) {

cAlmacenar.await();

}

// INV: !hayDato

// Critical Section

almacenado = producto;

hayDato = true;

// End critical section

// synchronization code for extraer() CPRE

cExtraer.signal();

mutex.leave();

}

12

Almacen1Monitor.java: extraer()

public Producto extraer() {

Producto result;

// Access protocol

mutex.enter();

// CPRE

if (!hayDato) {

cExtraer.await();

}

// INV: hayDato

// Critical Section

result = almacenado;

almacenado = null;

hayDato = false;

// End critical section

// code for almacenar() CPRE

cAlmacenar.signal();

mutex.leave();

return result;

}


Recommended