Subversion Repositories programming

Rev

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

Rev 414 Rev 415
Line 1... Line 1...
1
/*******************************************************************************
1
/*******************************************************************************
2
 * File: Scheduler.java
2
 * Scheduler.java
3
 *
3
 *
4
 * Holds the Scheduler class, which has a generic scheduler class, from which
4
 * Holds the Scheduler class, which has a generic scheduler class, from which
5
 * all schedulers should be derived.
5
 * all schedulers should be derived.
6
 *
6
 *
7
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
7
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
Line 25... Line 25...
25
 * IN THE SOFTWARE.
25
 * IN THE SOFTWARE.
26
 ******************************************************************************/
26
 ******************************************************************************/
27
 
27
 
28
import java.util.Vector;
28
import java.util.Vector;
29
 
29
 
-
 
30
/**
-
 
31
 * Base class for all of the specific Schedulers used in Project #1.
-
 
32
 *
-
 
33
 * @class CS431 Fall 2006
-
 
34
 * @author Ira W. Snyder (devel@irasnyder.com)
-
 
35
 */
30
abstract class Scheduler
36
public abstract class Scheduler
31
{
37
{
-
 
38
    /** The Processes that can currently be scheduled */
32
    protected Vector<Process> run_queue;
39
    protected Vector<Process> run_queue;
-
 
40
 
-
 
41
    /** A log of important transitions in the Scheduler */
33
    protected Vector<LogEntry> log;
42
    protected Vector<LogEntry> log;
-
 
43
 
-
 
44
    /** The current time in this scheduler */
34
    protected int cur_time = 0;
45
    protected int cur_time = 0;
-
 
46
 
-
 
47
    /** The process that is currently running on the CPU */
35
    protected Process cur_proc;
48
    protected Process cur_proc;
36
 
49
 
-
 
50
    /**
-
 
51
     * Constructor of the Scheduler class.
-
 
52
     */
-
 
53
    public Scheduler ()
-
 
54
    {
-
 
55
        this.run_queue = new Vector<Process> ();
-
 
56
        this.log = new Vector<LogEntry> ();
-
 
57
    }
-
 
58
 
-
 
59
    /**
-
 
60
     * This method must be overridden by an implementer of a Scheduler. It
-
 
61
     * will implement the specific behaivior of the specific Scheduler that
-
 
62
     * is being implemented.
-
 
63
     *
-
 
64
     * @return true if we have more things to do, false if we are done
-
 
65
     */
37
    protected abstract boolean step ();
66
    protected abstract boolean step ();
38
 
67
 
39
    /**
68
    /**
40
     * Add a new process to the run queue.
69
     * Add a new process to the run queue.
41
     *
70
     * <p>
42
     * If you need to support time-delayed processes, then you must override
71
     * If you need to support time-delayed processes, then you must override
43
     * this method, and handle pulling them into the run_queue yourself.
72
     * this method, and handle pulling them into the run_queue yourself.
-
 
73
     *
-
 
74
     * @param proc the Process to add
-
 
75
     * @param add_time the time that this process should be added to the run queue
44
     */
76
     */
45
    public void addProcess (Process proc, int add_time)
77
    public void addProcess (Process proc, int add_time)
46
    {
78
    {
47
        run_queue.add (new Process(proc));
79
        run_queue.add (new Process(proc));
48
    }
80
    }
49
 
81
 
50
    /**
82
    /**
51
     * Expire the currently running process.
83
     * Expire the currently running process.
52
     *
84
     * <p>
53
     * This removes the current process from the cpu and re-adds it to the
85
     * This removes the current process from the CPU and re-adds it to the
54
     * run_queue.
86
     * run_queue. It also logs the expiration.
55
     */
87
     */
56
    protected void expireCurrent ()
88
    protected void expireCurrent ()
57
    {
89
    {
58
        assert (cur_proc.time_left > 0);
90
        assert (cur_proc.time_left > 0);
59
 
91
 
Line 62... Line 94...
62
        run_queue.add (cur_proc);
94
        run_queue.add (cur_proc);
63
        log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.EXPIRE));
95
        log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.EXPIRE));
64
        cur_proc = null;
96
        cur_proc = null;
65
    }
97
    }
66
 
98
 
-
 
99
    /**
-
 
100
     * Terminate the currently running process.
-
 
101
     * <p>
-
 
102
     * This should be called if the process has no time left to run.
-
 
103
     */
67
    protected void completeCurrent ()
104
    protected void completeCurrent ()
