/* * BoundedBufferPSem.java (Implements Producer Using Semaphores). * * Written by : Nitin Motgi (nmotgi@cs.ucf.edu) * * This file implements producer using semaphores.This puts the data * into the buffer and other side which is consumer removes from it. * The buffer has finite size so the producer has to wait when it's * full, and the consumer has to wait when it's empty. And, of course * all elements added to the queue by the producer must be seen by * the consumer exactly once. * * Portions copyright(c) 2000 to School of Electrical Engineering and * Computer Science, UCF, Orlando. * * Use and distribution of this source code are strictly governed by * terms and conditions set by the authors. * * $Id : BoundedBufferPSem.java, v1.0.0 02/07/2001. $ * * Revision History Revisited: */ /* Start of BoundedBufferPSem.*/ public class BoundedBufferPSem extends Thread{ /* Each producer is given an ID so that the higher level routine can interact with a specific processor.*/ int nID; /* This is up level pointer points to the parent class which provides mechanism for synchronization.*/ BoundedBufferSem BBSem; /* End condition indicator set to push the Producer from endlessly producing an item and put in the queue.*/ boolean bEndCondition; /* * This function is constructor for this class. */ public BoundedBufferPSem(int nID,BoundedBufferSem BBSem){ this.nID = nID; /* Store ID.*/ this.BBSem = BBSem; /* Pointer to Higher class.*/ bEndCondition = true; /* Produce one item and put in shared data structure.*/ }/* End of BoundedBufferPSem.*/ /* * This program is heart of this thread. */ public void run(){ int nItem; /* Till the indicator turns false.*/ while(bEndCondition){ nItem = BBSem.ProduceItem(this); if(nItem == -1) continue; /* If BBSem does not want this producer to produce an item.*/ BBSem.Empty.P(); BBSem.Mutex.P(); BBSem.InsertItem(1 /* ADDS ONE ITEM TO SHARED QUEUE.*/); BBSem.Mutex.V(); BBSem.Full.V(); }/* End of while.*/ }/* End of run.*/ /* * Sets the end codition to true or false. */ public void SetEndCondition(boolean bState){ bEndCondition = bState; }/* End of SetEndCondition.*/ }/* End of BoundedBufferPSem.*/