Subversion Repositories programming

Rev

Rev 415 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
413 ira 1
/*******************************************************************************
2
 * RRScheduler.java
3
 *
4
 * Implementation of a Round-Robin Scheduler for CS431 Project #1.
5
 *
6
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 ******************************************************************************/
26
 
415 ira 27
/**
28
 * A Round-Robin Scheduler.
29
 *
30
 * @class CS431 Fall 2006
31
 * @author Ira W. Snyder (devel@irasnyder.com)
32
 */
33
public class RRScheduler extends Scheduler
413 ira 34
{
415 ira 35
    /** the interval at which to switch processes */
413 ira 36
    protected final int interval;
415 ira 37
 
38
    /** the amount of time that the current process has been running */
413 ira 39
    protected int cur_proc_runtime = 0;
40
 
415 ira 41
    /**
42
     * Constructor for the Round Robin Scheduler class.
43
     */
413 ira 44
    public RRScheduler (int interval)
45
    {
46
        super ();
47
        this.interval = interval;
48
    }
49
 
415 ira 50
    /**
51
     * Emulates a single time unit in a Round Robin Scheduler.
52
     *
53
     * @return false if there is nothing left to do, true otherwise
54
     */
413 ira 55
    protected boolean step ()
56
    {
57
        /* Stop if we have nothing left to do */
417 ira 58
        if (schedulerFinished ())
413 ira 59
            return false;
60
 
417 ira 61
        /* Pull in any new processes */
62
        queueWaitingProcesses ();
63
 
413 ira 64
        /* Get a new process if we need to */
65
        if (cur_proc == null)
66
        {
67
            startProcess (run_queue.firstElement ());
68
            cur_proc_runtime = 0;
69
        }
70
 
71
        /* Run the process */
72
        if (cur_proc_runtime < interval)
73
        {
74
            if (cur_proc.time_left > 0)
75
            {
76
                scheduleCurrent ();
77
                cur_proc_runtime++;
78
            }
79
            else
80
                completeCurrent ();
81
        }
82
        else
83
        {
84
            if (cur_proc.time_left > 0)
85
                expireCurrent ();
86
            else
87
                completeCurrent ();
88
        }
89
 
90
        return true;
91
    }
92
}
93
 
94
/* vim: set ts=4 sts=4 sw=4 expandtab: */
95