0% found this document useful (0 votes)
33 views10 pages

Operating System Lab: CPU & Memory Management

Uploaded by

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

Operating System Lab: CPU & Memory Management

Uploaded by

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

UNIVERSITI KUALA LUMPUR CITY CAMPUS

MALAYSIAN INSTITUTE OF INFORMATION TECHNOLOGY

Name of Course OPERATING SYSTEM


Course Code ITD13803
Lecturers Muhammad Rizal
Semester / Year Oct 2024
Week 4
Date 9Nov2024

Assessment Lab Activity : Operating System Lab: CPU, Memory, and I/O
Processes

Submission Submit to VLE week 4

Objective:

The objective of this lab session is to provide students with hands-on experience and practical
understanding of CPU scheduling algorithms, memory management techniques, and I/O process
handling in operating systems using Java.

Equipment/Software Required:

1. Personal computers or laptops with any operating system installed (preferably Linux or
Windows)
2. Java Development Kit (JDK) installed
3. Integrated Development Environment (IDE) such as Eclipse or IntelliJ IDEA
4. Programming languages like Java for coding exercises

Duration:

2 hours

Lab Tasks:

1. CPU Scheduling:
 Introduction to CPU scheduling algorithms (e.g., FCFS, SJF, Round Robin, Priority
Scheduling)
 Implement a CPU scheduler simulator in Java:
o Create processes with arrival time, burst time, and priority
o Implement one or more scheduling algorithms
o Simulate the execution of processes and calculate average waiting time and
turnaround time
 Discuss the performance comparison of different scheduling algorithms.

2. Memory Management:

 Overview of memory management techniques (e.g., Paging, Segmentation, Virtual


Memory)
 Hands-on with memory allocation algorithms:
o Implement a simple memory allocation simulator in Java:
 Simulate memory allocation using techniques like first fit, best fit, worst
fit
 Demonstrate memory allocation and deallocation operations
 Discuss the advantages and disadvantages of different memory allocation techniques.

3. I/O Process Handling:

 Introduction to I/O devices and I/O operations


 Implement I/O process handling in a simple operating system using Java:
o Simulate I/O-bound processes and CPU-bound processes
o Implement interrupt handling for I/O operations
 Discuss the importance of buffering, caching, and spooling in I/O operations.

4. Integration and Experimentation:

 Integrate CPU scheduling, memory management, and I/O process handling components
into a single operating system simulator using Java.
 Run experiments with different combinations of CPU scheduling algorithms, memory
allocation techniques, and I/O process handling strategies.
 Analyze and discuss the performance metrics such as throughput, response time, and
resource utilization.

Additional Activities:
 Conduct a group discussion on recent advancements and challenges in CPU, Memory,
and I/O management in modern operating systems.
 Assign research projects or presentations on topics related to real-world operating
system designs and implementations.
 Provide coding assignments or challenges to reinforce understanding and
implementation skills.

Conclusion:

The lab session provides students with practical insights into CPU scheduling, memory
management, and I/O process handling in operating systems using Java. By implementing
simulators and conducting experiments, students gain a deeper understanding of the core
concepts and challenges in operating system design and management. This hands-on experience
prepares them for real-world scenarios and fosters critical thinking and problem-solving skills.

Sample code
Output
Code

import java.util.ArrayList;
import java.util.Collections;

class Process {
int pid;
int arrivalTime;
int burstTime;
int priority;

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


this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
}
}

public class OperatingSystemSimulator {

// CPU Scheduling Simulator


public static void simulateCPUScheduling(ArrayList<Process> processes) {
Collections.sort(processes, (p1, p2) -> p1.arrivalTime - p2.arrivalTime);

int currentTime = 0;
for (Process process : processes) {
if (process.arrivalTime > currentTime) {
currentTime = process.arrivalTime;
}
System.out.println("Process " + process.pid + " started at time " + currentTime);
currentTime += process.burstTime;
System.out.println("Process " + process.pid + " completed at time " + currentTime);
}
}

// Memory Allocation Simulator


public static void simulateMemoryAllocation() {
// Implement memory allocation simulation logic here
System.out.println("Memory allocation simulation");
}
// I/O Process Handling Simulator
public static void simulateIOHandling() {
// Implement I/O handling simulation logic here
System.out.println("I/O handling simulation");
}

public static void main(String[] args) {


// CPU Scheduling Simulation
ArrayList<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 8, 3));
processes.add(new Process(2, 1, 4, 1));
processes.add(new Process(3, 2, 9, 2));
System.out.println("CPU Scheduling Simulation:");
simulateCPUScheduling(processes);

// Memory Allocation Simulation


System.out.println("\nMemory Allocation Simulation:");
simulateMemoryAllocation();

// I/O Process Handling Simulation


System.out.println("\nI/O Process Handling Simulation:");
simulateIOHandling();
}
}
Alternative CPU Scheduling algorithm sample

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Process {
int pid;
int arrivalTime;
int burstTime;
int remainingTime;

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


this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.remainingTime = burstTime;
}
}

public class RoundRobinScheduler {


// Round Robin CPU Scheduling Simulator
public static void simulateRoundRobinScheduling(ArrayList<Process> processes, int
timeQuantum) {
Queue<Process> queue = new LinkedList<>();
int currentTime = 0;
int processIndex = 0;

// Add processes that have arrived at the initial time


while (processIndex < processes.size() && processes.get(processIndex).arrivalTime <=
currentTime) {
queue.add(processes.get(processIndex));
processIndex++;
}

while (!queue.isEmpty()) {
Process currentProcess = queue.poll();

// Simulate the process running


System.out.println("Process " + currentProcess.pid + " started at time " + currentTime);

int timeSlice = Math.min(currentProcess.remainingTime, timeQuantum);


currentProcess.remainingTime -= timeSlice;
currentTime += timeSlice;

if (currentProcess.remainingTime > 0) {
System.out.println("Process " + currentProcess.pid + " paused at time " + currentTime);
queue.add(currentProcess); // Requeue the process if it has remaining time
} else {
System.out.println("Process " + currentProcess.pid + " completed at time " +
currentTime);
}

// Add new processes to the queue that have arrived while the current process was
running
while (processIndex < processes.size() && processes.get(processIndex).arrivalTime <=
currentTime) {
queue.add(processes.get(processIndex));
processIndex++;
}
}
}

public static void main(String[] args) {


// Round Robin CPU Scheduling Simulation
ArrayList<Process> processes = new ArrayList<>();
processes.add(new Process(1, 0, 8));
processes.add(new Process(2, 1, 4));
processes.add(new Process(3, 2, 9));
processes.add(new Process(4, 3, 5));

int timeQuantum = 3;
System.out.println("Round Robin CPU Scheduling Simulation with Time Quantum = " +
timeQuantum);
simulateRoundRobinScheduling(processes, timeQuantum);
}
}
Explanation of Code:
1. Queue of Processes: A queue (LinkedList) is used to handle the processes in a cyclic
order.
2. Time Quantum: The timeQuantum specifies how much CPU time each process receives
in one cycle before switching to the next process.
3. Process Scheduling Logic:
 Each process runs for a minimum of its remainingTime or the timeQuantum.
 After running, if the process has any remaining burst time, it’s requeued.
 The simulation continues until all processes have completed.
4. Arrival Time Handling: The processIndex is used to add processes to the queue as they
arrive (based on the current currentTime).

Sample Output

Explanation of Output:
This output demonstrates how each process runs for a specified time quantum (in this case, 3
units) and then either completes or pauses if it has remaining burst time. The scheduler cycles
through each process until all are completed.

You might also like