Quantcast
Channel: Active questions tagged operating-system+scheduling - Stack Overflow
Viewing all articles
Browse latest Browse all 209

Process scheduling simulator [closed]

$
0
0

I've an assignment to implementing a specific process-scheduling algorithm and simulating its behavior in a multiprogramming environment. It aims to demonstrate the performance of the scheduling algorithm in terms of CPU utilization, average turnaround time, and average waiting time.

import java.util.*;public class Scheduler {    private List<Process> processes;    private int currentTime = 0;    private List<String> ganttChart = new ArrayList<>();    private int contextSwitches = 0;    private int totalIdleTime = 0;    public Scheduler(List<Process> processes) {        this.processes = new ArrayList<>(processes);        this.processes.sort((a, b) -> a.arrivalTime - b.arrivalTime);    }    public void run() {        List<Process> readyQueue = new ArrayList<>();        int index = 0;        Process currentProcess = null;        while (index < processes.size() || !readyQueue.isEmpty() || currentProcess != null) {            // Add arriving processes to the ready queue            while (index < processes.size() && processes.get(index).arrivalTime <= currentTime) {                readyQueue.add(processes.get(index));                index++;            }            // Find the process with the shortest remaining time            Process nextProcess = getShortestRemainingTimeProcess(readyQueue);            // If the next process is different from the currently running process, preempt            if (currentProcess != null && nextProcess != null && nextProcess.id != currentProcess.id) {                ganttChart.add("CS"); // Context switch                contextSwitches++;                currentTime++; // Context switch takes 1 ms            }            // If no process is running, pick the next one from the ready queue            if (currentProcess == null || (nextProcess != null && nextProcess.id != currentProcess.id)) {                currentProcess = nextProcess;                if (currentProcess != null && currentProcess.responseTime == -1) {                    currentProcess.responseTime = currentTime - currentProcess.arrivalTime;                }            }            // If a process is running, execute it            if (currentProcess != null) {                ganttChart.add("P" + currentProcess.id);                currentProcess.remainingTime--;                // If the process completes, mark its completion time                if (currentProcess.remainingTime == 0) {                    currentProcess.completionTime = currentTime + 1;                    currentProcess = null;                }            } else {                // If no process is running, the CPU is idle                ganttChart.add("Idle");                totalIdleTime++;            }            currentTime++;        }    }    // Helper method to find the process with the shortest remaining time    private Process getShortestRemainingTimeProcess(List<Process> readyQueue) {        if (readyQueue.isEmpty()) {            return null;        }        Process shortestProcess = readyQueue.get(0);        for (Process p : readyQueue) {            if (p.remainingTime < shortestProcess.remainingTime ||                (p.remainingTime == shortestProcess.remainingTime && p.arrivalTime < shortestProcess.arrivalTime)) {                shortestProcess = p;            }        }        readyQueue.remove(shortestProcess);        return shortestProcess;    }    public void printResults() {        int totalTurnaround = 0, totalWaiting = 0;        for (Process p : processes) {            p.turnaroundTime = p.completionTime - p.arrivalTime;            p.waitingTime = p.turnaroundTime - p.burstTime;            totalTurnaround += p.turnaroundTime;            totalWaiting += p.waitingTime;        }        double avgTurnaround = (double) totalTurnaround / processes.size();        double avgWaiting = (double) totalWaiting / processes.size();        double cpuUtil = ((currentTime - totalIdleTime) / (double) currentTime) * 100;        System.out.println("Time  Process");        int start = 0;        for (int i = 0; i < ganttChart.size(); i++) {            if (i > 0 && !ganttChart.get(i).equals(ganttChart.get(i - 1))) {                System.out.printf("%d-%d  %s\n", start, i, ganttChart.get(i - 1));                start = i;            }        }        System.out.printf("%d-%d  %s\n\n", start, ganttChart.size(), ganttChart.get(ganttChart.size() - 1));        System.out.printf("Average Turnaround Time: %.2f\n", avgTurnaround);        System.out.printf("Average Waiting Time: %.2f\n", avgWaiting);        System.out.printf("CPU Utilization: %.2f%%\n", cpuUtil);    }}

This is my Process class

import java.util.*;public class Process {    int id;    int arrivalTime;    int burstTime;    int remainingTime;    int completionTime;    int turnaroundTime;    int waitingTime;    int responseTime = -1;    public Process(int id, int arrivalTime, int burstTime) {        this.id = id;        this.arrivalTime = arrivalTime;        this.burstTime = burstTime;        this.remainingTime = burstTime;    }}

And this is the main:

import java.util.*;public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.print("Enter number of processes: ");        int n = scanner.nextInt();        List<Process> processes = new ArrayList<>();        for (int i = 0; i < n; i++) {            System.out.print("Enter arrival time and burst time for process P" + (i + 1) +": ");            int arrival = scanner.nextInt();            int burst = scanner.nextInt();            processes.add(new Process(i + 1, arrival, burst));        }        Scheduler scheduler = new Scheduler(processes);        scheduler.run();        scheduler.printResults();    }}

It should be using shortest remaining time first (Preemptive SJF), using first come first serve.

Expected output:

Number  of  processes=  4   (P1,    P2, P3, P4)Arrival times   and burst   times   as  follows:P1: Arrival time    =   0,  Burst   time    =   8   msP2: Arrival time    =   1,  Burst   time    =   4   msP3: Arrival time    =   2,  Burst   time    =   5   msP4: Arrival time    =   3,  Burst   time    =   5   msScheduling  Algorithm:  Shortest    remaining   time    firstContext Switch: 1   msTime  Process/CS0-1  P11-2  CS2-6  P26-7  CS7-12  P312-13  CS13-18  P418-19  CS19-26  P1Performance MetricsAverage Turnaround  Time:   14Average Waiting Time:   8.5CPU Utilization:    84.62

Actual output:

Number  of  processes=  4   (P1,    P2, P3, P4)Arrival times   and burst   times   as  follows:P1: Arrival time    =   0,  Burst   time    =   8   msP2: Arrival time    =   1,  Burst   time    =   4   msP3: Arrival time    =   2,  Burst   time    =   5   msP4: Arrival time    =   3,  Burst   time    =   5   msScheduling  Algorithm:  Shortest    remaining   time    firstContext Switch: 1   msTime  Process/CS0-8  P18-9  CS9-13  P213-14  CS14-19  P319-20  CS20-25  P425-26 idlePerformance MetricsAverage Turnaround  Time:   14.75Average Waiting Time:   9.25CPU Utilization:    88.46%

What is the problem?


Viewing all articles
Browse latest Browse all 209

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>