Subversion Repositories programming

Rev

Rev 412 | Rev 415 | 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.Comparator;
28
import java.util.Vector;
29
import java.util.Vector;
29
import java.lang.ClassCastException;
30
import java.lang.ClassCastException;
30
 
31
 
31
class SJFScheduler implements Scheduler
32
class SJFScheduler extends Scheduler
32
{
33
{
-
 
34
    /**
-
 
35
     * An SJFProcess is a Process with these additions:
-
 
36
     * 1) It has an add time associated with it
-
 
37
     */
33
    private class SJFProcess implements Comparable
38
    private class SJFProcess extends Process
34
    {
39
    {
35
        public final Process proc;
-
 
36
        public final int add_time;
40
        public final int add_time;
37
 
41
 
38
        public SJFProcess (Process proc, int add_time)
42
        public SJFProcess (String name, int timeslice, int add_time)
39
        {
43
        {
40
            this.proc = proc;
44
            super(name, timeslice);
41
            this.add_time = add_time;
45
            this.add_time = add_time;
42
        }
46
        }
43
 
-
 
44
        // negative if this < rhs
-
 
45
        // positive if this > rhs
-
 
46
        public int compareTo (Object o)
-
 
47
        {
-
 
48
            if (! (o instanceof SJFProcess))
-
 
49
                throw new ClassCastException();
-
 
50
 
-
 
51
            SJFProcess rhs = (SJFProcess)o;
-
 
52
 
-
 
53
            return this.add_time - rhs.add_time;
-
 
54
        }
-
 
55
    }
47
    }
56
 
48
 
57
    private Vector<SJFProcess> processes;
49
    private Vector<SJFProcess> wait_queue;
58
 
50
 
59
    public SJFScheduler ()
51
    public SJFScheduler ()
60
    {
52
    {
-
 
53
        super ();
61
        this.processes = new Vector<SJFProcess> ();
54
        this.wait_queue = new Vector<SJFProcess> ();
62
    }
55
    }
63
 
56
 
64
    public void addProcess (Process proc, int add_time)
57
    public void addProcess (Process proc, int add_time)
65
    {
58
    {
-
 
59
        wait_queue.add (new SJFProcess (proc.name, proc.timeslice, add_time));
66
    }
60
    }
67
 
61
 
68
    public void run ()
62
    private Process findShortest ()
69
    {
63
    {
-
 
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;
-
 
71
    }
-
 
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;
70
    }
102
    }
71
}
103
}
72
 
104
 
73
/* vim: set ts=4 sts=4 sw=4 expandtab: */
105
/* vim: set ts=4 sts=4 sw=4 expandtab: */
74
 
106