Add fcfs scheduling (#3335)

Co-authored-by: Smiti <smmaheshwari@paypal.com>
This commit is contained in:
Smiti Maheshwari 2022-10-13 00:09:34 +05:30 committed by GitHub
parent 911b98472c
commit c59fc923bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 153 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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<ProcessDetails> processes;
FCFSScheduling(final List<ProcessDetails> 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<processesNumber; i++) {
processes.get(i).setWaitingTime(waitingTime + burstTime);
waitingTime = processes.get(i).getWaitingTime();
burstTime = processes.get(i).getBurstTime();
}
}
private void evaluateTurnAroundTime() {
for(int i=0; i<processes.size(); i++) {
processes.get(i).setTurnAroundTimeTime(processes.get(i).getBurstTime() + processes.get(i).getWaitingTime());
}
}
}

View File

@ -0,0 +1,50 @@
package com.thealgorithms.scheduling;
import com.thealgorithms.devutils.entities.ProcessDetails;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class FCFSSchedulingTest {
@Test
public void testingProcesses() {
List<ProcessDetails> 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<ProcessDetails> 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> processDetails = new ArrayList<>();
processDetails.add(process1);
processDetails.add(process2);
processDetails.add(process3);
return processDetails;
}
}