diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2125666 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto \ No newline at end of file diff --git a/CloudKernel/README.md b/CloudKernel/README.md new file mode 100644 index 0000000..8ffa132 --- /dev/null +++ b/CloudKernel/README.md @@ -0,0 +1,181 @@ +# CloudKernel ☁️⚙️ + +## Overview + +**CloudKernel** is a small Java-based simulation created for our **Operating Systems Lab**. +The purpose of this project is to show how a hypervisor-like system can manage multiple **Virtual Machines (VMs)** while coordinating shared resources. + +Instead of building a real operating system, this project focuses on demonstrating **important OS concepts** like synchronization, resource sharing, and concurrent execution using Java threads. + +The program simulates a system where several virtual machines start after the system boots, run tasks together, and share limited network resources. + +--- + +## 🎓 Academic Information + +**Course:** Operating Systems Lab +**Semester:** 4th Semester + +**Submitted to:** +Mam Amara Nadeem + +**Submitted by:** + +- **Moavia Amir** (2k24_BSAI_72) +- **Ali Raza** (2k24_BSAI_44) +- **Muhammad Arslan Nasir** (2k24_BSAI_26) + +**Submission Date:** +March 03, 2026 + +--- + +## 🎯 Project Goals + +This project was designed to help understand how operating systems manage: + +- System boot coordination +- Thread synchronization +- Limited resource sharing +- Parallel execution of processes + +All these ideas are implemented using **Java concurrency utilities**. + +--- + +## ⚙️ Key Concepts Used + +### 1. System Boot Coordination + +Before any virtual machine starts running, the system must finish its boot process. + +We simulate this using **CountDownLatch**. +It ensures that resources like **Disk and RAM** are ready before the virtual machines begin execution. + +--- + +### 2. VM Cycle Synchronization + +Each virtual machine performs its work in cycles. +To keep them synchronized, we use **CyclicBarrier**. + +This means all VMs must finish a cycle before the next one begins. + +--- + +### 3. Limited Network Access + +In real systems, hardware resources are limited. +In this simulation, only **two VMs can use the network at the same time**. + +This is managed using a **Semaphore**, which controls access to the shared network ports. + +--- + +## 🧩 Project Structure +``` +CloudKernel +│ +├── src +│ │ +│ ├── Main.java +│ │ +│ ├── core +│ │ ├── BootManager.java +│ │ ├── ClockSynchronizer.java +│ │ └── NetworkPortManager.java +│ │ +│ ├── entities +│ │ └── VirtualMachine.java +│ │ +│ └── utils +│ └── Logger.java +│ +├── doc +│ └── proposal +│ +└── README.md +``` +--- + +## 🏗 System Workflow + +The program runs in the following order: + +``` +System Boot +│ +▼ +BootManager initializes resources +│ +▼ +Virtual Machines start (Threads) +│ +▼ +VMs execute cycles together +│ +▼ +Network access controlled by Semaphore +│ +▼ +Logs printed to terminal +``` + +--- + +## ▶️ How to Run the Project + +### 1. Compile the project + +```bash +javac -d out -sourcepath src src/Main.java +``` +### 2. Run the program +```bash +java -cp out Main +``` +## 🖥 Example Output + +When the program runs, you may see output like: +``` +[BOOT] Disk initialized +[BOOT] RAM initialized +[BOOT] System ready + +[VM-1] Starting execution +[VM-2] Starting execution +[VM-3] Starting execution + +[VM-1] Requesting network access +[VM-2] Requesting network access + +[VM-1] Using network port +[VM-2] Using network port + +[VM-3] Waiting for network port +``` +The Logger class keeps the output organized so it is easier to read. + +## 🧠 What We Learned + +While building this project, we understood how operating systems handle: + ++ **Thread** synchronization + ++ **Shared** resource management + ++ **Parallel** execution + ++ **Process** coordination + +These concepts are important for understanding how real operating systems and cloud platforms work. + +--- + +## 📌 Conclusion + +CloudKernel is a simple educational simulation that demonstrates how a hypervisor-like system can coordinate virtual machines and manage shared resources. + +Although it is a simplified model, it provides a clear understanding of synchronization and concurrency in operating systems. + +--- \ No newline at end of file diff --git a/CloudKernel/doc/CloudKernel_ProjectReport.pdf b/CloudKernel/doc/CloudKernel_ProjectReport.pdf new file mode 100644 index 0000000..8b8891e Binary files /dev/null and b/CloudKernel/doc/CloudKernel_ProjectReport.pdf differ diff --git a/CloudKernel/out/Main.class b/CloudKernel/out/Main.class new file mode 100644 index 0000000..745078b Binary files /dev/null and b/CloudKernel/out/Main.class differ diff --git a/CloudKernel/out/core/BootManager.class b/CloudKernel/out/core/BootManager.class new file mode 100644 index 0000000..4c9ffa3 Binary files /dev/null and b/CloudKernel/out/core/BootManager.class differ diff --git a/CloudKernel/out/core/ClockSynchronizer.class b/CloudKernel/out/core/ClockSynchronizer.class new file mode 100644 index 0000000..65ce430 Binary files /dev/null and b/CloudKernel/out/core/ClockSynchronizer.class differ diff --git a/CloudKernel/out/core/NetworkPortManager.class b/CloudKernel/out/core/NetworkPortManager.class new file mode 100644 index 0000000..cf004f0 Binary files /dev/null and b/CloudKernel/out/core/NetworkPortManager.class differ diff --git a/CloudKernel/out/entities/VirtualMachine.class b/CloudKernel/out/entities/VirtualMachine.class new file mode 100644 index 0000000..3a8e891 Binary files /dev/null and b/CloudKernel/out/entities/VirtualMachine.class differ diff --git a/CloudKernel/out/utils/Logger.class b/CloudKernel/out/utils/Logger.class new file mode 100644 index 0000000..9ba1646 Binary files /dev/null and b/CloudKernel/out/utils/Logger.class differ diff --git a/CloudKernel/src/Main.java b/CloudKernel/src/Main.java new file mode 100644 index 0000000..44ceb3a --- /dev/null +++ b/CloudKernel/src/Main.java @@ -0,0 +1,58 @@ +import core.BootManager; +import core.ClockSynchronizer; +import core.NetworkPortManager; +import entities.VirtualMachine; +import utils.Logger; + +// Entry point for the CloudKernel simulation. +public class Main { + + private static final int NUM_VMS = 3; + private static final int NUM_CYCLES = 2; + + public static void main(String[] args) throws InterruptedException { + // Phase 1: boot + Logger.section("PHASE 1: SYSTEM BOOT [CountDownLatch]"); + Logger.log("HYPERVISOR", "CloudKernel v1.0 starting...", Logger.BOLD + Logger.GREEN); + + BootManager bootManager = new BootManager(); + bootManager.initDisk(); + bootManager.initRAM(); + bootManager.awaitBootCompletion(); + + Thread.sleep(500); + + // Phase 2: VM execution + Logger.section("PHASE 2: VM EXECUTION [CyclicBarrier + Semaphore]"); + int[] cycleNum = { 0 }; + ClockSynchronizer clock = new ClockSynchronizer(NUM_VMS, cycleNum); + NetworkPortManager networkManager = new NetworkPortManager(); + + Logger.log("HYPERVISOR", + "Launching " + NUM_VMS + " VMs for " + NUM_CYCLES + " cycles each...", + Logger.CYAN); + + Thread[] vmThreads = new Thread[NUM_VMS]; + for (int i = 1; i <= NUM_VMS; i++) { + int workDuration = 600 + (i * 200); + + VirtualMachine vm = new VirtualMachine( + "VM-" + i, NUM_CYCLES, clock, networkManager, workDuration); + vmThreads[i - 1] = new Thread(vm, "VM-" + i); + vmThreads[i - 1].start(); + } + + for (Thread t : vmThreads) { + t.join(); + } + + // Phase 3: shutdown + Logger.section("PHASE 3: SYSTEM SHUTDOWN"); + Logger.log("HYPERVISOR", "All VMs have completed execution.", Logger.GREEN); + Logger.log("HYPERVISOR", "Releasing all system resources...", Logger.YELLOW); + Thread.sleep(300); + Logger.log("HYPERVISOR", "CloudKernel has shut down cleanly. Goodbye. [OK]", Logger.BOLD + Logger.GREEN); + Logger.separator(); + System.out.println(); + } +} \ No newline at end of file diff --git a/CloudKernel/src/core/BootManager.java b/CloudKernel/src/core/BootManager.java new file mode 100644 index 0000000..7a7697a --- /dev/null +++ b/CloudKernel/src/core/BootManager.java @@ -0,0 +1,45 @@ +package core; + +import utils.Logger; +import java.util.concurrent.CountDownLatch; + +// Handles system boot readiness using CountDownLatch. +public class BootManager { + + private final CountDownLatch bootLatch = new CountDownLatch(2); + + public void initDisk() { + new Thread(() -> { + try { + Logger.log("BOOT", "Disk subsystem starting...", Logger.YELLOW); + Thread.sleep(1500); + Logger.log("BOOT", "Disk subsystem initialized. [OK]", Logger.GREEN); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + // Count down in finally to avoid deadlock on errors. + bootLatch.countDown(); + } + }, "Disk-Init-Thread").start(); + } + + public void initRAM() { + new Thread(() -> { + try { + Logger.log("BOOT", "RAM subsystem starting...", Logger.YELLOW); + Thread.sleep(1000); + Logger.log("BOOT", "RAM subsystem initialized. [OK]", Logger.GREEN); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + bootLatch.countDown(); + } + }, "RAM-Init-Thread").start(); + } + + public void awaitBootCompletion() throws InterruptedException { + Logger.log("BOOT", "Hypervisor waiting for subsystems...", Logger.CYAN); + bootLatch.await(); + Logger.log("BOOT", "All subsystems ready. CloudKernel is ONLINE. [OK]", Logger.GREEN); + } +} \ No newline at end of file diff --git a/CloudKernel/src/core/ClockSynchronizer.java b/CloudKernel/src/core/ClockSynchronizer.java new file mode 100644 index 0000000..cfbf217 --- /dev/null +++ b/CloudKernel/src/core/ClockSynchronizer.java @@ -0,0 +1,28 @@ +package core; + +import utils.Logger; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +// Keeps all VM threads synchronized at each cycle. +public class ClockSynchronizer { + + private final CyclicBarrier barrier; + + public ClockSynchronizer(int vmCount, int[] cycleNum) { + Runnable clockTick = () -> { + cycleNum[0]++; + Logger.log("CLOCK", + "=== Global Clock Tick #" + cycleNum[0] + + " - All VMs synchronized. Next cycle begins. ===", + Logger.BOLD + Logger.CYAN); + }; + + this.barrier = new CyclicBarrier(vmCount, clockTick); + } + + public void sync(String vmName) throws InterruptedException, BrokenBarrierException { + Logger.log(vmName, "Work unit done. Waiting at clock barrier...", Logger.YELLOW); + barrier.await(); + } +} \ No newline at end of file diff --git a/CloudKernel/src/core/NetworkPortManager.java b/CloudKernel/src/core/NetworkPortManager.java new file mode 100644 index 0000000..9cb5c4a --- /dev/null +++ b/CloudKernel/src/core/NetworkPortManager.java @@ -0,0 +1,31 @@ +package core; + +import utils.Logger; +import java.util.concurrent.Semaphore; + +// Manages limited network ports using a fair semaphore. +public class NetworkPortManager { + + private static final int TOTAL_PORTS = 2; + private final Semaphore networkPorts = new Semaphore(TOTAL_PORTS, true); + + public void acquirePort(String vmName) throws InterruptedException { + Logger.log(vmName, + "Requesting Network Port... (available: " + networkPorts.availablePermits() + "/" + TOTAL_PORTS + ")", + Logger.YELLOW); + + networkPorts.acquire(); + int inUse = TOTAL_PORTS - networkPorts.availablePermits(); + + Logger.log(vmName, + "Network Port GRANTED. (in use: " + inUse + "/" + TOTAL_PORTS + ") Transmitting data...", + Logger.GREEN); + } + + public void releasePort(String vmName) { + networkPorts.release(); + Logger.log(vmName, + "Network Port RELEASED. (available: " + networkPorts.availablePermits() + "/" + TOTAL_PORTS + ")", + Logger.CYAN); + } +} \ No newline at end of file diff --git a/CloudKernel/src/entities/VirtualMachine.java b/CloudKernel/src/entities/VirtualMachine.java new file mode 100644 index 0000000..eb0bfd8 --- /dev/null +++ b/CloudKernel/src/entities/VirtualMachine.java @@ -0,0 +1,51 @@ +package entities; + +import core.ClockSynchronizer; +import core.NetworkPortManager; +import utils.Logger; +import java.util.concurrent.BrokenBarrierException; + +// Represents one VM thread in the simulation. +public class VirtualMachine implements Runnable { + + private final String name; + private final int cycles; + private final ClockSynchronizer clock; + private final NetworkPortManager networkManager; + private final int workDuration; + + public VirtualMachine(String name, int cycles, ClockSynchronizer clock, + NetworkPortManager networkManager, int workDuration) { + this.name = name; + this.cycles = cycles; + this.clock = clock; + this.networkManager = networkManager; + this.workDuration = workDuration; + } + + @Override + public void run() { + Logger.log(name, "Virtual Machine is ONLINE.", Logger.GREEN); + + try { + for (int i = 1; i <= cycles; i++) { + Logger.log(name, "Cycle " + i + " - executing workload...", Logger.CYAN); + Thread.sleep(workDuration); + + networkManager.acquirePort(name); + Thread.sleep(500); + networkManager.releasePort(name); + + clock.sync(name); + } + + Logger.log(name, "All cycles complete. Shutting down gracefully. [OK]", Logger.GREEN); + + } catch (InterruptedException e) { + Logger.log(name, "Interrupted during execution!", Logger.RED); + Thread.currentThread().interrupt(); + } catch (BrokenBarrierException e) { + Logger.log(name, "Clock barrier broken - system error!", Logger.RED); + } + } +} \ No newline at end of file diff --git a/CloudKernel/src/utils/Logger.java b/CloudKernel/src/utils/Logger.java new file mode 100644 index 0000000..84495e3 --- /dev/null +++ b/CloudKernel/src/utils/Logger.java @@ -0,0 +1,38 @@ +package utils; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + +// Small utility for formatted console logging. +public class Logger { + + private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss"); + + public static final String RESET = "\u001B[0m"; + public static final String GREEN = "\u001B[32m"; + public static final String CYAN = "\u001B[36m"; + public static final String YELLOW = "\u001B[33m"; + public static final String RED = "\u001B[31m"; + public static final String BOLD = "\u001B[1m"; + + private Logger() { + // Prevent instantiation. + } + + public static void log(String tag, String message, String color) { + String timestamp = LocalTime.now().format(TIME_FORMAT); + System.out.printf("%s[%s] %-12s%s %s%n", + color, timestamp, "[" + tag + "]", RESET, message); + } + + public static void separator() { + System.out.println(BOLD + "-".repeat(65) + RESET); + } + + public static void section(String title) { + System.out.println(); + separator(); + System.out.println(BOLD + " " + title + RESET); + separator(); + } +} \ No newline at end of file diff --git a/ResQTemp/esp_web/.ipynb_checkpoints/main-checkpoint.html b/ResQTemp/esp_web/.ipynb_checkpoints/main-checkpoint.html new file mode 100644 index 0000000..c404297 --- /dev/null +++ b/ResQTemp/esp_web/.ipynb_checkpoints/main-checkpoint.html @@ -0,0 +1,85 @@ + + +
+ +