Subversion Repositories programming

Rev

Rev 415 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 415 Rev 417
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;
-
 
29
import java.util.Vector;
-
 
30
import java.lang.ClassCastException;
-
 
31
 
-
 
32
/**
28
/**
33
 * A Shortest Job First Scheduler
29
 * A Shortest Job First Scheduler
34
 *
30
 *
35
 * @class CS431 Fall 2006
31
 * @class CS431 Fall 2006
36
 * @author Ira W. Snyder (devel@irasnyder.com)
32
 * @author Ira W. Snyder (devel@irasnyder.com)
37
 */
33
 */
38
public class SJFScheduler extends Scheduler
34
public class SJFScheduler extends Scheduler
39
{
35
{
40
    /**
36
    /**
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
-
 
45
    {
-
 
46
        /** The time to add this process to the run queue */
-
 
47
        public final int add_time;
-
 
48
 
-
 
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
         */
-
 
57
        public SJFProcess (String name, int timeslice, int add_time)
-
 
58
        {
-
 
59
            super(name, timeslice);
-
 
60
            this.add_time = add_time;
-
 
61
        }
-
 
62
    }
-
 
63
 
-
 
64
    /** Holds all Processes that will be added to the run queue after a delay */
-
 
65
    private Vector<SJFProcess> wait_queue;
-
 
66
 
-
 
67
    /**
-
 
68
     * Constructor for the Shortest Job First Scheduler.
-
 
69
     */
-
 
70
    public SJFScheduler ()
-
 
71
    {
-
 
72
        super ();
-
 
73
        this.wait_queue = new Vector<SJFProcess> ();
-
 
74
    }
-
 
75
 
-
 
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
     */
-
 
83
    public void addProcess (Process proc, int add_time)
-
 
84
    {
-
 
85
        wait_queue.add (new SJFProcess (proc.name, proc.timeslice, add_time));
-
 
86
    }
-
 
87
 
-
 
88
    /**
-
 
89
     * Find the Process with the shortest timeslice in the run queue.
37
     * Find the Process with the shortest timeslice in the run queue.
90
     *
38
     *
91
     * @return the Process with the shortest timeslice
39
     * @return the Process with the shortest timeslice
92
     */
40
     */
93
    private Process findShortest ()
41
    private Process findShortest ()
Line 107... Line 55...
107
     * @return false if the scheduler is finished, true otherwise
55
     * @return false if the scheduler is finished, true otherwise
108
     */
56
     */
109
    protected boolean step ()
57
    protected boolean step ()
110
    {
58
    {
111
        /* Leave if we are truly done */
59
        /* Leave if we are truly done */
112
        if (cur_proc == null && run_queue.isEmpty () && wait_queue.isEmpty ())
60
        if (schedulerFinished ())
113
            return false;
61
            return false;
114
 
62
 
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)
63
        /* Pull in any new processes */
122
            {
-
 
123
                wait_queue.remove (p);
-
 
124
                run_queue.add (p);
64
        queueWaitingProcesses ();
125
            }
-
 
126
 
65
 
127
        /* If there is no process, then get one! */
66
        /* If there is no process, then get one! */
128
        if (cur_proc == null)
67
        if (cur_proc == null)
129
            startProcess (findShortest ());
68
            startProcess (findShortest ());
130
 
69