44
Concurrency: monitors & condition synchronization ©Magee/Kramer Concurrency 5 - Monitors & Condition Synchronization

Concurrency 5 - Monitors & Condition Synchronization

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Concurrency

5 - Monitors &

Condition Synchronization

��������� ��������� ��������

��������������������������������������������������������� ��

Page 2: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Repetition - Interference (Ornamental Garden Problem)

Garden

WestTurnstile

EastTurnstile

people

!��"���������������� ��������������������������������#��������������������� ����#������������#���#� ��$������������������������$��� ����%&����$� �������'�

�������

()*

Page 3: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Repetition - Running the Applet

����������+��������,������������������������ �������� �� ���������� ���������������-������������"��"��� �������������������� ��������� ���"��$����

Page 4: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Repetition - Model Checking (reveals the error)

.��� ������/�����������������������������

• 0������� ���"����� ������������������ ������ERROR�

Trace to property violation in TEST:goeast.arriveeast.value.read.0west.arrivewest.value.read.0east.value.write.1west.value.write.1enddisplay.value.read.1wrong

���� "���� ����������������"���������� ������ERROR������

���� "���� ����������������"���������� ������ERROR������

||TESTGARDEN = ( GARDEN || TEST ).

Page 5: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Repetition - Interference and Mutual Exclusion

� 1��������� ��%���'�

� ��������� �������%���'�

� ��������� �������� �������%23!'�

x = x + 1; || x = x + 1;

synchronized (obj) {x = x + 1;

}

LOCK = ( acq -> rel -> LOCK).

on

off

0 1

0&4.��+ 4.��+

� 5

���

synchronized (obj) {x = x + 1;

}66

Page 6: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Monitors & Condition Synchronization

��� �"���� ����������� �"�������������7�� ����"�� ������ �������� �������7� ����������$� �����8������������� ����"�� ������� ����������� ������

������� �������

������� ��������� �����

!�� �� �� "����������������$� �����8��� �������%�� ������'�#���%'- �����$%' ���������$���%' ���� ����������$� ����������������� ����������� �������������� �

Page 7: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

5.1 Condition Synchronization (Car Park)

�� ��������� �����5����������������

• ���� ������$�������#������������

• ���� ������$������#��������� "�$�%���9'

Page 8: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Model (Actions and Processes)

♦� ������������������

•arrive

•depart

♦1������$�"�� ������

•�������

•�"�������

•�������

��

Page 9: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Model (Structure Diagram)

♦� ������������������

•arrive

•depart

♦1������$�"�� ������

•�������

•�"�������

•�������

ARRIVALS CARPARK

CONTROLDEPARTURESarrive depart

CARPARK

��

Page 10: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Model (FSP)

/�������� ����� ������������ �������arrive ����depart

ARRIVALS = (arrive -> ARRIVALS).

DEPARTURES = (depart -> DEPARTURES).

CONTROL(N=4) = SPACES[N],

SPACES[i:0..N] = ( when(i>0) arrive -> SPACES[i-1]| when(i<N) depart -> SPACES[i+1]).

||CARPARK = (ARRIVALS || DEPARTURES || CONTROL(4)).

����

Page 11: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Program

♦�����

♦: �����������������"�� ����� ������ ��������������������

♦ !����� : ���������������$�������� ���� ��������

♦������ : ������ �����$�#�� ������������%���"��'�� ������

♦ ������ : ������� �����$�#�� �����"��������%��"��'�� ������

���������������

• ��������• �"��������• ��������

������ ;<��������������� ;<���������������� ;<��� ������

Page 12: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Program (Interesting part of Class Diagram)

������� �"�������

=�������

���!���������������%'��"���%'

��"��� ��"���

� ���%������'

� ���%������'

!������% ������'

Page 13: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

public void start() {CarParkControl c = new DisplayCarPark(disp, PLACES);arrivals.start( new Arrivals(c));departures.start( new Departures(c));

}

Car Park Program - Applet::start()

>����""���?�������%' ������ �������• ����������� ������ %#���� ����������$� ���'• ������� ������• ���������� ������

>�������������� ��������� �$�������� �������������� �������

Page 14: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Program - Arrivals and Departures threads

class Arrivals implements Runnable {CarParkControl carpark;

Arrivals(CarParkControl c) { carpark = c; }

public void run() {try {

while ( true ) {ThreadPanel.rotate(330);carpark. arrive ();ThreadPanel.rotate(30);

}} catch (InterruptedException _) {}

}}

