1

The problem I am facing is that I can not find a way to calculate the start time (called startedAt in the code), so I am able to calculate the turnAround times and the wait times. The logic I am using is the following:

  1. we first make a copy of the burst times "BurstTime" to store the remaining burst times remBurstTime.
  2. sort the processes according to their priorities (a lower priority number means higher priority )
  3. the element that arrives first will run until it reaches the arrival time of the process with the higher priority, i.e., we subtract the arrival time of the latter process from the burst time of the former process.
  4. run FCFS.

The processes class

class ProcessInfo{
    public int ArrivalTime;
    public int BurstTime ;
    public int remBurstTime ;
    public int Priority ;
    public int TurnAroundTime;
    public int CompletionTime;
    public double WaitTime;

    
    public ProcessInfo(int ArrivalTime , int BurstTime , int priority){
        this.ArrivalTime = ArrivalTime ;
        this.BurstTime = BurstTime ;
        this.Priority = priority ;
    }


}

The Preemptive priority method

public static void PriorityPreemptive(ProcessInfo[] process) {


        for (ProcessInfo processInfo : process) processInfo.remBurstTime = processInfo.BurstTime;

        for (int i = 0; i < process.length; i++) {

            for (int j = i + 1; j < process.length; ++j) {


                if (process[i].Priority > process[j].Priority) {
                    ProcessInfo temp = process[j];
                    process[j] = process[i];
                    process[i] = temp;
                    if (process[i].ArrivalTime < process[j].ArrivalTime) {
                        process[i].remBurstTime = process[i].BurstTime - process[j].ArrivalTime ;

                    }
                }


            }

        }

The FCFS class.

public static void FCFS(ProcessInfo[] process) {


        double TurnAroundTime = 0;
        double TotalTime = 0;
        int StartedAt = 0;

        if (process[0].ArrivalTime != StartedAt) {
            StartedAt = process[0].ArrivalTime;//initialize the startedAt time
        }

        //Now let's compute the avgTime and turnAroundTime of each process
        for (ProcessInfo processInfo : process) {

            processInfo.CompletionTime = StartedAt + processInfo.remBurstTime;//completion time (remBurst is the remaining burst time)
            StartedAt = processInfo.CompletionTime; // here is the issue

            processInfo.TurnAroundTime = processInfo.CompletionTime - processInfo.ArrivalTime;
            processInfo.WaitTime = processInfo.TurnAroundTime - processInfo.BurstTime;
            TotalTime += processInfo.WaitTime; // Calculate the total turnover time, and the value stored in avgTotalTime is the total turnover time
            TurnAroundTime += processInfo.TurnAroundTime; // Calculate the total weighted turnover time, and the value stored in avgPowerTime is the total weighted turnover time
            System.out.println("Started :\t " + StartedAt + "  Completion :\t " + processInfo.CompletionTime + "  TurnAround :\t "+ processInfo.TurnAroundTime + "  WaitTime :\t"+ processInfo.WaitTime);
        }

        double avgWaitingTime = TotalTime /  process.length;
        double avgTurnAroundTime =  TurnAroundTime /  process.length;



        System.out.println("The Average Waiting Time is :\t" + avgWaitingTime + "\nThe Average TurnAroundTime is :\t " + avgTurnAroundTime);
}

The results I am getting with this Algo are pretty close, I just need to find the right way to calculate StartedAt so that other times (waiting time, turnaround time) become correct.

I hope you can help me with this, thanks in advance.

  • I suspect that very few here are involved, or interested, in batch scheduling algorithms. I have never developed an app, (apart from trivial ulilities), that was not designed to run continuously until terminated by a user:( – Martin James Nov 05 '21 at 17:03
  • ...I could, of course, be wrong. Is anyone out there working on job/batch scheduling? – Martin James Nov 05 '21 at 17:05

0 Answers0