412 |
ira |
1 |
/*******************************************************************************
|
|
|
2 |
* File: Scheduler.java
|
|
|
3 |
*
|
|
|
4 |
* Holds the Scheduler class, which has a generic scheduler class, from which
|
|
|
5 |
* all schedulers should be derived.
|
|
|
6 |
*
|
|
|
7 |
* Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
|
|
|
8 |
*
|
|
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
10 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
11 |
* in the Software without restriction, including without limitation the rights
|
|
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
13 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
14 |
* furnished to do so, subject to the following conditions:
|
|
|
15 |
*
|
|
|
16 |
* The above copyright notice and this permission notice shall be included in
|
|
|
17 |
* all copies or substantial portions of the Software.
|
|
|
18 |
*
|
|
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
25 |
* IN THE SOFTWARE.
|
|
|
26 |
******************************************************************************/
|
|
|
27 |
|
413 |
ira |
28 |
import java.util.Vector;
|
|
|
29 |
|
|
|
30 |
abstract class Scheduler
|
412 |
ira |
31 |
{
|
413 |
ira |
32 |
protected Vector<Process> run_queue;
|
|
|
33 |
protected Vector<LogEntry> log;
|
|
|
34 |
protected int cur_time = 0;
|
|
|
35 |
protected Process cur_proc;
|
|
|
36 |
|
|
|
37 |
protected abstract boolean step ();
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Add a new process to the run queue.
|
|
|
41 |
*
|
|
|
42 |
* If you need to support time-delayed processes, then you must override
|
|
|
43 |
* this method, and handle pulling them into the run_queue yourself.
|
|
|
44 |
*/
|
|
|
45 |
public void addProcess (Process proc, int add_time)
|
|
|
46 |
{
|
|
|
47 |
run_queue.add (new Process(proc));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Expire the currently running process.
|
|
|
52 |
*
|
|
|
53 |
* This removes the current process from the cpu and re-adds it to the
|
|
|
54 |
* run_queue.
|
|
|
55 |
*/
|
|
|
56 |
protected void expireCurrent ()
|
|
|
57 |
{
|
|
|
58 |
assert (cur_proc.time_left > 0);
|
|
|
59 |
|
|
|
60 |
cur_proc.finished_at = cur_time;
|
|
|
61 |
|
|
|
62 |
run_queue.add (cur_proc);
|
|
|
63 |
log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.EXPIRE));
|
|
|
64 |
cur_proc = null;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
protected void completeCurrent ()
|
|
|
68 |
{
|
|
|
69 |
assert (cur_proc.time_left == 0);
|
|
|
70 |
|
|
|
71 |
cur_proc.finished_at = cur_time;
|
|
|
72 |
|
|
|
73 |
log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.COMPLETE));
|
|
|
74 |
cur_proc = null;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
protected void startProcess (Process proc)
|
|
|
78 |
{
|
|
|
79 |
cur_proc = proc;
|
|
|
80 |
cur_proc.started_at = cur_time;
|
|
|
81 |
run_queue.remove (cur_proc);
|
|
|
82 |
|
|
|
83 |
log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.START));
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
protected void scheduleCurrent ()
|
|
|
87 |
{
|
|
|
88 |
cur_proc.time_left--;
|
|
|
89 |
cur_time++;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public Scheduler ()
|
|
|
93 |
{
|
|
|
94 |
this.run_queue = new Vector<Process> ();
|
|
|
95 |
this.log = new Vector<LogEntry> ();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public void run ()
|
|
|
99 |
{
|
|
|
100 |
/* Keep step()ing until we're done */
|
|
|
101 |
while (step ())
|
|
|
102 |
; // do nothing
|
|
|
103 |
|
|
|
104 |
for (LogEntry e : log)
|
|
|
105 |
System.out.println (e);
|
|
|
106 |
|
|
|
107 |
/* Print time taken */
|
|
|
108 |
System.out.println ("\nSimulation took " + cur_time + " time units to complete!");
|
|
|
109 |
}
|
412 |
ira |
110 |
}
|
|
|
111 |
|
|
|
112 |
/* vim: set ts=4 sts=4 sw=4 expandtab: */
|
|
|
113 |
|