-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
113 lines (95 loc) · 3.74 KB
/
Driver.java
File metadata and controls
113 lines (95 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.io.*;
import java.util.ArrayList;
public class Driver{
static final String[] validAlgs = {"lru", "fifo", "sclru"};
static final int BUFFER_SPACE = 512;
public static void main(String args[]){
if(args.length != 5){
System.out.println("Calling structure: java Driver programlist commandlist [page size: powers of two up to 16] [algorithm: lru, fifo, sclru] [demand or prepage: d or p]");
}else{
//Start checking and loading values.
File programList = new File(args[0]);
File commandList = new File(args[1]);
int pageSize = 1;
String alg = args[3].toLowerCase();
String paging = args[4];
boolean validArgs = true;
if(!programList.exists()){
System.out.println("Could not load program list.");
validArgs = false;
}
if(!commandList.exists()){
System.out.println("Could not load command list.");
validArgs = false;
}
try{
pageSize = Integer.parseInt(args[2]);
if(pageSize > 16 || (pageSize % 2 != 0 && pageSize != 1)){
System.out.println("The page size should be 1, 2, 4, 8, or 16.");
validArgs = false;
}
}catch(NumberFormatException ex){
System.out.println("Please enter a valid number for a page size.");
validArgs = false;
}
boolean foundAlg = false;
for(String algName: validAlgs){
if(algName.equals(alg)){
foundAlg = true;
break;
}
}
if(!foundAlg){
System.out.println("Please enter a valid algorithm. (lru, fifo, sclru)");
validArgs = false;
}
if(!paging.equals("d") && !paging.equals("p")){
System.out.println("Please enter a valid paging option. d or p");
validArgs = false;
}
//Now that we have checked all the parameters, either continue or stop.
if(!validArgs){
System.out.println("Parameter validation failed. Please check your calling syntax.");
return;
}
//Set the page size for processes.
Process.setPageSize(pageSize);
//Start reading and loading the program list.
try{
FileReader programReader = new FileReader(programList);
BufferedReader programBuffer = new BufferedReader(programReader);
String inputLine;
ArrayList<Process> programs = new ArrayList<>();
while((inputLine = programBuffer.readLine()) != null && inputLine.length() > 0){
try{
//Seperate the arguments.
String[] inputs = inputLine.split(" ");
programs.add(new Process(Integer.parseInt(inputs[0]), Integer.parseInt(inputs[1])));
}catch(NumberFormatException ex){
System.out.println("Error in programlist file.");
return;
}
}
//Finished loading program list data into memory.
//Start reading from command list and perform swaps.
FrameTable frameTable = new FrameTable(BUFFER_SPACE, pageSize,
alg, paging, programs);
FileReader commandReader = new FileReader(commandList);
BufferedReader commandBuffer = new BufferedReader(commandReader);
while((inputLine = commandBuffer.readLine()) != null && inputLine.length() > 0){
try{
//Seperate the arguments.
String[] inputs = inputLine.split(" ");
frameTable.loadMem(Integer.parseInt(inputs[0]), Integer.parseInt(inputs[1]));
}catch(NumberFormatException ex){
System.out.println("Error while reading commandlist file");
return;
}
}
System.out.println(frameTable.getSwapCount() + "");
}catch(IOException ex){
System.out.println("Error handling file: " + ex);
}
}
}
}