1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream> #include <vector> #include <iomanip> // For std::fixed and std::setprecision struct Process { int id; // Process ID int arrival; // Arrival time (in milliseconds) int burst; // Burst time (in milliseconds) int start; // Start time (in milliseconds) int finish; // Finish time (in milliseconds) }; // Function to simulate FCFS scheduling void fcfsScheduling(std::vector<Process>& processes) { int currentTime = 0; std::cout << "Process ID\tArrival Time\tBurst Time\tStart Time\tFinish Time" << std::endl; for (auto& process : processes) { // Update start time and finish time process.start = std::max(currentTime, process.arrival); process.finish = process.start + process.burst; currentTime = process.finish; // Output process details std::cout << process.id << "\t\t" << process.arrival << "\t\t" << process.burst << "\t\t" << process.start << "\t\t" << process.finish << std::endl; } } int main() { int numProcesses; // Input number of processes std::cout << "Enter the number of processes: "; std::cin >> numProcesses; std::vector<Process> processes(numProcesses); for (int i = 0; i < numProcesses; ++i) { Process p; p.id = i + 1; // Assign process ID starting from 1 std::cout << "Enter arrival time for process " << p.id << " (milliseconds): "; std::cin >> p.arrival; std::cout << "Enter burst time for process " << p.id << " (milliseconds): "; std::cin >> p.burst; processes[i] = p; } // Call the scheduling function fcfsScheduling(processes); return 0; } |
Explanation:
- Headers and Structure:
- Includes
iostream
for input and output operations andiomanip
for formatting. - Defines the
Process
structure withid
,arrival
time,burst
time,start
time, andfinish
time.
- Includes
- fcfsScheduling Function:
- Takes a vector of
Process
structures as input. - Uses
currentTime
to keep track of the time as processes are scheduled. - Iterates through each process to determine its
start
andfinish
times:- The
start
time is the maximum of the current time and the process’s arrival time. - The
finish
time is thestart
time plus the burst time. - Updates
currentTime
to the finish time of the current process.
- The
- Prints out the details for each process, including its arrival, burst, start, and finish times.
- Takes a vector of
- main Function:
- Prompts the user to input the number of processes.
- Collects arrival and burst times for each process and initializes their attributes.
- Calls the
fcfsScheduling
function to simulate and display the scheduling results.