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 |
|
|
|
27 |
import java.util.Vector;
|
|
|
28 |
|
|
|
29 |
class RRScheduler extends Scheduler
|
|
|
30 |
{
|
|
|
31 |
protected final int interval;
|
|
|
32 |
protected int cur_proc_runtime = 0;
|
|
|
33 |
|
|
|
34 |
public RRScheduler (int interval)
|
|
|
35 |
{
|
|
|
36 |
super ();
|
|
|
37 |
this.interval = interval;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
protected boolean step ()
|
|
|
41 |
{
|
|
|
42 |
/* Stop if we have nothing left to do */
|
|
|
43 |
if (cur_proc == null && run_queue.isEmpty ())
|
|
|
44 |
return false;
|
|
|
45 |
|
|
|
46 |
/* Get a new process if we need to */
|
|
|
47 |
if (cur_proc == null)
|
|
|
48 |
{
|
|
|
49 |
startProcess (run_queue.firstElement ());
|
|
|
50 |
cur_proc_runtime = 0;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/* Run the process */
|
|
|
54 |
if (cur_proc_runtime < interval)
|
|
|
55 |
{
|
|
|
56 |
if (cur_proc.time_left > 0)
|
|
|
57 |
{
|
|
|
58 |
scheduleCurrent ();
|
|
|
59 |
cur_proc_runtime++;
|
|
|
60 |
}
|
|
|
61 |
else
|
|
|
62 |
completeCurrent ();
|
|
|
63 |
}
|
|
|
64 |
else
|
|
|
65 |
{
|
|
|
66 |
if (cur_proc.time_left > 0)
|
|
|
67 |
expireCurrent ();
|
|
|
68 |
else
|
|
|
69 |
completeCurrent ();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return true;
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/* vim: set ts=4 sts=4 sw=4 expandtab: */
|
|
|
77 |
|