FCFS调度算法中的等待时间

时间:2014-12-03 20:11:06

标签: java algorithm

我写了FCFS算法,但是当我想为第二个过程计算waiting time时,有一个错误。

我知道等待时间的计算方法是:

第一个流程的等待时间:0

对于其他人:

Waiting time[i] = FinishedTime[i] - arrivalTime[i] - BurstTime[i]

这是我的代码:

public class FCFS2 {

static int[] burstTimes = new int[3];
static int[] arrivalTimes = new int[3];
static Scanner in = new Scanner(System.in);
static int[] processes = new int[3];
static int[] waitingTimes = new int[3];
static int[] finishedTimes = new int[3];

public static void main(String[] args) {
    System.out.println("CPU Scheduling Algorithm - First Come First Serve");

    //read Arrival times
    for (int i = 0; i < 3; i++) {
        System.out.print("Enter Arrival time for process " + (i + 1) + ": ");
        arrivalTimes[i] = in.nextInt();
        processes[i] = i;
    }
    System.out.println();

    //read burst times
    for (int i = 0; i < 3; i++) {
        System.out.print("Enter Burst time for process " + (i + 1) + ": ");
        burstTimes[i] = in.nextInt();
    }

    // Sort by arrival Times
    for (int i = 0; i <= 3; i++) {
        for (int j = i + 1; j < 3; j++)
            if (arrivalTimes[i] > arrivalTimes[j]) {
                // swap in arrival times
                int tempAr = arrivalTimes[i];
                arrivalTimes[i] = arrivalTimes[j];
                arrivalTimes[j] = tempAr;

                //swap in burst times too
                int tempBr = burstTimes[i];
                burstTimes[i] = burstTimes[j];
                burstTimes[j] = tempBr;
            }
    }

    // calculate finished times
    finishedTimes[0] = arrivalTimes[0] + burstTimes[0];
    for (int k = 1; k < 3; k++) {
        finishedTimes[k] = finishedTimes[k - 1] + burstTimes[k];
    }
    System.out.println("\nArrival Times:");
    for (int k = 0; k < 3; k++) {
        System.out.print(arrivalTimes[k] + "  |  ");
    }
    System.out.println("\nBurst Times:");
    for (int k = 0; k < 3; k++) {
        System.out.print(burstTimes[k] + "  |  ");
    }

    System.out.println("\nFinished Times:");
    for (int k = 0; k < 3; k++) {
        System.out.print(finishedTimes[k] + "  |  ");
    }

    //calculate waiting times for each process
    waitingTimes[0] = 0;
    for (int i = 1; i < 3; i++) {
        waitingTimes[i] = finishedTimes[i] - arrivalTimes[i] - burstTimes[i];
    }
    System.out.println("\nWaiting Times:");
    for (int i = 0; i < 3; i++) {
        System.out.print(waitingTimes[i] + "  |  ");
    }
}

}

输出:

Enter Arrival time for process 1: 3
Enter Arrival time for process 2: 1
Enter Arrival time for process 3: 5

Enter Burst time for process 1: 5
Enter Burst time for process 2: 6
Enter Burst time for process 3: 2

Arrival Times:
1  |  3  |  5  |  
Burst Times:
6  |  5  |  2  |  
Finished Times:
7  |  12  |  14  |  
Waiting Times:
0  |  4  |  7  |  

但等待时间应为:0,5,7

2 个答案:

答案 0 :(得分:0)

由于我们使用Java,我将尝试使用面向对象的方式来解决这个问题。我们可以将等待时间计算为

waitingTime[i] = serviceTime - arrivalTime[i];
where serviceTime is actually current time.

在这里,我创建了一个Job类来保存每个作业信息和方法,以便按到达时间进行排序。

public class Job implements Comparable<Job>{
   private String id;
   private int arrivalTime;
   private int burstTime;

   //Getter and setters

   @Override
   public int compareTo(Job job) {
    int compareArrivalTime = ((Job) job).getArrivalTime();
    return this.arrivalTime - compareArrivalTime;
   }
}

然后使用所有相关的其他方法的主要方法。

