显示甘特图Java CPU调度

时间:2015-11-26 17:01:14

标签: java gantt-chart round-robin

我一直在尝试使用我的程序输出创建一个甘特图,但我似乎无法弄清楚如何做到这一点。这是一个循环的Robin CPU调度程序。

编辑:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class Display
{

/**
 * Display CPU Scheduling results
 * 
 * @param simulation
 *            Display Process table and Processes Execution Sequence
 */
public static void simulation()
{
    ArrayList<Pair> pairs = new ArrayList<Pair>();
    // Before starting the simulation, print first the generated processes
    Iterator<Process> it = RoundRobin.processes.iterator();

    RoundRobin.ps.println("--------------+---------------+--------------");
    RoundRobin.ps.println("ROUND ROBIN SIMULATION RESULT");
    RoundRobin.ps.println("--------------+---------------+--------------");
    RoundRobin.ps.println("TIME QUANTUM: " + RoundRobin.timeQuantum);
    RoundRobin.ps.println("--------------+---------------+--------------");
    RoundRobin.ps.printf("%27s%n", "PROCESSES");
    RoundRobin.ps.println("--------------+---------------+--------------");
    RoundRobin.ps.println("Process Name  | Arrival Time | Burst Time   |");

    while (it.hasNext())
    {
        Process process = it.next();
        RoundRobin.ps.printf("%-14s%1s%-13d%1s%-13s%1s%n", process.getName(), "| ", process.getArrivalTime(), "| ",
                process.getBurstTime(), "|");
    }
    RoundRobin.ps.println("--------------+---------------+--------------");
    RoundRobin.ps.println();

    RoundRobin.ps.printf("%10s%n", "|===========================| GANTT CHART |===========================|");

    CPU cpu = new CPU();
    Queue<Process> waitingQueue = new LinkedList<Process>();

    while (true)
    {
        // check if there are processes that has arrived and add them to the
        // waiting queue
        LinkedList<Process> arrivedProcesses = RoundRobin.getArrivedProcessed(cpu.getTime());

        it = arrivedProcesses.iterator();

        while (it.hasNext())
            waitingQueue.add(it.next());

        // check if the CPU is busy
        if (!cpu.isProcessing())
        {
            // if the CPU is not busy then get one of the process in the
            // waiting queue to be processed
            if (!waitingQueue.isEmpty())
                cpu.acceptProcess(waitingQueue.poll());
            else
                // if the waiting queue is empty then that means that the
                // CPU is idle
                pairs.add(new Pair(cpu.getTime(), "IDLE"));
        }

        // continue the process of the CPU
        if (cpu.isProcessing())
        {
            cpu.doProcess();
            pairs.add(new Pair(cpu.getTime(), cpu.getProcess().getName()));

            // check if the CPU has Finished processing the previous
            // proccess, if so continue to the next process in queue.
            if (cpu.hasFinishedProcess())
            {
                RoundRobin.finishedProcesses.add(cpu.getProcess());

                cpu.clearProcess();

                if (!waitingQueue.isEmpty())
                    cpu.acceptProcess(waitingQueue.poll());
            } else
            {
                // context switching
                Process process = cpu.getProcess();

                if (process.getProcessedBurstTime() % RoundRobin.timeQuantum == 0)
                {
                    // context switch by removing the current process and
                    // sending it back to the end of the ready queue.
                    cpu.clearProcess();
                    // RoundRobin.ps.println("\n");
                    waitingQueue.add(process);

                    // if there is a process assign it to the waiting queue.
                    if (!waitingQueue.isEmpty())
                        cpu.acceptProcess(waitingQueue.poll());
                }
            }
        }

        // verify if all processes have finished processing.
        if (waitingQueue.isEmpty() && !cpu.isProcessing() && RoundRobin.processes.isEmpty())

            break;

        cpu.incrementTime();
        RoundRobin.increaseWaitingTimeToWaitingProcesses(waitingQueue);

    }

    // Displays Gantt chart
    Iterator<Pair> pairIt = pairs.iterator();
    String first = "";
    String second = "";

    while (pairIt.hasNext())
    {
        Pair current = pairIt.next();

        first += "| " + current.getProcessState() + " |";
        second += "  " + current.getTime() + "  ";

        if (pairIt.hasNext())
        {
            first += " ====== ";
            second += "        ";
        }
    }

    RoundRobin.ps.println(first);
    RoundRobin.ps.println(second);

    RoundRobin.ps.printf("%n%10s", "|===========================|============|===========================|");

    RoundRobin.ps.println();
    RoundRobin.ps.println();
    RoundRobin.ps.println("Average Waiting Time: \t\t" + RoundRobin.getAverageWaitingTime());
    RoundRobin.ps.println("Average Turn Around Time: \t" + RoundRobin.getAverateTurnAroundTime());
    RoundRobin.ps.printf("%-1s%.0f%1s%n", "Utilization Rate: \t\t", RoundRobin.getUtilizationRate(cpu), "%");
    RoundRobin.ps.printf("%-1s%.2f%1s%n", "Throughput: \t\t\t", RoundRobin.getThroughput(cpu),
            " processes per unit time");
    RoundRobin.ps.println();
    RoundRobin.ps.flush();
}
}
/**
* Saves process State in an ArrayList to output after simulation ends.
* 
* @param Pair
*            represents one pair (time and processState) and keep these pairs
*            in an ArrayList
*/
class Pair
{
private String  processState;
private int     time;

public Pair(int time, String processState)
{
    this.time = time;
    this.processState = processState;
}

public String getProcessState()
{
    return this.processState;
}

public int getTime()
{
    return this.time;
}
}
{
    return this.processState;
}

