0% found this document useful (0 votes)
20 views8 pages

CPU Scheduling Algorithms in Java

Uploaded by

tanvisatav.foe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

CPU Scheduling Algorithms in Java

Uploaded by

tanvisatav.foe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GROUP B

Assignment 5

import [Link].*;

class Process {

int pid, arrivalTime, burstTime, remainingTime, completionTime, waitingTime, turnaroundTime,


priority;

boolean isCompleted = false;

Process(int pid, int arrivalTime, int burstTime, int priority) {

[Link] = pid;

[Link] = arrivalTime;

[Link] = burstTime;

[Link] = burstTime;

[Link] = priority;

public class CPUSchedulingSimulator {

static Scanner sc = new Scanner([Link]);

public static void main(String[] args) {

List<Process> processes = getProcessInput();

[Link]("\n===== First Come First Serve (FCFS) =====");

fcfs(new ArrayList<>(processes));
[Link]("\n===== Shortest Job First (SJF - Preemptive) =====");

sjfPreemptive(new ArrayList<>(processes));

[Link]("\n===== Priority Scheduling (Non-Preemptive) =====");

priorityScheduling(new ArrayList<>(processes));

[Link]("\n===== Round Robin (Preemptive) =====");

[Link]("Enter Time Quantum: ");

int quantum = [Link]();

roundRobin(new ArrayList<>(processes), quantum);

static List<Process> getProcessInput() {

[Link]("Enter number of processes: ");

int n = [Link]();

List<Process> processes = new ArrayList<>();

for (int i = 0; i < n; i++) {

[Link]("Enter Arrival Time, Burst Time, and Priority for Process " + (i + 1) + ":");

int at = [Link]();

int bt = [Link]();

int pr = [Link]();

[Link](new Process(i + 1, at, bt, pr));

return processes;

}
static void fcfs(List<Process> processes) {

[Link]([Link](p -> [Link]));

int currentTime = 0;

for (Process p : processes) {

currentTime = [Link](currentTime, [Link]);

[Link] = currentTime + [Link];

currentTime = [Link];

[Link] = [Link] - [Link];

[Link] = [Link] - [Link];

printResults(processes);

static void sjfPreemptive(List<Process> processes) {

int n = [Link](), completed = 0, time = 0;

Process current = null;

while (completed < n) {

Process shortest = null;

int minTime = Integer.MAX_VALUE;

for (Process p : processes) {

if ([Link] <= time && ![Link] && [Link] < minTime &&
[Link] > 0) {

minTime = [Link];

shortest = p;
}

if (shortest == null) {

time++;

continue;

[Link]--;

time++;

if ([Link] == 0) {

[Link] = true;

[Link] = time;

[Link] = [Link] - [Link];

[Link] = [Link] - [Link];

completed++;

printResults(processes);

static void priorityScheduling(List<Process> processes) {

int time = 0, completed = 0;

while (completed < [Link]()) {


Process highest = null;

for (Process p : processes) {

if ([Link] <= time && ![Link]) {

if (highest == null || [Link] < [Link]) {

highest = p;

if (highest == null) {

time++;

continue;

time += [Link];

[Link] = time;

[Link] = [Link] - [Link];

[Link] = [Link] - [Link];

[Link] = true;

completed++;

printResults(processes);

static void roundRobin(List<Process> processes, int quantum) {

Queue<Process> queue = new LinkedList<>();


int time = 0, completed = 0;

[Link]([Link](p -> [Link]));

int i = 0;

while (completed < [Link]()) {

while (i < [Link]() && [Link](i).arrivalTime <= time) {

[Link]([Link](i));

i++;

if ([Link]()) {

time++;

continue;

Process current = [Link]();

int execTime = [Link]([Link], quantum);

[Link] -= execTime;

time += execTime;

while (i < [Link]() && [Link](i).arrivalTime <= time) {

[Link]([Link](i));

i++;

if ([Link] > 0) {

[Link](current);
} else {

[Link] = time;

[Link] = [Link] - [Link];

[Link] = [Link] - [Link];

completed++;

printResults(processes);

static void printResults(List<Process> processes) {

[Link]("\n%-10s%-15s%-15s%-15s%-15s%-15s\n", "PID", "Arrival", "Burst",


"Completion", "Turnaround", "Waiting");

double totalTAT = 0, totalWT = 0;

for (Process p : processes) {

[Link]("%-10d%-15d%-15d%-15d%-15d%-15d\n", [Link], [Link],


[Link], [Link], [Link], [Link]);

totalTAT += [Link];

totalWT += [Link];

[Link]("\nAverage Turnaround Time: %.2f\n", totalTAT / [Link]());

[Link]("Average Waiting Time : %.2f\n", totalWT / [Link]());

}
Input:
Enter number of processes: 3
Enter Arrival Time, Burst Time, and Priority for Process 1:
072
Enter Arrival Time, Burst Time, and Priority for Process 2:
241
Enter Arrival Time, Burst Time, and Priority for Process 3:
413
Enter Time Quantum: 2

Output (FCFS):
===== First Come First Serve (FCFS) =====

PID Arrival Burst Completion Turnaround Waiting


1 0 7 7 7 0
2 2 4 11 9 5
3 4 1 12 8 7

Average Turnaround Time: 8.00


Average Waiting Time : 4.00

You might also like