0% found this document useful (0 votes)
1 views18 pages

OS Lab Easy Java 1

Uploaded by

sumit kumar
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)
1 views18 pages

OS Lab Easy Java 1

Uploaded by

sumit kumar
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

OPERATING SYSTEMS LAB – Java Programs (Easy Version)

Exam: 23 March 2026 (Monday) | Day Order: 4 | Time: 11:45 AM –


12:30 PM

Program 1: Basic System Information


Aim:
To display basic system information like OS name, version, and Java details.
Java Program:
public class BasicSystemInfo {
public static void main(String[] args) {

[Link]("OS Name : " + [Link]("[Link]"));


[Link]("OS Version : " + [Link]("[Link]"));
[Link]("OS Arch : " + [Link]("[Link]"));
[Link]("User Name : " + [Link]("[Link]"));
[Link]("Java Version: " + [Link]("[Link]"));

Runtime r = [Link]();
[Link]("Processors : " + [Link]());
[Link]("Total Memory: " + [Link]() + " bytes");
[Link]("Free Memory : " + [Link]() + " bytes");
}
}

Sample Output:
OS Name : Windows 11
OS Version : 10.0
OS Arch : amd64
User Name : student
Java Version: 17.0.6
Processors : 8
Total Memory: 268435456 bytes
Free Memory : 257234944 bytes
Program 2: Disk Information
Aim:
To display total, free, and usable disk space for all available drives.
Java Program:
import [Link];

public class DiskInfo {


public static void main(String[] args) {

File[] drives = [Link]();

[Link]("Drive Total(GB) Free(GB) Usable(GB)");


[Link]("------------------------------------------");

for (File drive : drives) {


double total = [Link]() / 1e9;
double free = [Link]() / 1e9;
double usable = [Link]() / 1e9;

[Link]("%-8s %.2f %.2f %.2f%n",


drive, total, free, usable);
}
}
}

Sample Output:
Drive Total(GB) Free(GB) Usable(GB)
------------------------------------------
C: 476.94 120.35 120.35
D: 931.51 450.20 450.20
Program 3: CPU and Memory Information
Aim:
To display CPU count and detailed heap/non-heap memory statistics.
Java Program:
import [Link].*;

