#fcfs and sjn with Gantt chart code

1 messages ยท Page 1 of 1 (latest)

crimson gyro
#

could anyone help me with this? thank you very much

whole egretBOT
#

<@&987246399047479336> please have a look, thanks.

whole egretBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Sure, I can help you with that. Here's an example of how you can implement the FCFS (First-Come, First-Served) and SJN (Shortest Job Next) scheduling algorithms in Java along with a Gantt chart representation:

#
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Process {
    private String name;
    private int arrivalTime;
    private int burstTime;

    public Process(String name, int arrivalTime, int burstTime) {
        this.name = name;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
    }

    public String getName() {
        return name;
    }

    public int getArrivalTime() {
        return arrivalTime;
    }

    public int getBurstTime() {
        return burstTime;
    }
}

class GanttChartEntry {
    private String processName;
    private int startTime;
    private int endTime;

    public GanttChartEntry(String processName, int startTime, int endTime) {
        this.processName = processName;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public String getProcessName() {
        return processName;
    }

    public int getStartTime() {
        return startTime;
    }

    public int getEndTime() {
        return endTime;
    }
}

public class SchedulingAlgorithm {

  // FCFS Scheduling Algorithm
  public static List<GanttChartEntry> fcfs(List<Process> processes) {
      List<GanttChartEntry> ganttChart = new ArrayList<>();
      Collections.sort(processes, Comparator.comparingInt(Process::getArrivalTime));

      int currentTime = 0;

      for (Process process : processes) {
          if (currentTime < process.getArrivalTime()) { // Idle time
              ganttChart.add(new GanttChartEntry("Idle", currentTime, process.getArrivalTime()));
              currentTime = process.getArrivalTime();
          }
          ganttChart.add(new GanttChartEntry(process.getName(), currentTime, currentTime + process.getBurstTime()));
          currentTime += process.getBurstTime();
      }

      return ganttChart;
  }
#
  // SJN Scheduling Algorithm
  public static List<GanttChartEntry> sjn(List<Process> processes) {
      List<GanttChartEntry> ganttChart = new ArrayList<>();
      Collections.sort(processes, Comparator.comparingInt(Process::getArrivalTime).thenComparingInt(Process::getBurstTime));

      int currentTime = 0;

      for (Process process : processes) {
          if (currentTime < process.getArrivalTime()) { // Idle time
              ganttChart.add(new GanttChartEntry("Idle", currentTime, process.getArrivalTime()));
              currentTime = process.getArrivalTime();
          }
          ganttChart.add(new GanttChartEntry(process.getName(), currentTime, currentTime + process.getBurstTime()));
          currentTime += process.getBurstTime();
      }

      return ganttChart;
  }

  public static void main(String[] args) {
    List<Process> processes = new ArrayList<>();
    processes.add(new Process("P1", 0, 6));
    processes.add(new Process("P2", 1, 4));
    processes.add(new Process("P3", 2, 8));
    processes.add(new Process("P4", 3, 1));

    System.out.println("FCFS Scheduling:");
    List<GanttChartEntry> fcfsGantt = fcfs(processes);
    printGantt(fcfsGantt);

    System.out.println("\nSJN Scheduling:");
    List<GanttChartEntry> sjnGantt = sjn(processes);
    printGantt(sjnGantt);
  }

  public static void printGantt(List<GanttChartEntry> ganttChart) {
    System.out.println("-------------------------------------------------");
    System.out.println("| Process | Start Time | End Time |");
    System.out.println("-------------------------------------------------");

    for (GanttChartEntry entry : ganttChart) {
        System.out.printf("|   %s   |     %d      |     %d    |\n", entry.getProcessName(), entry.getStartTime(), entry.getEndTime());
        System.out.println("-------------------------------------------------");
    }
  }
}
#

This code defines a Process class to represent each process with its name, arrival time, and burst time. The GanttChartEntry class represents each entry in the Gantt chart with the process name, start time, and end time.

The fcfs method implements the FCFS scheduling algorithm by sorting the processes based on their arrival times and then iterating over them to calculate the start and end times for each process. The same logic applies to the sjn method, but it additionally sorts the processes based on their burst times.

In the main method, we create a list of processes and call both scheduling algorithms (fcfs and sjn). Finally, we print the Gantt chart using the printGantt method.

Hope this helps! Let me know if you have any further questions.

scenic pasture
#

Either you read the bot answer, or if you want more specific support, u should provide more specific informationen of what u dont understand