Producer – Consumer in Ada
----------------------------------------------------------------------
-- Aufgabe: producer-consumer (summation_of_naturals) with bounded buffer
-- Methode: tasks for producer-consumer (simple sum_of_naturals problem)
-- AND task for buffer
----------------------------------------------------------------------
with Stringpack; use Stringpack;
procedure Bounded_Buffer2 is
task Buffer is -- server task
entry Insert(D: Natural);
entry Take(D: out Natural);
end Buffer;
task Producer is
entry Start(How_Many: Natural);
end Producer;
task Consumer is
entry Start(Break: Natural);
end Consumer;
task body Producer is
Local_How_Many: Natural;
begin
accept Start(How_Many: Natural) do
Local_How_Many := How_Many;
end Start;
Consumer.Start(Local_How_Many +1);
for I in 1..(Local_How_Many +1) loop
Buffer.Insert(I);
end loop;
end Producer;
task body Consumer is
Over, Item, Result: Natural;
begin
accept Start(Break: Natural) do
Over := Break;
end Start;
Result := 0;
Buffer.Take(Item);
while Item /= Over loop
Result := Result +Item;
Buffer.Take(Item);
end loop;
Print("Summe= " & Result);
end Consumer;
task body Buffer is
Length: constant Natural := 10;
B: array(0..Length-1) of Natural;
In_Ptr, Out_Ptr: Natural := 0;
Count: Natural := 0;
begin
loop
select
when Count < Length =>
accept Insert(D: Natural) do
B(In_Ptr) := D;
end Insert;
In_Ptr := (In_Ptr +1) mod Length;
Count := Count +1;
or
when Count > 0 =>
accept Take(D: out Natural) do
D := B(Out_Ptr);
end Take;
Out_Ptr := (Out_Ptr +1) mod Length;
Count := Count -1;
or
terminate;
end select;
end loop;
end Buffer;
N: Natural;
begin
Print("give natural");
N := Getint;
Producer.Start(N);
end Bounded_Buffer2;
Producer – Consumer in Java
import Utilities.*;
import Synchronization.*;
class BoundedBuffer extends MyObject
{
private int numSlots = 0;
private double[] buffer = null;
private int putIn = 0, takeOut = 0;
private int count = 0;
private BinarySemaphore mutex = null;
private CountingSemaphore elements = null;
private CountingSemaphore spaces = null;
public BoundedBuffer(int numSlots) {
super("BoundedBuffer with " + numSlots + " slots");
if (numSlots <= 0) throw new IllegalArgumentException("numSlots<=0");
this.numSlots = numSlots;
buffer = new double[numSlots];
mutex = new BinarySemaphore(1);
elements = new CountingSemaphore(0);
spaces = new CountingSemaphore(numSlots);
System.out.println("BoundedBuffer alive, numSlots=" + numSlots);
}
public void deposit(double value) {
P(spaces);
buffer[putIn] = value;
putIn = (putIn + 1) % numSlots;
P(mutex);
count++;
System.out.println(" after deposit, count=" + count
+ ", putIn=" + putIn);
V(mutex);
V(elements);
}
public double fetch() {
double value;
P(elements);
value = buffer[takeOut];
takeOut = (takeOut + 1) % numSlots;
P(mutex);
count--;
System.out.println(" after fetch, count=" + count
+ ", takeOut=" + takeOut);
V(mutex);
V(spaces);
return value;
}
}
class Producer extends MyObject implements Runnable {
private int pNap = 0; // milliseconds
private BoundedBuffer bb = null;
public Producer(String name, int pNap, BoundedBuffer bb) {
super(name);
this.pNap = pNap;
this.bb = bb;
}
public void run() {
double item;
int napping;
while (true) {
napping = 1 + (int) random(pNap);
nap(napping);
item = random();
bb.deposit(item);
}
}
}
class Consumer extends MyObject implements Runnable {
private int cNap = 0; // milliseconds
private BoundedBuffer bb = null;
public Consumer(String name, int cNap, BoundedBuffer bb) {
super(name);
this.cNap = cNap;
this.bb = bb;
}
public void run() {
double item;
int napping;
while (true) {
napping = 1 + (int) random(cNap);
nap(napping);
item = bb.fetch();
}
}
}
class ProducerConsumer extends MyObject {
public static void main(String[] args) {
// parse command line arguments, if any, to override defaults
GetOpt go = new GetOpt(args, "Us:p:c:R:");
go.optErr = true;
String usage = "Usage: -s numSlots -p pNap -c cNap -R runTime";
int ch = -1;
int numSlots = 20;
int pNap = 3; // defaults
int cNap = 3; // in
int runTime = 60; // seconds
System.out.println("ProducerConsumer: numSlots=" + numSlots);
// create the bounded buffer
BoundedBuffer bb = new BoundedBuffer(numSlots);
// start the Producer and Consumer threads
Thread producer = new Thread(new Producer("PROD", pNap*1000, bb));
Thread consumer = new Thread(new Consumer("Cons", cNap*1000, bb));
producer.start();
consumer.start();
System.out.println("All threads started");
// let them run for a while
nap(runTime*1000);
producer.stop();
consumer.stop();
System.exit(0);
}
}