public class CPUMemoryInfo {


public static void main(String[] args) {

// CPU Info
OperatingSystemMXBean os = [Link]();
[Link]("=== CPU Info ===");
[Link]("OS Name : " + [Link]());
[Link]("CPU Cores : " + [Link]());

// Memory Info
MemoryMXBean mem = [Link]();
MemoryUsage heap = [Link]();

[Link]("\n=== Memory Info ===");


[Link]("Heap Used : " + [Link]() / (1024*1024) + "
MB");
[Link]("Heap Max : " + [Link]() / (1024*1024) + "
MB");
[Link]("Heap Total : " + [Link]() / (1024*1024) + "
MB");

Runtime r = [Link]();
[Link]("Free Memory: " + [Link]() / (1024*1024) + "
MB");
}
}

Sample Output:
=== CPU Info ===
OS Name : Windows 11
CPU Cores : 8

=== Memory Info ===


Heap Used : 5 MB
Heap Max : 4096 MB
Heap Total : 256 MB
Free Memory: 251 MB
Program 4: Files in Directory
Aim:
To list all files and folders in the current directory with their size and type.
Java Program:
import [Link];

public class FilesInDirectory {


public static void main(String[] args) {

File dir = new File("."); // current directory


File[] list = [Link]();

[Link]("Name Type Size(bytes)");


[Link]("------------------------------------------");

for (File f : list) {


String type = [Link]() ? "DIR" : "FILE";
long size = [Link]() ? 0 : [Link]();
[Link]("%-22s %-8s %d%n", [Link](), type, size);
}

[Link]("\nTotal items: " + [Link]);


}
}

Sample Output:
Name Type Size(bytes)
------------------------------------------
[Link] FILE 512
[Link] FILE 430
[Link] FILE 128
output DIR 0

Total items: 4
Program 5: Round Robin Scheduling
Aim:
To simulate Round Robin CPU scheduling with a fixed time quantum.
Java Program:
import [Link].*;

public class RoundRobin {


public static void main(String[] args) {

int n = 3;
int[] burst = {5, 3, 4};
int[] remaining = {5, 3, 4};
int[] waiting = new int[n];
int quantum = 2;
int time = 0;

[Link]("Process Burst Quantum=" + quantum);


[Link]("P1=" + burst[0] + " P2=" + burst[1] + " P3=" +
burst[2]);
[Link]("\n--- Execution ---");

while (true) {
boolean done = true;
for (int i = 0; i < n; i++) {
if (remaining[i] > 0) {
done = false;
int run = [Link](quantum, remaining[i]);
[Link]("Time " + time + "-" + (time+run) + ": P" +
(i+1));
waiting[i] += time - (burst[i] - remaining[i]);
time += run;
remaining[i] -= run;
}
}
if (done) break;
}

[Link]("\nProcess Waiting Time");


for (int i = 0; i < n; i++)
[Link]("P" + (i+1) + " " + waiting[i]);
}
}

Sample Output:
Process Burst Quantum=2
P1=5 P2=3 P3=4

--- Execution ---


Time 0-2: P1
Time 2-4: P2
Time 4-6: P3
Time 6-8: P1
Time 8-9: P2
Time 9-11: P3
Time 11-12: P1

Process Waiting Time


P1 7
P2 4
P3 5
Program 6: FCFS Scheduling
Aim:
To simulate First Come First Serve CPU scheduling algorithm.
Java Program:
public class FCFS {
public static void main(String[] args) {

int[] pid = {1, 2, 3, 4};


int[] arrival = {0, 1, 2, 3};
int[] burst = {6, 4, 2, 5};
int n = [Link];

int[] completion = new int[n];


int[] turnaround = new int[n];
int[] waiting = new int[n];
int time = 0;

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


if (time < arrival[i]) time = arrival[i];
time += burst[i];
completion[i] = time;
turnaround[i] = completion[i] - arrival[i];
waiting[i] = turnaround[i] - burst[i];
}

[Link]("P Arrival Burst Waiting Turnaround");


[Link]("---------------------------------------");
for (int i = 0; i < n; i++)
[Link]("P" + pid[i] + " " + arrival[i]
+ " " + burst[i]
+ " " + waiting[i]
+ " " + turnaround[i]);
}
}

Sample Output:
P Arrival Burst Waiting Turnaround
---------------------------------------
P1 0 6 0 6
P2 1 4 5 9
P3 2 2 8 10
P4 3 5 9 14
Program 7: Optimal Page Replacement
Aim:
To simulate Optimal Page Replacement and count page faults.
Java Program:
import [Link].*;

public class OptimalPageReplacement {


public static void main(String[] args) {

int[] pages = {7, 0, 1, 2, 0, 3, 0, 4, 2};


int frames = 3;
int[] frame = new int[frames];
[Link](frame, -1);

int faults = 0;
[Link]("Page Frames Status");
[Link]("------------------------------");

for (int i = 0; i < [Link]; i++) {


int page = pages[i];
boolean hit = false;

for (int f : frame)


if (f == page) { hit = true; break; }

if (hit) {
[Link](page + " " + [Link](frame) + "
HIT");
} else {
faults++;
// find empty slot
int pos = -1;
for (int j = 0; j < frames; j++)
if (frame[j] == -1) { pos = j; break; }

// if no empty slot, find optimal victim


if (pos == -1) {
int farthest = -1;
for (int j = 0; j < frames; j++) {
int next = Integer.MAX_VALUE;
for (int k = i+1; k < [Link]; k++)
if (pages[k] == frame[j]) { next = k; break; }
if (next > farthest) { farthest = next; pos = j; }
}
}
frame[pos] = page;
[Link](page + " " + [Link](frame) + "
MISS");
}
}
[Link]("\nTotal Page Faults: " + faults);
}
}

Sample Output:
Page Frames Status
------------------------------
7 [7, -1, -1] MISS
0 [7, 0, -1] MISS
1 [7, 0, 1] MISS
2 [2, 0, 1] MISS
0 [2, 0, 1] HIT
3 [2, 0, 3] MISS
0 [2, 0, 3] HIT
4 [4, 0, 3] MISS
2 [4, 0, 2] MISS

Total Page Faults: 7


Program 8: System Calls
Aim:
To demonstrate system call equivalents in Java – process ID, file I/O, and child process.
Java Program:
import [Link].*;

public class SystemCalls {


public static void main(String[] args) throws Exception {

// 1. getpid() - get current process ID


long pid = [Link]().pid();
[Link]("1. Process ID : " + pid);

// 2. fork + exec - run a system command


Process child = [Link]().exec("cmd /c echo Hello from child");
BufferedReader br = new BufferedReader(
new InputStreamReader([Link]()));
[Link]("2. Child says : " + [Link]());
[Link](" Exit code : " + [Link]());

// 3. File write (open + write)


FileWriter fw = new FileWriter("[Link]");
[Link]("System Call Test");
[Link]();
[Link]("3. File write : Done");

// 4. File read
BufferedReader fr = new BufferedReader(new FileReader("[Link]"));
[Link]("4. File read : " + [Link]());
[Link]();

// 5. delete file (unlink)


new File("[Link]").delete();
[Link]("5. File delete: Done");
}
}

Sample Output:
1. Process ID : 12345
2. Child says : Hello from child
Exit code : 0
3. File write : Done
4. File read : System Call Test
5. File delete: Done
Program 9: Multithreading
Aim:
To demonstrate creating and running multiple threads simultaneously in Java.
Java Program:
class MyThread extends Thread {
String name;
MyThread(String name) { [Link] = name; }

public void run() {


for (int i = 1; i <= 3; i++) {
[Link](name + " - step " + i);
try { [Link](300); } catch (Exception e) {}
}
}
}

public class Multithreading {


public static void main(String[] args) throws Exception {

[Link]("--- Starting Threads ---");

MyThread t1 = new MyThread("Thread-A");


MyThread t2 = new MyThread("Thread-B");
MyThread t3 = new MyThread("Thread-C");

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

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

[Link]("--- All Threads Done ---");


}
}

Sample Output:
--- Starting Threads ---
Thread-A - step 1
Thread-B - step 1
Thread-C - step 1
Thread-A - step 2
Thread-B - step 2
Thread-C - step 2
Thread-A - step 3
Thread-B - step 3
Thread-C - step 3
--- All Threads Done ---
Program 10: LRU Page Replacement
Aim:
To simulate Least Recently Used page replacement and count page faults.
Java Program:
import [Link].*;

public class LRUPageReplacement {


public static void main(String[] args) {

int[] pages = {7, 0, 1, 2, 0, 3, 0, 4};


int frames = 3;
int faults = 0;

LinkedList<Integer> frame = new LinkedList<>();

[Link]("Page Frames Status");


[Link]("---------------------------------");

for (int page : pages) {


if ([Link](page)) {
[Link]((Integer) page);
[Link](page); // move to front = most recent
[Link](page + " " + frame + " HIT");
} else {
faults++;
if ([Link]() == frames)
[Link](); // remove least recently used
[Link](page);
[Link](page + " " + frame + " MISS");
}
}

[Link]("\nTotal Page Faults: " + faults);


}
}

Sample Output:
Page Frames Status
---------------------------------
7 [7] MISS
0 [0, 7] MISS
1 [1, 0, 7] MISS
2 [2, 1, 0] MISS
0 [0, 2, 1] HIT
3 [3, 0, 2] MISS
0 [0, 3, 2] HIT
4 [4, 0, 3] MISS

Total Page Faults: 6


Program 11: Sequential File Access
Aim:
To write and read records one by one in sequential order using a text file.
Java Program:
import [Link].*;

public class SequentialFileAccess {


public static void main(String[] args) throws Exception {

String file = "[Link]";

// WRITE sequentially
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
[Link]("101,Alice,92"); [Link]();
[Link]("102,Bob,85"); [Link]();
[Link]("103,Charlie,78"); [Link]();
[Link]();
[Link]("--- Written to file ---");

// READ sequentially
BufferedReader br = new BufferedReader(new FileReader(file));
[Link]("\nID Name Marks");
[Link]("--------------------");
String line;
while ((line = [Link]()) != null) {
String[] p = [Link](",");
[Link](p[0] + " " + p[1] + " " + p[2]);
}
[Link]();

new File(file).delete();
}
}

Sample Output:
--- Written to file ---

ID Name Marks
--------------------
101 Alice 92
102 Bob 85
103 Charlie 78
Program 12: Random File Access
Aim:
To read and write at any specific position in a file using RandomAccessFile.
Java Program:
import [Link].*;

public class RandomFileAccess {


public static void main(String[] args) throws Exception {

String file = "[Link]";


int recordSize = 20;

// Write 3 fixed-size records


RandomAccessFile raf = new RandomAccessFile(file, "rw");
[Link]([Link]("%-20s", "Alice-92"));
[Link]([Link]("%-20s", "Bob-85"));
[Link]([Link]("%-20s", "Charlie-78"));
[Link]();
[Link]("Records written.\n");

// Read record at position 1 (Bob) directly


raf = new RandomAccessFile(file, "r");
[Link](1 * recordSize); // jump to record 1
byte[] buf = new byte[recordSize];
[Link](buf);
[Link]("Record at position 1: " + new String(buf).trim());
[Link]();

// Update record at position 0


raf = new RandomAccessFile(file, "rw");
[Link](0);
[Link]([Link]("%-20s", "Alice-99"));
[Link]();

// Read all records


raf = new RandomAccessFile(file, "r");
[Link]("\nAll records after update:");
for (int i = 0; i < 3; i++) {
[Link](i * recordSize);
[Link](buf);
[Link]("Record " + i + ": " + new String(buf).trim());
}
[Link]();
new File(file).delete();
}
}

Sample Output:
Records written.

Record at position 1: Bob-85

All records after update:


Record 0: Alice-99
Record 1: Bob-85
Record 2: Charlie-78
Program 13: Clock Synchronization
Aim:
To simulate Lamport's Logical Clock for clock synchronization in distributed systems.
Java Program:
public class ClockSynchronization {

static int clock1 = 0, clock2 = 0, clock3 = 0;

static void event(String process, int clock, String action) {


[Link](process + " [Clock=" + clock + "] " + action);
}

public static void main(String[] args) {

[Link]("=== Lamport Logical Clock ===\n");

// Process 1 - internal event


clock1++;
event("P1", clock1, "Internal event");

// Process 2 - internal event


clock2++;
event("P2", clock2, "Internal event");

// P1 sends to P2
clock1++;
event("P1", clock1, "SEND to P2");
clock2 = [Link](clock1, clock2) + 1; // P2 updates
event("P2", clock2, "RECV from P1");

// P2 sends to P3
clock2++;
event("P2", clock2, "SEND to P3");
clock3 = [Link](clock2, clock3) + 1;
event("P3", clock3, "RECV from P2");

// P3 sends to P1
clock3++;
event("P3", clock3, "SEND to P1");
clock1 = [Link](clock3, clock1) + 1;
event("P1", clock1, "RECV from P3");

[Link]("\nFinal Clocks -> P1:" + clock1


+ " P2:" + clock2 + " P3:" + clock3);
}
}

Sample Output:
=== Lamport Logical Clock ===

P1 [Clock=1] Internal event


P2 [Clock=1] Internal event
P1 [Clock=2] SEND to P2
P2 [Clock=3] RECV from P1
P2 [Clock=4] SEND to P3
P3 [Clock=5] RECV from P2
P3 [Clock=6] SEND to P1
P1 [Clock=7] RECV from P3
Final Clocks -> P1:7 P2:4 P3:6
Program 14: Distributed Scheduling
Aim:
To simulate distributed task scheduling using Least Loaded node first strategy.
Java Program:
public class DistributedScheduling {

static String[] nodes = {"Node-A", "Node-B", "Node-C"};


static int[] capacity = {100, 80, 120};
static int[] load = {0, 0, 0};

// Assign task to least loaded node that has space


static void schedule(String task, int taskLoad) {
int best = -1;
for (int i = 0; i < [Link]; i++) {
if (load[i] + taskLoad <= capacity[i]) {
if (best == -1 || load[i] < load[best])
best = i;
}
}
if (best == -1) {
[Link](task + " (load=" + taskLoad + ") -> REJECTED");
} else {
load[best] += taskLoad;
[Link](task + " (load=" + taskLoad + ") -> " +
nodes[best]);
}
}

public static void main(String[] args) {

[Link]("=== Distributed Scheduling ===\n");

schedule("Task-1", 30);
schedule("Task-2", 50);
schedule("Task-3", 20);
schedule("Task-4", 70);
schedule("Task-5", 40);
schedule("Task-6", 60);

[Link]("\nNode Load Capacity");


[Link]("------------------------");
for (int i = 0; i < [Link]; i++)
[Link](nodes[i] + " " + load[i] + " " + capacity[i]);
}
}

Sample Output:
=== Distributed Scheduling ===

Task-1 (load=30) -> Node-A


Task-2 (load=50) -> Node-B
Task-3 (load=20) -> Node-A
Task-4 (load=70) -> Node-C
Task-5 (load=40) -> Node-A
Task-6 (load=60) -> Node-C

Node Load Capacity


------------------------
Node-A 90 100
Node-B 50 80
Node-C 130 120

You might also like