I am trying to get this program to work correctly. My main issue is with the logic. I can't figure out how to get the desired output. I have it attached becuase it wouldnt line up.
Here is the file thats being read in.
4 is the amount of time a process can be in memory. ABCD are process IDEN.
20,15, 5 are the memory it needs. 6,7,8,2 is how long it needs to run in memory.
4
A 20 6
B 20 7
C 15 8
D 5 2
The attched file explains the read ins a little more.
Thanks for helping.
Code:
#include <string>
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
#define maxmem 50
struct process
{
int time[30];
int memory[30];
char process1[30];
bool done[30];
};
//read the processs in
void memin(char process, char[], int position, int large);
//read it the process out
void memout(char [], int memory);
//its done kick out
void removemem(char memarray[], char memchar);
//put process in biggest free space avail
int worstfit(char[], int& position);
//get the time left for the process.
int dispatch(process&, queue<int>, int);
int main()
{
queue<int> jobs;
int availmem;
char dot ='.';
char memarray[50];
char memchar, reqmem;
bool done=true;
bool condition;
int quantum, totaltime = 0, totalprocess = 0;
int temp, currentp, time = 0;
int position = 0;
int timeleft, l, maxproc = 0, avgmem = 0, usedmem = 0;
float tp, maxUtil = 0.0;
string filename;
ifstream inp;
//Uses array of structures to read the elements of the file
process myprocess;
for (int xx = 0; xx < maxmem; xx++)
{
memarray[xx] = '.';
}
//Opens the file
cout << "Enter dat file name: ";
cin >> filename;
inp.open(filename.c_str());
int j = 0, i = 0;
if(!inp)
{ cout << "Can't open file" << endl;
exit(1);
}
inp >> quantum;
while(!inp.eof())
{
inp >> myprocess.process1[j];
inp >> myprocess.memory[j];
inp >> myprocess.time[j];
j++;
}
totalprocess = j - 1;
inp.close();
temp = totalprocess;
while (temp >= 0)
{
totaltime = myprocess.time[temp] + totaltime;
temp--;
}
cout << " 0....5....0....5....0....5....0....5....0....5...9" << endl;
memout(memarray, maxmem);
cout << endl;
availmem = 0;
int p = 0;
for (int x = 0; x < 29; x++)
myprocess.done[x] = false;
do
{ time++; condition = false;
// finds largest available memory space and returns it's starting position
availmem = worstfit(memarray, position);
// returns the process character name
memchar = myprocess.process1[p];
// returns the amount of memory required for process
reqmem = myprocess.memory[p];
// checks to see if enough room in memory and if so then adds to memory if hasn't been already.
if (availmem >= reqmem && myprocess.done[p] == false)
{
memin(memchar, memarray, position, reqmem);
myprocess.done[p] = true;
condition = true;
jobs.push(p);
}
// dispatch removes 1 click from the running time
temp = jobs.front();
timeleft = dispatch(myprocess, jobs, temp);
// move to end of queue if process hasn't completed
if(time%quantum == 0 && timeleft != 0)
{
currentp = jobs.front();
jobs.pop();
jobs.push(currentp);
}
// if process is complete then removes from memory
if (timeleft == 0)
{
memchar = myprocess.process1[temp];
removemem(memarray, memchar);
condition = true;
myprocess.done[temp] = true;
jobs.pop();
}
if (maxproc < jobs.size())
maxproc = jobs.size();
// gathering stats
availmem = worstfit(memarray, position);
usedmem = maxmem - availmem;
if (usedmem > maxUtil)
maxUtil = usedmem;
avgmem = (usedmem + avgmem);
// prints memory if a change occurs
//if (condition)
//{
cout << time << ": ";
memout(memarray, maxmem);
cout << endl;
//}
p++;
if(p >= totalprocess)
p = 0;
//cin >> l;
} while(jobs.size() != 0 && availmem != 50);
avgmem = avgmem/time;
maxUtil = (maxUtil * 100.0)/maxmem ;
tp = (totalprocess*100.0)/time;
cout << "Totaltime should take: " << totaltime << " largest availmem currently: " << availmem << " position: " << position << endl;
cout << "Max Proc Count: " << maxproc << " Avg Mem Utilization: " << avgmem << endl;
cout << "Max MemUtilization: " << maxUtil << " Throughput: " << tp << endl;
return 0;
} // end of main
int worstfit(char memarray[], int& position)
{
int large = 0;
int counter = 0;
for (int i = 0; i < 50; i++)
{
if(memarray[i] == '.')
counter++;
else counter = 0;
if(memarray[i+1] != '.')
{if (counter >= large)
{
large = counter;
position = i - counter + 1;
}
counter = 0;
}
}
return large;
}
void memin(char theprocess, char memarray[50], int position, int reqmem)
{
for (int j = position; j <= reqmem + position - 1; j++)
memarray[j] = theprocess;
}
void memout(char memarray[50], int memory)
{
for(int i = 0; i < memory; i++)
cout << memarray[i];
//cout << endl;
}
void removemem(char memarray[], char memchar)
{
for (int j = 0; j < maxmem; j++)
if (memarray[j] == memchar)
memarray[j] = '.';
}
int dispatch(process &myprocess, queue<int> jobs, int h)
{
int timeleft;
myprocess.time[h] = myprocess.time[h] -1 ;
timeleft = myprocess.time[h];
return timeleft;
}
I dont see a mistake, but on the supposedly correct one you have spaces to line stuff up and you have two line # 11's which does not make a bit of sense to me. It also looks like you are off by one, you do not have a line zero and therefore are off by one timestep relatively (?)
but basically, I am trying to get to that output with what I have.
I added after this an else:
if (availmem >= reqmem && myprocess.done[p] == false)
else
P++;
This gets me the desired output(kinda). It reads in way too many B's in the correct spot. and d's are in the correct spot. I am short 2 lines of D's, and 2 extra A's.
Bookmarks