![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
Producer consumer problem
Hello there!
I'm having trouble with a little producer/consumer problem. The problem consists on having 4 producers and 2 consumers. Each producer will produce 4 numbers, so the consumers will consume a total of 16 numbers. This is my code (this is a little big ):public ThreadType1( int id, StoreRegion1 store1, StoreRegion2 store2 ) // producer
{
this.id = id;
this.store1 = store1;
this.store2 = store2;
}
public void run()
{
int [] val = { 3, 2, 1, 4 };
int nBuff;
for ( int i = 0; i < val.length; ++i )
{
try
{
sleep( ( int ) ( 1 + 100 * Math.random() ) );
}
catch ( InterruptedException e ) {};
nBuff = store1.getInfoP();
store2.putVal( nBuff, 100 * id + val[i] );
}
}
public ThreadType2( int id, int niter, StoreRegion1 store1, StoreRegion2 store2 ) // consumer
{
this.id = id;
this.niter = niter;
this.store1 = store1;
this.store2 = store2;
}
public void run()
{
for ( int i = 0; i < niter; ++i )
{
try
{
sleep( ( int ) ( 1 + 100 * Math.random() ) );
}
catch ( InterruptedException e ) {};
int nBuff = store1.getInfoC();
int val = store2.getVal( nBuff );
System.out.println( "O valor produzido por P" + ( val / 100 ) +
" e por C" + id + " no buffer " + nBuff +
" foi " + ( val % 100 ) + "." );
store2.relBuff( nBuff );
}
}
public class ThreadType3 extends Thread
{
private int niter;
private StoreRegion1 store = null;
public ThreadType3( int niter, StoreRegion1 store )
{
this.niter = niter;
this.store = store;
}
public void run()
{
for ( int i = 0; i < niter; ++i )
store.processInfo();
}
}
public class StoreRegion1
{
private int mem = 0;
private Semaphore [] stat = new Semaphore[4];
private StoreRegion2 store = null;
public StoreRegion1( StoreRegion2 store )
{
for ( int i = 0; i < stat.length; ++i )
stat[i] = new Semaphore();
this.store = store;
}
public int getInfoP()
{
stat[2].up();
stat[0].down();
return mem;
}
public int getInfoC()
{
stat[3].up();
stat[1].down();
return mem;
}
public void processInfo()
{
stat[2].down();
stat[3].down();
int nBuff = store.getBuff();
mem = nBuff;
stat[0].up();
stat[1].up();
}
}
public class StoreRegion2
{
private int size = 0;
private boolean [] pSit = null;
private int [] mem = null;
private Semaphore [] stat;
public StoreRegion2( int size )
{
this.size = size;
pSit = new boolean[size];
for ( int i = 0; i < pSit.length; ++i )
pSit[i] = false;
mem = new int[size];
stat = new Semaphore[2*size+1];
for ( int i = 0; i < stat.length; ++i )
stat[i] = new Semaphore();
for ( int i = 0; i < size; ++i )
stat[0].up();
for ( int i = 1; i < size + 1; ++i )
stat[i].up();
}
public void putVal( int nBuff, int val )
{
stat[nBuff+1].down();
mem[nBuff] = val;
stat[nBuff+size+1].up();
}
public int getVal( int nBuff )
{
stat[nBuff+size+1].down();
int val = mem[nBuff];
stat[nBuff+1].up();
return val;
}
public int getBuff()
{
int nBuff = -1;
stat[0].down();
synchronized ( this )
{
for ( int i = 0; i < size; ++i )
{
if ( !pSit[i] )
{
nBuff = i;
pSit[i] = true;
break;
}
}
}
return nBuff;
}
public void relBuff( int nBuff )
{
synchronized ( this )
{
pSit[nBuff] = false;
}
stat[0].up();
}
}
public class SimulSituation // Main thread
{
public static void main( String [] args )
{
StoreRegion2 store2 = new StoreRegion2( 2 );
StoreRegion1 store1 = new StoreRegion1( store2 );
ThreadType1 [] thr1 = new ThreadType1[4];
for ( int i = 0; i < thr1.length; ++i )
{
thr1[i] = new ThreadType1( i + 1, store1, store2 );
}
ThreadType2 [] thr2 = new ThreadType2[2];
for ( int i = 0; i < thr2.length; ++i )
{
thr2[i] = new ThreadType2( i + 1, 8, store1, store2 );
}
ThreadType3 thr3 = new ThreadType3( 16, store1 );
thr3.start();
for ( int i = 0; i < thr2.length; ++i )
{
thr2[i].start();
}
for ( int i = 0; i < thr1.length; ++i )
{
thr1[i].start();
}
}
}Ok, this code works fine, but now I want to change it so it's possible to communicate 2 values between the producers and the consumers. I've try changing the ThreadType2 to: public void run()
{
for ( int i = 0; i < niter; ++i )
{
try
{
sleep( ( int ) ( 1 + 100 * Math.random() ) );
}
catch ( InterruptedException e ) {};
int nBuff1 = store1.getInfoC();
int val1 = store2.getVal( nBuff1 );
int nBuff2 = store1.getInfoC();
int val2 = store2.getVal( nBuff2 );
System.out.println( "O valor produzido por P" + ( val1 / 100 ) +
" e por P" + ( val2 / 100 ) + " e por C" + id + " no buffer " + nBuff1 +
" e no buffer " + nBuff2 + " foi " + ( val1 % 100 ) + " e " + (val2 % 100) + "." );
store2.relBuff( nBuff1 );
store2.relBuff( nBuff2 );
}
}and the simulations parameters so that the consumers only do 4 iterations: public class SimulSituation
{
public static void main( String [] args )
{
....
ThreadType2 [] thr2 = new ThreadType2[2];
for ( int i = 0; i < thr2.length; ++i )
{
thr2[i] = new ThreadType2( i + 1, 4, store1, store2 );
}
.....
}
}This almost works... but fails once every 50 runs, ocurring a deadlock.... I can't see what's the problem, can somebody help me? Thanks! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Challenging Programming Problem - "Pinball Ranking" | Sane | Coder's Corner Lounge | 38 | Jan 15th, 2008 5:16 PM |
| Problem solving | ReggaetonKing | Software Design and Algorithms | 7 | Jan 4th, 2008 1:49 PM |
| Stuck with a C problem | Polaris | C++ | 8 | Aug 19th, 2006 3:30 PM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 10:11 AM |