-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
95 lines (69 loc) · 3.08 KB
/
Main.java
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
/*
Author: Liam O'Shea
Date: November 25 2018
File: Main.java
Description: This is program reads a number of Processes from a text file, and simulates
the priority behaviour of a CPU triaging processes using a Heap data structure.
Output is shown for before, during, and after the time step.
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
//Declare variables
int t = 1, id, procTime, priority, arivTime; //Timestep and Process variables
Process temp; //Temporary variable to manipulate Processes
Heap<Process> heap = new Heap<Process>(); //Priority heap to store processes
ArrayList<Process> allProc = new ArrayList<Process>(); //List of all processes from text file
//Create file for processes and instantiate scanner using this file.
File processes = new File("processes.txt");
Scanner proc = new Scanner(processes);
//Creating and storing Processes
while(proc.hasNext()){
//Get process data
id = proc.nextInt();
procTime = proc.nextInt();
priority = proc.nextInt();
arivTime = proc.nextInt();
//Create process
temp = new Process(id, procTime, priority, arivTime);
//Put process in list
allProc.add(temp);
}
//Program loop (Ends when both list and heap are empty)
while(allProc.size() > 0 || heap.size() > 0){
//Check list of processes for processes arriving at this time step, add them to heap.
while (allProc.size() > 0 && allProc.get(0).getTimeArrival() == t) {
heap.add(allProc.remove(0));
}
//Beginning Output--------
System.out.println("\n********************\nTime Unit: " + t + "\n");
System.out.println("BEGINNING ------------");
System.out.print("Heap Contents: ");
heap.enumerate();
System.out.println("CPU Contents: \n");
//------------------------
//Get process to be worked on by CPU (top of heap)
temp = heap.deleteMax();
//During Output-----------
System.out.println("DURING ------------");
System.out.print("Heap Contents: ");
heap.enumerate();
System.out.println("CPU Contents: [" + temp + "]\n");
//------------------------
//Decrement time required by 1.
temp.setTimeReqd(temp.getTimeReqd() - 1);
//If process still requires more time, send back to heap
if(temp.getTimeReqd() > 0) heap.add(temp);
//After Output-------------
System.out.println("AFTER ------------");
System.out.print("Heap Contents: ");
heap.enumerate();
System.out.println("CPU Contents: ");
//-------------------------
//Increment time step
t++;
}
}
}