diff --git a/os-simulator/.DS_Store b/os-simulator/.DS_Store index 0893fd48d718765776876fd0a10d5b98e240e770..e8fd925ae42de4007916fb0481858fe61b3b91b8 100644 Binary files a/os-simulator/.DS_Store and b/os-simulator/.DS_Store differ diff --git a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOSystem.java b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOSystem.java index 3fc30331035b4ffceb9e886a7ca1e3111d7c421a..fe6af34a5758b57b0b7e35252cbd4389f8eb19a3 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOSystem.java +++ b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOSystem.java @@ -1,17 +1,16 @@ +/** + * IOSystem.java + */ package com.com1032.assignment.IOsystem; import java.io.IOException; - -/* Java IO system project - * This is a very high level abstraction for the IO operations, without scheduling, Interrupt Handling, DMA, Caching, spooling, locking, protection, streams, - * and many more functions can be added later on - * The project Define Buffers for the various peripherals using - * Java Memory Mapped files for faster IO operations - * Developed by Manal Helal for COM1032 Operating Systems Spring 2020 - Surrey University -*/ - import java.util.ArrayList; +/** + * Interface class containing necessary functions for interacting with IO devices in a system. + * + * @author felipedabrantes + */ public interface IOSystem { ArrayList<MemoryMappedFile> peripherals = new ArrayList<MemoryMappedFile>(); diff --git a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOType.java b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOType.java index 673cdc27a5625b16313764068401e950da2b19cc..7c0d7d65a5621ee79d5f73756ffc93fd7ec2fa3e 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOType.java +++ b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/IOType.java @@ -1,15 +1,18 @@ /** - * + * IOType.java */ package com.com1032.assignment.IOsystem; /** + * Enumeration class to state the type an IO device can be. + * * @author felipedabrantes - * */ public enum IOType { - + /**Device is an Input.*/ INPUT, + + /**Device is an Output.*/ OUTPUT } diff --git a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/JavaIOSystem.java b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/JavaIOSystem.java index 3ae883d49279f22a1e2a0a209e2aa108b2e89faa..3857b72ecdd2839916e55e3a1a4304e8c737041e 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/JavaIOSystem.java +++ b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/JavaIOSystem.java @@ -1,3 +1,6 @@ +/** + * JavaIOSystem.java + */ package com.com1032.assignment.IOsystem; import java.io.File; @@ -6,9 +9,13 @@ import java.util.ArrayList; import com.com1032.assignment.processscheduler.Hardware; - +/** + * A class to control all of the IO devices in the system. + * + * @author felipedabrantes + */ public class JavaIOSystem implements IOSystem{ - /**A list of peripherals stored as Memory Mapped Files.*/ + /**A list of peripherals stored as Memory Mapped File objects.*/ private ArrayList<MemoryMappedFile> peripherals = new ArrayList<MemoryMappedFile>(); /**Directory name of folder locations to create.*/ @@ -18,6 +25,7 @@ public class JavaIOSystem implements IOSystem{ * Constructor. Initialises fields. * * @param directoryName + * @param hardware * * @throws IOException if there is an error accessing hardware file. * @throws IndexOutOfBoundsException if there is an error with hardware file parameters. @@ -25,16 +33,21 @@ public class JavaIOSystem implements IOSystem{ public JavaIOSystem(String directoryName, Hardware hardware) throws IOException, IndexOutOfBoundsException { this.directoryName = directoryName; + try { + //Makes new directories. new File(directoryName + "/Peripherals/Buffers").mkdirs(); + //Clears directories of any files. for(File file: (new File(directoryName + "/Peripherals/Buffers")).listFiles()) { if(!file.isDirectory()) { file.delete(); } } + //Accesses peripherals info from hardware given. for(String peripheralInfo: hardware.getPeripheralsInfo()) { + //DeviceName, BufferFileName, IODeviceType String deviceName = peripheralInfo.split(" ")[0]; String bufferFileName = peripheralInfo.split(" ")[1]; IOType deviceType; @@ -46,11 +59,13 @@ public class JavaIOSystem implements IOSystem{ deviceType = IOType.OUTPUT; } + //Adds device to system. this.addDevice(deviceName, bufferFileName, deviceType); } } + //Errors that can happen with files. catch(IOException e) { throw new IOException("Error adding buffer folders."); } @@ -67,14 +82,17 @@ public class JavaIOSystem implements IOSystem{ * * @param deviceName to add. * @param BufferFileName + * @param deviceType, input or output. * * @throws IllegalArgumentException if device info is invalid. * @throws IOException if there is an error interacting with memory file. */ @Override public void addDevice(String deviceName, String bufferFileName, IOType deviceType) throws IllegalArgumentException, IOException { + //True when file name is already in system. boolean error = false; + //Loops to check device does not already exist. for(MemoryMappedFile peripheral: this.peripherals) { if(peripheral.getDeviceName().equals(deviceName)) { error = true; @@ -87,6 +105,7 @@ public class JavaIOSystem implements IOSystem{ } } + //If device name and file name is valid. if(error == false) { MemoryMappedFile newPeripheral = new MemoryMappedFile(this.directoryName + "/Peripherals/Buffers/", deviceName, bufferFileName, deviceType); this.peripherals.add(newPeripheral); @@ -103,9 +122,10 @@ public class JavaIOSystem implements IOSystem{ * @throws IOException if there is an error interacting with memory file. */ @Override - public void removeDevice(String deviceName) throws IOException { + public void removeDevice(String deviceName) throws IOException, IllegalArgumentException { boolean deviceFound = false; + //Loops to find device with given parameter. for (MemoryMappedFile peripheral: this.peripherals) { if (peripheral.getDeviceName().equals(deviceName)) { deviceFound = true; diff --git a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/MemoryMappedFile.java b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/MemoryMappedFile.java index b8e9caab21c76387d423a4319a319f4127d7cc54..e12501e30390e012a2313a9a96cb340992ed4bba 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/IOsystem/MemoryMappedFile.java +++ b/os-simulator/src/main/java/com/com1032/assignment/IOsystem/MemoryMappedFile.java @@ -1,3 +1,6 @@ +/** + * MemoryMappedFile.java + */ package com.com1032.assignment.IOsystem; import java.io.File; @@ -6,6 +9,12 @@ import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; + +/** + * Class that manages peripherals and interacts with memory mapped files. + * + * @author felipedabrantes + */ public class MemoryMappedFile { /**The peripheral name.*/ @@ -14,17 +23,20 @@ public class MemoryMappedFile { private String filename = null; /**The length of data in the file*/ private int dataLength = 0; + /**Whether peripheral is input or output.*/ private IOType type; - + /**The directory where the buffer folders are stored.*/ private final String BUFFERS_DIRECTORY; /** * Constructor. Initialises fields. * - * @param deviceName - * @param filename + * @param bufferDirectory where the buffer folders are stored. + * @param deviceName to add. + * @param filename of buffer file. + * @param type of IO device. * * @throws IOException if there is an error interacting with memory file. * @@ -37,12 +49,9 @@ public class MemoryMappedFile { else this.setDeviceName(""); - if (filename != null) - this.filename = filename; - else - this.filename = "largeFile.txt"; - + this.filename = filename; this.type = type; + this.create(); } diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/CPUSimulator.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/CPUSimulator.java index 0391ab3fe0325684a10557fbc1b248e47085548b..ab833c08a4dc8983a4f7734f9c33152f1dfceffb 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/CPUSimulator.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/CPUSimulator.java @@ -1,5 +1,5 @@ /** - * + * CPUSimulator.java */ package com.com1032.assignment.processscheduler; @@ -9,8 +9,9 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; /** + * Class used to simulate CPU. + * * @author felipedabrantes - * */ public class CPUSimulator extends Thread{ /**Processes running on CPU.*/ @@ -31,8 +32,10 @@ public class CPUSimulator extends Thread{ /** * Constructor. Initialises fields. * - * @param os + * @param errorReport + * @param globalTime * @param cpuQueue + * @param hardware */ public CPUSimulator(StringBuffer errorReport, Clock globalTime, List<PCB> cpuQueue, Hardware hardware) { this.cpuQueue = cpuQueue; @@ -42,10 +45,16 @@ public class CPUSimulator extends Thread{ } + /** + * Ran when thread is started. + */ @Override public void run() { + + //Set running to true. this.running.set(true); + //Runs until stopped externally. while(this.running.get()) { //Temporarily stop executing for however milliseconds. @@ -55,11 +64,15 @@ public class CPUSimulator extends Thread{ e.printStackTrace(); } - //Runs process in queue. + //Runs process in CPU queue. this.runCode(); } } + + /** + * Main function that compiles and executes code in CPU queue. + */ public void runCode() { if(!this.cpuQueue.isEmpty()) { @@ -110,10 +123,17 @@ public class CPUSimulator extends Thread{ } + /** + * Compiles the given line. + * + * @param processRunning + * @param line to compile. + */ public void compileLine(PCB processRunning, String line) { Map<String, Integer> variables = processRunning.getVariables(); + //Ignore empty line. if(line.equals("")) { return; } diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Clock.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Clock.java index 612abc64ebdb92bd0f15005a3266569e24a7cde2..0bae3f409f9a586748e77ecf91eb3c6a879ca821 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Clock.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Clock.java @@ -1,30 +1,26 @@ /** - * + * Clock.java */ package com.com1032.assignment.processscheduler; /** + * Class used to simulate system clock. + * * @author felipedabrantes - * */ public class Clock { - + /**Time of clock.*/ private int time = 0; - private int cycle = 0; - - @Override - public String toString() { - return String.valueOf(this.time); - } - public int getTime() { - return this.time; - } + /**Cycle increases when time exceeds max.*/ + private int cycle = 0; - public int getCycle() { - return this.cycle; - } + /** + * Increases time by parameter given and cycle if necessary. + * + * @param time to increase by. + */ public void increaseTime(int time) { this.time += time; @@ -34,9 +30,35 @@ public class Clock { } } + + /** + * Resets the time and cycles to zero. + */ public void resetTime() { this.time = 0; this.cycle = 0; } - + + + /** + * @return the time + */ + public int getTime() { + return this.time; + } + + /** + * @return the cycle + */ + public int getCycle() { + return this.cycle; + } + + /** + * Returns String when object is printed. + */ + @Override + public String toString() { + return String.valueOf(this.time); + } } diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Hardware.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Hardware.java index 6c488b69ea259263ac6bde766ea4087972d6e5db..9e7d3df019c7d4fe22ed2f53935dbf0c103b2e37 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Hardware.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/Hardware.java @@ -1,5 +1,5 @@ /** - * + * Hardware.java */ package com.com1032.assignment.processscheduler; @@ -10,16 +10,22 @@ import java.util.List; import java.util.Scanner; /** + * Class used to store the hardware of a system. + * * @author felipedabrantes - * */ public class Hardware { + /**Number of processors of the system. Max 1.*/ private int processorNum = 0; + /**Number of cores per processor. Max 4.*/ private int coresPerProcessor = 0; + /**RAM size, in GB.*/ private int RAMSize = 0; + /**Disk size, in GB.*/ private int diskSize = 0; + /**List of peripherals info.*/ private List<String> peripheralsInfo = new ArrayList<String> (); @@ -50,6 +56,7 @@ public class Hardware { public Hardware(String fileName) throws IOException, NumberFormatException { try { + //Reads file, should be in specific format. File file = new File(fileName); Scanner fileReader = new Scanner(file); @@ -65,9 +72,14 @@ public class Hardware { //Closes file. fileReader.close(); + //Processor cannot be more than 4. + if(this.processorNum > 1) { + throw new NumberFormatException("Processor num cannot be higher than 1."); + } + //Cores per processor cannot be more than 4. if(this.coresPerProcessor > 4) { - throw new NumberFormatException(); + throw new NumberFormatException("Cores per processors cannot be higher than 4."); } } @@ -109,7 +121,6 @@ public class Hardware { return this.diskSize; } - /** * @return the peripheralsInfo */ diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/IOProcessManager.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/IOProcessManager.java index 92551425a94896f16cf6008e0c1629e3f5a073e1..e2ebefc896ba7e9b7f91ec01e171ca7c89411e6f 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/IOProcessManager.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/IOProcessManager.java @@ -1,5 +1,5 @@ /** - * + * IOProcessManager.java */ package com.com1032.assignment.processscheduler; @@ -12,8 +12,9 @@ import java.util.concurrent.atomic.AtomicBoolean; import com.com1032.assignment.IOsystem.JavaIOSystem; /** + * Class used to manage and run blocked processes interacting with IO devices. + * * @author felipedabrantes - * */ public class IOProcessManager extends Thread { @@ -36,6 +37,7 @@ public class IOProcessManager extends Thread { * Constructor. Initialises fields. * * @param directoryName + * @param hardware * @param errorReport * @param IOReport * @param blockedQueue @@ -46,6 +48,7 @@ public class IOProcessManager extends Thread { */ public IOProcessManager(String directoryName, Hardware hardware, StringBuffer errorReport, StringBuffer IOReport, List<PCB> blockedQueue) throws IndexOutOfBoundsException, IOException { + this.errorReport = errorReport; this.blockedQueue = blockedQueue; this.IOReport = IOReport; @@ -54,11 +57,19 @@ public class IOProcessManager extends Thread { } + /** + * Ran when thread is started. + */ @Override public void run() { + + //Set running to true. this.running.set(true); + //Runs until stopped externally. while(this.running.get()) { + + //Temporarily stop executing for however milliseconds. try { Thread.sleep(50); } catch (InterruptedException e) { @@ -90,6 +101,7 @@ public class IOProcessManager extends Thread { this.compileLine(processRunning, codeLines[processRunning.getProgramCounter()]); } + //Ran when there is a syntax error in code. catch (IndexOutOfBoundsException e) { StringBuffer errorMessage = new StringBuffer(); errorMessage.append("\n - Error in Line "); @@ -104,6 +116,7 @@ public class IOProcessManager extends Thread { this.errorReport.append(errorMessage); } + //Ran when input buffer is invalid. catch (NumberFormatException e) { StringBuffer errorMessage = new StringBuffer(); errorMessage.append("\n - Error in Line "); diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OS.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OS.java index 60f968622a9074940a13be4938cfdbe38c63892f..8f00c6c140b475711a0a1fb8d9bbf9eeed15875b 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OS.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OS.java @@ -1,14 +1,15 @@ -package com.com1032.assignment.processscheduler; /** * OS.java */ +package com.com1032.assignment.processscheduler; import java.io.IOException; import java.util.concurrent.TimeUnit; /** + * Class used to simulate OS. Controls the process scheduler. + * * @author felipedabrantes - * */ public class OS { @@ -16,14 +17,20 @@ public class OS { private Clock globalTime = new Clock(); /**The hardware of the OS.*/ private Hardware hardware = null; + /**The class the handles the process scheduling.*/ private ProcessScheduler ps = null; + /**Directory name of folder locations to create.*/ private final String directoryName; /** * Constructor. Initialises fields. + * + * @param hardwareFileName + * @param directoryName + * * @throws IOException * @throws NumberFormatException */ @@ -35,34 +42,50 @@ public class OS { /** * Runs process scheduling simulation with processes in OS. + * + * @param algorithm wanted. + * @param quantum to use. + * + * @throws IOException if there is an error accessing hardware file. + * @throws IndexOutOfBoundsException if there is an error with hardware file parameters. */ - public void turnOn(SchedulingAlgorithm algorithm, int quantum) { - this.ps = new ProcessScheduler(this, this.directoryName); + public void turnOn(SchedulingAlgorithm algorithm, int quantum) throws IndexOutOfBoundsException, IOException { + this.ps = new ProcessScheduler(this, this.directoryName); - try { - this.ps.startThreads(algorithm, quantum); - } - catch(Exception e) { - e.printStackTrace(); - } + //Starts running threads. + this.ps.startThreads(algorithm, quantum); } /** - * Stop running threads. - * @throws InterruptedException + * Stops running threads. + * + * @throws InterruptedException if timer is interrupted while sleeping. */ public void turnOff() throws InterruptedException { + //Stops threads. ps.stopThreads(); + //Waits for threads to complete. TimeUnit.MILLISECONDS.sleep(2000); + //Resets process scheduler and global time. this.ps = null; this.globalTime.resetTime(); } - public void resetOS(SchedulingAlgorithm algorithm, int quantum) throws InterruptedException { + /** + * Turns OS off and on again. + * + * @param algorithm wanted. + * @param quantum to use. + * + * @throws InterruptedException if timer is interrupted while sleeping. + * @throws IOException if there is an error accessing hardware file. + * @throws IndexOutOfBoundsException if there is an error with hardware file parameters. + */ + public void resetOS(SchedulingAlgorithm algorithm, int quantum) throws InterruptedException, IndexOutOfBoundsException, IOException { this.turnOff(); this.turnOn(algorithm, quantum); } @@ -75,7 +98,6 @@ public class OS { return this.globalTime; } - /** * @return the hardware */ @@ -83,7 +105,6 @@ public class OS { return this.hardware; } - /** * @return the ps */ @@ -91,6 +112,9 @@ public class OS { return this.ps; } + /** + * @return the directoryName + */ public String getDirectoryName() { return this.directoryName; } diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OSCommandLine.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OSCommandLine.java index 19dafefe5614a1bcc575e34bf46ee02dccd828e9..eeb23e463feaf43d93c0aa223de3300bf05e85a2 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OSCommandLine.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/OSCommandLine.java @@ -1,5 +1,5 @@ /** - * + * OSCommandLine.java */ package com.com1032.assignment.processscheduler; @@ -12,27 +12,39 @@ import java.util.StringTokenizer; import com.com1032.assignment.tests.AllTests; /** + * Class to let user control OS operations from command line. + * * @author felipedabrantes - * */ public class OSCommandLine { /** - * @param args[0] hardware file - * @param args[1] location to store folders + * Main file used when application is launched. + * + * @param args[0] hardware file. + * @param args[1] location to store folders. */ public static void main(String[] args) { OS os = null; + //Try to create OS object from given hardware file. try { os = new OS(args[0], args[1]); System.out.println("Located hardware file!"); } catch(Exception e){ - System.out.println(e); + System.out.println(e.getMessage()); + System.exit(0); + } + + //Tries to turn on threads, error if set up goes wrong. + try { + os.turnOn(SchedulingAlgorithm.FIFO, 5); + } + catch(Exception e) { + System.out.println(e.getMessage()); System.exit(0); } - os.turnOn(SchedulingAlgorithm.FIFO, 5); // Create a stream for input BufferedReader data = null; @@ -47,21 +59,30 @@ public class OSCommandLine { System.out.print("--> "); System.out.flush(); - // Read in a line + //Read a line. String line = data.readLine(); - // Check for various conditions - if (line == null) System.exit(0); // Ctrl-D check - line = line.trim(); // Trim off extra whitespace - if (line.length() == 0) { // Is anything left? - System.out.println(); - continue; + //Ctrl-D check. + if (line == null) { + System.exit(0); + } + + //Trim off extra whitespace. + line = line.trim(); + + //Ignore if command is empty. + if (line.length() == 0) { + System.out.println(); + continue; } - // Tokenize command line + //Tokenize command line. StringTokenizer cmds = new StringTokenizer (line); + + //Access command. String cmd = cmds.nextToken(); + //Reports. if (cmd.equalsIgnoreCase("processReport") || cmd.equalsIgnoreCase("pR")) { System.out.println(os.getPs().getProcessReport()); continue; @@ -77,6 +98,7 @@ public class OSCommandLine { continue; } + //Algorithm setting. else if (cmd.equalsIgnoreCase("algorithm")) { String algorithm = cmds.nextToken(); String quantumString = cmds.nextToken(); @@ -103,30 +125,33 @@ public class OSCommandLine { } + //Test function. else if (cmd.equalsIgnoreCase("test")) { System.out.println("Running Tests, OS will reset after completion..."); os.turnOff(); - AllTests all = new AllTests(); try { - all.runAllTests(os); + AllTests.runAllTests(os); System.out.println("Completed Tests Successfully!"); System.out.println("\nResetting OS..."); os.turnOn(SchedulingAlgorithm.FIFO, 5); } catch (Exception e) { System.out.println("Tests failed."); + System.out.println(e.getMessage()); // e.printStackTrace(); System.exit(0); } } + //Reset function. else if (cmd.equalsIgnoreCase("reset")) { System.out.println("Reseting..."); os.resetOS(SchedulingAlgorithm.FIFO, 5); System.out.println("Complete!"); } + //Quit function. else if (cmd.equalsIgnoreCase("quit")) { System.out.println("Quitting..."); os.turnOff(); @@ -134,34 +159,37 @@ public class OSCommandLine { System.exit(0); } + //Help function. else if (cmd.equalsIgnoreCase("help")) { OSCommandLine.help(); continue; } + //Ran if command is not understood. else { System.out.println("Unknown command."); continue; } } - // Handler for Integer.parseInt(...) + //Handler for Integer.parseInt(...). catch (NumberFormatException e) { System.out.println("Incorrect argument type."); } - // Handler for nextToken() + //Handler for nextToken(). catch (NoSuchElementException e) { System.out.println("Incorrect number of elements."); } - // Handler for IOType + //Handler for IOType. catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } + //Handler for error with file operations. catch (IOException e) { - System.out.println(e); + System.out.println(e.getMessage()); } catch (InterruptedException e) { - System.out.println(e); + System.out.println(e.getMessage()); } } @@ -176,6 +204,7 @@ public class OSCommandLine { System.out.println (" - errorReport"); System.out.println (" - ioReport"); System.out.println (" - algorithm"); + System.out.println (" - test"); System.out.println (" - reset"); System.out.println (" - quit"); System.out.println (" - help"); diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/PCB.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/PCB.java index f6e2a56ba3b16cd409d7caf3d27e223bec3f2799..2cd5de402fe4963f749341ecd5a13a969a20c64a 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/PCB.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/PCB.java @@ -1,5 +1,5 @@ /** - * + * PCB.java */ package com.com1032.assignment.processscheduler; @@ -7,8 +7,9 @@ import java.util.HashMap; import java.util.Map; /** + * Class used to store processes' info. + * * @author felipedabrantes - * */ public class PCB { /**The unique ID to identify the process.*/ @@ -62,55 +63,93 @@ public class PCB { } + /** + * Estimates burst time based on number of code lines. + * + * @return estimated burst time. + */ public int estimateBurstTime() { int codeLines = this.code.split("\r\n|\r|\n").length; return codeLines; } - + /** + * Increases program counter by 1. + */ public void increaseProgramCounter() { this.programCounter++; } + /** + * Decreases program counter by 1. + */ public void decreaseProgramCounter() { this.programCounter--; } + /** + * Decreases remaining time by given parameter. + * + * @param time to decrease by. + */ public void decreaseRemainingTime(int time) { this.remainingTime -= time; } + /** + * Increase consecutive time on CPU by 1. + */ public void increaseConsecutiveTimeOnCPU() { this.consecutiveTimeOnCPU++; } + /** + * Reset consecutive time on CPU to 0. + */ public void resetConsecutiveTimeOnCPU() { this.consecutiveTimeOnCPU = 0; } + + /** + * Sets id. + * * @param id */ public void setId(int id) { this.id = id; } + /** + * Sets state. + * + * @param state + */ public void setState(ProcessState state) { this.state = state; } + /** + * Sets failed. + * + * @param failed + */ public void setFailed(boolean failed) { this.failed = failed; } /** + * Sets completion time. + * * @param completionTime */ public void setCompletionTime(int completionTime) { this.completionTime = completionTime; } - /** + * Sets remaining time. + * * @param remainingTime */ public void setRemainingTime(int remainingTime) { @@ -118,6 +157,8 @@ public class PCB { } /** + * Sets arrival time. + * * @param arrivalTime */ public void setArrivalTime(int arrivalTime) { @@ -215,9 +256,10 @@ public class PCB { return this.consecutiveTimeOnCPU; } - - - + + /** + * Returns String when object is printed. + */ public String toString() { StringBuffer info = new StringBuffer(); diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessCreation.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessCreation.java index c2ef36eb11355639479889e5c71ddf3b37ec4aba..b3936009f69af5f77880d0c522cbe9f9a32e1f93 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessCreation.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessCreation.java @@ -1,5 +1,5 @@ /** - * + * ProcessCreation.java */ package com.com1032.assignment.processscheduler; @@ -15,8 +15,9 @@ import java.util.Scanner; import java.util.concurrent.atomic.AtomicBoolean; /** + * Class used to control thread used when creating processes. + * * @author felipedabrantes - * */ public class ProcessCreation extends Thread { @@ -41,6 +42,9 @@ public class ProcessCreation extends Thread { /** * Constructor. Initialises fields. * + * @param directoryName + * @param errorReport + * @param globalTime * @param jobQueue of the system. */ public ProcessCreation(String directoryName, StringBuffer errorReport, Clock globalTime, List<PCB> jobQueue) { @@ -50,18 +54,23 @@ public class ProcessCreation extends Thread { this.directoryName = directoryName; + //Creates folders for user to input processes. this.createFolders(); + //Clears folders. this.clearFolders(); } /** - * Function ran when thread starts. + * Ran when thread is started. */ @Override public void run() { + + //Set running to true. this.running.set(true); + //Runs until stopped externally. while(this.running.get()) { //Fetches files where new processes are added. this.fetchFiles(); @@ -103,7 +112,6 @@ public class ProcessCreation extends Thread { /** * Creates PCBs from the files storing the process info. - * */ public void createProcessFromFiles() { //Tries to open file. @@ -162,7 +170,6 @@ public class ProcessCreation extends Thread { * Moves file to different depending if file is valid or invalid. * * @param file to move. - * * @param valid, true if file is valid. */ public void fileHandling(File file, boolean valid) { @@ -212,9 +219,10 @@ public class ProcessCreation extends Thread { /** - * Makes folders. + * Creates folders. */ public void createFolders() { + //Makes parent folder as well. new File(this.directoryName + "/Processes/New_Processes").mkdirs(); new File(this.directoryName + "/Processes/Valid_Processes").mkdirs(); new File(this.directoryName + "/Processes/Invalid_Processes").mkdirs(); diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessDispatcher.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessDispatcher.java index a26a3709413599eead25d0a9224523c8b46ff88d..01224e49aea93ca0342135879a7864c11d9f4040 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessDispatcher.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessDispatcher.java @@ -1,5 +1,5 @@ /** - * + * ProcessDispatcher.java */ package com.com1032.assignment.processscheduler; @@ -8,8 +8,9 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; /** + * Class used to control the thread that schedules processes. + * * @author felipedabrantes - */ public class ProcessDispatcher extends Thread { @@ -44,11 +45,17 @@ public class ProcessDispatcher extends Thread { /** * Constructor. Initialises fields * + * @param globalTime + * @param hardware + * @param processReport + * @param algorithm + * @param quantum + * @param jobQueue * @param readyQueue * @param blockedQueue * @param terminatedQueue * @param cpuQueue - * @param globalTime + * */ public ProcessDispatcher(Clock globalTime, Hardware hardware, StringBuffer processReport, SchedulingAlgorithm algorithm, int quantum, @@ -72,14 +79,18 @@ public class ProcessDispatcher extends Thread { /** - * Main function to run processes with the desired algorithm. - * + * Ran when thread is started. */ @Override public void run() { + + //Set running to true. this.running.set(true); + //Runs until stopped externally. while(this.running.get()) { + + //Temporarily stop executing for however milliseconds. try { Thread.sleep(0); } catch (InterruptedException e) { diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessScheduler.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessScheduler.java index 4da551efba0db1bf694ee0d0024c6332fb23f63e..28f1aca2e77eb4d560796be3527727aa9ab25eba 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessScheduler.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessScheduler.java @@ -1,5 +1,5 @@ /** - * + * ProcessScheduler.java */ package com.com1032.assignment.processscheduler; @@ -9,8 +9,9 @@ import java.util.List; import com.com1032.assignment.IOsystem.*; /** + * Class used to control all of the threads that control the system's process scheduling. + * * @author felipedabrantes - * */ public class ProcessScheduler { @@ -47,6 +48,7 @@ public class ProcessScheduler { /**The directory to make all folders in.*/ private String directoryName = null; + //Classes that control the separate threads. private ProcessCreation pc = null; private ProcessDispatcher pd = null; private CPUSimulator cpu = null; @@ -94,11 +96,13 @@ public class ProcessScheduler { public void stopThreads() { + //Sets running to false which externally stops threads. this.pc.running.set(false); this.pd.running.set(false); this.cpu.running.set(false); this.io.running.set(false); + //Sets all classes controlling threads to null. this.pc = null; this.pd = null; this.cpu = null; @@ -106,22 +110,40 @@ public class ProcessScheduler { } + /** + * Sets the wanted algorithm. + * + * @param algorithm + * @param quantum + */ public void setAlgorithm(SchedulingAlgorithm algorithm, int quantum) { this.pd.setAlgorithm(algorithm, quantum); } + /** + * @return the processReport + */ public String getProcessReport() { return this.processReport.toString(); } + /** + * @return the errorReport + */ public String getErrorReport() { return this.errorReport.toString(); } + /** + * @return the IOReport + */ public String getIOReport() { return this.IOReport.toString(); } + /** + * @return the JavaIOSystem + */ public JavaIOSystem getJavaIOSystem() { return this.io.getIOSystem(); } diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessState.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessState.java index afc23c069ca91bc96fd11e8b6913067a1c099816..620f3b05116eaee5853b33bb0ec6f994674ab9d2 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessState.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/ProcessState.java @@ -1,5 +1,5 @@ /** - * State.java + * ProcessState.java */ package com.com1032.assignment.processscheduler; diff --git a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/SchedulingAlgorithm.java b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/SchedulingAlgorithm.java index 8d17399d6fb5bf678151aaa4cf69b0a830bfdbc1..cd3a8297a171470aac496e64bab27091cec61ce8 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/processscheduler/SchedulingAlgorithm.java +++ b/os-simulator/src/main/java/com/com1032/assignment/processscheduler/SchedulingAlgorithm.java @@ -4,7 +4,7 @@ package com.com1032.assignment.processscheduler; /** - * Enumeration of scheduling algorithms. + * Enumeration of the scheduling algorithms. * * Preemptive: Might temporarily interrupt the process scheduled to resume it later. * diff --git a/os-simulator/src/main/java/com/com1032/assignment/tests/AllTests.java b/os-simulator/src/main/java/com/com1032/assignment/tests/AllTests.java index d0c61eb5c1e78d1c9238683b81356b788fa5ad44..d14e0fffbe31b97b630cff56964d20833dff10fa 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/tests/AllTests.java +++ b/os-simulator/src/main/java/com/com1032/assignment/tests/AllTests.java @@ -1,26 +1,152 @@ /** - * + * AllTests.java */ package com.com1032.assignment.tests; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import com.com1032.assignment.processscheduler.OS; /** + * Class with static functions to run all available tests for OS. + * * @author felipedabrantes - * */ public class AllTests { - public void runAllTests(OS os) throws IOException, InterruptedException { - IOSystemTest ioTest = new IOSystemTest(os); - PSAlgorithmsTest PSAlgorithmsTest = new PSAlgorithmsTest(os); - PSBlockedTest PSBlockedTest = new PSBlockedTest(os); + /** + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void runAllTests(OS os) throws IOException, InterruptedException, ComparisonFailedException { + + //IO devices tests. + IOSystemTest.OutputDeviceTest(os); + IOSystemTest.InputDeviceTest(os); + + //Algorithms tests. + PSAlgorithmsTest.FIFOProcessTest(os); + PSAlgorithmsTest.SPFProcessTest(os); + PSAlgorithmsTest.SRTFProcessTest(os); + PSAlgorithmsTest.RRProcessTest(os); + + //Blocked Processes tests. + PSBlockedTest.FIFOBlockedProcessTest(os); + PSBlockedTest.RRBlockedProcessTest(os); + PSBlockedTest.ErrorBlockedProcessTest(os); + PSBlockedTest.ErrorReportBlockedProcessTest(os); - ioTest.allTests(); - PSAlgorithmsTest.allTests(); - PSBlockedTest.allTests(); } + + + /** + * Copies the required test process from '_Tests' folder to 'New_Processes'. + * + * @param os + * @param fileName of process to move. + * + * @throws IOException + */ + static public void copyProcessFile(OS os, String fileName) throws IOException { + try { + Path testSource = Paths.get(os.getDirectoryName() + "/Processes/_Tests/" + fileName); + Path target = Paths.get(os.getDirectoryName() + "/Processes/New_Processes/" + fileName); + Files.copy(testSource, target, StandardCopyOption.REPLACE_EXISTING); + } + catch(IOException e) { + throw new IOException("Error accessing '_Tests' folder. Make sure it is in 'Processes' folder."); + } + } + + + /** + * Generates a simpler process report from the report given. + * + * @param report + * + * @return simplified report. + */ + static public String simplifyProcessReport(String report) { + StringBuffer simpleReport = new StringBuffer(); + + String action = null; + String name = null; + String state = null; + boolean failed = false; + + String[] split = report.split("\n"); + + for(String line: split) { + if(!line.equals("")) { + + action = line.split(" ")[1]; + name = line.split(" ")[8]; + + if(line.split(" ")[10].equals("FAILED")) { + failed = true; + state = line.split(" ")[13]; + } + else { + state = line.split(" ")[11]; + } + + simpleReport.append(action + " "); + simpleReport.append(name + " "); + simpleReport.append(state + " "); + simpleReport.append(failed); + simpleReport.append("\n"); + + failed = false; + } + } + + return simpleReport.toString(); + + } + + + /** + * Generates a simpler error report from the report given. + * + * @param report + * + * @return simplified error report. + */ + static public String simplifyErrorReport(String report) { + StringBuffer simpleReport = new StringBuffer(); + + String lineNum = null; + String name = null; + String state = null; + + String[] split = report.split("\n"); + + for(String line: split) { + + if(!line.equals("")) { + lineNum = line.split(" ")[5]; + name = line.split(" ")[10]; + state = line.split(" ")[13]; + + simpleReport.append(lineNum + " "); + simpleReport.append(name + " "); + simpleReport.append(state); + + break; + } + } + + return simpleReport.toString(); + + } + } diff --git a/os-simulator/src/main/java/com/com1032/assignment/tests/ComparisonFailedException.java b/os-simulator/src/main/java/com/com1032/assignment/tests/ComparisonFailedException.java new file mode 100644 index 0000000000000000000000000000000000000000..5236708dea6d22a813d213d7e0462a12bf1824eb --- /dev/null +++ b/os-simulator/src/main/java/com/com1032/assignment/tests/ComparisonFailedException.java @@ -0,0 +1,24 @@ +/** + * + */ +package com.com1032.assignment.tests; + +/** + * @author felipedabrantes + * + */ +public class ComparisonFailedException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * @param message + */ + public ComparisonFailedException(String message) { + super(message); + } + +} diff --git a/os-simulator/src/main/java/com/com1032/assignment/tests/IOSystemTest.java b/os-simulator/src/main/java/com/com1032/assignment/tests/IOSystemTest.java index 83a0f9f4969caf53363f6771e5003a2765d5db6f..19e0cc916e9a3337fa73399bf237f8b513f70fee 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/tests/IOSystemTest.java +++ b/os-simulator/src/main/java/com/com1032/assignment/tests/IOSystemTest.java @@ -1,5 +1,5 @@ /** - * + * IOSystemTest.java */ package com.com1032.assignment.tests; @@ -7,10 +7,6 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import java.util.Scanner; import java.util.concurrent.TimeUnit; @@ -18,37 +14,30 @@ import com.com1032.assignment.processscheduler.OS; import com.com1032.assignment.processscheduler.SchedulingAlgorithm; /** + * Test class with static functions to test IO devices of the OS. + * * @author felipedabrantes - * */ public class IOSystemTest { - - private OS os; - - public IOSystemTest(OS os) { - this.os = os; - } - public void allTests() throws IOException, InterruptedException { - try { - this.OutputDeviceTest(); - this.InputDeviceTest(); - } - catch(AssertionError e) { - e.printStackTrace(); - } - } - - - public void OutputDeviceTest() throws IOException, InterruptedException { + /** + * Tests the output device buffer files. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void OutputDeviceTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); os.getPs().setAlgorithm(SchedulingAlgorithm.FIFO, 5); //Copy files from test. //File contains a print statement to device printer. - this.copyProcessFile("blocked4_1.txt"); + AllTests.copyProcessFile(os, "blocked4_1.txt"); //Wait for processes to execute. TimeUnit.MILLISECONDS.sleep(2000); @@ -60,14 +49,23 @@ public class IOSystemTest { fileReader.close(); if(!actualResult.equals(expectedResult)) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - public void InputDeviceTest() throws IOException, InterruptedException { + /** + * Tests the input device buffer files. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void InputDeviceTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); @@ -79,7 +77,7 @@ public class IOSystemTest { //Copy files from test. //File contains a print statement to device printer. - this.copyProcessFile("blocked2_1.txt"); + AllTests.copyProcessFile(os, "blocked2_1.txt"); //Wait for processes to execute. TimeUnit.MILLISECONDS.sleep(2000); @@ -91,23 +89,9 @@ public class IOSystemTest { fileReader.close(); if(!actualResult.equals(expectedResult)) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - - - - public void copyProcessFile(String fileName) throws IOException { - try { - Path testSource = Paths.get(os.getDirectoryName() + "/Processes/_Tests/" + fileName); - Path target = Paths.get(os.getDirectoryName() + "/Processes/New_Processes/" + fileName); - Files.copy(testSource, target, StandardCopyOption.REPLACE_EXISTING); - } - catch(IOException e) { - throw new IOException("Error with _Tests folder. Make sure it is added to Processes folder."); - } - - } } diff --git a/os-simulator/src/main/java/com/com1032/assignment/tests/PSAlgorithmsTest.java b/os-simulator/src/main/java/com/com1032/assignment/tests/PSAlgorithmsTest.java index efdd52d9a2104935dca8e8259a1702ac244f2f54..2a76ebd2f0ff8067261075927f75d4a7adaffbf3 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/tests/PSAlgorithmsTest.java +++ b/os-simulator/src/main/java/com/com1032/assignment/tests/PSAlgorithmsTest.java @@ -1,62 +1,45 @@ /** - * + * PSAlgorithmsTest.java */ package com.com1032.assignment.tests; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import java.util.concurrent.TimeUnit; import com.com1032.assignment.processscheduler.OS; import com.com1032.assignment.processscheduler.SchedulingAlgorithm; /** + * Test class with static functions to test the scheduling algorithms of the OS. + * * @author felipedabrantes - * */ public class PSAlgorithmsTest { - private OS os = null; /** + * Tests the FIFO algorithm. + * + * @param os * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. */ - public PSAlgorithmsTest(OS os) { - this.os = os; - } - - - public void allTests() throws IOException, InterruptedException { - try { - this.FIFOProcessTest(); - this.SPFProcessTest(); - this.SRTFProcessTest(); - this.RRProcessTest(); - } - - catch(AssertionError e) { - e.printStackTrace(); - } - - } - - public void FIFOProcessTest() throws IOException, InterruptedException { + static public void FIFOProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); //Copy files from test. - this.copyProcessFile("terminate14.txt"); + AllTests.copyProcessFile(os, "terminate14.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate70.txt"); + AllTests.copyProcessFile(os, "terminate70.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate4.txt"); + AllTests.copyProcessFile(os, "terminate4.txt"); TimeUnit.MILLISECONDS.sleep(6000); String actualReport = os.getPs().getProcessReport(); - String simpleReport = this.simplifyReport(actualReport); + String simpleReport = AllTests.simplifyProcessReport(actualReport); StringBuffer expectedReport = new StringBuffer(); expectedReport.append("Ready terminate14 NEW false\n"); @@ -70,31 +53,40 @@ public class PSAlgorithmsTest { expectedReport.append("Terminated terminate4 TERMINATED false\n"); if(!simpleReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - public void SPFProcessTest() throws IOException, InterruptedException { + /** + * Tests the SPF algorithm. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void SPFProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with SPF algorithm. os.turnOn(SchedulingAlgorithm.SPF, 5); //Copy files from test first. - this.copyProcessFile("terminate70.txt"); + AllTests.copyProcessFile(os, "terminate70.txt"); TimeUnit.MILLISECONDS.sleep(500); - this.copyProcessFile("terminate14.txt"); + AllTests.copyProcessFile(os, "terminate14.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate4.txt"); + AllTests.copyProcessFile(os, "terminate4.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate28.txt"); + AllTests.copyProcessFile(os, "terminate28.txt"); TimeUnit.MILLISECONDS.sleep(7000); String actualReport = os.getPs().getProcessReport(); - String simpleReport = this.simplifyReport(actualReport); + String simpleReport = AllTests.simplifyProcessReport(actualReport); StringBuffer expectedReport = new StringBuffer(); expectedReport.append("Ready terminate70 NEW false\n"); @@ -111,28 +103,38 @@ public class PSAlgorithmsTest { expectedReport.append("Terminated terminate28 TERMINATED false\n"); if(!simpleReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - public void SRTFProcessTest() throws IOException, InterruptedException { + + /** + * Tests the SRTF algorithm. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void SRTFProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with SPF algorithm. os.turnOn(SchedulingAlgorithm.SRTF, 5); //Add files. - this.copyProcessFile("terminate14.txt"); + AllTests.copyProcessFile(os, "terminate14.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate4.txt"); + AllTests.copyProcessFile(os, "terminate4.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate28.txt"); + AllTests.copyProcessFile(os, "terminate28.txt"); TimeUnit.MILLISECONDS.sleep(3000); String actualReport = os.getPs().getProcessReport(); - String simpleReport = this.simplifyReport(actualReport); + String simpleReport = AllTests.simplifyProcessReport(actualReport); os.turnOff(); @@ -150,26 +152,36 @@ public class PSAlgorithmsTest { expectedReport.append("Terminated terminate28 TERMINATED false\n"); if(!simpleReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } } - public void RRProcessTest() throws IOException, InterruptedException { + + /** + * Tests the RR algorithm. Quantum 5. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void RRProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with SPF algorithm. os.turnOn(SchedulingAlgorithm.RR, 5); //Add files. - this.copyProcessFile("terminate14.txt"); + AllTests.copyProcessFile(os, "terminate14.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate4.txt"); + AllTests.copyProcessFile(os, "terminate4.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate28.txt"); + AllTests.copyProcessFile(os, "terminate28.txt"); TimeUnit.MILLISECONDS.sleep(3000); String actualReport = os.getPs().getProcessReport(); - String simpleReport = this.simplifyReport(actualReport); + String simpleReport = AllTests.simplifyProcessReport(actualReport); StringBuffer expectedReport = new StringBuffer(); expectedReport.append("Ready terminate14 NEW false\n"); @@ -191,56 +203,10 @@ public class PSAlgorithmsTest { expectedReport.append("Terminated terminate28 TERMINATED false\n"); if(!simpleReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - - - public void copyProcessFile(String fileName) throws IOException { - Path testSource = Paths.get(os.getDirectoryName() + "/Processes/_Tests/" + fileName); - Path target = Paths.get(os.getDirectoryName() + "/Processes/New_Processes/" + fileName); - Files.copy(testSource, target, StandardCopyOption.REPLACE_EXISTING); - } - - - public String simplifyReport(String report) { - StringBuffer simpleReport = new StringBuffer(); - - String action = null; - String name = null; - String state = null; - boolean failed = false; - - String[] split = report.split("\n"); - - for(String line: split) { - if(!line.equals("")) { - - action = line.split(" ")[1]; - name = line.split(" ")[8]; - - if(line.split(" ")[10].equals("FAILED")) { - failed = true; - state = line.split(" ")[13]; - } - else { - state = line.split(" ")[11]; - } - - simpleReport.append(action + " "); - simpleReport.append(name + " "); - simpleReport.append(state + " "); - simpleReport.append(failed); - simpleReport.append("\n"); - - failed = false; - } - } - - return simpleReport.toString(); - - } } diff --git a/os-simulator/src/main/java/com/com1032/assignment/tests/PSBlockedTest.java b/os-simulator/src/main/java/com/com1032/assignment/tests/PSBlockedTest.java index 6feb1fe29f132097c90e1fe70f8ceee26b25b131..8432705b13d886dfe1a90871b4af8811c4b4f13f 100644 --- a/os-simulator/src/main/java/com/com1032/assignment/tests/PSBlockedTest.java +++ b/os-simulator/src/main/java/com/com1032/assignment/tests/PSBlockedTest.java @@ -1,65 +1,47 @@ /** - * + * PSBlockedTest.java */ package com.com1032.assignment.tests; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import java.util.concurrent.TimeUnit; import com.com1032.assignment.processscheduler.OS; import com.com1032.assignment.processscheduler.SchedulingAlgorithm; /** + * Test class with static functions to test blocked processes in the OS. + * * @author felipedabrantes - * */ public class PSBlockedTest { - - private OS os = null; /** + * Test used to test blocked processes with FIFO algorithm. + * + * @param os * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. */ - public PSBlockedTest(OS os) { - this.os = os; - } - - - public void allTests() throws IOException, InterruptedException { - try { - this.FIFOBlockedProcessTest(); - this.RRBlockedProcessTest(); - this.ErrorBlockedProcessTest(); - this.ErrorReportBlockedProcessTest(); - } - - catch(AssertionError e) { - e.printStackTrace(); - } - } - - - public void FIFOBlockedProcessTest() throws IOException, InterruptedException { + static public void FIFOBlockedProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); //Copy files from test. - this.copyProcessFile("blocked4_1.txt"); + AllTests.copyProcessFile(os, "blocked4_1.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("blocked6_2.txt"); + AllTests.copyProcessFile(os, "blocked6_2.txt"); TimeUnit.MILLISECONDS.sleep(200); - this.copyProcessFile("blocked18_3.txt"); + AllTests.copyProcessFile(os, "blocked18_3.txt"); //Wait for processes to execute. TimeUnit.MILLISECONDS.sleep(5000); String actualReport = os.getPs().getProcessReport(); - String simpleReport = this.simplifyReport(actualReport); + String simpleReport = AllTests.simplifyProcessReport(actualReport); StringBuffer expectedReport = new StringBuffer(); expectedReport.append("Ready blocked4_1 NEW false\n"); @@ -82,30 +64,40 @@ public class PSBlockedTest { expectedReport.append("Terminated blocked18_3 TERMINATED false\n"); if(!simpleReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - public void RRBlockedProcessTest() throws IOException, InterruptedException { + + /** + * Test used to test blocked processes with FIFO algorithm. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void RRBlockedProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); //Copy files from test. - this.copyProcessFile("blocked18_3.txt"); + AllTests.copyProcessFile(os, "blocked18_3.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("blocked4_1.txt"); + AllTests.copyProcessFile(os, "blocked4_1.txt"); TimeUnit.MILLISECONDS.sleep(100); - this.copyProcessFile("terminate14.txt"); + AllTests.copyProcessFile(os, "terminate14.txt"); //Wait for processes to execute. TimeUnit.MILLISECONDS.sleep(5000); String actualReport = os.getPs().getProcessReport(); - String simpleReport = this.simplifyReport(actualReport); + String simpleReport = AllTests.simplifyProcessReport(actualReport); StringBuffer expectedReport = new StringBuffer(); @@ -128,26 +120,35 @@ public class PSBlockedTest { expectedReport.append("Terminated blocked18_3 TERMINATED false\n"); if(!simpleReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same."); } os.turnOff(); } - public void ErrorBlockedProcessTest() throws IOException, InterruptedException { + /** + * Test used to test the invalid blocked processes, ones with errors. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void ErrorBlockedProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); //Copy files from test. //Code has a device not in hardware file. - this.copyProcessFile("blockedInvalid6_1.txt"); + AllTests.copyProcessFile(os, "blockedInvalid6_1.txt"); //Wait for processes to execute. TimeUnit.MILLISECONDS.sleep(1000); String actualProcessReport = os.getPs().getProcessReport(); - String simpleProcessReport = this.simplifyReport(actualProcessReport); + String simpleProcessReport = AllTests.simplifyProcessReport(actualProcessReport); StringBuffer expectedReport = new StringBuffer(); @@ -157,111 +158,44 @@ public class PSBlockedTest { expectedReport.append("Terminated blockedInvalid6_1 TERMINATED true\n"); if(!simpleProcessReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - public void ErrorReportBlockedProcessTest() throws IOException, InterruptedException { + /** + * Test used to test the error report with blocked processes. + * + * @param os + * + * @throws IOException if there is an error accessing system files. + * @throws InterruptedException if there is an error with hardware file parameters. + * @throws ComparisonFailedException if comparison fails. + */ + static public void ErrorReportBlockedProcessTest(OS os) throws IOException, InterruptedException, ComparisonFailedException { //Start OS with FIFO algorithm.. os.turnOn(SchedulingAlgorithm.FIFO, 5); //Copy files from test. //Code has a device not in hardware file. - this.copyProcessFile("blockedInvalid6_1.txt"); + AllTests.copyProcessFile(os, "blockedInvalid6_1.txt"); //Wait for processes to execute. TimeUnit.MILLISECONDS.sleep(1000); String actualErrorReport = os.getPs().getErrorReport(); - String simpleErrorReport = this.simplifyErrorReport(actualErrorReport); + String simpleErrorReport = AllTests.simplifyErrorReport(actualErrorReport); StringBuffer expectedReport = new StringBuffer(); expectedReport.append("3: blockedInvalid6_1 BLOCKED"); if(!simpleErrorReport.equals(expectedReport.toString())) { - throw new IOException(); + throw new ComparisonFailedException("Expected results are not the same as actual results."); } os.turnOff(); } - - - public void copyProcessFile(String fileName) throws IOException { - Path testSource = Paths.get(os.getDirectoryName() + "/Processes/_Tests/" + fileName); - Path target = Paths.get(os.getDirectoryName() + "/Processes/New_Processes/" + fileName); - Files.copy(testSource, target, StandardCopyOption.REPLACE_EXISTING); - } - - - public String simplifyReport(String report) { - StringBuffer simpleReport = new StringBuffer(); - - String action = null; - String name = null; - String state = null; - boolean failed = false; - - String[] split = report.split("\n"); - - for(String line: split) { - if(!line.equals("")) { - - action = line.split(" ")[1]; - name = line.split(" ")[8]; - - if(line.split(" ")[10].equals("FAILED")) { - failed = true; - state = line.split(" ")[13]; - } - else { - state = line.split(" ")[11]; - } - - simpleReport.append(action + " "); - simpleReport.append(name + " "); - simpleReport.append(state + " "); - simpleReport.append(failed); - simpleReport.append("\n"); - - failed = false; - } - } - - return simpleReport.toString(); - - } - - - public String simplifyErrorReport(String report) { - StringBuffer simpleReport = new StringBuffer(); - - String lineNum = null; - String name = null; - String state = null; - - String[] split = report.split("\n"); - - for(String line: split) { - - if(!line.equals("")) { - lineNum = line.split(" ")[5]; - name = line.split(" ")[10]; - state = line.split(" ")[13]; - - simpleReport.append(lineNum + " "); - simpleReport.append(name + " "); - simpleReport.append(state); - - break; - } - } - - return simpleReport.toString(); - - } - }