public static void main(String[] args) {
    List<Job> jobList  = generateDummyJobs();
    Collections.sort(jobList);
    printJobs(jobList);
    calculateWaitTime(jobList);       
}

private static List<Job> generateDummyJobs() {
    List<Job> jobList = new ArrayList<Job>();
    Job job1 = new Job("P1", 3, 5);
    Job job2 = new Job("P2", 1, 6);
    Job job3 = new Job("P3", 5, 2);
    jobList.add(job1);
    jobList.add(job2);
    jobList.add(job3);
    return jobList;       
}

private static void printJobs(List<Job> jobList) {
    for(Job job :jobList) {
        System.out.println("[" + job.getId() + ", " + job.getArrivalTime() + ", " + job.getBurstTime() + "]" );    
    }
}

private static void calculateWaitTime(List<Job> jobList) {
    int serviceTime = 1;
    for(Job job :jobList) {
        System.out.println(job.getId() + ": " + (serviceTime - job.getArrivalTime()));
        serviceTime += job.getBurstTime();
    }
}

答案 1 :(得分:0)

您在等待时间计算错误的原因是因为for (int k = 1; k < 3; k++) {finishedTimes[k] = finishedTimes[k - 1] + burstTimes[k];}已初始化为1但应为0。

                static int[] burstTimes = new int[5];
                static int[] arrivalTimes = new int[5];
                static Scanner in = new Scanner(System.in);
                static int[] processes = new int[5];
                static int[] waitingTimes = new int[5];
                static int[] finishedTimes = new int[5];
                static int[] turnAroundTime = new int[5];

                public static void main(String[] args) {
                    System.out.println("CPU Scheduling Algorithm - First Come First Serve");

                    //read Arrival times
                    for (int i = 0; i < 5; i++) {
                        System.out.print("Enter Arrival time for process " + (i + 1) + ": ");
                        arrivalTimes[i] = in.nextInt();
                        processes[i] = i;
                    }
                    System.out.println();

                    //read burst times
                    for (int i = 0; i < 5; i++) {
                        System.out.print("Enter Burst time for process " + (i + 1) + ": ");
                        burstTimes[i] = in.nextInt();
                    }

                    // Sort by arrival Times
                    for (int i = 0; i <= 5; i++) {
                        for (int j = i + 1; j < 5; j++)
                            if (arrivalTimes[i] > arrivalTimes[j]) {
                                // swap in arrival times
                                int tempAr = arrivalTimes[i];
                                arrivalTimes[i] = arrivalTimes[j];
                                arrivalTimes[j] = tempAr;

                                //swap in burst times too
                                int tempBr = burstTimes[i];
                                burstTimes[i] = burstTimes[j];
                                burstTimes[j] = tempBr;
                            }
                    }

                    // calculate finished times
                    finishedTimes[0] = arrivalTimes[0] + burstTimes[0];
                    for (int k = 1; k < 5; k++) {
                        finishedTimes[k] = finishedTimes[k - 1] + burstTimes[k];
                    }
                    System.out.println("\nArrival Times:");
                    for (int k = 0; k < 5; k++) {
                        System.out.print(arrivalTimes[k] + "  |  ");
                    }
                    System.out.println("\nBurst Times:");
                    for (int k = 0; k < 5; k++) {
                        System.out.print(burstTimes[k] + "  |  ");
                    }

                    System.out.println("\nFinished Times:");
                    for (int k = 0; k < 5; k++) {
                        System.out.print(finishedTimes[k] + "  |  ");
                    }



                    //calculate waiting times for each process
                    waitingTimes[0] = 0;
                    turnAroundTime[0] = 0;
                    for (int i = 0; i < 5; i++) {
                        turnAroundTime[i] = finishedTimes[i] - arrivalTimes[i];
                        waitingTimes[i] = turnAroundTime[i] - burstTimes[i];
                    }

                    System.out.println("\nTurnAround Times:");
                    for (int k = 0; k < 5; k++) {
                        System.out.print(turnAroundTime[k] + "  |  ");
                    }
                    System.out.println("\nWaiting Times:");
                    for (int i = 0; i < 5; i++) {
                        System.out.print(waitingTimes[i] + "  |  ");
                    }
                }