@�#����#��� "�� �������� ��������� �����������A

3� �����$-���������� �����

carpark. depart ()

Page 15: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Car Park Program - CarParkControl Monitor

class CarParkControl {protected int spaces, capacity;

CarParkControl( int n) {capacity = spaces = n;

}

synchronized void arrive () {... --spaces; ...

}

synchronized void depart () {... ++spaces; ...

}}

�����������$� �����8�����A�

��� ���������A�%�"� ��;;('

��� ������ "�$A�%�"� ��;;&'

��������� �������B synchronized

+� �"��������B protected

Page 16: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Condition Synchronization in Java

����"��������������������� ���� ����!"���%����"��� ����'�

public final void wait() throws InterruptedException;

public final void notify();

public final void notifyAll();

,�������������������� %���������������������������notify '�=�������������$� �����8�������� ������ ������#����������C�

,������������-������������ ������� 5����������$� ������ ��

,������"�%��������'����������#����������������C� �?��5�����

Object ���� �������

Page 17: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Condition Synchronization in Java (enter/exit)

���������•••• #���� �� �������#�������������� 5������������ ������ ������#�������� ������D

•••• #$��� �� �������#���������������������� ���

Thread A Thread B

wait()notify()

Monitor

data

%���&' ���������������������$�� ���� ������-"�� ���������������������������� ���� ������

Page 18: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Condition Synchronization in FSP and Java

FSP: when ( cond) action->NEWSTATE

synchronized void act() throws InterruptedException {while ( !cond ) wait ();// modify monitor datanotifyAll ();

}

>������������"������ �����$������:��������� ��������� ��� ��������������� ��� ��������������������#���������:����������� �������

�� (���&' ����� �����$�����#�����������������%�'������ �$����#�������������������� ���������#���������� ��������������������� �������

Page 19: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

CarParkControl - Condition Synchronization

class CarParkControl {protected int spaces, capacity;

synchronized void arrive() throws Int’Exc’ {while (spaces==0) wait ();--spaces;notify ();

}

synchronized void depart() throws Int’Exc’ {while (spaces==capacity) wait ();++spaces;notify ();

}}

,�$������������������������notify()������������������notifyAll() A

Page 20: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Models to Monitors - Guidelines

•••• ������ ���������%��������������� �����'����� "�� ������������������

•••• ������� ���������%��������"�������� �����'����� "�� ���������������

+� ����������� ������������ ���������� ����������� "�� ������������(����)�� ������#�� ���������#��������"���������&' ���� "�� ��������������

>���#��������"� �������������������������������� ����������� ���������

���������������������������� ������������������� ���#����������������������� (���&' %����� (&''�

Page 21: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

5.2 Semaphores

3� �"����������#����$������������������#���������:"�� �����$� �����8����������"���������$��� ��

����#�%'�� ��� �<(�� �� �� ���%�'D

���"%'�� �� �� ���%�'D

3� �"���� � ������������ ����� �����������$����:�����������

0�����$�� "�� ������������ �����#����

����#�%'���� %�<('���� �� �� ���%�'D���� ��� ����� ��������� �������"�� ���

���"%'�������� %"�� ��������� ��������'���� �#�������������� ������� �� ���%�'D

���*�+�, -�������

���*�+., -�.��"/���

Page 22: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Modelling Semaphores

const Max = 3range Int = 0..Max

SEMAPHORE(N=0) = SEMA[ N],SEMA[v:Int] = ( up->SEMA[v+1]

|when (v>0) down->SEMA[v-1]),SEMA[Max+1] = ERROR.

>�������������$8������$-�#�����$� ������� �"�����������������������������������������1������������������ �����������#��������������������+==.=���& ��������������������

����

Page 23: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Modelling Semaphores

� �������#� ������$�� �"����#���������%'���������� �"���������������������(��

� ������" ���������������

>�� ����������������up ���� up ���� up ���� up

up up

down

up

down

up

down

-1 0 1 2 3

Page 24: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Semaphore Demo - Model

LOOP = ( mutex.down ->critical-> mutex.up ->LOOP).

||SEMADEMO = (p[1..3]:LOOP|| {p[1..3]}:: mutex :SEMAPHORE(1)).

>�����"�� ������p[1..3] ��������������� �"�����mutex���������� ������$��� ������� ����%� �����critical '������ �������� ���

2��� �������� ������-������� �"���������������������0��%�(�

1������ERROR��������� ����������SEMADEMO��

1���������$ �� �"���������� �����%�����Max=1'��

����

Page 25: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Semaphore Demo - Modelp.1.mutex.down

p.2.mutex.down

p.3.mutex.down p.3.critical

p.3.mutex.up

p.2.critical

p.2.mutex.up

p.1.critical

p.1.mutex.up

0 1 2 3 4 5 6

Page 26: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Semaphores in Java

3� �"�������"��������C� ����;<��� "�� ����������������

public class Semaphore {private int value;

public Semaphore ( int n) { value = n; }

synchronized public void up() {++value;notify ();

}

synchronized public void down() throws Int’Exc’ {while (value == 0) wait ();--value;

}}

1��"�� �� �-�� �"������������1����� � ����� ���������������� "�� ���������/���1������ ������� ������ ���

1��"�� �� �-�� �"������������1����� � ����� ���������������� "�� ���������/���1������ ������� ������ ���

Page 27: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

SEMADEMO display

current semaphore value

thread 1 is executing critical actions.

thread 2 is blocked waiting.

thread 3 is executing non-critical actions.

Page 28: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

SEMADEMO

What if we adjust the time that each thread spends in its critical section ?

♦large resource requirement -more conflict?

(eg. more than 67% of a rotation)?

♦ small resource requirement -no conflict?

(eg. less than 33% of a rotation)?

Hence the time a thread spends in its critical section should be kept as short as possible.

Page 29: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

SEMADEMOProgram - MutexLoop

class MutexLoop implements Runnable {Semaphore mutex ;

MutexLoop (Semaphore sema) {mutex=sema;}

public void run() {try {

while (true) {while (!ThreadPanel.rotate());mutex . down(); // acquirewhile (ThreadPanel.rotate()); // criticalmutex . up(); // release

}} catch (InterruptedException _) {}

}} ThreadPanel.rotate() ��������false #�������� ������

���: ���� ���� ������%����� ����'�����true �����#����

>�������������� �"��������� ��������$������""����start() ������

Page 30: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

5.3 Bounded Buffer

��!�����!� �� ��������������������� �������������

1�� ������"��������������������$���������� "�� ���������� �����$��������� "�� ����

E ����!����+�� "��9

Page 31: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Bounded Buffer - a Data-Independent Model

PRODUCER BUFFER CONSUMERput get

BOUNDEDBUFFER

����

>����������� ����.0&+�022+=��������"��������������� ���������������-�������� ������ �����������������:����"������� ������

put put

get

put

get

put

get

put

get get

0 1 2 3 4 5

Page 32: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Bounded Buffer - a Data-Independent Model

PRODUCER = (put->PRODUCER).

CONSUMER = (get->CONSUMER).

BUFFER(N=5) = COUNT[0],COUNT[i:0..N] = (when (i<N) put->COUNT[i+1]

|when (i>0) get->COUNT[i-1]).

||BOUNDEDBUFFER =(PRODUCER || BUFFER(5) || CONSUMER).

Page 33: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Bounded Buffer Program - Buffer Monitor

class BufferImpl implements Buffer {protected Object[] buf;protected int in, out, count, size;…synchronized void put (Object o) throws Int’Exc’ {

while (count==size) wait ();buf[in] = o;count++;in = (in+1) % size;notifyAll ();

}}

public interface Buffer {public void put(Object o) throws InterruptedException;public Object get() throws InterruptedException;

}

We separate the interface to permit an alternative implementation later.

Page 34: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Similarly for get()

synchronized Object get () throws Int’Exc’ {1. while (count==0) wait ();2. Object obj = buf[out]; 3. buf[out] = null;4. count--;5. out = (out+1) % size;6. notifyAll ();7. return obj;

}

•••• %������������ ��������notifyAll() �����������&�*/*�� ��������0'�

•••• %��������������� �����2�

Page 35: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Bounded Buffer Program - Producer Process

class Producer implements Runnable {Buffer buf;String alpha = "abcdefghijklmnopqrstuvwxyz";

Producer(Buffer b) { buf = b; }

public void run() {try {

int i = 0;while ( true ) {

ThreadPanel.rotate(12);buf. put ( new Character(alpha.charAt(i)));i=(i+1) % alpha.length();ThreadPanel.rotate(348);

}} catch (InterruptedException _) {}

}}

3� �����$ �������#�� �� �����!� */��&'

Page 36: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

3�""��������-������������������������� ������������ ����������$� �����8�����-�#��������������*��� �"������ ��� ��������( �������� ���������������������������

5.4 Nested Monitors

class SemaBuffer implements Buffer {protected Object buf[];protected int in, out, count, size;

Semaphore full ; 33�������!��� ������Semaphore empty ; 33�������!��� �������

SemaBuffer( int s) {size = s; in = out = count = 0;buf = new Object[size];full = new Semaphore( 0);empty = new Semaphore( size );

}}

Page 37: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Nested Monitors - Bounded Buffer Program

synchronized public void put(Object o) throws Int’Exc’ {empty. down();buf[in] = o;count++;in = (in+1) % size;full. up();

}

synchronized public Object get() throws Int’Exc’ {full. down();Object o = buf[out]; buf[out] = null;count--;out = (out+1) % size;empty. up();return o;

}

��� ����� �� �������$���/��-#�� �������� ������� ��� ���8����

���������!�����������������

����( ����� �� ������������������-�#�� �������� �����������( ���8����

Page 38: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

PRODUCER = (put -> PRODUCER).

CONSUMER = (get -> CONSUMER).

SEMAPHORE(N=0) = SEMA[ N],SEMA[v:Int] = (up->SEMA[v+1]

|when (v>0) down->SEMA[v-1]).

BUFFER = (put -> empty.down -> full.up -> BUFFER|get -> full.down -> empty.up -> BUFFER).

||BOUNDEDBUFFER =( PRODUCER || BUFFER || CONSUMER

|| empty:SEMAPHORE(5)|| full:SEMAPHORE(0) ).

���������!�����������������

Nested Monitors - Bounded Buffer Model

Page 39: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

���� ����$����"���� �����"���������#���4�5�

Composingpotential DEADLOCK

States Composed: 28 Transitions: 32 in 60msTrace to DEADLOCK:

get

Nested Monitors - Bounded Buffer Model

>���Consumer ���������get �� ���� ���-�������������������� "�$��1����� ���������������������� ����������� �"�����full ��>���Producer ���������put �� ���� �������������������-������������� ����%�(�

Page 40: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

���� ����$����"���� �����"���������#���4�5�

)' ����� ��� �����3� ��������/��&'-�� 5����������� ���������������synchronized public Object get()

*' 3� �"�������&'�� 5���������������� ���������3� �"����synchronized public void down()

F' 3� �"�������&' ������������$������#����� ������������&'

G' !���� ��� �����3� ��������"��%'-���� ������������������synchronized public void put(Object)

>��������������������#�������� �������������!���*

Composingpotential DEADLOCK

States Composed: 28 Transitions: 32 in 60msTrace to DEADLOCK:

get

Nested Monitors - Bounded Buffer Model

Page 41: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

>������$�#�$�����������������������$����� �������/��1���������� "��-����������� �� �������� �����$������������������� ��������� ������������������������� 5������������� ��� �� �"������������ �� ������

public void put(Object o) throws Int’Exc’ {empty.down();synchronized (this) {

buf[in] = o;count++;in = (in+1) % size;

}full.up();

}

Nested Monitors - Revised Bounded Buffer Program

Page 42: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

>����� �"������ ��������������� �����������"���� ������� ���� ����>���������� ��$������������ "�� ���������#����������� �"������ ���������������� ���� ��������

���������!�����������������

6����)�������

BUFFER = (put -> BUFFER|get -> BUFFER).

PRODUCER = (empty.down -> put -> full.up -> PRODUCER).CONSUMER = (full.down -> get -> empty.up -> CONSUMER).

Nested Monitors - Revised Bounded Buffer Model

Page 43: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

5.5 Monitor invariants

���������� ������ ����������������������� �� ������������������������� �"���������>�������������� ���������#����������������������������� ����������������� ��������������������������( ��������$�� ��� ��� ��������

1&H%���!����������'� (�≤ �"� �� ≤ &

1&H%3� �"����'� (�≤ ����

1&H%������'� (�≤ ���� ≤ ��8����� (�≤ �� < ��8����� (�≤ ����< ��8���� �� ;�%����+ ����'�% ��8�

4������� ������������-����� ��������������#������ ������������� %#���'9

Page 44: Concurrency 5 - Monitors & Condition Synchronization

Concurrency: monitors & condition synchronization ©Magee/Kramer

Summary

��� �"���� ����������� �"�������������7�� ����"�� ������ �������� �������7� ����������$� �����8������������� ����"�� ������� ������������ ������

������� �������

������� ��������� �����

!�� �� �� "����������������$� �����8��� �������%�� ������'�#���%'- �����$%' ���������$���%' ���� ����������$� ����������������� ����������� �������������� �