68
    {
105
    {
69
        assert (cur_proc.time_left == 0);
106
        assert (cur_proc.time_left == 0);
70
 
107
 
71
        cur_proc.finished_at = cur_time;
108
        cur_proc.finished_at = cur_time;
72
 
109
 
73
        log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.COMPLETE));
110
        log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.COMPLETE));
74
        cur_proc = null;
111
        cur_proc = null;
75
    }
112
    }
76
 
113
 
-
 
114
    /**
-
 
115
     * Move a process from the run_queue to the CPU. This removes it from the
-
 
116
     * run_queue. Then it adds a log entry.
-
 
117
     * 
-
 
118
     * @param proc move this process from the run_queue to the CPU
-
 
119
     */
77
    protected void startProcess (Process proc)
120
    protected void startProcess (Process proc)
78
    {
121
    {
79
        cur_proc = proc;
122
        cur_proc = proc;
80
        cur_proc.started_at = cur_time;
123
        cur_proc.started_at = cur_time;
81
        run_queue.remove (cur_proc);
124
        run_queue.remove (cur_proc);
82
 
125
 
83
        log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.START));
126
        log.add (new LogEntry (cur_proc, cur_time, LogEntry.MsgType.START));
84
    }
127
    }
85
 
128
 
-
 
129
    /**
-
 
130
     * Run the current process. Also increment the current time.
-
 
131
     */
86
    protected void scheduleCurrent ()
132
    protected void scheduleCurrent ()
87
    {
133
    {
88
        cur_proc.time_left--;
134
        cur_proc.time_left--;
89
        cur_time++;
135
        cur_time++;
90
    }
136
    }
91
 
137
 
-
 
138
    /**
-
 
139
     * Print a Gantt Chart detailing the results of this scheduler's run.
-
 
140
     */
92
    protected void printGanttChart ()
141
    protected void printGanttChart ()
93
    {
142
    {
94
        Vector<String> t = new Vector<String> ();
143
        Vector<String> t = new Vector<String> ();
95
        Vector<String> l = new Vector<String> ();
144
        Vector<String> l = new Vector<String> ();
96
 
145
 
Line 117... Line 166...
117
                add2 += " ";
166
                add2 += " ";
118
 
167
 
119
            if (timeline.length() + add1.length() > 79)
168
            if (timeline.length() + add1.length() > 79)
120
            {
169
            {
121
                t.add (timeline + "|");
170
                t.add (timeline + "|");
122
 
-
 
123
                templen = labels.length() - String.valueOf(exptime).length();
171
                templen = labels.length() - String.valueOf(exptime).length();
124
                labels = labels.substring (0, templen+1) + String.valueOf(exptime);
172
                labels = labels.substring (0, templen+1) + String.valueOf(exptime);
125
                
-
 
126
                l.add (labels);
173
                l.add (labels);
-
 
174
 
-
 
175
                /* Clear the current timeline and labels */
127
                timeline = new String();
176
                timeline = new String();
128
                labels = new String();
177
                labels = new String();
129
            }
178
            }
130
 
179
 
131
            timeline += add1;
180
            timeline += add1;
132
            labels += add2;
181
            labels += add2;
133
        }
182
        }
134
 
183
 
135
        /* Done, add the last values of timeline and labels to the Vectors */
184
        /* Done, add the last values of timeline and labels to the Vectors */
136
        t.add (timeline + "|");
185
        t.add (timeline + "|");
137
 
-
 
138
        templen = labels.length() - String.valueOf(exptime).length();
186
        templen = labels.length() - String.valueOf(exptime).length();
139
        labels = labels.substring (0, templen+1) + String.valueOf(exptime);
187
        labels = labels.substring (0, templen+1) + String.valueOf(exptime);
140
        l.add (labels);
188
        l.add (labels);
141
 
189
 
142
        assert (l.size() == t.size());
190
        assert (l.size() == t.size());
Line 147... Line 195...
147
            System.out.println (l.elementAt (i));
195
            System.out.println (l.elementAt (i));
148
            System.out.println ();
196
            System.out.println ();
149
        }
197
        }
150
    }
198
    }
151
 
199
 
152
    public Scheduler ()
-
 
153
    {
200
    /**
154
        this.run_queue = new Vector<Process> ();
201
     * Run the simulation of the scheduler. Note that this only works once,
155
        this.log = new Vector<LogEntry> ();
202
     * if you try to run it twice, it is unlikely that it will work.
156
    }
203
     */
157
 
-
 
158
    public void run ()
204
    public void run ()
159
    {
205
    {
160
        /* Keep step()ing until we're done */
206
        /* Keep step()ing until we're done */
161
        while (step ())
207
        while (step ())
162
            ; // do nothing
208
            ; // do nothing