Sie sind auf Seite 1von 8

23/02/2011

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

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

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

The semaphore operations: P(S) and V(S)


P(S)
if S > 0 then S = S 1, else suspend execution of the process that called P(S)

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

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

Semaphores and condition synchronisation


Condition synchronisation involves delaying a process until some Boolean condition is true. condition synchronisation problems can be solved using busy waiting:
the process simply sits in a loop until the condition is true busy waiting is inefficient

The producer-consumer problem


Example of condition synchronisation (order-of-execution) problem There are two types of processes in this problem
A producer process executes a statement produce to create a data element and then sends this element to the consumer process Upon receipt of a data element a consumer process executes a statement consume with the data element as a parameter

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

Producer-consumers: ubiquitous in computing systems


Producer Communications line Web browser Keyboard Word processor Joystick Game program Consumer Web browser Communications line Operating system Printer Game program Display screen

put it in the buffer


Buffer

take item from the buffer

Consumer

consume (use) data item


Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 7 Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 8

The producer-consumer problem


There are two synchronisation issues in the producer-consumer problem
1. A producer can not take an element from an empty buffer 2. Since the size of any buffer is finite, a producer cannot append a data element to a full buffer

Infinite buffer problem


There are not such a thing as an infinite buffer, but if the buffer is very large compared to the rate at which data is produced, we can avoid the overhead of a finite buffer algorithm If there is an infinite buffer, only one interaction must be synchronised:
the consumer must not attempt a take operation from an empty buffer

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

10

Infinite buffer solution


Use a single semaphore (n) ( n represents items ready in buffer) The consumer waits on n (block) whenever it would read from an empty buffer The semaphore has the value 0 whenever the buffer is empty When the producer puts an item in the buffer, it performs a V operation on the semaphore
If the consumer is already blocked, it will be unblocked by this operation If the consumer is not blocked the semaphore will be incremented and the consumer will not block when it next attempts to read an item.

Infinite buffer solution


// Producer process Object x = null; integer in = 0; while(true) { // produce data x ... buf[in] = x; in = in + 1; V(n); } // Consumer process Object w = null; integer out = 0; while(true) { P(n); w = buf[out]; out = out + 1; // use the data w ... }

// Shared variables Object[] buf = new Object[]; semaphore n = 0;


Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 11 Brian Logan 2007 G52CON Lecture 7: Semaphores 12

23/02/2011

Infinite buffer solution


Both the Producer and Consumer processes consist of some initialisation followed by an infinite loop. The Producer repeatedly generates some data, puts it in the next location in the buffer (using the index in), and then performs a V operation to signal that there is a data item ready for consumption. The Consumer waits on the semaphore until the buffer is not empty, then reads the data from the buffer into a local variable (using an index out, which points to the last item read), and then does something with the data.
Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 13

An example trace 1
// Producer process Object x = null; // Consumer process Object w = null;

n == 0 buf == []

Brian Logan 2007

G52CON Lecture 7: Semaphores

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 == []

Brian Logan 2007

G52CON Lecture 7: Semaphores

15

Brian Logan 2007

G52CON Lecture 7: Semaphores

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 == []

Brian Logan 2007

G52CON Lecture 7: Semaphores

17

Brian Logan 2007

G52CON Lecture 7: Semaphores

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]

Brian Logan 2007

G52CON Lecture 7: Semaphores

19

Brian Logan 2007

G52CON Lecture 7: Semaphores

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);

Explaining the trace 8 to 9 (V with the consumer blocked)


Once the Producer has placed an item in the buffer, it performs a V operation on the semaphore. This wakes up the suspended Consumer, which resumes at the point at which it blocked. Note that the value of n remains unchanged. n would only have been incremented by the V operation if there were no processes suspended on n.
21 Gabriela Ochoa 2011 G52CON Lecture 9: Semaphores 2 22

} n == 0 buf == [o1]

Brian Logan 2007

G52CON Lecture 7: Semaphores

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]

