Subversion Repositories programming

Rev

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

Rev 413 Rev 415
Line 27... Line 27...
27
 
27
 
28
import java.util.Comparator;
28
import java.util.Comparator;
29
import java.util.Vector;
29
import java.util.Vector;
30
import java.lang.ClassCastException;
30
import java.lang.ClassCastException;
31
 
31
 
-
 
32
/**
-
 
33
 * A Shortest Job First Scheduler
-
 
34
 *
-
 
35
 * @class CS431 Fall 2006
-
 
36
 * @author Ira W. Snyder (devel@irasnyder.com)
-
 
37
 */
32
class SJFScheduler extends Scheduler
38
public class SJFScheduler extends Scheduler
33
{
39
{
34
    /**
40
    /**
35
     * An SJFProcess is a Process with these additions:
41
     * An SJFProcess is a Process with these additions:
36
     * 1) It has an add time associated with it
42
     * 1) It has an add time associated with it
37
     */
43
     */
38
    private class SJFProcess extends Process
44
    private class SJFProcess extends Process
39
    {
45
    {
-
 
46
        /** The time to add this process to the run queue */
40
        public final int add_time;
47
        public final int add_time;
41
 
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
         */
42
        public SJFProcess (String name, int timeslice, int add_time)
57
        public SJFProcess (String name, int timeslice, int add_time)
43
        {
58
        {
44
            super(name, timeslice);
59
            super(name, timeslice);
45
            this.add_time = add_time;
60
            this.add_time = add_time;
46
        }
61
        }
47
    }
62
    }
48
 
63
 
-
 
64
    /** Holds all Processes that will be added to the run queue after a delay */
49
    private Vector<SJFProcess> wait_queue;
65
    private Vector<SJFProcess> wait_queue;
50
 
66
 
-
 
67
    /**
-
 
68
     * Constructor for the Shortest Job First Scheduler.
-
 
69
     */
51
    public SJFScheduler ()
70
    public SJFScheduler ()
52
    {
71
    {
53
        super ();
72
        super ();
54
        this.wait_queue = new Vector<SJFProcess> ();
73
        this.wait_queue = new Vector<SJFProcess> ();
55
    }
74
    }
56
 
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
     */
57
    public void addProcess (Process proc, int add_time)
83
    public void addProcess (Process proc, int add_time)
58
    {
84
    {
59
        wait_queue.add (new SJFProcess (proc.name, proc.timeslice, add_time));
85
        wait_queue.add (new SJFProcess (proc.name, proc.timeslice, add_time));
60
    }
86
    }
61
 
87
 
-
 
88
    /**
-
 
89
     * Find the Process with the shortest timeslice in the run queue.
-
 
90
     *
-
 
91
     * @return the Process with the shortest timeslice
-
 
92
     */
62
    private Process findShortest ()
93
    private Process findShortest ()
63
    {
94
    {
64
        Process shortest = new Process ("fake", Integer.MAX_VALUE);
95
        Process shortest = new Process ("fake", Integer.MAX_VALUE);
65
 
96
 
66
        for (Process p : run_queue)
97
        for (Process p : run_queue)
Line 68... Line 99...
68
                shortest = p;
99
                shortest = p;
69
 
100
 
70
        return shortest;
101
        return shortest;
71
    }
102
    }
72
 
103
 
-
 
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
     */
73
    protected boolean step ()
109
    protected boolean step ()
74
    {
110
    {
75
        /* Leave if we are truly done */
111
        /* Leave if we are truly done */
76
        if (cur_proc == null && run_queue.isEmpty () && wait_queue.isEmpty ())
112
        if (cur_proc == null && run_queue.isEmpty () && wait_queue.isEmpty ())
77
            return false;
113
            return false;