URGENT: Segfault Problem

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.

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Job j1("low", 25, "3rd");
  Job j2("high", 100, "1st");
  Job j3("high", 25, "2nd");
  Job j4("low", 25, "4th");
  
  Scheduler<Job> sch(0,50);
  
  sch.schedule(j1);
  sch.schedule(j2);
  sch.schedule(j3);
  sch.schedule(j4);
  
  sch.print_scheduler(cout);
  cout << endl <<endl;
  Job j;
 
 while(!sch.empty()){
   j = sch.run();
   cout << "Running Job, "<<j <<endl << endl;
   sch.print_scheduler(cout);
   cout << endl;
  } 
}


scheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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)
    {
        return true;
    }
    
    else
    {
        return false;
    }
}


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
    }
    
    else if(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();
    }
    
    else if(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
    }
    
    else if(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 <------
    }
    
   else if(low_.empty() == true) //Low empty, picks next high priority.
    {
      selected = high_.front();
      time_ = time_ + selected.duration_;
      high_.pop_front(); 
    }
    
   else if(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?).

OUTPUT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Running Job, Name:1st Arrival:0 Duration:100 

Scheduler, Time:100, Late:50
low queue
        Name:3rd Arrival:0 Duration:25 
        Name:4th Arrival:0 Duration:25 
high queue
        Name:2nd Arrival:0 Duration:25 

Running Job, Name:2nd Arrival:0 Duration:25 

Scheduler, Time:125, Late:50
low queue
        Name:3rd Arrival:0 Duration:25 
        Name:4th Arrival:0 Duration:25 
high queue


RUN FINISHED; Segmentation fault;

Last edited on
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?

http://www.cplusplus.com/reference/deque/deque/front/
"Calling this function on an empty container causes undefined behavior."

Try checking if the the deques are empty() before you call front().

Will look into it.
Last edited on
1
2
3
if(high_.size() == 0)
{
}


^ Added this as the first line of Run().

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.
Last edited on
Asked 2 other classmates, neither one said they have a "if the deque is empty" checker before other stuff in Run(). Guess that's not the issue :(
run() is always returning selected even if you fail all of you if checks which is what must be happenning.
Post your latest run() code.
Topic archived. No new replies allowed.