n== 0 buf == [o1]

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

Brian Logan 2007

G52CON Lecture 7: Semaphores

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

} n == 0 buf == [o 1, o2] Indices in and out are incremented

Brian Logan 2007

G52CON Lecture 7: Semaphores

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]

G52CON Lecture 7: Semaphores

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);

Single element buffer problem


Buffer: Single data item Synchronisation conditions
the producer may only produce an item when the buffer is empty; and the consumer may only consume an item when the buffer is full.

} 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

Single element buffer solution


Two binary semaphores: empty and full The buffer is initially empty, so the empty semaphore is initialised to 1 The full semaphore is initialised to 0 The processes take turn, with the producer starting first
Producer: (i) waits on empty until the consumer takes the item. (ii) Uses the full semaphore to signal that there is a new item in the buffer ready for collection Consumer: (i) waits on full until the producer signals that there is a new item. (ii) Uses empty to signal that it has consumed the last item

Single element buffer solution


// Producer process Object x = null; while(true) { // produce data x ... P(empty); buf = x; V(full); } // Consumer process Object y = null; while(true) { P(full); y = buf; V(empty); // use the data y ... }

// Shared variables Object buf; binary semaphore empty = 1, full = 0;


Brian Logan 2007 G52CON Lecture 8: Semaphores II 34

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

33

Bounded buffer problem


Buffer: a bounded or fixed-size buffer Synchronisation conditions
the producer may only produce an item when there is an empty slot in the buffer; and the consumer may only consume an item when there is a full slot in the buffer

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

Bounded buffer solution


Two general semaphores: empty and full All the spaces in the buffer are initially empty, so the empty semaphore is initialised to n the size of the buffer The full semaphore is initialised to 0 (indicating that any slot is full) The processes take turn, with the producer starting first
Producer: (i) waits on empty if all the slots are full. (ii) Uses the full semaphore to signal that there is a new item in the buffer ready for collection Consumer: (i) waits on full if all the slots are empty (not full slots). (ii) Uses empty to signal that it has consumed the last item

Bounded buffer solution


The semaphores function as resource counters:
empty counts the number of empty buffer slots full counts the number of full slots.

When neither process is updating the buffer, the sum of the two semaphores is n, the total number of buffer slots

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

37

Gabriela Ochoa 2011

G52CON Lecture 9: Semaphores 2

38

Bounded buffer solution


// Producer process Object x = null; integer in = 0; while(true) { // produce data x ... P(empty); buf[in] = x; in = (in + 1) % n; V(full); } // Consumer process Object y = null; integer out = 0; while(true) { P(full); y = buf[out]; out = (out + 1) % n; V(empty); // use the data y ... }

Properties of the bounded buffer solution


The bounded buffer solution satisfies the following properties:
Mutual Exclusion: yes Absence of Deadlock: yes Absence of Unnecessary Delay: yes Eventual Entry: yes
39 Brian Logan 2007 G52CON Lecture 8: Semaphores II 40

// 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

Bounded buffer solution synchronisation conditions


Properties of the solution:
data items are not overwritten before they are read: yes data items are read only once: yes no items are read from an empty buffer: yes items are consumed in the order they are produced: yes all items produced are eventually consumed: yes

Variants of the problem


The single Producersingle Consumer problem can be generalised:
multiple producerssingle consumer single producermultiple consumers multiple producersmultiple consumers

Brian Logan 2007

G52CON Lecture 8: Semaphores II

41

Brian Logan 2007

G52CON Lecture 8: Semaphores II

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.

The next lecture


Suggested reading: Andrews (2000), chapter 5; Ben-Ari (1982), chapter 5; Burns & Davies (1993), chapter 7, sections 7.47.9. Resources: http://www.cs.gmu.edu/cne/workbenches/pcsem/pcsem.html http://www.cs.gmu.edu/cne/modules/ipc/map.html

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

Das könnte Ihnen auch gefallen