Subversion Repositories programming

Rev

Rev 413 | Rev 415 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
414 ira 92
    protected void printGanttChart ()
93
    {
94
        Vector<String> t = new Vector<String> ();
95
        Vector<String> l = new Vector<String> ();
96
 
97
        String timeline = new String();
98
        String labels = new String();
99
        String add1 = new String();
100
        String add2 = new String();
101
 
102
        int exptime = 0;
103
        int templen;
104
 
105
        for (LogEntry e : log)
106
        {
107
            if (e.msg == LogEntry.MsgType.EXPIRE || e.msg == LogEntry.MsgType.COMPLETE)
108
            {
109
                exptime = e.time;
110
                continue;
111
            }
112
 
113
            add1 = "|---" + e.proc.name + "---";
114
            add2 = "" + e.time;
115
 
116
            for (int i=0; add2.length() < add1.length(); i++)
117
                add2 += " ";
118
 
119
            if (timeline.length() + add1.length() > 79)
120
            {
121
                t.add (timeline + "|");
122
 
123
                templen = labels.length() - String.valueOf(exptime).length();
124
                labels = labels.substring (0, templen+1) + String.valueOf(exptime);
125
 
126
                l.add (labels);
127
                timeline = new String();
128
                labels = new String();
129
            }
130
 
131
            timeline += add1;
132
            labels += add2;
133
        }
134
 
135
        /* Done, add the last values of timeline and labels to the Vectors */
136
        t.add (timeline + "|");
137
 
138
        templen = labels.length() - String.valueOf(exptime).length();
139
        labels = labels.substring (0, templen+1) + String.valueOf(exptime);
140
        l.add (labels);
141
 
142
        assert (l.size() == t.size());
143
 
144
        for (int i=0; i<l.size(); i++)
145
        {
146
            System.out.println (t.elementAt (i));
147
            System.out.println (l.elementAt (i));
148
            System.out.println ();
149
        }
150
    }
151
 
413 ira 152
    public Scheduler ()
153
    {
154
        this.run_queue = new Vector<Process> ();
155
        this.log = new Vector<LogEntry> ();
156
    }
157
 
158
    public void run ()
159
    {
160
        /* Keep step()ing until we're done */
161
        while (step ())
162
            ; // do nothing
163
 
414 ira 164
        /* Verbose print of each log entry */
165
        //for (LogEntry e : log)
166
        //   System.out.println (e);
413 ira 167
 
414 ira 168
        /* Gantt Chart */
169
        printGanttChart ();
170
 
413 ira 171
        /* Print time taken */
172
        System.out.println ("\nSimulation took " + cur_time + " time units to complete!");
173
    }
412 ira 174
}
175
 
176
/* vim: set ts=4 sts=4 sw=4 expandtab: */
177