Skip to content

Commit

Permalink
Added code files
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadayman28 committed Nov 18, 2024
0 parents commit 8fd2165
Show file tree
Hide file tree
Showing 22 changed files with 635 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ProcessSchedulingSimulator/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ProcessSchedulingSimulator/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ProcessSchedulingSimulator/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions ProcessSchedulingSimulator/ProcessSchedulingSimulator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
93 changes: 93 additions & 0 deletions ProcessSchedulingSimulator/src/model/Process.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package model;

public class Process {
// Attributes
private int id; // Process ID
private int arrivalTime; // Arrival time of the process
private int burstTime; // Burst time (CPU time required)
private int priority; // Priority of the process (for priority scheduling)
private int remainingTime; // Remaining burst time (used for Round Robin and Preemptive algorithms)

// Constructor
public Process(int id, int arrivalTime, int burstTime, int priority) {
this.id = id;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
this.remainingTime = burstTime; // Initially, the remaining time is the burst time
}

// Getters and Setters
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getArrivalTime() {
return arrivalTime;
}

public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}

public int getBurstTime() {
return burstTime;
}

public void setBurstTime(int burstTime) {
this.burstTime = burstTime;
}

public int getPriority() {
return priority;
}

public void setPriority(int priority) {
this.priority = priority;
}

public int getRemainingTime() {
return remainingTime;
}

public void setRemainingTime(int remainingTime) {
this.remainingTime = remainingTime;
}

// Method to display process details in a string format
@Override
public String toString() {
return "Process{" +
"id=" + id +
", arrivalTime=" + arrivalTime +
", burstTime=" + burstTime +
", priority=" + priority +
", remainingTime=" + remainingTime +
'}';
}

// Method to check if the process has completed execution
public boolean isCompleted() {
return remainingTime == 0;
}

// Method to execute the process for a given amount of time (used in Round Robin)
public void executeFor(int time) {
if (remainingTime > 0) {
if (remainingTime > time) {
remainingTime -= time;
} else {
remainingTime = 0; // Process is completed
}
}
}

