CPU Scheduling Algorithms in C
CPU Scheduling Algorithms in C
The FIFO page replacement algorithm is generally less efficient in reducing page faults compared to LRU and Optimal algorithms. FIFO replaces the oldest page in memory, which can lead to suboptimal decisions and more frequent page faults if the oldest page is frequently used. LRU improves on this by replacing the least recently used page, thereby better predicting future needs based on past usage, leading to fewer page faults. Optimal replacement theoretically offers the fewest page faults by replacing the page that will not be used for the longest time; however, it requires future knowledge. Thus, while FIFO is simple, its efficiency in minimizing page faults is lower than LRU and Optimal under most circumstances .
In Round Robin scheduling, each process receives a fixed time quantum in which to execute. If a process's burst time exceeds this quantum, the process is paused, and control passes to the next process in line. Processes with shorter burst times may complete in their initial quantum, minimizing their waiting time and turnaround time. The Round Robin approach handles variance in burst times by cycling through the ready queue, thus preventing longer processes from blocking others, which contrasts with FCFS, where such processes may bottleneck the system .
The FCFS algorithm calculates waiting time by accumulating burst times of preceding processes, leading to high waiting times if a long process precedes others . In contrast, the Round Robin algorithm uses time quanta to ensure that all processes receive equal CPU time early, reducing waiting times for shorter processes since the CPU cycles through each process avoiding long wait durations. While Round Robin might introduce more context switches, it tends to offer better average waiting times for shorter processes or when processes have varied burst times since it doesn't allow long processes to block the queue as in FCFS .
Choosing a specific CPU scheduling algorithm has significant implications for process execution time and overall system efficiency. FCFS can lead to long wait times under heavy loads or when processes are not evenly distributed in size, causing poor performance due to the convoy effect. SJF, while optimal in minimizing average waiting time under perfect conditions, is susceptible to starvation of longer processes. Round Robin provides fairness but increases context switches, possibly leading to inefficiencies if the time quantum is not optimally chosen. Priority-based scheduling offers flexibility but can cause low-priority process starvation. The choice of algorithm affects the balance between CPU utilization, throughput, turnaround time, and responsiveness, necessitating careful consideration of workload characteristics .
The primary challenge the Optimal Page Replacement algorithm faces is its requirement to foresee future requests in order to make the optimal decision about which page to replace. Unlike other strategies like LRU or FIFO, which use historical data to make decisions, the Optimal strategy needs access to or assumptions about future memory accesses to replace the page that will not be used for the longest time in the future. This requirement makes it impractical in real-world scenarios without predictive technology, as future page requests are typically unknown. However, it is used as a benchmark to measure the effectiveness of other algorithms .
The Shortest Job First (SJF) algorithm selects processes based on their burst times, always choosing the process with the shortest burst time available for execution next. Initially, processes and their burst times are input, and the algorithm then sorts these processes in ascending order of burst time. This sorting ensures that the next available process with the shortest execution requirement is selected, which minimizes waiting time across all processes, leading to a reduction in the average waiting time .
In the priority CPU scheduling algorithm, processes are executed based on their priority levels. The algorithm starts by taking input for burst time and priority for each process. It then sorts the processes in descending order of their priority values (i.e., higher priority values are selected first). The sorting is achieved by iterating over processes and swapping them based on their priority values. After sorting, the execution order follows the sorted priority, executing the process with the highest priority first and proceeding to the lower priorities .
In the FIFO page replacement algorithm, pages are replaced based on the order they were introduced into memory. When a new page needs to be loaded, and all frames are full, the processor replaces the oldest page—the one that has been in the frames the longest. This is managed by maintaining a queue of pages; when a page fault occurs and a new page is inserted, the page at the front of the queue (the oldest) is removed, and the new page is placed at the rear .
In the LRU page replacement algorithm, when a page fault occurs and the frames are fully occupied, the algorithm identifies the page in memory that has not been used for the longest period of time (the least recently used page). It does this by calculating the "distance" (i.e., time since last use) for each page in the current frames, finds the maximum distance, and replaces that page with the new page. If the page has been accessed more recently, this length of time (`distance[j]`) is smaller. By maintaining a list of distances and identifying the page with the largest value, the LRU algorithm identifies which page to swap out .
In FCFS CPU scheduling, the waiting time for each process is calculated by summing up the burst times of all preceding processes. Specifically, `wt[0]` is initially `0`, and for each subsequent process `i`, `wt[i]` is calculated as the burst time of the previous process plus the waiting time of the previous process (`wt[i] = bt[i-1] + wt[i-1]`). The turnaround time for each process is then calculated by adding its burst time to its waiting time (`tat[i] = bt[i] + wt[i]`).