Beruflich Dokumente
Kultur Dokumente
Content
G52CON: Concepts of Concurrency Lecture 9: Semaphores 2 Gabriela Ochoa School of Computer Science & IT gxo@cs.nott.ac.uk Definition of semaphores Semaphores and condition synchronisation The producer-consumer problem
General description Infinite buffer: problem and solution Single element buffer: problem and solution Bounded buffer: problem and solution
Semaphores
A semaphore S is a (shared) integer variable which can
take only non-negative values A semaphore S must be initialised with a value k 0 The only permissible operations on S are the atomic actions: P and V (acquire and release in Java, wait and signal in some books) A general semaphore can have any non-negative value A binary semaphore has values 0 or 1 only
V(S)
if list of blocked processes is empty S=S+1 else unblock an arbitrary process from the list of blocked process on S
Semaphores are not only useful for implementing mutual exclusion, but can be used for general condition synchronisation.
Brian Logan 2007 G52CON Lecture 9: Semaphores 2 5
Communication between consumer-producer processes is done through a shared buffer (a queue of data elements) These processes do not take turns accessing the buffer, they both work concurrently
Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 6
23/02/2011
Producer-Consumer problem
produce data item
Producer
Consumer
10
23/02/2011
An example trace 1
// Producer process Object x = null; // Consumer process Object w = null;
n == 0 buf == []
14
An example trace 2
// Producer process Object x = null; integer in = 0; // Consumer process Object w = null; integer out = 0;
An example trace 3
// Producer process Object x = null; integer in = 0; // Consumer process Object w = null; integer out = 0; while(true)
n == 0 buf == []
n == 0 buf == []
15
16
An example trace 4
// Producer process Object x = null; integer in = 0; // Consumer process Object w = null; integer out = 0; while(true) { P(n);
An example trace 5
// Producer process Object x = null; integer in = 0; // Consumer process Object w = null; integer out = 0; while(true) { P(n);
} n == 0 buf == []
} n == 0 buf == []
17
18
23/02/2011
An example trace 6
// Producer process Object x = null; integer in = 0; while(true) // Consumer process Object w = null; integer out = 0; while(true) { P(n);
An example trace 7
// Producer process Object x = null; integer in = 0; while(true) { // produce data v ... buf[in] = x; // Consumer process Object w = null; integer out = 0; while(true) { P(n);
} n == 0 buf == []
} n == 0 buf == [o1]
19
20
An example trace 8
// Producer process Object x= null; integer in = 0; while(true) { // produce data x ... buf[in] = x; in = in + 1; } // Consumer process Object w = null; integer out = 0; while(true) { P(n);
} n == 0 buf == [o1]
An example trace 9
// Producer process Object x = null; integer in = 0; while(true) { // produce data v ... buf[in] = x; in = in + 1; V(n); } // Consumer process Object w = null; integer out = 0; while(true) { P(n);
An example trace 10
// Producer process Object x = null; integer in = 0; while(true) { // produce data v ... buf[in] = x; // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out];
} n == 0 buf == [o 1, o2]
The Consumer reads the item in the buffer, copying it to w The producer has generated another data item and places it in the buffer
23 Brian Logan 2007 G52CON Lecture 7: Semaphores 24
23/02/2011
An example trace 11
// Producer process Object x = null; integer in = 0; while(true) { // produce data v ... buf[in] = x; in = in + 1; } // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1;
An example trace 12
// Producer process // Consumer process Object w = null; Object x = null; integer out = 0; integer in = 0; while(true) { while(true) { P(n); // produce data x w = buf[out]; ... out = out + 1; buf[in] = x; in = in + 1; V(n); } } n == 1 buf == [o 1, o2] The Producer signals that another data item is ready for collection, by performing a V operation which increments the semaphore. Next time, the consumer performs P, it wont block, but will decrement n
25 Brian Logan 2007 G52CON Lecture 7: Semaphores 26
An example trace 13
// Producer process Object x = null; integer in = 0; while(true) { // produce data v ... buf[in] = x; // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1; // use the data w ... } n == 1 buf == [o1, o2 , o3]
An example trace 14
// Producer process Object x = null; integer in = 0; while(true) { // produce data v ... buf[in] = x; in = in + 1; } // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1; // use the data w ... }
n == 1 buf == [o 1, o2 , o3]
if the Producer manages to produce another data item before the consumer can consume any more items, then the semaphore will be incremented again.
Brian Logan 2007 G52CON Lecture 7: Semaphores 27 Brian Logan 2007 G52CON Lecture 7: Semaphores 28
An example trace 15
// Producer process Object x = null; integer in = 0; while(true) { // produce data x ... buf[in] = x; in = in + 1; V(n); } n == 2 buf == // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1; // use the data w ... } [o 1, o2 , o3]
An example trace 16
// Producer process Object x = null; integer in = 0; while(true) { // produce data x ... // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1; // use the data w ... }
The value of the semaphore represents the number of unconsumed items in the buffer: n = the number of V operations - the number of P operations.
Brian Logan 2007 G52CON Lecture 7: Semaphores 29 Brian Logan 2007
n == 2 buf == [o 1, o2 , o3]
30
23/02/2011
An example trace 17
// Producer process Object x = null; integer in = 0; while(true) { // produce data x ... // Consumer process Object w = null; integer out = 0; while(true) { P(n);
} n == 1 buf == [o 1, o2 , o3]
Each time the Consumer reads an item from the buffer, n is decremented, each time the Producer adds an item to the buffer, n is incremented. If n == 0, no items in the buffer, next time the consumer tries to read, it will be blocked
Brian Logan 2007 G52CON Lecture 7: Semaphores 31 Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 32
33
Bounded buffers
A bounded buffer of length n is a circular communication buffer containing n slots. The buffer contains a queue of items which have produced but not yet consumed. For example
0 1 2 3 4 5 o5 o6 in 6 7 8 9 o1 o2 o3 o4 out
out is the index of the item at the head of the queue, and in is the index of the first empty slot at the end of the queue.
Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 35 Brian Logan 2007 G52CON Lecture 8: Semaphores II 36
23/02/2011
When neither process is updating the buffer, the sum of the two semaphores is n, the total number of buffer slots
37
38
// Shared variables integer n = BUFFER_SIZE; Object[] buf = new Object[n]; general semaphore empty = n, full = 0;
Brian Logan 2007 G52CON Lecture 8: Semaphores II
41
42
23/02/2011
Exercise 2: Semaphores
a) devise a solution to multiple Producermultiple Consumer problem using a bounded buffer which ensures that:
no items are read from an empty buffer; data items are read only once; data items are not overwritten before they are read; items are consumed in the order they are produced; and all items produced are eventually consumed.
Monitors
b) does your solution satisfy the properties of Mutual Exclusion, Absence of Deadlock, Absence of Unnecessary Delay and Eventual Entry?
Brian Logan 2007 G52CON Lecture 8: Semaphores II 43 Brian Logan 2007 G52CON Lecture 8: Semaphores II 44