Coder Social home page Coder Social logo

producers-consumers-rmi's Introduction

producers-consumers-rmi

Description

This project contains a simple implementation of a distributed Producer-Consumer problem based on Java RMI (Remote Method Invocation).

As a mean of practice a pair of semaphores was implemented (i.e. fair and unfair semaphore) that employ solely Java's synchronized primitive.

Semaphores

There are two available semaphores: FairSemaphore and UnfairSemaphore. Both are subclasses of abstract class Semaphore, which provides typical semaphore actions for acquiring and releasing critical section, that is:

//acquire specific number of units, default is 1
acquire(int units);
acquire(); === acquire(1);

//release specific number of units, default is 1
release(int units);
release(); === release(1);

Those two differ only in implementation details:

FairSemaphore ensures that no starvation occurs. It employs a queue that stores which one of the competing threads should enter the critical section next.

UnfairSemaphore in turn, prioritizes not on equity, but on throughput. Every time there is enough units for any single of waiting threads to go on, that thread is allowed to enter the critical section (if there are more such threads, an undetermined one is getting awaken). Note that this behavior may lead to starvation of threads that require more semaphore units than others.

Producer-consumer code

Producer and consumer code for accessing the buffer that draws on built semaphores is following:

cs = new FairSemaphore();
full = new FairSemaphore(0);
empty = new UnfairSemaphore(bufferSize);

//producer
public void add(int count) {
  empty.acquire(count);
  cs.acquire();
  //[ADD ITEM]
  cs.release();
  full.release(count);
}

//consumer
public void remove(int count) {
  full.acquire(count);
  cs.acquire();
  //[REMOVE ITEM]
  cs.release();
  empty.release(count);
}

The reason for the empty semaphore to be an instance of UnfairSemaphore is simple. If it was fair a situation might occur, when Producer that puts more than 1 unit would have insufficient free space in buffer to proceed and would be forced to wait blocking the access. If at the same time Consumer would require all buffer units to work, he would have to wait as well. That would effectively lead to a deadlock. However, when UnfairSemaphore is used instead, remaining producers (e.g. those that produce 1 unit) can do their job normally and fill the buffer.

Examples

There are 2 examples in the project:

Local semaphores test

The first one (located in src/local/) is a test of semaphores with multiple local threads. Analyzing the output of LocalSemaphoreTest you can notice that threads are treated fair while competing for buffer access.

RMI

The second example (located in src/rmi/) utilizes Java RMI to build a server that provides access to shared buffer, alongside with consumers and producers who want to gain that access.

ProducersConsumers server takes 2 arguments, i.e. [port number] that the server should run at and the [capacity] of the buffer.
Producer and Consumer take 2 arguments, i.e. [address:port] of the RMI server and number of [units] to produce/consume each round.

Note: Although the producers and consumers are distributed processes, the critical section itself is acquired and released only locally (on the server). This is NOT a distributed mutual exclusion.

Project file structure

All source code is located under subdirectories of src/ directory:

  • local/ contains local test of semaphores (without RMI),
  • rmi/ contains example of RMI server, producer and consumer,
  • common/ contains code shared by local and RMI examples,
  • semaphore/ contains semaphore classes.

License

This project is released under MIT License.

producers-consumers-rmi's People

Contributors

misko321 avatar

Stargazers

Sotiris Sotiriou avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.