0

I have a program that will execute in the debugger and output the statements at the end, but stops working when I run it normally. I am baffled and frustrated. It will terminate with code 0 if stepped through with the debugger, but if I just run it normally, a window pops up saying "Prog2-1.exe has stopped working".

The program includes "Process.h", iostream, fstream, queue and vector at this point. I am listing them here as the code sample formatting is not showing them properly. Process.cpp also includes Process.h.

int main() {
    using std::cout;
    using std::ifstream;
    using std::queue;
    using std::vector;
    int pi = 0;
    int pa = 0;
    int t = 0;
    int mem = 32;
    int count = 0;
    queue <Process> newP; //Processes loaded here from text file
    vector <Process> run; //Process move here when executing
    vector <Process> exit; //Processes move here when done executing
    ifstream infile;
    infile.open("C:\\Users\\Austin\\eclipse-workspace\\OSCProg2\\data.txt"); // Open text file
    if(infile.fail()) // Fail-safe in case of file open failure
    {
        cout << "error";
    } //end Fail-safe
    while(!infile.eof()) // Reads file, fills newP queue.
    {
        infile >> pi >> pa >> t; //Reads in a line of data
        Process temp(pi, pa, t); //Assigns data to a Process
        newP.push(temp); //Puts Process in newP queue
    } //end while
    infile.close(); //closes ifstream
    while(exit.size() < 25) //Execution loop
    {
        if(!run.empty()) //Skips this step if there are no Processes in run state
        {
            for(unsigned int i = 0; i < run.size(); i++)    //Parses through the processes in run
            {                                               //Checking for any who have finished running.
                if(run.at(i).getTime() <= 0) //If remaining time <= 0 (execution complete)
                {
                    exit.push_back(run.at(i)); //Add to exit
                    mem = mem + run.at(i).getPages(); //Open previously occupied space in memory
                    run.erase(run.begin() + i); //Remove from run state vector
                } //end inner if
            } //end for
        } //end outer if
        if(!newP.empty()) //Skips this step if there are no more Processes in newP
        {
            while(newP.front().getPages() <= mem) //While the next process can fit in remaining memory
            {
                mem = mem - newP.front().getPages(); //Allocate space in memory (reduce open space)
                run.push_back(newP.front()); //Add next Process to run
                newP.pop(); //Remove Process from newP queue
            } //end while
        } //end if
        if(!run.empty()) //Skips this step if there are no processes in run state
        {
            for(unsigned int i = 0; i < run.size(); i++) //For each Process in run state
            {
                run.at(i).setTime(0); //1 unit of time passes - reqTime decrements
            } //end for
        } //end if
        count++;
    } //end Execution loop
    cout << "Simulation complete. All processes executed.";
    cout << "\nResult: " << count << " units of time required to complete.";
}//end main


//Process.cpp
Process::Process(int pi, int pa, int rt) {
    pid = pi;
    pages = pa;
    reqTime = rt;
}

int Process::getPID() {
    return pid;
}

void Process::setPID(int p) {
    pid = p;
}

int Process::getPages() {
    return pages;
}

void Process::setPages(int p) {
    pages = p;
}

int Process::getTime() {
    return reqTime;
}

void Process::setTime(int t) {
    reqTime = t;
}

void Process::passTime() {
    reqTime = reqTime - 1;
}

//Process.h
#ifndef PROCESS_H_
#define PROCESS_H_

class Process {
    private:
        int pid; //Process ID or PID
        int pages; //Number of pages the process requires
        int reqTime; //Time required to execute

    public:
        Process(int pi, int pa, int rt); //Constructor
        int getPID();            //Returns PID
        void setPID(int p);  //Sets PID to p
        int getPages();          //Returns pages
        void setPages(int p); //Sets pages to p
        int getTime();          //
        void setTime(int t);
        void passTime();
};

#endif
Austin K.
  • 1
  • 1
  • 1
    How is `Process` declared? – wally Nov 03 '17 at 19:39
  • please provide a [mcve], ie including the includes and `Process` – 463035818_is_not_a_number Nov 03 '17 at 19:40
  • 2
    Undefined Behavior: You call `newP.front()` when `newP` is empty. – 1201ProgramAlarm Nov 03 '17 at 19:44
  • If you single step in the debugger, the processes will be created *a lot* slower and might avoid some race conditions. Also see here why [while(!infile.eof()) is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Bo Persson Nov 03 '17 at 19:47
  • @1201ProgramAlarm Please explain. The only instance of newP.front() I am seeing occurs inside an if statement that explicitly checks to see if newP is empty. – Austin K. Nov 03 '17 at 19:52
  • Never mind @1201ProgramAlarm, I realized what you meant. Since it is the condition of a loop, it can be called after the last Process is moved out, thus rendering newP empty. I added !newP.empty() as a condition to that loop before the front condition, and it works now. Thank you! – Austin K. Nov 03 '17 at 19:55

0 Answers0