| 413 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* RRScheduler.java
|
|
|
3 |
*
|
|
|
4 |
* Implementation of a Round-Robin Scheduler for CS431 Project #1.
|
|
|
5 |
*
|
|
|
6 |
* Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
|
|
|
7 |
*
|
|
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
9 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
10 |
* in the Software without restriction, including without limitation the rights
|
|
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
12 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
13 |
* furnished to do so, subject to the following conditions:
|
|
|
14 |
*
|
|
|
15 |
* The above copyright notice and this permission notice shall be included in
|
|
|
16 |
* all copies or substantial portions of the Software.
|
|
|
17 |
*
|
|
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
23 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
24 |
* IN THE SOFTWARE.
|
|
|
25 |
******************************************************************************/
|
|
|
26 |
|
| 415 |
ira |
27 |
/**
|
|
|
28 |
* A Round-Robin Scheduler.
|
|
|
29 |
*
|
|
|
30 |
* @class CS431 Fall 2006
|
|
|
31 |
* @author Ira W. Snyder (devel@irasnyder.com)
|
|
|
32 |
*/
|
|
|
33 |
public class RRScheduler extends Scheduler
|
| 413 |
ira |
34 |
{
|
| 415 |
ira |
35 |
/** the interval at which to switch processes */
|
| 413 |
ira |
36 |
protected final int interval;
|
| 415 |
ira |
37 |
|
|
|
38 |
/** the amount of time that the current process has been running */
|
| 413 |
ira |
39 |
protected int cur_proc_runtime = 0;
|
|
|
40 |
|
| 415 |
ira |
41 |
/**
|
|
|
42 |
* Constructor for the Round Robin Scheduler class.
|
|
|
43 |
*/
|
| 413 |
ira |
44 |
public RRScheduler (int interval)
|
|
|
45 |
{
|
|
|
46 |
super ();
|
|
|
47 |
this.interval = interval;
|
|
|
48 |
}
|
|
|
49 |
|
| 415 |
ira |
50 |
/**
|
|
|
51 |
* Emulates a single time unit in a Round Robin Scheduler.
|
|
|
52 |
*
|
|
|
53 |
* @return false if there is nothing left to do, true otherwise
|
|
|
54 |
*/
|
| 413 |
ira |
55 |
protected boolean step ()
|
|
|
56 |
{
|
|
|
57 |
/* Stop if we have nothing left to do */
|
|
|
58 |
if (cur_proc == null && run_queue.isEmpty ())
|
|
|
59 |
return false;
|
|
|
60 |
|
|
|
61 |
/* Get a new process if we need to */
|
|
|
62 |
if (cur_proc == null)
|
|
|
63 |
{
|
|
|
64 |
startProcess (run_queue.firstElement ());
|
|
|
65 |
cur_proc_runtime = 0;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/* Run the process */
|
|
|
69 |
if (cur_proc_runtime < interval)
|
|
|
70 |
{
|
|
|
71 |
if (cur_proc.time_left > 0)
|
|
|
72 |
{
|
|
|
73 |
scheduleCurrent ();
|
|
|
74 |
cur_proc_runtime++;
|
|
|
75 |
}
|
|
|
76 |
else
|
|
|
77 |
completeCurrent ();
|
|
|
78 |
}
|
|
|
79 |
else
|
|
|
80 |
{
|
|
|
81 |
if (cur_proc.time_left > 0)
|
|
|
82 |
expireCurrent ();
|
|
|
83 |
else
|
|
|
84 |
completeCurrent ();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return true;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/* vim: set ts=4 sts=4 sw=4 expandtab: */
|
|
|
92 |
|