// Method to reset remaining time if needed (used in case of new scheduling)
public void reset() {
this.remainingTime = burstTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scheduler;

import model.Process;
import java.util.List;

public class FirstComeFirstServeScheduler implements Scheduler {

@Override
public void schedule(List<Process> processes) {
// First-Come, First-Served (FCFS) scheduling
processes.sort((p1, p2) -> Integer.compare(p1.getArrivalTime(), p2.getArrivalTime()));

int currentTime = 0;
for (Process process : processes) {
if (process.getArrivalTime() > currentTime) {
currentTime = process.getArrivalTime(); // If a process arrives later, wait until it arrives
}

System.out.println("Process " + process.getId() + " starts at time " + currentTime);
currentTime += process.getBurstTime(); // Simulate the process execution
System.out.println("Process " + process.getId() + " ends at time " + currentTime);
}
}
}
100 changes: 100 additions & 0 deletions ProcessSchedulingSimulator/src/scheduler/PriorityScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package scheduler;

import model.Process;
import simulation.SimulationMode;

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

public class PriorityScheduler implements Scheduler {

@Override
public void schedule(List<Process> processes) {
if (SimulationMode.PREEMPTIVE.equals(SimulationMode.PREEMPTIVE)) {
preemptivePriorityScheduling(processes);
} else {
nonPreemptivePriorityScheduling(processes);
}
}

// Non-preemptive Priority Scheduling
private void nonPreemptivePriorityScheduling(List<Process> processes) {
// Sort processes by arrival time first, and then by priority (lowest priority number = highest priority)
processes.sort(Comparator.comparingInt(Process::getArrivalTime)
.thenComparingInt(Process::getPriority));

int currentTime = 0;
List<Process> readyQueue = new ArrayList<>();
int index = 0;

while (index < processes.size() || !readyQueue.isEmpty()) {
// Add processes that have arrived to the ready queue
while (index < processes.size() && processes.get(index).getArrivalTime() <= currentTime) {
readyQueue.add(processes.get(index));
index++;
}

// If there are processes in the ready queue, execute the one with the highest priority (lowest priority number)
if (!readyQueue.isEmpty()) {
// Sort ready queue based on priority
readyQueue.sort(Comparator.comparingInt(Process::getPriority));

Process currentProcess = readyQueue.remove(0); // Get the highest priority process

// Execute the process
System.out.println("Process " + currentProcess.getId() + " starts at time " + currentTime);
currentTime += currentProcess.getBurstTime(); // Simulate execution for the burst time
System.out.println("Process " + currentProcess.getId() + " ends at time " + currentTime);
} else {
// If no process is ready, increment time
currentTime++;
}
}
}

// Preemptive Priority Scheduling (same as before)
private void preemptivePriorityScheduling(List<Process> processes) {
// Sort processes by arrival time initially
processes.sort(Comparator.comparingInt(Process::getArrivalTime));
int currentTime = 0;
List<Process> readyQueue = new ArrayList<>();
int index = 0;
Process runningProcess = null;

while (index < processes.size() || !readyQueue.isEmpty() || runningProcess != null) {
// Add processes that have arrived to the ready queue
while (index < processes.size() && processes.get(index).getArrivalTime() <= currentTime) {
readyQueue.add(processes.get(index));
System.out.println("Process " + processes.get(index).getId() + " enters ready queue at time " + currentTime);
index++;
}

// If a process is running, check if it has completed
if (runningProcess != null && runningProcess.isCompleted()) {
System.out.println("Process " + runningProcess.getId() + " ends at time " + currentTime);
runningProcess = null;
}

// Select the next process to execute if none is running
if (runningProcess == null && !readyQueue.isEmpty()) {
// Sort ready queue by priority (lower number = higher priority)
readyQueue.sort(Comparator.comparingInt(Process::getPriority));

// Select the process with the highest priority
runningProcess = readyQueue.remove(0);

// Log the process starting execution
System.out.println("Process " + runningProcess.getId() + " starts at time " + currentTime);
}

// Execute the running process for one unit of time
if (runningProcess != null) {
runningProcess.executeFor(1);
}

// Increment the time step
currentTime++;
}
}
}
125 changes: 125 additions & 0 deletions ProcessSchedulingSimulator/src/scheduler/RoundRobinScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package scheduler;

import model.Process;
import simulation.SimulationMode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class RoundRobinScheduler implements Scheduler {

private int timeQuantum; // Time slice for round robin scheduling

public RoundRobinScheduler(int timeQuantum) {
this.timeQuantum = timeQuantum;
}

@Override
public void schedule(List<Process> processes) {
Queue<Process> queue = new LinkedList<>();
int time = 0;
int index = 0;

// Sort processes by arrival time
processes.sort((p1, p2) -> Integer.compare(p1.getArrivalTime(), p2.getArrivalTime()));

while (index < processes.size() || !queue.isEmpty()) {
// Add all processes that have arrived by the current time to the queue
while (index < processes.size() && processes.get(index).getArrivalTime() <= time) {
queue.add(processes.get(index));
index++;
}

if (!queue.isEmpty()) {
Process current = queue.poll();

if (current.getRemainingTime() > 0) {
int executionTime = Math.min(current.getRemainingTime(), timeQuantum);
current.setRemainingTime(current.getRemainingTime() - executionTime);
time += executionTime;

System.out.println("Process " + current.getId() + " executed for " + executionTime + " units at time " + time);

// If the process is not finished, re-add it to the queue
if (current.getRemainingTime() > 0) {
queue.add(current);
} else {
System.out.println("Process " + current.getId() + " ends at time " + time);
}
}
} else {
// If no processes are ready to execute, increment time
time++;
}
}
}


// Non-preemptive Round Robin (just executes each process in turn)
private void nonPreemptiveRoundRobin(List<Process> processes) {
Queue<Process> queue = new LinkedList<>();
int currentTime = 0;

// Sort processes by arrival time
processes.sort((p1, p2) -> Integer.compare(p1.getArrivalTime(), p2.getArrivalTime()));

int index = 0;
while (index < processes.size() || !queue.isEmpty()) {
// Add processes that have arrived to the ready queue
while (index < processes.size() && processes.get(index).getArrivalTime() <= currentTime) {
queue.add(processes.get(index));
index++;
}

if (!queue.isEmpty()) {
Process currentProcess = queue.poll();

// Execute the process for its entire burst time
System.out.println("Process " + currentProcess.getId() + " starts at time " + currentTime);
currentTime += currentProcess.getBurstTime();
System.out.println("Process " + currentProcess.getId() + " ends at time " + currentTime);
} else {
currentTime++; // No process is ready, increment time
}
}
}

// Preemptive Round Robin (interrupts processes based on time quantum)
private void preemptiveRoundRobin(List<Process> processes) {
Queue<Process> queue = new LinkedList<>();
int currentTime = 0;

// Sort processes by arrival time
processes.sort((p1, p2) -> Integer.compare(p1.getArrivalTime(), p2.getArrivalTime()));

int index = 0;
while (index < processes.size() || !queue.isEmpty()) {
// Add processes that have arrived to the ready queue
while (index < processes.size() && processes.get(index).getArrivalTime() <= currentTime) {
queue.add(processes.get(index));
index++;
}

if (!queue.isEmpty()) {
Process currentProcess = queue.poll();

// Execute the process for one time quantum (if it doesn't complete)
int remainingTime = currentProcess.getRemainingTime();
int timeSlice = Math.min(timeQuantum, remainingTime);
currentProcess.executeFor(timeSlice);
currentTime += timeSlice;

System.out.println("Process " + currentProcess.getId() + " executed for " + timeSlice + " units at time " + currentTime);

if (currentProcess.isCompleted()) {
System.out.println("Process " + currentProcess.getId() + " ends at time " + currentTime);
} else {
queue.add(currentProcess); // Re-add process to the queue if not completed
}
} else {
currentTime++; // No process is ready, increment time
}
}
}
}
9 changes: 9 additions & 0 deletions ProcessSchedulingSimulator/src/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scheduler;

import model.Process;
import java.util.List;

public interface Scheduler {
// Method to schedule processes based on the selected algorithm
void schedule(List<Process> processes);
}
Loading

0 comments on commit 8fd2165

Please sign in to comment.