You need to sign in or sign up before continuing.×
Beta

Superphore Master

Description:

The Superphore Master is calling your name! But your significant other is on the other line. Who do you handle first?

In this Kata you will create an engine for queueing access to resources.

A simple class for Process is already created that has functions for start, pause, resume, and stop. The start method tells the process it can start running. The pause method tells a running process to pause running. resume() is called after a process is paused and the resource becomes available to the process again. stop() is called when the process is done using the resource(). There are no guts to the Process class; It is just a representation of a thread wanting access to a resource.

function Process() {
  this.started = [];
  this.paused = [];
}

Process.prototype.addResource = function (queue, resourceName) {
  this[queue].push(resourceName);
};

Process.prototype.removeResource = function (queue, resourceName) {
  var pos = this[queue].indexOf(resourceName);
  if (pos == -1) {
    throw 'Process not previously in ' + queue + ' for resource: ' + resourceName;
  }
  this[queue].splice(pos, 1);
};

Process.prototype.start = function (resourceName) {
  this.addResource('started', resourceName);
};

Process.prototype.pause = function (resourceName) {
  this.addResource('paused', resourceName);
};

Process.prototype.resume = function (resourceName) {
  this.removeResource('paused', resourceName);
};

Process.prototype.stop = function (resourceName) {
  this.removeResource('started', resourceName);
};

You will implement a SuperphoreMaster class with methods startUsing, and stopUsing.

The startUsing method takes three parameters. The first is the string name of the resource. The second is the priority at which this process runs (larger numbers get precedence, a process with priority of 1 runs last). The third parameter is the process to run.

  • If the resource is not in use then the process' start method gets called immediately.
  • If the resource is in use, but by a lower priority process, then the lower priority process gets paused and the new process gets started. Call pause() on the process currently using the resource and start() on the new process taking over.
  • Otherwise, since the resource is in use by a higher priority process, the new process gets placed in a queue to be started when the resource is available (see stopUsing() method for more information about queue priority).
  • Note: A process CAN relock the same resource. If the process is currently using that resource, it should have its start method called again. It will then take an extra call to the stopUsing method by that process before the process is done with the resource. If the process is not currently using the resource then it gets placed in to the queue again so that the process will need to be started another time before the queue is empty.

The stopUsing method takes two parameters. The first parameter is the string name of the resource. The second parameter is the process that was using the resource and is now done with it.

  • If the resource is not currently in use by the passed in process then throw an exception.
  • Otherwise, call stop() on the process

* if there are processes in the queue waiting to use the resource then call start() on the next process with the highest priority. If there are several processes of the same priority then whichever process asked for the resource first becomes the next to be started (FIFO). If a process was previously paused() and is regaining the resource then instead of calling start() on the process, call resume().

Example on start/stop/resume/pause:

  • process #1, with priority 1, starts using 'mother-in-law' (start() called on process #1)
  • process #2, with priority 2, starts using 'mother-in-law' (since process #2 has higher priority, start() is called on process #2 and pause() is called on process #1)
  • process #3, with priority 1, starts using 'mother-in-law' (since it has lower priority it gets put in the queue)
  • process #2 stops using 'mother-in-law' (stop() called on process #2 ,resume() called on process #1 since it ties process #3 for priority and asked for the resource first)
  • process #1 stops using 'mother-in-law' (stop() called on process #1, start() called on process #3 since it is the only process in the queue)
  • process #3 stops using 'mother-in-law' (stop() called on process #3)

Here is the wikipedia page on Semaphores which was the impetus for this Kata: https://en.wikipedia.org/wiki/Semaphore_(programming)

Algorithms

Similar Kata:

More By Author:

Check out these other kata created by Austin Haws

Stats:

CreatedSep 16, 2016
PublishedSep 18, 2016
Warriors Trained28
Total Skips0
Total Code Submissions78
Total Times Completed3
JavaScript Completions3
Total Stars3
% of votes with a positive feedback rating50% of 1
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Ad
Contributors
  • Austin Haws Avatar
Ad