// Constants and global variables
const int NUM_PROCESSES = 10; // Number of processes
const int LED_PINS[NUM_PROCESSES] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // LED pins
int arrivalTime[NUM_PROCESSES]; // Arrival time for each process
int burstTime[NUM_PROCESSES]; // Burst time for each process
int remainingTime[NUM_PROCESSES]; // Remaining time for each process (used in RR)
int schedulingAlgorithm = 0; // Variable to store the selected algorithm as an integer
int timeQuantum; // Time quantum for RR scheduling
int currentTime = 0; // Simulation time
// Setup function
void setup() {
Serial.begin(9600);
// Set up LED pins as output
for (int i = 0; i < NUM_PROCESSES; i++) {
pinMode(LED_PINS[i], OUTPUT);
digitalWrite(LED_PINS[i], LOW);
}
Serial.println("Welcome! Please enter the process parameters.");
// Input process parameters from Serial Monitor
for (int i = 0; i < NUM_PROCESSES; i++) {
Serial.print("Enter arrival time for process ");
Serial.print(i + 1);
Serial.println(":");
while (Serial.available() == 0) {} // Wait for input
arrivalTime[i] = Serial.parseInt();
Serial.print("Enter burst time for process ");
Serial.print(i + 1);
Serial.println(":");
while (Serial.available() == 0) {} // Wait for input
burstTime[i] = Serial.parseInt();
remainingTime[i] = burstTime[i];
}
// Choose scheduling algorithm
Serial.println("Select scheduling algorithm:");
Serial.println("1: FCFS");
Serial.println("2: SJF");
Serial.println("3: HRRN");
Serial.println("4: RR");
while (Serial.available() == 0) {}
schedulingAlgorithm = Serial.parseInt();
// If Round Robin is selected, ask for time quantum
if (schedulingAlgorithm == 4) {
Serial.println("Enter time quantum:");
while (Serial.available() == 0) {}
timeQuantum = Serial.parseInt();
}
}
// Helper functions
void executeFCFS() {
// First Come, First Serve scheduling logic
for (int i = 0; i < NUM_PROCESSES; i++) {
int startTime = max(currentTime, arrivalTime[i]);
int endTime = startTime + burstTime[i];
Serial.print("Process ");
Serial.print(i + 1);
Serial.print(" executed from ");
Serial.print(startTime);
Serial.print(" to ");
Serial.println(endTime);
// Simulate execution on LED
currentTime = startTime;
executeProcessOnLED(i, burstTime[i]);
currentTime += burstTime[i];
}
}
void executeSJF() {
// Shortest Job First scheduling logic
bool completed[NUM_PROCESSES] = {false};
for (int count = 0; count < NUM_PROCESSES; count++) {
int shortestJob = -1;
int shortestTime = 32767; // Use a large value to represent max int
for (int i = 0; i < NUM_PROCESSES; i++) {
if (!completed[i] && arrivalTime[i] <= currentTime && burstTime[i] < shortestTime) {
shortestJob = i;
shortestTime = burstTime[i];
}
}
if (shortestJob != -1) {
int startTime = currentTime;
int endTime = startTime + burstTime[shortestJob];
Serial.print("Process ");
Serial.print(shortestJob + 1);
Serial.print(" executed from ");
Serial.print(startTime);
Serial.print(" to ");
Serial.println(endTime);
executeProcessOnLED(shortestJob, burstTime[shortestJob]);
completed[shortestJob] = true;
currentTime = endTime;
}
}
}
void executeHRRN() {
// Highest Response Ratio Next scheduling logic
bool completed[NUM_PROCESSES] = {false};
for (int count = 0; count < NUM_PROCESSES; count++) {
int highestRatioIndex = -1;
float highestRatio = -1.0;
for (int i = 0; i < NUM_PROCESSES; i++) {
if (!completed[i] && arrivalTime[i] <= currentTime) {
float responseRatio = (float)(currentTime - arrivalTime[i] + burstTime[i]) / burstTime[i];
if (responseRatio > highestRatio) {
highestRatio = responseRatio;
highestRatioIndex = i;
}
}
}
if (highestRatioIndex != -1) {
int startTime = currentTime;
int endTime = startTime + burstTime[highestRatioIndex];
Serial.print("Process ");
Serial.print(highestRatioIndex + 1);
Serial.print(" executed from ");
Serial.print(startTime);
Serial.print(" to ");
Serial.println(endTime);
executeProcessOnLED(highestRatioIndex, burstTime[highestRatioIndex]);
completed[highestRatioIndex] = true;
currentTime = endTime;
}
}
}
void executeRR() {
// Round Robin scheduling logic
bool completed[NUM_PROCESSES] = {false};
int processesCompleted = 0;
while (processesCompleted < NUM_PROCESSES) {
for (int i = 0; i < NUM_PROCESSES; i++) {
if (arrivalTime[i] <= currentTime && remainingTime[i] > 0) {
int execTime = min(timeQuantum, remainingTime[i]);
int startTime = currentTime;
int endTime = startTime + execTime;
Serial.print("Process ");
Serial.print(i + 1);
Serial.print(" executed from ");
Serial.print(startTime);
Serial.print(" to ");
Serial.println(endTime);
executeProcessOnLED(i, execTime);
currentTime = endTime;
remainingTime[i] -= execTime;
if (remainingTime[i] == 0) {
completed[i] = true;
processesCompleted++;
}
}
}
}
}
void executeProcessOnLED(int processIndex, int duration) {
digitalWrite(LED_PINS[processIndex], HIGH);
delay(duration * 1000); // Convert seconds to milliseconds
digitalWrite(LED_PINS[processIndex], LOW);
}
void loop() {
// Using switch-case for scheduling algorithm selection
switch (schedulingAlgorithm) {
case 1: // FCFS
executeFCFS();
break;
case 2: // SJF
executeSJF();
break;
case 3: // HRRN
executeHRRN();
break;
case 4: // RR
executeRR();
break;
default:
Serial.println("Invalid scheduling algorithm selected.");
break;
}
// Prevent further execution after completing the chosen algorithm
while (1);
}