943,031 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 26831
  • Java RSS
Jul 26th, 2005
2

Round-Robin Scheduling Algorithm

Expand Post »
Hi,
would someone please help me with the following:
I would like to implement the Round-Robin scheduling algorith for the below code. I am really facing a problem whenever i try to implement Round-Robin scheduling algorith in this code. Your prompt action and support is highly appreciated and I am certain that one of you would do his/her best to help me by sharing his great knowledge with others.
The given Hint by someone is, I need to change the Run() method in Scheduling Algorith.java
Java Syntax (Toggle Plain Text)
  1. // Run() is called from Scheduling.main() and is where
  2. // the scheduling algorithm written by the user resides.
  3. // User modification should occur within the Run() function.
  4.  
  5. import java.util.Vector;
  6. import java.io.*;
  7.  
  8. public class SchedulingAlgorithm {
  9.  
  10. public static Results Run(int runtime, Vector processVector, Results result) {
  11. int i = 0;
  12. int comptime = 0;
  13. int currentProcess = 0;
  14. int previousProcess = 0;
  15. int size = processVector.size();
  16. int completed = 0;
  17. String resultsFile = "Summary-Processes";
  18.  
  19. result.schedulingType = "Batch (Nonpreemptive)";
  20. result.schedulingName = "First-Come First-Served";
  21. try {
  22. //BufferedWriter out = new BufferedWriter(new FileWriter(resultsFile));
  23. //OutputStream out = new FileOutputStream(resultsFile);
  24. PrintStream out = new PrintStream(new FileOutputStream(resultsFile));
  25. sProcess process = (sProcess) processVector.elementAt(currentProcess);
  26. out.println("Process: " + currentProcess + " registered... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
  27. while (comptime < runtime) {
  28. if (process.cpudone == process.cputime) {
  29. completed++;
  30. out.println("Process: " + currentProcess + " completed... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
  31. if (completed == size) {
  32. result.compuTime = comptime;
  33. out.close();
  34. return result;
  35. }
  36. for (i = size - 1; i >= 0; i--) {
  37. process = (sProcess) processVector.elementAt(i);
  38. if (process.cpudone < process.cputime) {
  39. currentProcess = i;
  40. }
  41. }
  42. process = (sProcess) processVector.elementAt(currentProcess);
  43. out.println("Process: " + currentProcess + " registered... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
  44. }
  45. if (process.ioblocking == process.ionext) {
  46. out.println("Process: " + currentProcess + " I/O blocked... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
  47. process.numblocked++;
  48. process.ionext = 0;
  49. previousProcess = currentProcess;
  50. for (i = size - 1; i >= 0; i--) {
  51. process = (sProcess) processVector.elementAt(i);
  52. if (process.cpudone < process.cputime && previousProcess != i) {
  53. currentProcess = i;
  54. }
  55. }
  56. process = (sProcess) processVector.elementAt(currentProcess);
  57. out.println("Process: " + currentProcess + " registered... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
  58. }
  59. process.cpudone++;
  60. if (process.ioblocking > 0) {
  61. process.ionext++;
  62. }
  63. comptime++;
  64. }
  65. out.close();
  66. } catch (IOException e) { /* Handle exceptions */ }
  67. result.compuTime = comptime;
  68. return result;
  69. }
  70. }
=================================================
Java Syntax (Toggle Plain Text)
  1. // This file contains the main() function for the Scheduling
  2. // simulation. Init() initializes most of the variables by
  3. // reading from a provided file. SchedulingAlgorithm.Run() is
  4. // called from main() to run the simulation. Summary-Results
  5. // is where the summary results are written, and Summary-Processes
  6. // is where the process scheduling summary is written.
  7.  
  8. // Created by Alexander Reeder, 2001 January 06
  9.  
  10. import java.io.*;
  11. import java.util.*;
  12. import sProcess;
  13. import Common;
  14. import Results;
  15. import SchedulingAlgorithm;
  16.  
  17. public class Scheduling {
  18.  
  19. private static int processnum = 5;
  20. private static int meanDev = 1000;
  21. private static int standardDev = 100;
  22. private static int runtime = 1000;
  23. private static Vector processVector = new Vector();
  24. private static Results result = new Results("null","null",0);
  25. private static String resultsFile = "Summary-Results";
  26.  
  27. private static void Init(String file) {
  28. File f = new File(file);
  29. String line;
  30. String tmp;
  31. int cputime = 0;
  32. int ioblocking = 0;
  33. double X = 0.0;
  34.  
  35. try {
  36. //BufferedReader in = new BufferedReader(new FileReader(f));
  37. DataInputStream in = new DataInputStream(new FileInputStream(f));
  38. while ((line = in.readLine()) != null) {
  39. if (line.startsWith("numprocess")) {
  40. StringTokenizer st = new StringTokenizer(line);
  41. st.nextToken();
  42. processnum = Common.s2i(st.nextToken());
  43. }
  44. if (line.startsWith("meandev")) {
  45. StringTokenizer st = new StringTokenizer(line);
  46. st.nextToken();
  47. meanDev = Common.s2i(st.nextToken());
  48. }
  49. if (line.startsWith("standdev")) {
  50. StringTokenizer st = new StringTokenizer(line);
  51. st.nextToken();
  52. standardDev = Common.s2i(st.nextToken());
  53. }
  54. if (line.startsWith("process")) {
  55. StringTokenizer st = new StringTokenizer(line);
  56. st.nextToken();
  57. ioblocking = Common.s2i(st.nextToken());
  58. X = Common.R1();
  59. while (X == -1.0) {
  60. X = Common.R1();
  61. }
  62. X = X * standardDev;
  63. cputime = (int) X + meanDev;
  64. processVector.addElement(new sProcess(cputime, ioblocking, 0, 0, 0));
  65. }
  66. if (line.startsWith("runtime")) {
  67. StringTokenizer st = new StringTokenizer(line);
  68. st.nextToken();
  69. runtime = Common.s2i(st.nextToken());
  70. }
  71. }
  72. in.close();
  73. } catch (IOException e) { /* Handle exceptions */ }
  74. }
  75.  
  76. private static void debug() {
  77. int i = 0;
  78.  
  79. System.out.println("processnum " + processnum);
  80. System.out.println("meandevm " + meanDev);
  81. System.out.println("standdev " + standardDev);
  82. int size = processVector.size();
  83. for (i = 0; i < size; i++) {
  84. sProcess process = (sProcess) processVector.elementAt(i);
  85. System.out.println("process " + i + " " + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.numblocked);
  86. }
  87. System.out.println("runtime " + runtime);
  88. }
  89.  
  90. public static void main(String[] args) {
  91. int i = 0;
  92.  
  93. if (args.length != 1) {
  94. System.out.println("Usage: 'java Scheduling <INIT FILE>'");
  95. System.exit(-1);
  96. }
  97. File f = new File(args[0]);
  98. if (!(f.exists())) {
  99. System.out.println("Scheduling: error, file '" + f.getName() + "' does not exist.");
  100. System.exit(-1);
  101. }
  102. if (!(f.canRead())) {
  103. System.out.println("Scheduling: error, read of " + f.getName() + " failed.");
  104. System.exit(-1);
  105. }
  106. System.out.println("Working...");
  107. Init(args[0]);
  108. if (processVector.size() < processnum) {
  109. i = 0;
  110. while (processVector.size() < processnum) {
  111. double X = Common.R1();
  112. while (X == -1.0) {
  113. X = Common.R1();
  114. }
  115. X = X * standardDev;
  116. int cputime = (int) X + meanDev;
  117. processVector.addElement(new sProcess(cputime,i*100,0,0,0));
  118. i++;
  119. }
  120. }
  121. result = SchedulingAlgorithm.Run(runtime, processVector, result);
  122. try {
  123. //BufferedWriter out = new BufferedWriter(new FileWriter(resultsFile));
  124. PrintStream out = new PrintStream(new FileOutputStream(resultsFile));
  125. out.println("Scheduling Type: " + result.schedulingType);
  126. out.println("Scheduling Name: " + result.schedulingName);
  127. out.println("Simulation Run Time: " + result.compuTime);
  128. out.println("Mean: " + meanDev);
  129. out.println("Standard Deviation: " + standardDev);
  130. out.println("Process #\tCPU Time\tIO Blocking\tCPU Completed\tCPU Blocked");
  131. for (i = 0; i < processVector.size(); i++) {
  132. sProcess process = (sProcess) processVector.elementAt(i);
  133. out.print(Integer.toString(i));
  134. if (i < 100) { out.print("\t\t"); } else { out.print("\t"); }
  135. out.print(Integer.toString(process.cputime));
  136. if (process.cputime < 100) { out.print(" (ms)\t\t"); } else { out.print(" (ms)\t"); }
  137. out.print(Integer.toString(process.ioblocking));
  138. if (process.ioblocking < 100) { out.print(" (ms)\t\t"); } else { out.print(" (ms)\t"); }
  139. out.print(Integer.toString(process.cpudone));
  140. if (process.cpudone < 100) { out.print(" (ms)\t\t"); } else { out.print(" (ms)\t"); }
  141. out.println(process.numblocked + " times");
  142. }
  143. out.close();
  144. } catch (IOException e) { /* Handle exceptions */ }
  145. System.out.println("Completed.");
  146. }
  147. }
Last edited by Ancient Dragon; May 10th, 2009 at 5:04 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mu42774 is offline Offline
2 posts
since Jul 2005
Jul 29th, 2005
0

Re: Round-Robin Scheduling Algorithm

Hi everyone,

You can try and see the below example and see where you have gone wrong

http://www.utdallas.edu/~ilyen/anima...am/manual.html

I hoped this helped you

Yours Sincerely

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
May 10th, 2009
0

Re: Round-Robin Scheduling Algorithm

Hi,
would someone please help me with the following quotions
(the required is to implement a simulator for processes scheduling that
takes a file contains N of processes each with required details,and then
after reading these information from the input file ,the scheduler should
schedule the processes for execution.
input:N processes ,each with ID,cpu required time ,arrival time .
(Quantum for "Round Robin"algorithm),(Priority for "High Priority first algorithm)
(firs come first served(FCFS);

Output:for each process,start time,finish time ,and waiting time ,and average waiting
time for all processes
note:
in c++(object oriented programming language.
(please help me)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amerabdullah is offline Offline
4 posts
since May 2009
May 10th, 2009
0

Re: Round-Robin Scheduling Algorithm

In the rules, use CODE TAGS! I can't read it that unformatted code.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 22nd, 2009
0
Re: Round-Robin Scheduling Algorithm
that helped me alot thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Johny cash is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help please, thanks
Next Thread in Java Forum Timeline: How to build a simple online bookstore





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC