我有一个程序将在调试器中执行并在结尾输出语句,但在我正常运行时停止工作。我感到困惑和沮丧。如果使用调试器,它将以代码0终止,但是如果我只是正常运行它,会弹出一个窗口说" Prog2-1.exe已停止工作"。
该程序包括" Process.h",iostream,fstream,queue和vector。我在这里列出它们,因为代码示例格式化没有正确显示它们。 Process.cpp还包括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