OS Lab Report
OS Lab Report
LAB REPORT
ON
Operating System
BY
Sajal Udash
Submitted to:
Indra PC
Jan, 2025
1
Contents
1. Write a program of FCFC algorithm in context of os?.........................................................................3
1.1. Source Code.....................................................................................................................................3
2. Write a program of Lru algorithm in context of os?.............................................................................6
2. Write a program of Priority algorithm in context of os?......................................................................8
1. Write a program of Sjf algorithm in context of os?............................................................................10
2. Write a program of Optimal LRU in context of os?..........................................................................14
3. Write a program of Page Replacement in context of os?.................................................................17
4. Write a program of Round Robin in context of os?..........................................................................21
5. Write a program of Banker Algorithm in context of os?...................................................................23
6. Write a program of Worst Fit in context of os?.................................................................................27
7. Write a program of Base Fit in context of os?....................................................................................31
8. Write a program of Next Fit in context of os?....................................................................................34
2
1. Write a program of FCFC algorithm in context of os?
1.1. Source Code
import java.util.Scanner;
class Process {
int pid;
int arrivalTime;
int burstTime;
int completionTime;
int turnAroundTime;
int waitingTime;
3
for (int i = 0; i < n; i++) {
System.out.print("Enter arrival time of process " + (i + 1) + ": ");
int arrivalTime = scanner.nextInt();
System.out.print("Enter burst time of process " + (i + 1) + ": ");
int burstTime = scanner.nextInt();
processes[i] = new Process(i + 1, arrivalTime, burstTime);
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].arrivalTime > processes[j].arrivalTime) {
Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
int currentTime = 0;
for (Process process : processes) {
if (currentTime < process.arrivalTime) {
currentTime = process.arrivalTime;
}
process.completionTime = currentTime + process.burstTime;
process.turnAroundTime = process.completionTime - process.arrivalTime;
process.waitingTime = process.turnAroundTime - process.burstTime;
currentTime = process.completionTime;
}
System.out.println("\nProcess\tArrival\tBurst\tCompletion\tTurnaround\tWaiting");
for (Process process : processes) {
System.out.println(process.pid + "\t" + process.arrivalTime + "\t" + process.burstTime + "\t" +
process.completionTime + "\t\t" + process.turnAroundTime + "\t\t" + process.waitingTime);
4
}
double totalTurnAroundTime = 0, totalWaitingTime = 0;
for (Process process : processes) {
totalTurnAroundTime += process.turnAroundTime;
totalWaitingTime += process.waitingTime;
}
scanner.close();
}
}
Output
5
2. Write a program of Lru algorithm in context of os?
2.1. Source code
import java.util.*;
public class LRUCache {
private final int capacity;
private final LinkedHashSet<Integer> cache;
public LRUCache(int capacity) {
this.capacity = capacity;
this.cache = new LinkedHashSet<>();
}
public void accessPage(int page) {
if (cache.contains(page)) {
cache.remove(page);
}
else if (cache.size() == capacity) {
int first = cache.iterator().next(); // Get the first element
cache.remove(first); // Remove the least recently used page
System.out.println("Page " + first + " removed (LRU).");
}
cache.add(page);
System.out.println("Page " + page + " added.");
}
public void displayCache() {
System.out.println("Cache: " + cache);
}
6
int capacity = scanner.nextInt();
lruCache.accessPage(page);
lruCache.displayCache();
}
scanner.close();
}
}
OUTPUT
7
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Process {
int id;
int arrivalTime;
int burstTime;
int priority;
int completionTime;
int turnaroundTime;
int waitingTime;
System.out.println("Process\tArrival\tBurst\tPriority\tCompletion\tTurnaround\tWaiting");
for (Process p : processes) {
System.out.printf("P%d\t%d\t%d\t%d\t\t%d\t\t%d\t\t%d\n",
p.id, p.arrivalTime, p.burstTime, p.priority,
p.completionTime, p.turnaroundTime, p.waitingTime);
}
}
int currentTime = 0;
for (Process process : processes) {
// If the CPU is idle, move the current time to the process's arrival time
if (currentTime < process.arrivalTime) {
currentTime = process.arrivalTime;
8
}
Output
9
class Process {
int id;
int arrivalTime;
int burstTime;
int completionTime;
int turnaroundTime;
int waitingTime;
sjfScheduling(processes);
System.out.println("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting");
for (Process p : processes) {
System.out.printf("P%d\t%d\t%d\t%d\t\t%d\t\t%d\n",
10
p.id, p.arrivalTime, p.burstTime,
p.completionTime, p.turnaroundTime, p.waitingTime);
}
}
if (!available.isEmpty()) {
Process shortest = available.stream()
.min(Comparator.comparingInt(p -> p.burstTime))
.orElse(null);
11
shortest.turnaroundTime = shortest.completionTime - shortest.arrivalTime;
shortest.waitingTime = shortest.turnaroundTime - shortest.burstTime;
completed.add(shortest);
} else {
currentTime++;
}
}
}
}
Output
12
13
2. Write a program of Optimal LRU in context of os?
2.1. Source code
import java.util.*;
14
System.out.println("Page " + currentPage + " added to the frame.");
} else {
System.out.println("Page " + currentPage + " accessed (No page fault).");
}
System.out.println("Current frames: " + frames);
}
15
farthestUsage = nextUse;
pageToReplace = framePage;
}
}
return pageToReplace;
}
}
Output
16
public class PageReplacement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of frames: ");
int frameCount = scanner.nextInt();
System.out.print("Enter the number of pages: ");
int pageCount = scanner.nextInt();
int[] pages = new int[pageCount];
System.out.println("Enter the reference string (page numbers):");
for (int i = 0; i < pageCount; i++) {
pages[i] = scanner.nextInt();
}
Set<Integer> frames = new HashSet<>();
Map<Integer, Integer> pageIndices = new HashMap<>();
int pageFaults = 0;
17
System.out.println("Page " + currentPage + " accessed (No page fault).");
}
pageIndices.put(currentPage, i);
System.out.println("Current frames: " + frames);
}
return lruPage;
}
}
Output
18
19
4. Write a program of Round Robin in context of os?
Source code
import java.util.LinkedList;
import java.util.Queue;
class Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int turnaroundTime
int waitingTime;
20
};
int timeQuantum = 3;
roundRobinScheduling(processes, timeQuantum);
System.out.println("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting");
for (Process p : processes) {
System.out.printf("P%d\t%d\t%d\t%d\t\t%d\t\t%d\n",
p.id, p.arrivalTime, p.burstTime,
p.completionTime, p.turnaroundTime, p.waitingTime);
}
}
while (!readyQueue.isEmpty()) {
Process currentProcess = readyQueue.poll();
int executionTime = Math.min(currentProcess.remainingTime, timeQuantum);
currentProcess.remainingTime -= executionTime;
currentTime += executionTime;
while (processIndex < processes.length && processes[processIndex].arrivalTime <= currentTime)
{
readyQueue.add(processes[processIndex]);
processIndex++;
}
21
if (currentProcess.remainingTime > 0) {
readyQueue.add(currentProcess);
} else {
currentProcess.completionTime = currentTime;
currentProcess.turnaroundTime = currentProcess.completionTime - currentProcess.arrivalTime;
currentProcess.waitingTime = currentProcess.turnaroundTime - currentProcess.burstTime;
}
}
}
}
Output
22
import java.util.Scanner;
23
int[][] need = new int[numProcesses][numResources];
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numResources; j++) {
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
boolean isSafe = bankersAlgorithm(allocation, maximum, available, need, numProcesses,
numResources);
if (isSafe) {
System.out.println("The system is in a safe state.");
} else {
System.out.println("The system is NOT in a safe state.");
}
sc.close();
}
24
for (int j = 0; j < numResources; j++) {
if (need[i][j] > work[j]) {
canProceed = false;
break;
}
}
if (canProceed) {
for (int j = 0; j < numResources; j++) {
work[j] += allocation[i][j];
}
safeSequence[count++] = i;
finished[i] = true;
found = true;
}
}
}
if (!found) {
return false;
}
}
25
return true;
}
}
Output
26
Scanner scanner = new Scanner(System.in);
27
allocated[worstIndex] = blocks[worstIndex] == 0; // Mark block as allocated if fully used
} else {
System.out.println("Process " + (i + 1) + " of size " + processes[i] + " cannot be
allocated.");
}
}
System.out.println("\nRemaining memory in each block:");
for (int i = 0; i < blockCount; i++) {
System.out.println("Block " + (i + 1) + ": " + blocks[i]);
}
scanner.close();
}
}
Output
28
29
7. Write a program of Base Fit in context of os?
Source code
import java.util.Scanner;
public class BestFit {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of memory blocks: ");
int blockCount = scanner.nextInt();
int[] blocks = new int[blockCount
System.out.println("Enter the sizes of the memory blocks:");
for (int i = 0; i < blockCount; i++) {
blocks[i] = scanner.nextInt();
}
System.out.print("Enter the number of processes: ");
int processCount = scanner.nextInt();
int[] processes = new int[processCount];
System.out.println("Enter the sizes of the processes:");
for (int i = 0; i < processCount; i++) {
processes[i] = scanner.nextInt();
}
for (int i = 0; i < processCount; i++) {
int bestIndex = -1;
for (int j = 0; j < blockCount; j++) {
if (blocks[j] >= processes[i]) {
if (bestIndex == -1 || blocks[j] < blocks[bestIndex]) {
bestIndex = j;
}
}
}
30
if (bestIndex != -1) {
System.out.println("Process " + (i + 1) + " of size " + processes[i]
+ " allocated to block " + (bestIndex + 1) + " of size " + blocks[bestIndex]);
blocks[bestIndex] -= processes[i];
} else {
System.out.println("Process " + (i + 1) + " of size " + processes[i] + " cannot be
allocated.");
}
}
scanner.close();
}
}
Output
31
32
8. Write a program of Next Fit in context of os?
Source code
import java.util.Scanner;
int lastAllocatedIndex = 0;
33
+ " allocated to block " + (index + 1) + " of size " + blocks[index]);
blocks[index] -= processes[i];
lastAllocatedIndex = index;
allocated = true;
break;
}
}
if (!allocated) {
System.out.println("Process " + (i + 1) + " of size " + processes[i] + " cannot be
allocated.");
}
}
System.out.println("\nRemaining memory in each block:");
for (int i = 0; i < blockCount; i++) {
System.out.println("Block " + (i + 1) + ": " + blocks[i]);
}
scanner.close();
}
}
Output
34
35