18 Sep 2015
Inter process Communication using Bounding Buffer
Cooperating processes in operating system commonly shares information with other processes executing in the system. It has several reasons to provide an environment that allows process cooperation. Such as
- Information Sharing
- Computation Speedup
- Modularity
In this article, we are sharing an example code of bounding buffer commonly used in operating systems.
Bounding Buffer Interface
package process; public interface Buffer{ public void insert(E item); public E remove(); }
Bounding Buffer Implementation
package process; public class BoundedBufferimplements Buffer { private static final int BUFFER_SIZE = 5; private int count; // number of items in the buffer private int in; // points to the next free position private int out; // points to the next full position private E[] buffer; public BoundedBuffer() { // buffer is initially empty count = 0; in = 0; out = 0; buffer = (E[]) new Object[BUFFER_SIZE]; } // producers calls this method public void insert(E item) { while (count == BUFFER_SIZE) ; // do nothing -- no free space // add an item to the buffer buffer[in] = item; in = (in + 1) % BUFFER_SIZE; ++count; } // consumers calls this method public E remove() { E item; while (count == 0) ; // do nothing -- nothing to consume // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER_SIZE; --count; return item; } public int Size(){ return count; } public int getNextFreePos(){ return in; } public int getNextFullPos(){ return out; } }
No Responses