In this step, we'll implement the functionality to collect information about the processes to be scheduled. For FCFS scheduling, we need to know:
- The number of processes
- The burst time for each process (execution time required by each process)
Let's modify our fcfs.cpp file to include this functionality:
#include <iostream>
#include <iomanip> // For formatted output
using namespace std;
int main() {
cout << "FCFS Scheduling Algorithm Implementation" << endl;
cout << "----------------------------------------" << endl;
// Variables declaration
int n; // Number of processes
int burst_time[20]; // Array to store burst time of each process
// Get the number of processes from the user
cout << "\nEnter the number of processes (maximum 20): ";
cin >> n;
// Input validation
if (n <= 0 || n > 20) {
cout << "Invalid number of processes. Please enter a value between 1 and 20." << endl;
return 1;
}
// Get burst time for each process
cout << "\nEnter the Burst Time for each process:" << endl;
for (int i = 0; i < n; i++) {
cout << "Process P" << i + 1 << ": ";
cin >> burst_time[i];
// Input validation for burst time
if (burst_time[i] <= 0) {
cout << "Burst time must be a positive integer. Please restart the program." << endl;
return 1;
}
}
// Display the entered data
cout << "\nProcess\tBurst Time" << endl;
cout << "--------------------" << endl;
for (int i = 0; i < n; i++) {
cout << "P" << i + 1 << "\t" << burst_time[i] << endl;
}
return 0;
}
Let's go through what we've added:
- Variables to store the number of processes (
n) and the burst time for each process (burst_time[20]).
- Input prompts to collect the number of processes and the burst time for each process.
- Input validation to ensure the number of processes is within a reasonable range (1-20) and burst times are positive.
- A table display to show the entered data for verification.
Let's compile and run our updated program:
g++ fcfs.cpp -o fcfs
./fcfs
When you run the program, you'll be prompted to enter the number of processes and their burst times. Try entering the following data:
3
5
9
4
This represents 3 processes with burst times of 5, 9, and 4 time units respectively. You should see output similar to:
FCFS Scheduling Algorithm Implementation
----------------------------------------
Enter the number of processes (maximum 20): 3
Enter the Burst Time for each process:
Process P1: 5
Process P2: 9
Process P3: 4
Process Burst Time
--------------------
P1 5
P2 9
P3 4
This confirms that our input functionality is working correctly. In the next step, we'll calculate the waiting time and turnaround time for each process.