bamcclur 0 Newbie Poster

So I pretty much finished a memory management simulator, and it worked for the test cases my professor gave me, but when I added a test case it didn't work as it should have.

The main issue is, the first job will start, and finish, but the wait time wont display properly.


Sorry if the code is weird at some parts, my whole group has been doing different sections.

#include <cmath>                                                                //include cmath
#include <iostream>                                                             //include iostream for I/O
#include <fstream>                                                              //include filestream for opening the file

using namespace std;                                                            //use the standard namespace

char memory[256]={'_'};
int currtime=0;
int length=0;
double tHole=0;

struct Job{                                                                     //create a struct to hold the data for each job
    char pid;//psh
    int arrival;//psh
    int service;
    int size;//psh
    int duration;//psh
    int newTime;//time in new
    int readyTime;//time in ready
    int pstate;//process state
};        
bool openFile(ifstream& ins, char* filename);                                   //a function to upen the file
void readFile(ifstream& ins, Job theJobs[]);                                    //a function to read the file
bool insertJob0(char mem[], Job& theJob);                                       //if arg 1 is 0, the job scheduling system
bool insertJob1(char mem[], Job& theJob);                                       //if arg 1 is 0, the job scheduling system
bool insertJob2(char mem[], Job& theJob);                                       //if arg 1 is 0, the job scheduling system
void doWork(char mem[],Job& currJob);
void printMem(char mem[]);
        
int main(int arg, char* args[]){
    fill_n(memory,256,'_');                                                     //initialize the memory array to all blank memories
    ifstream ins;                                                               //create an input file stream
    if(!openFile(ins, args[2])){   int wait;   cin>>wait;  return 0;}           //if the file doesn't open, create a wait variable, wait for input, then exit
    Job theJobs[70]={'_',0,0,0,0,0,1,-2};                                        //create 26 structs of type job
    readFile(ins,theJobs);                                                      //read in the values for the jobs
    
    int c=atoi(args[1]);                                                        //create an integer out of the first given parameter
    int currJob=0;
    int tick=0;
    while (tick<=length){
        for(int a=0;a<=length;a++){
            if (theJobs[a].arrival<=currtime && (theJobs[a].pstate==-2 || theJobs[a].pstate==0)){
                theJobs[a].pstate=0;
                theJobs[currJob].pstate=2;
                switch (c){
                case 0:
                    insertJob0(memory,theJobs[a]);
                break;
                case 1:
                    insertJob1(memory,theJobs[a]);
                break;
                case 2:
                    insertJob2(memory,theJobs[a]);
                break;
                default:
                    cout<<"Improper parameter \""<<args[1]<<"\""<<endl; 
                    cout<<"Enter a key to exit:"; int wait; cin>>wait; return 0;  
                }  
            }
            if(theJobs[currJob].duration==0 || theJobs[a].duration<theJobs[currJob].duration){
                if(theJobs[currJob].pstate==-1){
                    theJobs[a].pstate=2;
                    currJob=a;
                }
                else{
                    if(theJobs[a].pstate==1){
                        theJobs[a].pstate=2;
                        theJobs[currJob].pstate=1;
                        currJob=a;
                    }
                }
            }
        }
        currtime++;
        if(currJob>=0){
            doWork(memory,theJobs[currJob]);
        }
        if(theJobs[currJob].duration==0){
            tick++;
        }
        for( int a=0;a<=length;a++){
            switch (theJobs[a].pstate){
            case 0:
                theJobs[a].newTime++;            
            break;
            case 1:
            case 2:
                theJobs[a].readyTime++;            
            break;
            default:
                cout<<"";
            }        
        }
    }
    cout<<" Job#    | Running  | New      | Ready    |Memory Size"<<endl;
    cout<<"         | Duration | Duration | Duration |"<<endl;
    cout<<"=========+==========+==========+==========+==========="<<endl;
    cout<<""<<endl;
    int tWait=0;
    for(int a=0; a<=length; a++){
        printf(" pid%d\t | %03d      | %03d      | %03d      | %03d\n",
            theJobs[a].pid-65,
            theJobs[a].service,
            theJobs[a].newTime,
            theJobs[a].readyTime,
            theJobs[a].size
        );
        tWait+=theJobs[a].newTime+theJobs[a].readyTime;
    }
    cout<<"=========+==========+==========+==========+==========="<<endl;
    cout<<endl;
    printf("Total simulated time units: %04d\n",currtime);
    printf("Total number of jobs: %02d\n",length+1);
    printf("Average hole percent: %2.3f\n",tHole/(double)((length+1)*2));
    printf("Average waiting time: %2.3f\n",((double)((double)tWait/(double)length)));
    
    
    cout<<"Enter a key to exit:"; int wait; cin>>wait; return 0;  
    return 0;
}



