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.*;
-
 
29
import java.lang.ClassCastException;
-
 
30
 
-
 
31
class FCFSScheduler implements Scheduler
28
class FCFSScheduler extends Scheduler
32
{
29
{
33
    private class FCFSProcess implements Comparable
-
 
34
    {
30
    /*
35
        public final Process proc;
-
 
36
        public final int add_time;
-
 
37
 
-
 
38
        public FCFSProcess (Process proc, int add_time)
-
 
39
        {
-
 
40
            this.proc = proc;
-
 
41
            this.add_time = add_time;
-
 
42
        }
-
 
43
 
-
 
44
        // negative if this < rhs
-
 
45
        // positive if this > rhs
-
 
46
        public int compareTo (Object o)
-
 
47
        {
-
 
48
            if (! (o instanceof FCFSProcess))
-
 
49
                throw new ClassCastException();
-
 
50
 
-
 
51
            FCFSProcess rhs = (FCFSProcess)o;
-
 
52
 
-
 
53
            return this.add_time - rhs.add_time;
-
 
54
        }
-
 
55
    }
-
 
56
 
-
 
57
    private Vector<FCFSProcess> processes;
-
 
58
 
-
 
59
    public FCFSScheduler ()
-
 
60
    {
-
 
61
        processes = new Vector<FCFSProcess> ();
-
 
62
    }
-
 
63
 
-
 
64
    public void addProcess (Process proc, int add_time)
31
    public void addProcess (Process proc, int add_time)
65
    {
32
    {
66
        processes.add (new FCFSProcess (proc, add_time));
33
        run_queue.add (proc);
67
    }
34
    }
-
 
35
    */
68
 
36
 
69
 
-
 
70
    public void run ()
37
    protected boolean step ()
71
    {
38
    {
-
 
39
        /* Check if we're completely finished */
-
 
40
        if (cur_proc == null && run_queue.isEmpty ())
72
        int starttime = 0;
41
            return false;
-
 
42
 
-
 
43
        /* Check if we have a process, if not, load one */
73
        int endtime = 0;
44
        if (cur_proc == null)
-
 
45
            startProcess (run_queue.elementAt (0));
74
 
46
 
75
        /* Sort the process list by time added */
47
        /* If we're able, run the current process */
-
 
48
        if (cur_proc.time_left > 0)
-
 
49
            scheduleCurrent ();
-
 
50
        else
76
        Collections.sort (processes);
51
            completeCurrent ();
77
 
52
 
78
        for (FCFSProcess p : processes)
-
 
79
        {
-
 
80
            endtime = starttime + p.proc.timeslice;
-
 
81
            System.out.printf ("%s start=%d end=%d\n", p.proc.name, starttime, endtime-1);
-
 
82
            starttime = endtime;
53
        return true;
83
        }
-
 
84
    }
54
    }
85
}
55
}
86
 
56
 
87
/* vim: set ts=4 sts=4 sw=4 expandtab: */
57
/* vim: set ts=4 sts=4 sw=4 expandtab: */
88
 
58