Closed VatsalPandya47 closed 9 months ago
import java.util.*;
class Process { String name; int arrivalTime; int burstTime; int priority; int startTime; int completionTime; int turnaroundTime; int waitingTime; int remainingTime; int ioBoundTime;
Process(String name, int arrivalTime, int burstTime, int priority, int ioBoundTime) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
this.remainingTime = burstTime;
this.ioBoundTime = ioBoundTime;
}
}
public class Scheduler {
public static void main(String[] args) {
List
// Choose the scheduling algorithm to simulate
String algorithm = "preemptiveSJF"; // Change this to the algorithm you want to simulate
switch (algorithm) {
case "roundRobin":
roundRobin(processes, 3);
break;
case "fifo":
fifo(processes);
break;
case "sjf":
sjf(processes);
break;
case "preemptiveSJF":
preemptiveSJF(processes);
break;
default:
System.out.println("Invalid scheduling algorithm");
}
}
// Simulated Round Robin
public static void roundRobin(List<Process> processes, int quantum) {
// Implement Round Robin algorithm here
}
// Simulated FIFO
public static void fifo(List<Process> processes) {
// Implement FIFO algorithm here
}
// Simulated Shortest Job First
public static void sjf(List<Process> processes) {
// Implement SJF algorithm here
}
// Simulated Preemptive Shortest Job First
public static void preemptiveSJF(List<Process> processes) {
PriorityQueue<Process> queue = new PriorityQueue<>(Comparator.comparingInt(p -> p.burstTime));
int time = 0;
while (!processes.isEmpty() || !queue.isEmpty()) {
for (Iterator<Process> iterator = processes.iterator(); iterator.hasNext(); ) {
Process process = iterator.next();
if (process.arrivalTime <= time) {
queue.offer(process);
iterator.remove();
}
}
if (!queue.isEmpty()) {
Process currentProcess = queue.poll();
currentProcess.startTime = Math.max(currentProcess.arrivalTime, time);
System.out.println("Processing: " + currentProcess.name);
if (currentProcess.remainingTime > currentProcess.ioBoundTime) {
currentProcess.remainingTime--;
time++;
queue.offer(currentProcess);
} else {
time += currentProcess.remainingTime;
currentProcess.completionTime = time;
currentProcess.turnaroundTime = currentProcess.completionTime - currentProcess.arrivalTime;
currentProcess.waitingTime = currentProcess.turnaroundTime - currentProcess.burstTime;
}
} else {
time++;
}
}
printResults(processes);
}
// Print results
public static void printResults(List<Process> processes) {
System.out.println("\nProcess\tStart Time\tCompletion Time\tTurnaround Time\tWaiting Time");
for (Process process : processes) {
System.out.println(process.name + "\t\t" + process.startTime + "\t\t\t" + process.completionTime + "\t\t\t" +
process.turnaroundTime + "\t\t\t" + process.waitingTime);
}
}
}
The project is a Java-based simulation of various process scheduling algorithms in operating systems. It includes implementations of the following scheduling algorithms:
The project features include:
The project aims to demonstrate how different scheduling algorithms work and their impact on process execution in an operating system environment. It provides flexibility for further customization and extension, such as adding additional features like priority aging or handling different types of processes.