Source

COS 520 Project (Bane) / source / util.cpp


#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include "Resource.hpp"
#include "Process.hpp"
using namespace std;


// Generates a number from a beta distribution limited to the range (1,10)
unsigned int randomBeta() {

  // y varies uniformly on (0,1)
  float y = float(rand())/float(RAND_MAX);

  // Concentrate points around .5
  y = .5 - pow( (.5-y), 3);

  // translate to wider range of ints
  y = int(y * 10);

  // limit to range (1,10)
  if (y > 10) {
    y = 10;
  }
  if (y < 1) {
    y = 1;
  }

  return static_cast<unsigned int>(y);
}


// Define column widths for display
#define R_ID 10  // ID number
#define R_NUMOWNERS 10  // number of owners (hopefully 0 or 1)
#define R_CONFLICT 10  // Y/N: is there a conflict?
#define R_OWNERS 30  // list ID of each owner

#define P_ID 10  // ID number
#define P_GENMESSAGE 10  // displays "REQUEST" or "RELEASE" when msg generated
#define P_RESOURCE 15  // list of Resource ID's owned by P
#define P_QUEUESIZE 10 // number of messages in priority_queue
#define P_TOPMESSAGE 10 // width for each message ID:  prints top 3 messages in queue

// Generate a display of the state of the simulation
// void display (Resource* r[], Process* p[]) {
void display (const Resource& SystemResource, const Process* const ProcessesBegin,
	      const Process* const ProcessesEnd)
{

  // Resource title bar
  cout.width(R_NUMOWNERS);
  cout << "#Owners";
  cout.width(R_CONFLICT);
  cout << "Conflict?";
  cout.width(R_OWNERS);
  cout << "List of Owners";
  cout << endl;

  // Display info about each Resource
  cout.width(R_NUMOWNERS);
  // cout << r[i]->sizeOfOwnerList();  // print size of owner list (hopefully 0 or 1)
  cout << left << SystemResource.sizeOfOwnerList();
  if (SystemResource.conflict()) {
    cout << "YES";
    exit(100);
  }
  else {
    cout << "NO";
  }
  // r[i]->printOwnerList(R_OWNERS);  // prints each owner(Process) ID in a total width of R_OWNERS
  SystemResource.printOwnerList(R_OWNERS);
  cout << endl;
  //}

  cout << endl;


  // Process title bar
  cout.width(P_ID);
  cout << "PROC ID";
  cout.width(P_GENMESSAGE);
  cout << "Msg.Gen?";
  cout.width(P_RESOURCE);
  cout << "HasResource";
  cout.width(P_QUEUESIZE);
  cout << "#Msgs.";
  cout.width(P_TOPMESSAGE);
  cout << "Top";
  cout << endl;

  // Display info about each Process
  // for (unsigned int i=0; i<sizeof(p); i++) {
  for (const Process *i=ProcessesBegin; i < ProcessesEnd; ++i) {
    cout.width(P_ID);
    cout << i->getId();  // print Process ID
    i->printRecentMessage(P_GENMESSAGE);  // print recent message type (if any)
    i->printResource(P_RESOURCE);  // print owned resource (if any)
    cout.width(P_QUEUESIZE);  // print number of messages in queue
    cout << i->sizeQueue();
    cout.width(P_TOPMESSAGE);
    if (i->sizeQueue() > 0) {
      cout << i->getTop()->getAction();  // print ID of top message (if any)
    }
    else {
      cout << "";
    }
    cout << endl;
  }

  cout << endl;
  cout << endl;
  cout << endl;

}