VatsalPandya47 / README.md

Hi there 👋, I'm Vatsal 🏢 Aspiring Software Engineer @University of Nebraska-Lincoln currently in Lincoln, NE 📫 How to reach me: vatsalpandya84@gmail.com
0 stars 0 forks source link

Scheduler #4

Closed VatsalPandya47 closed 9 months ago

VatsalPandya47 commented 9 months ago

The project is a Java-based simulation of various process scheduling algorithms in operating systems. It includes implementations of the following scheduling algorithms:

  1. Round Robin: Processes are executed in a cyclic manner with a fixed time slice.
  2. FIFO (First In, First Out): Processes are executed in the order they arrive.
  3. SJF (Shortest Job First): The process with the shortest burst time is executed next.
  4. Preemptive SJF: Similar to SJF, but a shorter job can preempt a currently running job.

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.

VatsalPandya47 commented 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 processes = new ArrayList<>(); processes.add(new Process("P1", 0, 8, 3, 2)); processes.add(new Process("P2", 1, 4, 2, 1)); processes.add(new Process("P3", 2, 9, 1, 3)); processes.add(new Process("P4", 3, 5, 4, 2));

    // 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);
    }
}

}