From c59fc923bf29a511fac513d33a81c364ebd59a54 Mon Sep 17 00:00:00 2001 From: Smiti Maheshwari Date: Thu, 13 Oct 2022 00:09:34 +0530 Subject: [PATCH] Add fcfs scheduling (#3335) Co-authored-by: Smiti --- .../devutils/entities/ProcessDetails.java | 56 +++++++++++++++++++ .../scheduling/FCFSScheduling.java | 47 ++++++++++++++++ .../scheduling/FCFSSchedulingTest.java | 50 +++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java create mode 100644 src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java create mode 100644 src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java diff --git a/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java b/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java new file mode 100644 index 00000000..873e4636 --- /dev/null +++ b/src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java @@ -0,0 +1,56 @@ +package com.thealgorithms.devutils.entities; + +public class ProcessDetails { + private String processId; + private int arrivalTime; + private int burstTime; + private int waitingTime; + private int turnAroundTime; + + public ProcessDetails(final String processId, final int arrivalTime, final int burstTime) { + this.processId = processId; + this.arrivalTime = arrivalTime; + this.burstTime = burstTime; + } + + public String getProcessId() { + return processId; + } + + public int getArrivalTime() { + return arrivalTime; + } + + public int getBurstTime() { + return burstTime; + } + + + public int getWaitingTime() { + return waitingTime; + } + + public int getTurnAroundTimeTime() { + return turnAroundTime; + } + + public void setProcessId(final String processId) { + this.processId = processId; + } + + public void setArrivalTime(final int arrivalTime) { + this.arrivalTime = arrivalTime; + } + + public void setBurstTime(final int burstTime) { + this.burstTime = burstTime; + } + + public void setWaitingTime(final int waitingTime) { + this.waitingTime = waitingTime; + } + + public void setTurnAroundTimeTime(final int turnAroundTime) { + this.turnAroundTime = turnAroundTime; + } +} diff --git a/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java b/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java new file mode 100644 index 00000000..b1c3411b --- /dev/null +++ b/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java @@ -0,0 +1,47 @@ +package com.thealgorithms.scheduling; + +import com.thealgorithms.devutils.entities.ProcessDetails; + +import java.util.List; + +/** + * Non-pre-emptive First Come First Serve scheduling. This can be understood here - https://www.scaler.com/topics/first-come-first-serve/ + */ +public class FCFSScheduling { + + private List processes; + + FCFSScheduling(final List processes) { + this.processes = processes; + } + + public void scheduleProcesses() { + evaluateWaitingTime(); + evaluateTurnAroundTime(); + } + + private void evaluateWaitingTime() { + int processesNumber = processes.size(); + + if(processesNumber == 0) { + return; + } + + int waitingTime = 0; + int burstTime = processes.get(0).getBurstTime(); + + processes.get(0).setWaitingTime(waitingTime); // for the first process, waiting time will be 0. + + for(int i=1; i processes = addProcessesForFCFS(); + final FCFSScheduling fcfsScheduling = new FCFSScheduling(processes); // for sending to FCFS + + fcfsScheduling.scheduleProcesses(); + + assertEquals(3, processes.size()); + + assertEquals("P1", processes.get(0).getProcessId()); + assertEquals(0, processes.get(0).getWaitingTime()); + assertEquals(10, processes.get(0).getTurnAroundTimeTime()); + + assertEquals("P2", processes.get(1).getProcessId()); + assertEquals(10, processes.get(1).getWaitingTime()); + assertEquals(15, processes.get(1).getTurnAroundTimeTime()); + + assertEquals("P3", processes.get(2).getProcessId()); + assertEquals(15, processes.get(2).getWaitingTime()); + assertEquals(23, processes.get(2).getTurnAroundTimeTime()); + + } + + private List addProcessesForFCFS() { + final ProcessDetails process1 = new ProcessDetails("P1", 0, 10); + final ProcessDetails process2 = new ProcessDetails("P2", 1, 5); + final ProcessDetails process3 = new ProcessDetails("P3", 2, 8); + + final List processDetails = new ArrayList<>(); + processDetails.add(process1); + processDetails.add(process2); + processDetails.add(process3); + + return processDetails; + } + +}