public int getTime()
{
    return this.time;
}

}

上面的代码输出以下内容:

|===========================| GANTT CHART |===========================|

| P1 | ====== | P1 | ====== | P1 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P3 | ====== | P3 | ====== | P3 | ====== | P3 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P4 | ====== | P4 | ====== | P4 | ====== | P4 | ====== | P3 | ====== | P3 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P4 | ====== | P4 | ====== | P4 | ====== | P4 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P4 | ====== | P4 | ====== | P4 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P2 | ====== | P5 | ====== | P2 | ====== | P2 |
  0            1            2            3            4            5            6            7            8            9            10            11            12            13            14            15            16            17            18            19            20            21            22            23            24            25            26            27            28            29            30            31            32            33            34            35            36            37            38            39            40            41            42            43            44            45            46            47            48            49            50            51            52            53            54            55            56            57            58            59            60            61            62            63            64            65            66  

|===========================|============|===========================|

我不能不想要图表中的重复过程。而不是显示3个连续的过程P1 P1 P1我只需要一个P1。凭借其CPU时钟时间。如下所示。

|===================================================================================================| GANTT CHART |==========================================================================================================================================================|

| P1 | ====== | P2 | ====== | P3 | ====== | P2  | ====== | P4 | ====== | P3 | ====== | P2 | ====== | P5 | ====== | P4 | ====== | P2 | ====== | P5 | ====== | P4 | ====== | P2 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== | P5 | ====== |
0             3             7             11             15            19            21            25            29            33            37            41            44            48            52            56            60            64            65            66    

|===================================================================================================|============|===========================================================================================================================================================|

可以找到完整的源代码here.

1 个答案:

答案 0 :(得分:1)

如果不是立即显示结果,我建议创建一个代表一对(time和processState)的小类,并在模拟运行时将这些对保留在ArrayList中。最后,您可以简单地遍历此列表并创建甘特图的两行。

小班可能看起来像这样:

private class Pair {
    private String processState;
    private int time;

    public Pair(int time, String processState) {
        this.time = time;
        this.processState = processState;
    }

    public int getTime() {
        return this.time;
    }

    public String getProcessState() {
        return this.processState;
    }
}

可以作为私人课程加入“展示”课程。在'模拟'方法的开头,你会把:

ArrayList<Pair> pairs = new ArrayList<Pair>();

现在用:

替换每个'logTime()'调用
pairs.add(new Pair(time, processState));

计算完成后你可以做某事。那样:

Iterator<Pair> pairIt = pairs.iterator();
String first = "";
String second = "";

while(pairIt.hasNext()) {
    Pair current = pairIt.next();

    first += "| " + current.getProcessState() + " |";
    second += "  " + current.getTime() + "  ";

    if(pairIt.hasNext()) {
        first += " ====== ";
        second += "        ";
    }
}

RoundRobin.ps.printf(first);
RoundRobin.ps.printf(second);

当然,它没有考虑'processState'或'time'的不同长度,但它应该让你知道它是如何工作的。然后,您可以根据“第一个”或“第二个”的长度调整标题的长度('===== |甘特图| ====')。