weird problem in buffer using semaphores

M

Moiz

Hey all,
I was just learning about concurrency programming for a project using a text
book. I want to implement a buffer using semaphores. I have used 3 semaphores.
the code is as follows
public class Buffer
{
protected Object[] buf;
protected int in=0;
protected int out=0;
protected int size;
Semaphore spaces,elements,mutex;

// creates a bounded buffer with capacity cap
public Buffer(int cap)
{
size=cap;
buf=new Object[cap];
spaces=new Semaphore(size);
elements=new Semaphore(0);
mutex=new Semaphore(1);
}

// places the object o in the buffer; if no space is left in
// the buffer, block until a space becomes available
public void put(Object o)
{
spaces.down();
mutex.down();
//synchronized (this){
buf[in]=o;
in=(in+1)%size;
//}
mutex.up();
elements.up();


}

// take an item from the buffer and return it; if there is no item in
// the buffer, block until one becomes available
public Object get()
{
elements.down();
Object o;
mutex.down();
//synchronized(this){
o=buf[out];
buf[out]=null;
out=(out+1)%size;
//}
mutex.up();
spaces.up();

return (o);

}
}

The Semaphore class is usual
public class Semaphore
{
protected int value; // value of the semaphore

// class constructor that initialises the value of the semaphore
// to initial
public Semaphore(int initial)
{
value = initial;
}

// increment the value of the semaphore and wake up one waiting
// thread, if there is one
public synchronized void up()
{
++value;
notifyAll();
}

// wait for the semaphore to have a positive value and then
// decrement the value by one
public synchronized void down()
{
// block thread until value of the semaphore is positive
while(value == 0) {
try {
wait();
} catch (java.lang.InterruptedException e) {
// if call is interrupted, print the current
// call stack, but do not exit the loop
e.printStackTrace(System.err);
}
}
// decrement value of the semaphore
--value;
}
}
And i am testing the buffer implementation with a TestBuffer class.It works fine
when only one producer and once consumer access the buffer. But when i change it
so that 2 producers and one consumer access the buffer the program is stuck. no
progress is made for sometime and i finally kill it.
the TestBuffer class is
class Producer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Producer(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(10*i));
System.out.println("Producer terminated");
}
}

// Simple producer thread that takes 10 items from the buffer
class Consumer extends Thread
{
Buffer b; // buffer

// constructor that is passed the buffer
public Consumer(Buffer b)
{
this.b = b;
}

// run takes 10 items from the buffer
public void run()
{
for (int i = 0; i < 10; i++)
System.out.println(b.get());
}
}

// Simple test case that has one producer and one consumer accessing a
// shared buffer.
class Producer2 extends Thread{

Buffer b; // buffer

// constructor that is passed the buffer
public Producer2(Buffer b)
{
this.b = b;
}

// run places 10 items in the buffer
public void run()
{
for (int i = 0; i < 10; i++)
b.put(new Integer(23*i));
System.out.println("Producer 2 terminated");
}
}
public class TestBuffer
{
// main method that implements test case
public static void main(String[] argv)
{
// create buffer
Buffer buf = new Buffer(5);

// create and start up producer and consumer threads
Producer p = new Producer(buf);
Consumer c = new Consumer(buf);
Producer2 p2=new Producer2(buf);
p.start();
c.start();
p2.start();


}
}
I am using mutual exclusion in the buffer class too. i have no idea why it gets
stuck. can anyone please help me.
Thanks
Moiz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top