0% found this document useful (0 votes)
7 views10 pages

Lecture 2

Uploaded by

naheba6024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Lecture 2

Uploaded by

naheba6024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Producer/Consumer Problem

General one or more producers are generating data and placing


Statement: these in a buffer
a single consumer is taking items out of the buffer one
at a time
only one producer or consumer may access the buffer
at any one time
The
Problem: ensure that the producer can’t add
data into full buffer and consumer
can’t remove data from an empty
buffer
0 1 2 3 4

b[1] b[2] b[3] b[4] b[5]

out in

Note: shaded area indicates portion of buffer that is occupied

Figure 5.8 I nfinite Buffer for the Producer/Consumer Problem


/* pr ogr am producerconsumer */
i nt n;
binary_semaphore s = 1, delay = 0; Figure 5.9
voi d producer()
{
whi l e (true) {
produce();
semWaitB(s); An Incorrect
append();
n++;
i f (n==1) semSignalB(delay);
Solution
}
semSignalB(s);
to the
}
voi d consumer() Infinite-Buffer
{
semWaitB(delay);
whi l e (true) {
Producer/Consu
semWaitB(s);
take(); mer
n--;
semSignalB(s);
consume();
Problem Using
}
i f (n==0) semWaitB(delay); Binary
}
voi d main() Semaphores
{
n = 0;
par begi n (producer, consumer);
}
Table 5.4
Possible Scenario for the Program of Figure 5.9
Producer Consumer s n Delay
1 1 0 0
2 semWaitB(s) 0 0 0
3 n++ 0 1 0
4 i f (n==1) 0 1 1
(semSignalB(delay))
5 semSignalB(s) 1 1 1
6 semWaitB(delay) 1 1 0
7 semWaitB(s) 0 1 0
8 n-- 0 0 0
9 semSignalB(s) 1 0 0
10 semWaitB(s) 0 0 0
11 n++ 0 1 0
12 i f (n==1) 0 1 1
(semSignalB(delay))
13 semSignalB(s) 1 1 1
14 i f (n==0) (semWaitB(delay)) 1 1 1
15 semWaitB(s) 0 1 1
16 n-- 0 0 1
Note: White semSignalB(s)
areas
17 1 0 1
18 i f (n==0) (semWaitB(delay)) 1 0 0
represent the
critical 19 semWaitB(s) 0 0 0
section
controlled by 20 n-- 0 –1 0
semaphore 21 semSignalB(s) 1 –1 0
s.
Figure 5.10

A Correct
Solution to the
Infinite-Buffer
Producer/Cons
umer Problem
Using Binary
Semaphores
/* pr ogr am producerconsumer */
semaphore n = 0, s = 1;
voi d producer()
Figure 5.11 {
whi l e (true) {
produce();
A Solution semWait(s);
append();
to the semSignal(s);
Infinite- semSignal(n);
Buffer }
}
Producer/C voi d consumer()
onsumer {
whi l e (true) {
Problem semWait(n);
Using semWait(s);
Semaphores take();
semSignal(s);
consume();
}
}
voi d main()
{
par begi n (producer, consumer);
}
b[1] b[2] b[3] b[4] b[5] b[n]

out in

(a)

b[1] b[2] b[3] b[4] b[5] b[n]

in out

(b)

Figure 5.12 Finite Circular Buffer for the Producer/Consumer Problem


Figure 5.13

A Solution to
the Bounded-
Buffer
Producer/Cons
umer Problem
Using
Semaphores
Implementation of
Semaphores
 Imperativethat the semWait and
semSignal operations be implemented as
atomic primitives
 Can be implemented in hardware or firmware
 Software schemes such as Dekker’s or
Peterson’s algorithms can be used
 Useone of the hardware-supported
schemes for mutual exclusion
Figure 5.14
Two Possible Implementations of Semaphores

semWait(s) semWait(s)
{ {
whi l e ( compare_and_swap(s.flag, 0 , 1) == 1) inhibit interrupts;
/* do nothing */; s.count--;
s.count--; i f (s.count < 0) {
i f (s.count < 0) { /* place this process in s.queue */;
/* place this process in s.queue*/; /* block this process and allow interrupts */;
/* block this process (must also set s.flag to 0) }
*/; el se
} allow interrupts;
s.flag = 0; }
}
semSignal(s)
semSignal(s) {
{ inhibit interrupts;
whi l e ( compare_and_swap(s.flag, 0 , 1) == 1) s.count++;
/* do nothing */; i f (s.count <= 0) {
s.count++; /* remove a process P from s.queue */;
i f (s.count <= 0) { /* place process P on ready list */;
/* remove a process P from s.queue */; }
/* place process P on ready list */; allow interrupts;
} }
s.flag = 0;
}

(a) Compare and Swap Instruction (b) Interrupts

You might also like