2012-09-25 21:29:00 -04:00
|
|
|
/*Memory Management Program
|
|
|
|
* Used to test the algorithm and print out results.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
public class memoryManagement{
|
2012-11-09 20:20:40 -05:00
|
|
|
|
2012-11-16 22:13:29 -05:00
|
|
|
static final int JOBAMOUNT = 200;
|
|
|
|
static final int MEMORYSIZE = 100;
|
2012-11-12 20:04:32 -05:00
|
|
|
|
2012-09-25 21:29:00 -04:00
|
|
|
public static void main(String args[])throws Exception{
|
2012-11-12 20:04:32 -05:00
|
|
|
|
2012-09-25 21:29:00 -04:00
|
|
|
File file = new File("null");
|
|
|
|
Scanner keyboard = new Scanner(System.in);
|
|
|
|
Scanner fileScan;
|
|
|
|
StringTokenizer token;
|
|
|
|
Random rand = new Random();
|
|
|
|
|
|
|
|
String read = null;
|
2012-11-06 19:30:50 -05:00
|
|
|
int jobLength = 0;
|
2012-09-25 21:29:00 -04:00
|
|
|
long timeStart, timeEnd;
|
|
|
|
|
2012-11-06 19:30:50 -05:00
|
|
|
//*Job Info*
|
|
|
|
int[] id = new int[JOBAMOUNT];
|
|
|
|
int[] size = new int[JOBAMOUNT];
|
|
|
|
int[] time = new int[JOBAMOUNT];
|
|
|
|
|
2012-09-25 21:29:00 -04:00
|
|
|
//******Add your algorithm class here******//
|
2012-11-13 17:22:47 -05:00
|
|
|
//baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE);
|
2012-11-16 22:13:29 -05:00
|
|
|
threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE);
|
2012-11-16 13:01:41 -05:00
|
|
|
//FirstFit David01 = new FirstFit();
|
2012-11-16 22:13:29 -05:00
|
|
|
//NextFit David02 = new NextFit();
|
2012-11-17 02:24:16 -05:00
|
|
|
BestFitAlgorithm David_Weber_BestFit = new BestFitAlgorithm(MEMORYSIZE);
|
|
|
|
WorstFitAlgorithm David_Weber_WorstFit = new WorstFitAlgorithm(MEMORYSIZE);
|
2012-09-25 21:29:00 -04:00
|
|
|
|
|
|
|
//Gets a file name, else creates five random jobs
|
|
|
|
do{
|
|
|
|
System.out.println("Type filename to load jobs from a file or just press enter for random jobs");
|
|
|
|
read = keyboard.nextLine();
|
|
|
|
file = new File(read + ".txt");
|
|
|
|
if(!read.equals("") && !file.exists())
|
|
|
|
System.out.println("File not found, try again");
|
|
|
|
}while(!read.equals("") && !file.exists());
|
|
|
|
|
|
|
|
//Create random jobs or read from the file and create jobs
|
|
|
|
if(read.equals("")){
|
2012-11-13 17:22:47 -05:00
|
|
|
System.out.print("Creating " + JOBAMOUNT + " random jobs...");
|
2012-09-25 21:29:00 -04:00
|
|
|
jobLength = JOBAMOUNT;
|
2012-11-06 19:30:50 -05:00
|
|
|
for(int i = 0; i < jobLength; i++){
|
|
|
|
id[i] = i+1;
|
2012-11-16 22:13:29 -05:00
|
|
|
size[i] = rand.nextInt(5)+1;
|
|
|
|
time[i] = rand.nextInt(1000)+2001;
|
2012-11-06 19:30:50 -05:00
|
|
|
}
|
2012-09-25 21:29:00 -04:00
|
|
|
System.out.println("complete");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
System.out.print("File found, reading file...");
|
|
|
|
fileScan = new Scanner(file);
|
|
|
|
for(jobLength = 0; fileScan.hasNextLine() ; jobLength++){
|
|
|
|
token = new StringTokenizer(fileScan.nextLine(),",");
|
2012-11-06 19:34:40 -05:00
|
|
|
id[jobLength] = jobLength+1;
|
2012-11-06 19:30:50 -05:00
|
|
|
size[jobLength] = Integer.parseInt(token.nextToken());
|
|
|
|
time[jobLength] = Integer.parseInt(token.nextToken());
|
2012-09-25 21:29:00 -04:00
|
|
|
}
|
|
|
|
fileScan.close();
|
|
|
|
System.out.println("complete");
|
|
|
|
System.out.println(jobLength+" jobs found on file");
|
|
|
|
}
|
2012-11-17 05:07:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-25 21:29:00 -04:00
|
|
|
|
|
|
|
//Send jobs to algorithm, time is calculated and printed out after completion
|
2012-11-13 17:22:47 -05:00
|
|
|
System.out.print("Sending jobs to threaded allocation algorithm...");
|
2012-09-25 21:29:00 -04:00
|
|
|
timeStart = System.currentTimeMillis();
|
2012-11-13 17:22:47 -05:00
|
|
|
//Note that we use `jobLength - 1` to compensate for the id above
|
|
|
|
for(int i = 0; i < jobLength - 1; i++){
|
2012-11-16 22:13:29 -05:00
|
|
|
Bradlee_Speice.allocate(id[i], size[i], time[i]);
|
2012-11-16 13:01:41 -05:00
|
|
|
//David01.allocate(id[i], size[i], time[i]);
|
2012-11-16 22:13:29 -05:00
|
|
|
//David02.allocate(id[i], size[i], time[i]);
|
2012-09-25 21:29:00 -04:00
|
|
|
}
|
|
|
|
timeEnd = System.currentTimeMillis() - timeStart;
|
|
|
|
System.out.println("complete");
|
2012-11-17 05:07:03 -05:00
|
|
|
System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength +
|
|
|
|
" jobs is " + timeEnd + " milliseconds");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-17 02:24:16 -05:00
|
|
|
//***Best Fit (David Weber)***
|
|
|
|
timeStart = System.currentTimeMillis();
|
|
|
|
for(int i = 0; i < jobLength - 1; i++){
|
2012-11-17 05:07:03 -05:00
|
|
|
David_Weber_BestFit.allocate(id[i], size[i], time[i]);
|
2012-11-17 02:24:16 -05:00
|
|
|
}
|
|
|
|
timeEnd = System.currentTimeMillis() - timeStart;
|
|
|
|
System.out.println("complete");
|
|
|
|
System.out.println("Elapsed time for threaded best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds");
|
|
|
|
|
2012-11-17 05:07:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-11-17 02:24:16 -05:00
|
|
|
//***Worst Fit (David Weber)***
|
|
|
|
timeStart = System.currentTimeMillis();
|
|
|
|
for(int i = 0; i < jobLength - 1; i++){
|
|
|
|
//David_Weber_WorstFit.allocate(id[i], size[i], time[i]);
|
|
|
|
}
|
|
|
|
timeEnd = System.currentTimeMillis() - timeStart;
|
|
|
|
System.out.println("complete");
|
|
|
|
System.out.println("Elapsed time for threaded worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds");
|
2012-11-17 05:07:03 -05:00
|
|
|
*/
|
2012-11-17 02:24:16 -05:00
|
|
|
|
|
|
|
//Put other algorithms here.
|
|
|
|
|
|
|
|
|
2012-09-25 21:29:00 -04:00
|
|
|
System.out.println("Completed Successfully");
|
2012-11-13 17:22:47 -05:00
|
|
|
//Forcibly close down all threads
|
|
|
|
System.exit(0);
|
2012-09-25 21:29:00 -04:00
|
|
|
}
|
2012-11-16 13:01:41 -05:00
|
|
|
}
|