Subversion Repositories programming

Rev

Rev 413 | 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
 
415 ira 32
/**
33
 * A Shortest Job First Scheduler
34
 *
35
 * @class CS431 Fall 2006
36
 * @author Ira W. Snyder (devel@irasnyder.com)
37
 */
38
public class SJFScheduler extends Scheduler
412 ira 39
{
413 ira 40
    /**
41
     * An SJFProcess is a Process with these additions:
42
     * 1) It has an add time associated with it
43
     */
44
    private class SJFProcess extends Process
412 ira 45
    {
415 ira 46
        /** The time to add this process to the run queue */
412 ira 47
        public final int add_time;
48
 
415 ira 49
        /**
50
         * Construct an instance of the SJFProcess class.
51
         *
52
         * @param name the name of this Process
53
         * @param timeslice this Process' burst time
54
         * @param add_time the time when this process will be added
55
         *                 to the run queue
56
         */
413 ira 57
        public SJFProcess (String name, int timeslice, int add_time)
412 ira 58
        {
413 ira 59
            super(name, timeslice);
412 ira 60
            this.add_time = add_time;
61
        }
62
    }
63
 
415 ira 64
    /** Holds all Processes that will be added to the run queue after a delay */
413 ira 65
    private Vector<SJFProcess> wait_queue;
412 ira 66
 
415 ira 67
    /**
68
     * Constructor for the Shortest Job First Scheduler.
69
     */
412 ira 70
    public SJFScheduler ()
71
    {
413 ira 72
        super ();
73
        this.wait_queue = new Vector<SJFProcess> ();
412 ira 74
    }
75
 
415 ira 76
    /**
77
     * Add a process to the wait queue, so that they can be pulled into the
78
     * run queue at the appropriate time.
79
     *
80
     * @param proc the Process to add
81
     * @param add_time the time when the Process is to be added
82
     */
412 ira 83
    public void addProcess (Process proc, int add_time)
84
    {
413 ira 85
        wait_queue.add (new SJFProcess (proc.name, proc.timeslice, add_time));
412 ira 86
    }
87
 
415 ira 88
    /**
89
     * Find the Process with the shortest timeslice in the run queue.
90
     *
91
     * @return the Process with the shortest timeslice
92
     */
413 ira 93
    private Process findShortest ()
412 ira 94
    {
413 ira 95
        Process shortest = new Process ("fake", Integer.MAX_VALUE);
96
 
97
        for (Process p : run_queue)
98
            if (p.timeslice < shortest.timeslice)
99
                shortest = p;
100
 
101
        return shortest;
412 ira 102
    }
413 ira 103
 
415 ira 104
    /**
105
     * Emulate a single time unit in a Shortest Job First scheduler.
106
     *
107
     * @return false if the scheduler is finished, true otherwise
108
     */
413 ira 109
    protected boolean step ()
110
    {
111
        /* Leave if we are truly done */
112
        if (cur_proc == null && run_queue.isEmpty () && wait_queue.isEmpty ())
113
            return false;
114
 
115
        /* Create a clone of wait_queue. This is so we can delete things from
116
         * the actual wait_queue, while iterating over it */
117
        Vector<SJFProcess> wq_clone = (Vector<SJFProcess>)wait_queue.clone ();
118
 
119
        /* Pull anything that gets added at this time into run_queue */
120
        for (SJFProcess p : wq_clone)
121
            if (p.add_time == cur_time)
122
            {
123
                wait_queue.remove (p);
124
                run_queue.add (p);
125
            }
126
 
127
        /* If there is no process, then get one! */
128
        if (cur_proc == null)
129
            startProcess (findShortest ());
130
 
131
        /* If the current process is set to run, do so, return false */
132
        if (cur_proc.time_left > 0)
133
            scheduleCurrent ();
134
        else
135
            completeCurrent ();
136
 
137
        return true;
138
    }
412 ira 139
}
140
 
141
/* vim: set ts=4 sts=4 sw=4 expandtab: */
142