mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-07-01 18:56:51 +00:00
Adds FCFS scheduling algorithm.
This commit is contained in:
parent
4d71f4009f
commit
62bdec461f
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.scaler</groupId>
|
||||
@ -31,10 +30,16 @@
|
||||
<version>1.18.24</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javatuples</groupId>
|
||||
<artifactId>javatuples</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<pluginManagement>
|
||||
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
@ -78,4 +83,4 @@
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
</project>
|
@ -0,0 +1,41 @@
|
||||
package com.scaler.scheduling;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
import com.scaler.scheduling.models.IncomingProcess;
|
||||
import com.scaler.scheduling.models.ScheduledProcess;
|
||||
|
||||
public class FirstComeFirstServe {
|
||||
|
||||
public List<ScheduledProcess> schedule(List<IncomingProcess> processes) {
|
||||
Queue<IncomingProcess> queue = prepareQueue(processes);
|
||||
List<ScheduledProcess> scheduledProcesses = new LinkedList<>();
|
||||
|
||||
int time = 0;
|
||||
int index = 0;
|
||||
while (!queue.isEmpty()) {
|
||||
IncomingProcess process = queue.poll();
|
||||
time += process.getBurstTime();
|
||||
index += 1;
|
||||
|
||||
process.setCompletedAt(time);
|
||||
scheduledProcesses.add(new ScheduledProcess(index, process));
|
||||
}
|
||||
|
||||
return scheduledProcesses;
|
||||
}
|
||||
|
||||
private Queue<IncomingProcess> prepareQueue(List<IncomingProcess> processes) {
|
||||
Queue<IncomingProcess> queue = new LinkedList<>();
|
||||
|
||||
List<IncomingProcess> sortedProcesses = new LinkedList<>(processes);
|
||||
sortedProcesses.sort((p1, p2) -> p1.getArrivalTime() - p2.getArrivalTime());
|
||||
|
||||
for (IncomingProcess process : sortedProcesses) {
|
||||
queue.add(process);
|
||||
}
|
||||
return queue;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.scaler.scheduling.models;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class IncomingProcess {
|
||||
private int id;
|
||||
private int arrivalTime;
|
||||
private int burstTime;
|
||||
|
||||
private int completedAt;
|
||||
|
||||
public IncomingProcess(int id, int arrivalTime, int burstTime) {
|
||||
this.id = id;
|
||||
this.arrivalTime = arrivalTime;
|
||||
this.burstTime = burstTime;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.scaler.scheduling.models;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class ScheduledProcess {
|
||||
|
||||
private int index;
|
||||
private IncomingProcess process;
|
||||
|
||||
public int getId() {
|
||||
return process.getId();
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.scaler.scheduling;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.javatuples.Pair;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.scaler.scheduling.models.IncomingProcess;
|
||||
import com.scaler.scheduling.models.ScheduledProcess;
|
||||
|
||||
public class FirstComeFirstServeTest {
|
||||
|
||||
private static AtomicInteger generator = new AtomicInteger();
|
||||
|
||||
private List<Pair<Integer, Integer>> processTimes = List.of(
|
||||
Pair.with(2, 6),
|
||||
Pair.with(1, 8),
|
||||
Pair.with(0, 3),
|
||||
Pair.with(4, 4));
|
||||
|
||||
private List<Integer> expectedOrder = List.of(3, 2, 1, 4);
|
||||
private List<Integer> expectedCompletionTimes = List.of(3, 11, 17, 21);
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
FirstComeFirstServe firstComeFirstServe = new FirstComeFirstServe();
|
||||
List<ScheduledProcess> scheduledProcesses = firstComeFirstServe.schedule(incomingProcesses(processTimes));
|
||||
|
||||
assertEquals("If n processes are given, then n processes should be scheduled", processTimes.size(),
|
||||
scheduledProcesses.size());
|
||||
|
||||
List<Integer> actualOrder = scheduledProcesses.stream().map(ScheduledProcess::getId)
|
||||
.collect(Collectors.toList());
|
||||
assertEquals("The order of the processes should be the same as the order of the input", expectedOrder,
|
||||
actualOrder);
|
||||
|
||||
List<Integer> actualCompletionTimes = scheduledProcesses.stream().map(ScheduledProcess::getProcess)
|
||||
.map(IncomingProcess::getCompletedAt)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals("The completion time of the processes should be the same as the expected completion times",
|
||||
expectedCompletionTimes,
|
||||
actualCompletionTimes);
|
||||
|
||||
}
|
||||
|
||||
private static List<IncomingProcess> incomingProcesses(List<Pair<Integer, Integer>> processTimes) {
|
||||
|
||||
return processTimes.stream()
|
||||
.map(p -> new IncomingProcess(generator.incrementAndGet(), p.getValue0(), p.getValue1()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user