· 9 years ago · Jan 25, 2017, 10:02 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.IO;
7using Combinatorics.Collections;
8
9namespace CPU_Scheduler_Simulation
10{
11 public class Simulation
12 {
13 public int quantum; // quantum for the simulation
14 List<PCB> processTable = new List<PCB>(); // contains list of PCBs
15 public Scheduler SimScheduler = new Scheduler(); // create a scheduler object
16 public string filename; // name of file containing information of PCBs
17 public string filepath; // in the same folder as the solution file
18 public bool debugStatements = false;
19 List<PCB> copy = new List<PCB>(); // holds a copy of the original list, processTable
20 Data data = new Data();
21
22 public Simulation() { } // default constructor
23
24 //pushes info from lines on .dat files into processTable as a PCB
25 public void readDataFiles()
26 {
27 Console.WriteLine("--BEGIN FILE I/O");
28 filename = "processes.dat";
29 //Directory.SetCurrentDirectory(@"..\..\..\"); // default is \bin\Debug -> this sets the current directory up a few folders
30 filepath = Path.Combine(Environment.CurrentDirectory, filename);
31
32 Console.WriteLine("\tFile + Path: {0}", filepath);
33
34 if (File.Exists(filepath))
35 {
36 Console.WriteLine("\t{0} exists. File will be processed.", filepath); // debugging purposes
37
38 var reader = new StreamReader(File.OpenRead(filepath)); // opens the read stream
39
40 while (!reader.EndOfStream) // loops until end of file
41 {
42 var line = reader.ReadLine(); // reads in line
43 var values = line.Split(new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); // creates array of values in line
44 addProcess(values); // calls function that adds process to process table
45 }
46
47 Console.WriteLine("\tFile read completed.");
48 Console.WriteLine("\tThere were " + processTable.Count + " processes added.");
49 Console.WriteLine("--END FILE I/O\n");
50 reader.Close();
51 }
52 else
53 {
54 Console.WriteLine("File {0} not found.", filename);
55 }
56 }
57
58 // adds a process onto the process table. called from readDataFiles()
59 public void addProcess(string [] values) {
60 PCB process = new PCB(); // the new process we will add to our list
61 process.PID = Convert.ToInt32(values[0]); // process PID
62 process.priorityNumber = Convert.ToInt32(values[1]); // process priority number
63 process.arrivalTime = Convert.ToInt32(values[2]); // process arrival time
64
65 for (int i = 3; i < values.Length; i++) // since bursts start at the 4th column (index 3 in an array), we start reading in bursts there
66 {
67 var burst = Convert.ToInt32(values[i]); // burst time
68 if (burst != 0)
69 {
70 if (i % 2 != 0) // if in an odd column, then it's a CPU burst
71 {
72 process.CPU.Enqueue(burst); // add CPU burst to the process' CPU burst list
73 }
74 else // then it's in an even column, so it's an I/O burst
75 {
76 process.IO.Enqueue(burst); // add I/O burst to the process' I/O burst list
77 }
78 }
79 }
80 processTable.Add(process); // adds process to process table
81 //copy.Add(process);
82 if(debugStatements)Console.WriteLine("Process with PID {0} added.", process.PID);
83 }
84
85 //sets up the scheduler and runs the simulation
86 public void startSim(List<int> quantum1, List<int> quantum2) {
87 Console.WriteLine("Simulation beginning.\n");
88 data.writeRandomProcessesToFile();
89 readDataFiles(); // reads in processes in the .dat file
90 processTable = processTable.OrderBy(p => p.arrivalTime).ToList(); // order the processes by arrival time
91
92 var integers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8}; // for all test cases
93
94 //var x = new Permutations<int>(integers, GenerateOption.WithoutRepetition); // create all permutations of the integers list - will run through 9! runs of the simulation
95 List<PCB> copy = new List<PCB>();
96 List<int> numCPU = new List<int> { 2, 3, 4 };
97 //foreach (var v in integers)
98 for (int i = 0; i < quantum1.Count; i++)
99 {
100 Console.WriteLine(data.currentRun(integers));
101 // create a new scheduler every time we start the scheduler
102 Scheduler simScheduler = new Scheduler();
103 simScheduler.setQuantums(quantum1[i], quantum2[i]); // set the quantums
104 simScheduler.numCPUs = numCPU[2]; // set the number of CPUs
105 copy = processTable.ConvertAll(pcb => (PCB)pcb.Clone()).ToList(); // create a copy of the original list
106 simScheduler.loadBalancing(processTable, integers); // load balances the processes
107 endSim(processTable, simScheduler, integers); // output the data
108 processTable = copy; // reset the processTable with original list in copy
109 }
110 Console.ReadKey();
111 //data.writeStatsToFile();
112 }
113
114 //ends the simulation
115 public void endSim(List<PCB> list, Scheduler scheduler, IList<int> v) {
116 Console.WriteLine("Simulation complete.\n");
117 Console.WriteLine(data.endSimOutput(scheduler));
118 Statistics stats = new Statistics(list, scheduler); // object that holds all of the stats
119 stats.runStatistics();
120 //Console.ReadKey();
121 data.addToLists(stats, v);
122 }
123 }
124}