bool insertJob0(char mem[], Job& theJob){
    bool insert=false;
    for(int a=0;a<256;a++){
        if(mem[a]=='_'){
            insert=true;
            for(int b=0;b<theJob.size;b++){
                if (a+b>=256 || mem[a+b]!='_'){
                    insert=false;
                }
            }
            if (insert){
                for(int b=0;b<theJob.size;b++){
                    mem[a+b]=theJob.pid;
                }
                theJob.pstate=1;
                printMem(memory);
                return true;
            }
        }
    }
    return false;
}

bool insertJob1(char mem[], Job& theJob){
    int insert=-1;
    int temp_insert = -1;
    int temp_space = 0;
    int best_space = 257;
    for(int a=0;a<256;a++){
        if(mem[a]=='_' && a<255){
            if(temp_space == 0) temp_insert = a;
            temp_space++;
        }
        else{
            if(temp_space >= theJob.size && temp_space < best_space){
                best_space = temp_space;
                insert = temp_insert;
            }
            temp_space = 0;
        }
    }
    
    if (insert >= 0){
        for(int b=0;b<theJob.size;b++){
            mem[insert+b]=theJob.pid;
        }
        theJob.pstate=1;
        printMem(memory);
        return true;
    }
    return false;
}

bool insertJob2(char mem[], Job& theJob){
    int insert=-1;
    int temp_insert = -1;
    int temp_space = 0;
    int worst_space = -1;
    for(int a=0;a<256;a++){
        if(mem[a]=='_' && a<255){
            if(temp_space == 0) temp_insert = a;
            temp_space++;
        }
        else{
            if(temp_space >= theJob.size && temp_space > worst_space){
                worst_space = temp_space;
                insert = temp_insert;
            }
            temp_space = 0;
        }
    }
    if (insert >= 0){
        for(int b=0;b<theJob.size;b++){
            mem[insert+b]=theJob.pid;
        }
        theJob.pstate=1;
        printMem(memory);
        return true;
    }
    return false;
}

void doWork(char mem[], Job& theJob){
    if (theJob.pstate==2){
        if(theJob.duration==1){
            for(int a=0;a<256;a++)if(mem[a]==theJob.pid)mem[a]='_';
            theJob.pstate=-1;
            printMem(memory);
        }
        theJob.duration=(theJob.duration-1);
    }
    return;
}

void printMem(char mem[]){
    cout<<"000    ";
    for(int b=0;b<64;b++){
        cout<<memory[b];
    }
    cout<<endl;
    cout<<"064    ";
    for(int b=64;b<128;b++){
        cout<<memory[b];
    }
    cout<<endl;
    cout<<"128    ";
    for(int b=128;b<192;b++){
        cout<<memory[b];
    }
    cout<<endl;
    cout<<"192    ";
    for(int b=192;b<256;b++){
        cout<<memory[b];
    }
    cout<<endl;
    int holes=0;
    for(int a=0;a<256;a++){
        if(mem[a]=='_'){
            holes++;
        }
    }
    tHole+=(double)holes/(double)2.56;
    printf("Percent holes: %3.2f\n\n\n",(double)holes/(double)2.56); 
    return;   
}

bool openFile(ifstream& ins, char* filename)
{
	ins.open(filename);
	if(ins.good())return true;
	cout<<endl<<filename<<endl<<"Error opening file"<<endl;
    return false;
}

void readFile(ifstream& ins, Job theJobs[])
{
    int dummy=0;
	while(ins.good()){
        ins>>dummy;
        ins>>theJobs[dummy].arrival;
        ins>>theJobs[dummy].service;
        ins>>theJobs[dummy].size;
        theJobs[dummy].pid=(char)(dummy+65);
        theJobs[dummy].duration=theJobs[dummy].service;
        theJobs[dummy].pstate=-2;
        theJobs[dummy].readyTime=1;
    }
    length=dummy;
    ins.close();
}

the test files look like:
datafile1.txt
0 0 1 32
1 1 7 32
2 4 25 64
3 7 4 16
4 8 20 16
5 9 25 32
6 30 20 15
7 31 30 30
8 32 11 62

datafile2.txt
0 0 30 16
1 1 5 16
2 2 25 64
3 3 2 32
4 4 20 64
5 20 25 28
6 40 20 16
7 42 15 30
8 45 11 18
9 45 20 10

datafile3.txt (my own)
0 0 1 16
1 1 1 16
2 2 1 16
3 3 1 16
4 4 1 16
5 5 1 16
6 6 1 16
7 7 1 16
8 8 1 16
9 9 1 16
10 10 1 32
11 11 1 16
12 12 1 64
13 13 1 16
14 14 1 32
15 15 1 16
16 16 1 32
17 17 1 16
18 18 1 32

to run properly, two parameters must be sent to the program, a number, either 0, 1, or 2, and the file's name.