Subversion Repositories programming

Rev

Rev 412 | 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
 * SJFScheduler.java
3
 *
4
 * This file holds the implementation of the SJFScheduler class, which is a
5
 * Shortest Job First Scheduler, non-preemptive.
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.Comparator;
412 ira 29
import java.util.Vector;
30
import java.lang.ClassCastException;
31
 
413 ira 32
class SJFScheduler extends Scheduler
412 ira 33
{
413 ira 34
    /**
35
     * An SJFProcess is a Process with these additions:
36
     * 1) It has an add time associated with it
37
     */
38
    private class SJFProcess extends Process
412 ira 39
    {
40
        public final int add_time;
41
 
413 ira 42
        public SJFProcess (String name, int timeslice, int add_time)
412 ira 43
        {
413 ira 44
            super(name, timeslice);
412 ira 45
            this.add_time = add_time;
46
        }
47
    }
48
 
413 ira 49
    private Vector<SJFProcess> wait_queue;
412 ira 50
 
51
    public SJFScheduler ()
52
    {
413 ira 53
        super ();
54
        this.wait_queue = new Vector<SJFProcess> ();
412 ira 55
    }
56
 
57
    public void addProcess (Process proc, int add_time)
58
    {
413 ira 59
        wait_queue.add (new SJFProcess (proc.name, proc.timeslice, add_time));
412 ira 60
    }
61
 
413 ira 62
    private Process findShortest ()
412 ira 63
    {
413 ira 64
        Process shortest = new Process ("fake", Integer.MAX_VALUE);
65
 
66
        for (Process p : run_queue)
67
            if (p.timeslice < shortest.timeslice)
68
                shortest = p;
69
 
70
        return shortest;
412 ira 71
    }
413 ira 72
 
73
    protected boolean step ()
74
    {
75
        /* Leave if we are truly done */
76
        if (cur_proc == null && run_queue.isEmpty () && wait_queue.isEmpty ())
77
            return false;
78
 
79
        /* Create a clone of wait_queue. This is so we can delete things from
80
         * the actual wait_queue, while iterating over it */
81
        Vector<SJFProcess> wq_clone = (Vector<SJFProcess>)wait_queue.clone ();
82
 
83
        /* Pull anything that gets added at this time into run_queue */
84
        for (SJFProcess p : wq_clone)
85
            if (p.add_time == cur_time)
86
            {
87
                wait_queue.remove (p);
88
                run_queue.add (p);
89
            }
90
 
91
        /* If there is no process, then get one! */
92
        if (cur_proc == null)
93
            startProcess (findShortest ());
94
 
95
        /* If the current process is set to run, do so, return false */
96
        if (cur_proc.time_left > 0)
97
            scheduleCurrent ();
98
        else
99
            completeCurrent ();
100
 
101
        return true;
102
    }
412 ira 103
}
104
 
105
/* vim: set ts=4 sts=4 sw=4 expandtab: */
106