Q1) Write a program to implement FCFS (with arrival time=0 for all) Calculate waiting time, turnaround time for each process. Calculate avg. waiting time, avg turnaround time.
import java.util.Scanner;
public class FCFS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int[] bt = new int[n]; // burst time
int[] wt = new int[n]; // waiting time
int[] tat = new int[n]; // turnaround time
for (int i = 0; i < n; i++) {
System.out.print("Enter burst time for process " + (i + 1) + ": ");
bt[i] = sc.nextInt();
}
// FCFS scheduling logic (arrival time = 0 for all)
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
}
for (int i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
double totalWt = 0, totalTat = 0;
System.out.println("\\nProcess\\tBurst Time\\tWaiting Time\\tTurnaround Time");
for (int i = 0; i < n; i++) {
totalWt += wt[i];
totalTat += tat[i];
System.out.println((i + 1) + "\\t\\t\\t\\t" + bt[i] + "\\t\\t\\t\\t" + wt[i] + "\\t\\t\\t\\t" + tat[i]);
}
System.out.printf("\\nAverage Waiting Time: %.2f\\n", totalWt / n);
System.out.printf("Average Turnaround Time: %.2f\\n", totalTat / n);
}
}
Q2) Write a program to implement SJF (with arrival time=0 for all) Calculate waiting time, turnaround time for each process. Calculate avg. waiting time, avg turnaround time
import java.util.Scanner;
import java.util.Arrays;
public class SJF {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int[] bt = new int[n]; // burst time
int[] wt = new int[n]; // waiting time
int[] tat = new int[n]; // turnaround time
int[] pid = new int[n]; // process ID
for (int i = 0; i < n; i++) {
System.out.print("Enter burst time for process " + (i + 1) + ": ");
bt[i] = sc.nextInt();
pid[i] = i + 1;
}
// Sort processes by burst time
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (bt[i] > bt[j]) {
int temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = pid[i];
pid[i] = pid[j];
pid[j] = temp;
}
}
}
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
}
for (int i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
double totalWt = 0, totalTat = 0;
System.out.println("\\nProcess\\tBurst Time\\tWaiting Time\\tTurnaround Time");
for (int i = 0; i < n; i++) {
totalWt += wt[i];
totalTat += tat[i];
System.out.println(pid[i] + "\\t\\t" + bt[i] + "\\t\\t" + wt[i] + "\\t\\t" + tat[i]);
}
System.out.printf("\\nAverage Waiting Time: %.2f\\n", totalWt / n);
System.out.printf("Average Turnaround Time: %.2f\\n", totalTat / n);
}
}
Q3) Write a program to implement Non preemptive Priority scheduling. Calculate waiting time, turnaround time for each process. Calculate avg. waiting time, avg. turnaround time
import java.util.*;
public class PriorityScheduling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int[] bt = new int[n]; // burst times
int[] priority = new int[n]; // priority values (lower number = higher priority)
int[] wt = new int[n]; // waiting times
int[] tat = new int[n]; // turnaround times
Integer[] pid = new Integer[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter burst time for process " + (i + 1) + ": ");
bt[i] = sc.nextInt();
System.out.print("Enter priority for process " + (i + 1) + " (lower = higher priority): ");
priority[i] = sc.nextInt();
pid[i] = i;
}
// Sort by priority
Arrays.sort(pid, Comparator.comparingInt(i -> priority[i]));
wt[pid[0]] = 0;
for (int i = 1; i < n; i++) {
wt[pid[i]] = wt[pid[i - 1]] + bt[pid[i - 1]];
}
for (int i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
double totalWt = 0, totalTat = 0;
System.out.println("\\nProcess\\tBT\\tPriority\\tWT\\tTAT");
for (int i = 0; i < n; i++) {
totalWt += wt[i];
totalTat += tat[i];
System.out.println((i + 1) + "\\t" + bt[i] + "\\t" + priority[i] + "\\t\\t" + wt[i] + "\\t" + tat[i]);
}
System.out.printf("\\nAverage Waiting Time: %.2f\\n", totalWt / n);
System.out.printf("Average Turnaround Time: %.2f\\n", totalTat / n);
}
}
Q4) Write a program to implement Round Robin. Calculate waiting time, turnaround time for each process. Calculate avg. waiting time, avg turnaround time
import java.util.*;
public class RoundRobin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int[] bt = new int[n]; // burst times
int[] rt = new int[n]; // remaining times
int[] wt = new int[n]; // waiting times
int[] tat = new int[n]; // turnaround times
for (int i = 0; i < n; i++) {
System.out.print("Enter burst time for process " + (i + 1) + ": ");
bt[i] = sc.nextInt();
rt[i] = bt[i];
}
System.out.print("Enter time quantum: ");
int quantum = sc.nextInt();
int time = 0;
boolean done;
do {
done = true;
for (int i = 0; i < n; i++) {
if (rt[i] > 0) {
done = false;
if (rt[i] > quantum) {
time += quantum;
rt[i] -= quantum;
} else {
time += rt[i];
wt[i] = time - bt[i];
rt[i] = 0;
}
}
}
} while (!done);
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
double totalWt = 0, totalTat = 0;
System.out.println("\\nProcess\\tBT\\tWT\\tTAT");
for (int i = 0; i < n; i++) {
totalWt += wt[i];
totalTat += tat[i];
System.out.println((i + 1) + "\\t" + bt[i] + "\\t" + wt[i] + "\\t" + tat[i]);
}
System.out.printf("\\nAverage Waiting Time: %.2f\\n", totalWt / n);
System.out.printf("Average Turnaround Time: %.2f\\n", totalTat / n);
}
}
Q5) Write a program to simulate the First Fit Memory Allocation Technique
import java.util.Scanner;
public class FirstFit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of memory blocks: ");
int m = sc.nextInt();
int[] blockSize = new int[m];
for (int i = 0; i < m; i++) {
System.out.print("Enter size of block " + (i + 1) + ": ");
blockSize[i] = sc.nextInt();
}
System.out.print("\\nEnter number of processes: ");
int n = sc.nextInt();
int[] processSize = new int[n];
int[] allocation = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter size of process " + (i + 1) + ": ");
processSize[i] = sc.nextInt();
allocation[i] = -1;
}
// First Fit Allocation
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
System.out.println("\\nProcess No.\\tProcess Size\\tBlock Allocated");
for (int i = 0; i < n; i++) {
System.out.print((i + 1) + "\\t\\t" + processSize[i] + "\\t\\t");
if (allocation[i] != -1)
System.out.println(allocation[i] + 1);
else
System.out.println("Not Allocated");
}
}
}
Q6) Write programs to simulate the Best Fit Memory Allocation Technique
import java.util.Scanner;
public class BestFit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of memory blocks: ");
int m = sc.nextInt();
int[] blockSize = new int[m];
for (int i = 0; i < m; i++) {
System.out.print("Enter size of block " + (i + 1) + ": ");
blockSize[i] = sc.nextInt();
}
System.out.print("\\nEnter number of processes: ");
int n = sc.nextInt();
int[] processSize = new int[n];
int[] allocation = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter size of process " + (i + 1) + ": ");
processSize[i] = sc.nextInt();
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
System.out.println("\\nProcess No.\\tProcess Size\\tBlock Allocated");
for (int i = 0; i < n; i++) {
System.out.print((i + 1) + "\\t\\t" + processSize[i] + "\\t\\t");
if (allocation[i] != -1)
System.out.println(allocation[i] + 1);
else
System.out.println("Not Allocated");
}
}
}