Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. package caoss.simulator.os.scheduling;
  2.  
  3. import static caoss.simulator.os.OperatingSystem.OS;
  4.  
  5. import java.util.Queue;
  6. import java.util.concurrent.LinkedBlockingQueue;
  7.  
  8. import caoss.simulator.Program;
  9. import caoss.simulator.configuration.Hardware;
  10. import caoss.simulator.hardware.DeviceId;
  11. import caoss.simulator.hardware.Timer;
  12. import caoss.simulator.os.Dispatcher;
  13. import caoss.simulator.os.Logger;
  14. import caoss.simulator.os.ProcessControlBlock;
  15. import caoss.simulator.os.Scheduler;
  16.  
  17.  
  18. public class FSOScheduler implements Scheduler<SchedulingState> {
  19.  
  20. private static final int QUANTUM = 10;
  21. private static final int QUOTA = 20;
  22. private static final int PERIOD = 150;
  23. private static final int PRIOLEVELS = 4;
  24.  
  25.  
  26. private final Queue<ProcessControlBlock<SchedulingState>>[] readyQueues;
  27. private final Queue<ProcessControlBlock<SchedulingState>> blockedQueue;
  28. private ProcessControlBlock<SchedulingState> running; // just one CPU
  29.  
  30. private final Timer timer = (Timer) Hardware.devices.get(DeviceId.TIMER);
  31.  
  32. private long lastUpgradeTime = 0; // last priority upgrade time
  33.  
  34.  
  35. @SuppressWarnings("unchecked")
  36. public FSOScheduler( ) {
  37. this.readyQueues = (Queue<ProcessControlBlock<SchedulingState>>[]) new LinkedBlockingQueue[PRIOLEVELS];
  38. this.blockedQueue = new LinkedBlockingQueue<ProcessControlBlock<SchedulingState>>();
  39.  
  40. for (int i = 0; i < PRIOLEVELS; i++) {
  41. this.readyQueues[i] = new LinkedBlockingQueue<ProcessControlBlock<SchedulingState>>();
  42. }
  43.  
  44. this.lastUpgradeTime = Hardware.clock.getTime();
  45. }
  46.  
  47.  
  48. @Override
  49. public synchronized void newProcess(Program program) {
  50.  
  51. ProcessControlBlock<SchedulingState> pcb =
  52. new ProcessControlBlock<SchedulingState>(program,
  53. new SchedulingState(0, QUOTA));
  54. Logger.info("Create process " + pcb.pid + " to run program " + program.getFileName());
  55.  
  56. if (OS.getRunningProcess() == null)
  57. dispatch(pcb);
  58. else
  59. this.readyQueues[0].add(pcb);
  60.  
  61.  
  62. upgrade();
  63. logQueues();
  64. }
  65.  
  66. @Override
  67. public synchronized void ioRequest(ProcessControlBlock<SchedulingState> pcb) {
  68. Logger.info("Process " + pcb.pid + ": IO request");
  69.  
  70. // TODO:
  71. long time = Hardware.clock.getTime();
  72. long decorredTime = time - (pcb.getSchedulingState().getSchedulingTime());
  73.  
  74. long quota = pcb.getSchedulingState().getQuota();
  75. pcb.getSchedulingState().setQuota((int) (quota - decorredTime));
  76.  
  77. if (quota <= 0) {
  78. pcb.getSchedulingState().setQuota(QUOTA);
  79. int level = pcb.getSchedulingState().getLevel();
  80. if (level < readyQueues.length - 1)
  81. pcb.getSchedulingState().setLevel(level + 1);
  82. }
  83.  
  84. blockedQueue.add(pcb);
  85.  
  86. chooseNext();
  87.  
  88. upgrade();
  89. logQueues();
  90. }
  91.  
  92. @Override
  93. public synchronized void ioConcluded(ProcessControlBlock<SchedulingState> pcb) {
  94. Logger.info("Process " + pcb.pid + ": IO concluded");
  95.  
  96. // TODO:
  97. blockedQueue.remove(pcb);
  98.  
  99. if (OS.getRunningProcess() == null)
  100. dispatch(pcb);
  101. else {
  102. int level = pcb.getSchedulingState().getLevel();
  103. readyQueues[level].add(pcb);
  104. }
  105.  
  106. upgrade();
  107. logQueues();
  108. }
  109.  
  110. @Override
  111. public synchronized void quantumExpired(ProcessControlBlock<SchedulingState> pcb) {
  112. Logger.info("Process " + pcb.pid + ": quantum expired");
  113.  
  114. // TODO:
  115. int quota = pcb.getSchedulingState().getQuota();
  116. pcb.getSchedulingState().setQuota(quota - QUANTUM);
  117.  
  118. if ( (pcb.getSchedulingState().getQuota()) <= 0) {
  119. Logger.info("Process " + pcb.pid + ": quota expired");
  120. int currLevel = pcb.getSchedulingState().getLevel();
  121. if (currLevel < readyQueues.length - 1)
  122. pcb.getSchedulingState().setLevel(currLevel + 1);
  123. pcb.getSchedulingState().setQuota(QUOTA);
  124. }
  125.  
  126. readyQueues[pcb.getSchedulingState().getLevel()].add(pcb);
  127.  
  128.  
  129. chooseNext();
  130.  
  131. upgrade();
  132. logQueues();
  133. }
  134.  
  135. @Override
  136. public synchronized void processConcluded(ProcessControlBlock<SchedulingState> pcb) {
  137. Logger.info("Process " + pcb.pid + ": execution concluded");
  138.  
  139. long turnarround = Hardware.clock.getTime() - pcb.arrivalTime;
  140. Logger.info("Process " + pcb.pid + ": turnarround time: " + turnarround);
  141.  
  142. chooseNext();
  143.  
  144. upgrade();
  145. logQueues();
  146. }
  147.  
  148.  
  149.  
  150. private void upgrade() {
  151. long currentTime = Hardware.clock.getTime();
  152. long elapsed = currentTime - this.lastUpgradeTime;
  153. if (elapsed >= PERIOD) {
  154. Logger.info("Upgrade priorities");
  155.  
  156. // TODO:
  157. if (running != null) {
  158. running.getSchedulingState().setQuota(QUOTA);
  159.  
  160. if (running.getSchedulingState().getLevel() == 3)
  161. running.getSchedulingState().setLevel(1);
  162. else
  163. running.getSchedulingState().setLevel(0);
  164. }
  165.  
  166. for (ProcessControlBlock<SchedulingState> curr: blockedQueue) {
  167. curr.getSchedulingState().setQuota(QUOTA);
  168. if (curr.getSchedulingState().getLevel() == 3)
  169. curr.getSchedulingState().setLevel(1);
  170. else
  171. curr.getSchedulingState().setLevel(0);
  172. }
  173.  
  174. for (ProcessControlBlock<SchedulingState> curr: readyQueues[0]) {
  175. curr.getSchedulingState().setQuota(QUOTA);
  176. }
  177.  
  178. for (int j = 1; j <= 2; j++)
  179. while(!readyQueues[j].isEmpty()) {
  180. ProcessControlBlock<SchedulingState> pcb = readyQueues[j].poll();
  181. pcb.getSchedulingState().setQuota(QUOTA);
  182. pcb.getSchedulingState().setLevel(0);
  183. readyQueues[0].add(pcb);
  184. }
  185.  
  186. while(!readyQueues[3].isEmpty()) {
  187. ProcessControlBlock<SchedulingState> pcb = readyQueues[3].poll();
  188. pcb.getSchedulingState().setQuota(QUOTA);
  189. pcb.getSchedulingState().setLevel(1);
  190. readyQueues[1].add(pcb);
  191. }
  192.  
  193. this.lastUpgradeTime = currentTime;
  194. }
  195. }
  196.  
  197.  
  198. private void chooseNext() {
  199. for (Queue<ProcessControlBlock<SchedulingState>> queue : this.readyQueues) {
  200. if (queue.size() > 0) {
  201. dispatch(queue.poll());
  202. return;
  203. }
  204. }
  205.  
  206. dispatch(null);
  207. }
  208.  
  209. private void dispatch(ProcessControlBlock<SchedulingState> pcb) {
  210. Dispatcher.dispatch(pcb);
  211. running = pcb;
  212.  
  213. if (pcb != null) {
  214. SchedulingState state = pcb.getSchedulingState();
  215. state.setSchedulingTime(Hardware.clock.getTime());
  216. timer.set(QUANTUM);
  217. Logger.info("Run process "+ pcb.pid +" (quantum="+ QUANTUM +", quota="+ state.getQuota()+")");
  218. }
  219. else
  220. timer.set(0);
  221. }
  222.  
  223.  
  224. private void logQueues() {
  225. int i = 0;
  226. for (Queue<ProcessControlBlock<SchedulingState>> queue : this.readyQueues) {
  227. Logger.info("Queue " + i + ": " + queue);
  228. i++;
  229. }
  230. Logger.info("Blocked " + blockedQueue);
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement