GPicasso
asked on
can't create object of type "System" before it's declared
I think maybe I need to divide my code up into headers and stuff but the C++ way of doing this seems completely wrong to me I don't see the pattern.
Maybe my mistake is something else
Maybe my mistake is something else
#include <stack>
#include <vector>
#include <string>
#include <queue>
#include <iostream>
//#include "scheduler.h"
using namespace std;
#define NUMBER_OF_PROCESSORS 8
#define MAX_JOBS 2
System system_; // this simulates system features normally outside of the scheduler
class JobRequest
/* This represents a request to create a job it had yet to actually
* move on to that point in it's lifecycle.. note the lack of PID
*/
{
private:
string name_;
int numberOfProcessors_;
int numberOfTicks_;
public:
JobRequest(string name, int numberOfProcessors, int numberOfTicks)
{
name_ = name_;
numberOfProcessors_ = numberOfProcessors;
numberOfTicks_ = numberOfTicks;
}
string getName()
/*returns the name of the job, exmple: ls, pwd, javac*/
{
return name_;
}
int getProcessingRequirements()
/*returns the number of processors required to run this job*/
{
return numberOfProcessors_;
}
int getTicksRequired()
/* Returns the number of ticks this program is going to consume*/
{
return numberOfTicks_;
}
};
class Processor
/* This class represents processors *
* it includes a processor ID but it's not *
* yet important *
* */
{
private:
int processorID_; // This is a unique integet to tell one processor
// from the next
string type_; //This tells what type of processor
// is attached to the object
public:
Processor()
{
processorID_ = 0;
type_ = "GenuineMakeBelieve";
// this is all just examples we could put all kinds of stuff
// in here to tie this to a real processor and get statistics
// or even have affinity for some jobs to certian types of processors
// but this is all make believe anyhow
}
int getID()
/* This should return the processor ID*/
{
return processorID_;
}
string getType()
/*This should return a string identifying the sort of processor
*/
{
return type_;
}
};
class System
/* This class represents the system it's for storing the state of anything
* that doesn't clearly belong in some other class
* right now it only stores the free processor pool and the current next
* process ID
*/
{
private:
int nextPid;
vector<Processor> freePool_;
public:
System()
{
//freePool_ = new vector<Processor>;
// I shouldn't need this because these System objects will stick around
// for the duration of the program's execution anyhow. like "new System"
nextPid = 0;
}
int getPID() /// static int getPID() ???
/*This allocates and returns the next available process ID*/
{
return ++nextPid;
}
Processor getProcessor()
/* This method gets a free processor from the system*/
{
Processor r = freePool_.back();
freePool_.pop_back();
return r;
}
void takeProcessor(Processor processor)
/* This method gives the processor back to the system*/
{
freePool_.push_back(processor);
}
};
class Job;
bool operator>= (Job &job1, Job &job2);
bool operator<= (Job &job1, Job &job2);
bool operator== (Job &job1, Job &job2);
bool operator!= (Job &job1, Job &job2);
ostream &operator<< (ostream& out, const Job &job);
class Job
/* The job class represents an actual job with a name
* a stack of captured processors a list of ticks and a PID for the job*/
{
/*
friend bool operator>= (Job &job1, Job &job2);
friend bool operator<= (Job &job1, Job &job2);
friend bool operator== (Job &job1, Job &job2);
friend bool operator!= (Job &job1, Job &job2);
friend ostream &operator<<(ostream& out,const Job& job); */
private:
vector<Processor> captiveProcessors_;
string name_;
int PID_;
int ticksRemaining_;
public:
Job(string name, int processors, int ticks)
{
name_ = name;
ticksRemaining_ = ticks;
PID_ = system_.getPID();
}
void takeProcessor(Processor processor)
/*This takes a processor and makes it captive*/
{
captiveProcessors_.push_back(processor);
}
int tick()
/*decrement the ticks and return ticks remaining*/
{
return --ticksRemaining_;
}
friend bool operator>= (Job &job1, Job &job2)
{
return job1.ticksRemaining_ >= job2.ticksRemaining_;
}
friend bool operator<= (Job &job1, Job &job2)
{
return job1.ticksRemaining_ <= job2.ticksRemaining_;
}
friend bool operator== (Job &job1, Job &job2)
{
return job1.ticksRemaining_ == job2.ticksRemaining_;
}
friend bool operator!= (Job &job1, Job &job2)
{
return job1.ticksRemaining_ != job2.ticksRemaining_;
}
friend ostream &operator<<(ostream& out,const Job& job)
{
out <<job.name_ << "\t" job.PID_ << "\t" + job.ticksRemaining_ << "\t\n";
return out;
}
};
class Scheduler
/* This class represents a scheduler object
What should happen here is the user should be able to submit
* a job request (sendjobrequest). it should get enqueud in a regular queue and
* at each tick it will
* convert those requests into
*/
{
private:
vector<Job> runQueue_; // This is the queue that running jobs sit in
vector<Job> waitQueue_; // This is the queue that not running jobs sit in
void tickRunQueue()
/*private method to manipulate the runQueue*/
{
for (int i=0; i<=runQueue_.size();i++)
{
if (0 >= runQueue_.at(i).tick())
/* if there are no ticks left anyhow then free the memory and remove
the pointer from the Queue*/
{
//delete(runQueue_.back());
runQueue_.pop_back();
}
cout << runQueue_.at(i);
}
}
void tickWaitQueue()
/* private method to move a */
{
runQueue_.push_back(waitQueue_.back());
waitQueue_.pop_back();
}
public:
Scheduler()
{
//runQueue_ = new vector<Job>();
//waitQueue_ = new vector<Job>();
}
processJobRequest(JobRequest jobRequest)
/* This converts a job request into an actual job
This is where jobRequests go to die*/
{
waitQueue_.push_back(new Job(jobRequest.getName(), jobRequest.getProcessingRequirements(),jobRequest.getTicksRequired()));
//delete(jobRequest);
}
tick()
{
// I would make a queue that you can put new submissions to and a threaded
// shell.. but I looked at pthreads,, no that's not going to happen, and
// honestly I don't know any other sensible way to allow users to submit
// a job during any tick.. but here are some comments
// if (submissionqueue) {pop the thing off the queue and processoJobRequest()
// good enough
// I take that back maybe I'll use kbhit() to get some text input if I have time
// forget it I don't even have the curses libraries installed.. no interactive shell
// if queue is not empty and enough processors are available then pop
// a job off and give it processors
}
};
int main()
{
// System objects, scheduler and system get created here
// Processors get created too
//system_ = new System();
Scheduler scheduler; // = new Scheduler();
/*make the processors*/
for (int i = 0; i < NUMBER_OF_PROCESSORS; i++)
{
system_.takeProcessor(new Processor());
}
scheduler.processJobRequest( JobRequest("nethack", 2, 4));
scheduler.processJobRequest( JobRequest("gcc", 4, 2));
scheduler.processJobRequest( JobRequest("pwd", 7, 3));
scheduler.processJobRequest( JobRequest("javac", 3, 8));
scheduler.processJobRequest( JobRequest("clear", 3, 3));
scheduler.processJobRequest( JobRequest("fortune", 1, 1));
scheduler.processJobRequest( JobRequest("nethack", 2, 4));
scheduler.processJobRequest( JobRequest("gcc", 4, 2));
scheduler.processJobRequest( JobRequest("pwd", 7, 3));
scheduler.processJobRequest( JobRequest("javac", 3, 8));
scheduler.processJobRequest( JobRequest("clear", 3, 3));
scheduler.processJobRequest( JobRequest("fortune", 1, 1));
scheduler.processJobRequest( JobRequest("nethack", 2, 4));
scheduler.processJobRequest( JobRequest("gcc", 4, 2));
scheduler.processJobRequest( JobRequest("pwd", 7, 3));
scheduler.processJobRequest( JobRequest("javac", 3, 8));
scheduler.processJobRequest( JobRequest("clear", 3, 3));
scheduler.processJobRequest( JobRequest("fortune", 1, 1));
scheduler.processJobRequest( JobRequest("nethack", 2, 4));
scheduler.processJobRequest( JobRequest("gcc", 4, 2));
scheduler.processJobRequest( JobRequest("pwd", 7, 3));
scheduler.processJobRequest( JobRequest("javac", 3, 8));
scheduler.processJobRequest( JobRequest("clear", 3, 3));
scheduler.processJobRequest( JobRequest("fortune", 1, 1));
while(1)
{
scheduler.tick();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hmm .. I dont think anyone has suggested this so far, so - just use a local variable and the rest is OK.
//system_ = new System();
System system_;
Scheduler scheduler; // = new Scheduler();
>> the C++ way of doing this seems completely wrong to me I don't see the pattern.
Maybe, but thats not how Cipher sees the matrix
//system_ = new System();
System system_;
Scheduler scheduler; // = new Scheduler();
>> the C++ way of doing this seems completely wrong to me I don't see the pattern.
Maybe, but thats not how Cipher sees the matrix
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This here will compile:
Open in new window