| 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <iostream> #include <vector> #include <algorithm> struct Process {     int id;     int arrivalTime;     int burstTime;     int waitingTime;     int turnaroundTime; }; // Function to calculate waiting time and turnaround time for FCFS void calculateFCFS(std::vector<Process>& processes) {     int n = processes.size();     processes[0].waitingTime = 0; // First process has no waiting time     processes[0].turnaroundTime = processes[0].burstTime;     for (int i = 1; i < n; ++i) {         processes[i].waitingTime = processes[i-1].waitingTime + processes[i-1].burstTime;         processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;     } } // Function to calculate waiting time and turnaround time for SJF void calculateSJF(std::vector<Process>& processes) {     int n = processes.size();     std::sort(processes.begin(), processes.end(), [](const Process& a, const Process& b) {         return a.burstTime < b.burstTime;     });     processes[0].waitingTime = 0;     processes[0].turnaroundTime = processes[0].burstTime;     for (int i = 1; i < n; ++i) {         processes[i].waitingTime = processes[i-1].waitingTime + processes[i-1].burstTime;         processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;     } } // Function to print the process details void printProcessDetails(const std::vector<Process>& processes, const std::string& algorithm) {     std::cout << algorithm << " Scheduling\n";     std::cout << "ID\tArrival\tBurst\tWaiting\tTurnaround\n";     for (const auto& p : processes) {         std::cout << p.id << '\t' << p.arrivalTime << '\t' << p.burstTime << '\t'                   << p.waitingTime << '\t' << p.turnaroundTime << '\n';     }     std::cout << std::endl; } int main() {     std::vector<Process> processes = {         {1, 0, 8, 0, 0},         {2, 1, 4, 0, 0},         {3, 2, 9, 0, 0},         {4, 3, 5, 0, 0}     };     // FCFS Scheduling     std::vector<Process> fcfsProcesses = processes;     calculateFCFS(fcfsProcesses);     printProcessDetails(fcfsProcesses, "FCFS");     // SJF Scheduling     std::vector<Process> sjfProcesses = processes;     calculateSJF(sjfProcesses);     printProcessDetails(sjfProcesses, "SJF");     return 0; } | 
Explanation:
- Process Structure:
- Contains process details: id,arrivalTime,burstTime,waitingTime, andturnaroundTime.
 
- Contains process details: 
- calculateFCFS Function:
- Calculates waiting and turnaround times for the First-Come, First-Served scheduling.
- The first process has zero waiting time, and each subsequent process waits for the previous one to complete.
 
- calculateSJF Function:
- Calculates waiting and turnaround times for the Shortest Job First scheduling.
- Processes are sorted by burstTime, then FCFS scheduling is applied to the sorted list.
 
- printProcessDetails Function:
- Prints the process details in tabular format.
 
- main Function:
- Initializes a list of processes with IDs, arrival times, and burst times.
- Calls calculateFCFSandcalculateSJFto compute scheduling metrics.
- Prints the results for both scheduling algorithms.
 Compilation:To compile the program, use: 
 Run the program with:1g++ cpu_scheduling.cpp -o cpu_scheduling
 1./cpu_scheduling
 
