Round-Robin Scheduling Algorithm

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 2
Reputation: mu42774 is an unknown quantity at this point 
Solved Threads: 0
mu42774 mu42774 is offline Offline
Newbie Poster

Round-Robin Scheduling Algorithm

 
1
  #1
Jul 26th, 2005
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
  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. }
=================================================
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Round-Robin Scheduling Algorithm

 
0
  #2
Jul 29th, 2005
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
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: amerabdullah is an unknown quantity at this point 
Solved Threads: 0
amerabdullah amerabdullah is offline Offline
Newbie Poster

Re: Round-Robin Scheduling Algorithm

 
0
  #3
May 10th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 976
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Round-Robin Scheduling Algorithm

 
0
  #4
May 10th, 2009
In the rules, use CODE TAGS! I can't read it that unformatted code.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: Johny cash is an unknown quantity at this point 
Solved Threads: 0
Johny cash Johny cash is offline Offline
Newbie Poster
 
0
  #5
24 Days Ago
that helped me alot thanks
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC