Subversion Repositories programming

Rev

Rev 412 | Rev 414 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 412 Rev 413
Line 23... Line 23...
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
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
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
 * IN THE SOFTWARE.
25
 * IN THE SOFTWARE.
26
 ******************************************************************************/
26
 ******************************************************************************/
27
 
27
 
-
 
28
import java.util.Vector;
-
 
29
 
28
interface Scheduler
30
abstract class Scheduler
29
{
31
{
-
 
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
     */
30
    void addProcess (Process proc, int add_time);
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
 
31
    void run ();
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
    }
32
}
110
}
33
 
111
 
34
/* vim: set ts=4 sts=4 sw=4 expandtab: */
112
/* vim: set ts=4 sts=4 sw=4 expandtab: */
35
 
113