-
Troubleshooting code
need help troubleshooting... i want this to copy from the preList into the readyList whenever the requirements are met... when x%1000 = 0 and whenever preList[i][0] = x.... why won't this code work??
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
long int preList[21][2];
long int readyList[21][2];
int main()
{
fstream inFile;
inFile.open("times.txt", ios::in);
int arrivalTime;
float timeToProcess;
int i, z = 0;
long int x = 0;
int p;
//make sure file opens
if (!inFile)
{
cout<<"Error opening file."<<endl;
exit(1);
}
//open file, get stuff in there
while(!inFile.eof())
{
for(i=0; i<=21; i++)
{
inFile>>arrivalTime>>timeToProcess;
preList[i][0] = arrivalTime * 1000;
preList[i][1] = timeToProcess;
cout<<preList[i][0]<<" "<<preList[i][1]<<endl;
}
}
while (x < 800000)
{
//set x%1000 into p
p = x%1000;
//if p == 0, it means x is at a number ending in 3 zeros..., so go check preList to see if process is ready to come in or not
if (p == 0)
{
//if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
if (preList[i][0] == x)
{
readyList[z][0] = preList[i][0];
readyList[z][1] = preList[i][1];
cout<<readyList[z][0]<<" "<<readyList[z][1]<<endl;
z++;
}
}
x++;
}
return 0;
}
-
> for(i=0; i<=21; i++)
This should be:
for(i=0; i<21; i++)
> //if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
> if (preList[i][0] == x)
This should be:
if (preList[z][0] == x)
-
first, what exactly is going wrong?
second, just looking at your comments, think about the first run: what's the remainder of 0/1000? I'm not sure if that's really hurting you in any way, but from your comment it looks like a logic flaw.
-
i'm just getting a list of zeros for the output... here is the updated code with changes made... the point is when "arrivalTime" in preList is equal to "x", copy all data from node with "arrivalTime = x" into first node in "readyList", and then when the next node in "preList" is equal to x, add that to next available slot in "readyList''... i hope that all makes sense... here's the updated code...
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
long int preList[21][2];
long int readyList[21][2];
int main()
{
fstream inFile;
inFile.open("times.txt", ios::in);
int arrivalTime;
float timeToProcess;
int i, z = 0;
long int x = 0;
int p;
//make sure file opens
if (!inFile)
{
cout<<"Error opening file."<<endl;
exit(1);
}
//open file, get stuff in there
while(!inFile.eof())
{
for(i=0; i<22; i++)
{
inFile>>arrivalTime>>timeToProcess;
preList[i][0] = arrivalTime * 1000;
preList[i][1] = timeToProcess;
// cout<<preList[i][0]<<" "<<preList[i][1]<<endl;
}
}
i = 0;
while (x < 800000)
{
//set x%1000 into p
p = x%1000;
//if p == 0, it means x is at a number ending in 3 zeros..., so go check preList to see if process is ready to come in or not
if (p == 0)
{
//if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
if (preList[z][0] == x)
{
preList[i][0] = readyList[z][0];
preList[i][1] = readyList[z][1];
cout<<readyList[z][0]<<" "<<readyList[z][1]<<endl;
z++;
}
}
x++;
}
return 0;
}
-
> readyList[z][0] = preList[i][0];
> readyList[z][1] = preList[i][1];
And z here also.
readyList[z][0] = preList[z][0];
readyList[z][1] = preList[z][1];
-
> for(i=0; i<22; i++)
I'm assuming you made this 22 for debugging purposes.
-
there's actually 22 items in my array once read in from the file, so 0-21 will be the "values"
-
>there's actually 22 items in my array once read in from the file, so 0-21 will be the "values"
>long int preList[21][2];
>long int readyList[21][2];
And your arrays only have 21 elements? :confused:
-
yeah, you're right... good catch...
-
new problem!!! here is the updated version of the code i've been working on. I explain in the comments in the code what needs to be done. If someone could help me out here i would GREATLY appreciate it because I am lost. Here is the code with the comments about what needs to be done...
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
long int preList[22][2];
long int readyList[22][2];
int timeQuanta[4] = {50, 100, 250, 500};
int overhead[6] = {0, 5, 10, 15, 20, 25};
int main()
{
fstream inFile;
inFile.open("times.txt", ios::in);
int arrivalTime;
float timeToProcess;
int i, z = 0;
long int x = 0;
int p;
int newTime = 0;
bool done = 0;
int quantumEntered;
cout<<"Please enter time quantum number 1 - 4"<<endl;
cin>>quantumEntered;
//make sure file opens
if (!inFile)
{
cout<<"Error opening file."<<endl;
exit(1);
}
//open file, get stuff in there
while(!inFile.eof())
{
for(i=0; i<22; i++)
{
inFile>>arrivalTime>>timeToProcess;
preList[i][0] = arrivalTime * 1000;
preList[i][1] = timeToProcess;
cout<<preList[i][0]<<" "<<preList[i][1]<<endl;
}
}
cout<<endl<<endl;
while (x < 800000)
{
//set x%1000 into p
p = x%1000;
//if p == 0, it means x is at a number ending in 3 zeros..., so go check preList to see if process is ready to come in or not
if (p == 0)
{
//if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
if (preList[z][0] == x)
{
//copy into readyList at arrival time
cout<<"Time is: "<<x<<endl;
readyList[z][0] = preList[z][0];
readyList[z][1] = preList[z][1];
cout<<"ready list service time: "<<readyList[z][1]<<endl;
//add processing time to arrival time to obtain and see new time
newTime = x + readyList[z][1];
cout<<"Time after run is: "<<newTime<<endl;
cout<<endl;
z++;
}
//SUPPOSED TO SEND PROCESS TO BE PROCESSED INTO PROCESSOR. IF PROCESS ARRIVES AT 30,000, AND HAS A JOB LENGTH OF 1,700, THE JOB WILL BE DONE
//AT 31,700... SUPPOSE A NEW PROCESS ARRIVES AT 35,000... THERE IS NO JOB IN THE "PROCESSOR" AT THAT TIME, SO IT IS IMMEDIATELY TAKEN TO BE PROCESSED
//THE TIME QUANTUM IS SUPPOSED TO GO AND CHECK THE "READY LIST" TO SEE IF THERE IS A JOB WAITING; IF THERE IS, THE CURRENT JOB IN THE
//PROCESSOR GETS AN ALLOCATED AMOUNT OF TIME (timeQuana) TO PROCESS. WHEN "QUANTA" REACHES 0, THE JOB SHOULD BE SWITCHED FROM THE CURRENT JOB
//TO THE JOB WAITING IN THE READY LIST, THE NEW JOB IN GETS A TIME QUANTA OF 50, AND THE PROCESS IS REPEATED UNTIL THE ORIGINAL PROCESS IS DONE.
//HELP!
if (readyList[z] != 0)
{
int quantum = timeQuanta[quantumEntered-1];
newTime = newTime + 1;
//cout<<"new time: "<<newTime<<endl;
//cout<<"quantum: "<<quantum<<endl;
if(quantum == 0)
{
quantum = timeQuanta[quantumEntered -1];
}
quantum--;
}
}
x++;
}
return 0;
}
as it says above, i need to have the timeQuantum work as a "pre-emptive" scheduler. it should be "time slices" to switch the processes from a simulated "processor" back into the ready list with the remaining time left to process (readyList[z][1] - however much work just got done)... i'm totally lost here.. please someone help!!!
-
updated code: someone help please!!
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
long int preList[22][2];
long int readyList[22][2];
int timeQuanta[4] = {50, 100, 250, 500};
int overheadArray[6] = {0, 5, 10, 15, 20, 25};
int main()
{
fstream inFile;
inFile.open("times.txt", ios::in);
int arrivalTime;
float timeToProcess;
int i, z = 0;
long int x = 0;
int p;
int newTime = 0;
int quantumEntered, overheadEntered;
cout<<"Please enter time quantum number 1 - 4"<<endl;
cin>>quantumEntered;
int quantum = timeQuanta[quantumEntered-1];
cout<<"Please enter overhead numer 1 - 6"<<endl;
cin>>overheadEntered;
int overhead = overheadArray[overheadEntered-1];
quantum = quantum + overhead;
//make sure file opens
if (!inFile)
{
cout<<"Error opening file."<<endl;
exit(1);
}
//open file, get stuff in there
while(!inFile.eof())
{
for(i=0; i<22; i++)
{
inFile>>arrivalTime>>timeToProcess;
preList[i][0] = arrivalTime * 1000;
preList[i][1] = timeToProcess;
cout<<preList[i][0]<<" "<<preList[i][1]<<endl;
}
}
cout<<endl<<endl;
while (x < 800000)
{
//set x%1000 into p
p = x%1000;
//if p == 0, it means x is at a number ending in 3 zeros..., so go check preList to see if process is ready to come in or not
if (p == 0)
{
//if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
if (preList[z][0] == x)
{
//copy into readyList at arrival time
cout<<"Time is: "<<x<<endl;
readyList[z][0] = preList[z][0];
readyList[z][1] = preList[z][1];
cout<<"ready list service time: "<<readyList[z][1]<<endl;
//add processing time to arrival time to obtain and see new time
newTime = x + readyList[z][1];
cout<<"Time after run is: "<<newTime<<endl;
cout<<endl;
z++;
}
//SUPPOSED TO SEND PROCESS TO BE PROCESSED INTO PROCESSOR. IF PROCESS ARRIVES AT 30,000, AND HAS A JOB LENGTH OF 1,700, THE JOB WILL BE DONE
//AT 31,700... SUPPOSE A NEW PROCESS ARRIVES AT 35,000... THERE IS NO JOB IN THE "PROCESSOR" AT THAT TIME, SO IT IS IMMEDIATELY TAKEN TO BE PROCESSED
//THE TIME QUANTUM IS SUPPOSED TO GO AND CHECK THE "READY LIST" TO SEE IF THERE IS A JOB WAITING; IF THERE IS, THE CURRENT JOB IN THE
//PROCESSOR GETS AN ALLOCATED AMOUNT OF TIME (timeQuanta) TO PROCESS. WHEN "QUANTA" REACHES 0, THE JOB SHOULD BE SWITCHED FROM THE CURRENT JOB
//TO THE JOB WAITING IN THE READY LIST, THE NEW JOB IN GETS A TIME QUANTA OF 50, AND THE PROCESS IS REPEATED UNTIL THE ORIGINAL PROCESS IS DONE.
if (readyList[z] != 0)
{
newTime = newTime + 1;
if(quantum == 0)
{
//go check readyList, if there is something in there
//switch processes in "cpu"
quantum = timeQuanta[quantumEntered-1] + overhead;
}
cout<<"Quantum is: "<<quantum<<endl;
quantum--;
}
}
x++;
}
return 0;
}
-
updated code: anyone out there??????
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
long int preList[22][2];
long int readyList[22][3];
int timeQuanta[4] = {50, 100, 250, 500};
int overheadArray[6] = {0, 5, 10, 15, 20, 25};
int main()
{
fstream inFile;
inFile.open("times.txt", ios::in);
int arrivalTime;
float timeToProcess;
int i, z = 0;
long int x = 0;
int p;
int newTime = 0;
int quantumEntered, overheadEntered, timeArrived;
int averageWaitTime, averageTurnAroundTime = 0;
cout<<"Please enter time quantum number 1 - 4"<<endl;
cin>>quantumEntered;
int quantum = timeQuanta[quantumEntered-1];
cout<<"Please enter overhead numer 1 - 6"<<endl;
cin>>overheadEntered;
int overhead = overheadArray[overheadEntered-1];
quantum = quantum + overhead;
//make sure file opens
if (!inFile)
{
cout<<"Error opening file."<<endl;
exit(1);
}
//open file, get stuff in there
while(!inFile.eof())
{
for(i=0; i<22; i++)
{
inFile>>arrivalTime>>timeToProcess;
preList[i][0] = arrivalTime * 1000;
preList[i][1] = timeToProcess;
timeArrived = x;
cout<<preList[i][0]<<" "<<preList[i][1]<<endl;
}
}
cout<<endl<<endl;
while (x < 800000)
{
//set x%1000 into p
p = x%1000;
//if p == 0, it means x is at a number ending in 3 zeros..., so go check preList to see if process is ready to come in or not
if (p == 0)
{
//if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
if (preList[z][0] == x)
{
//copy into readyList at arrival time
cout<<"Time is: "<<x<<endl;
readyList[z][0] = preList[z][0];
readyList[z][1] = preList[z][1];
readyList[z][2] = 1;
if (readyList[z][2] != 0)
{
newTime = newTime + 1;
if(quantum == 0)
{
//go check readyList, if there is something in there
//switch processes in "cpu"
//reset quantum to alloted quantum chosen
quantum = timeQuanta[quantumEntered-1] + overhead;
}
cout<<"Quantum is: "<<quantum<<endl;
quantum--;
}
cout<<"ready list service time: "<<readyList[z][1]<<endl;
//add processing time to arrival time to obtain and see new time
newTime = x + readyList[z][1];
cout<<"Time after run is: "<<newTime<<endl;
cout<<endl;
z++;
}
}
x++;
}
//averageWaitTime = (time process enters CPU - timeArrived)/22;
//cout<<"average wait time: "<<averageWaitTime<<endl;
averageTurnAroundTime = (newTime - timeArrived)/22;
cout<<"average turnaround time: "<<averageTurnAroundTime<<endl;
return 0;
}