I'm writing a queue/scheduler program. The problem is after it prints out the high queue correctly, it segfaults. I've spent hours trying to fix this code and cant find a solution.
template<typename T>
Scheduler<T>::Scheduler(int time, int late){ //Scheduler constructor
time_ = time;
late_ = late;
}
template<typename T>
void Scheduler<T>::schedule(T &job){ //Adds job to appropriate queue
job.arrival_ = this->time_; // Sets the arrival time of the job
if(job.priority_ == "low")
{
low_.push_back(job);
}
if(job.priority_ == "high")
{
high_.push_back(job);
}
}
template<typename T>
ostream& Scheduler<T>::print_scheduler(ostream& out){ //Prints scheduler
out << "Scheduler, " << "Time:" << this->time_;
out << ", Late:" << this->late_ << endl;
out << "low queue" << endl;
for(int i = 0; i < low_.size(); i++) //Prints low priority queue
{
out << " ";
out << low_[i] << endl;
}
out << "high queue" << endl;
for(int i = 0; i < high_.size(); i++) //Prints high priority queue
{
out << " ";
out << high_[i] << endl;
}
return out;
}
template<typename T>
bool Scheduler<T>::empty(){
if(high_.size() == 0 && low_.size() == 0)
{
returntrue;
}
else
{
returnfalse;
}
}
template<typename T>
T Scheduler<T>::run(){
T selected;
if(time_ - high_.front().arrival_ > late_) //High priority late, takes next high priority.
{
selected = high_.front();
time_ = time_ + selected.duration_;
high_.pop_front();
//THIS WORKS FINE
}
elseif(time_ - high_.front().arrival_ < late_ && time_ - low_.front().arrival_ >= late_) //Low priority late, takes next low priority.
{
selected = low_.front();
time_ = time_ + selected.duration_;
low_.pop_front();
}
elseif(time_ - high_.front().arrival_ < late_ && time_ - low_.front().arrival_ < late_) //Neither late, takes next high priority.
{
selected = high_.front();
time_ = time_ + selected.duration_;
high_.pop_front();
//THIS WORKS FINE
}
elseif(high_.empty() == true) //High empty, picks next low priority.
{
selected = low_.front();
time_ = time_ + selected.duration_;
low_.pop_front();
//I THINK ERROR IS IN THIS HIGH EMPTY STATEMENT SOMEHOW <------
}
elseif(low_.empty() == true) //Low empty, picks next high priority.
{
selected = high_.front();
time_ = time_ + selected.duration_;
high_.pop_front();
}
elseif(high_.empty() == true && low_.empty() == true)
{
}
return selected;
}
#endif /* SCHEDULER_H */
I have 2 deques, one low and one high priority. Currently the code correctly goes through and prints the jobs inside the high_ queue. When it gets past that point it dies with a segfault. How do I get it to stop dying at this point and go through with printing out the low queue? There is also a job.cpp and job.h but I didnt feel that it was necessary to post (yet?).
Not having seen all of the code, my best guess is that after you finish running the high priority jobs you are calling high_.front() on a deque that is empty.
First line of code in run() if(time_ - high_.front().arrival_ > late_) // <--- What happens if high_ is empty?
It doesnt segfault anymore but it still doesn't print the contents of the low_ queue, it just infinitely spits out "Running Job" with all the blank/default values.