What is the need for FCFS Scheduling?
In C programming , FCFS scheduling is required in systems where the priority is simple, fair, and predictable rather than fast or responsive. It is particularly effective when work comes in a natural flow and has to be processed in the order in which it comes in. It is suitable in several cases. Some of them are as follows:
- Batch systems in which all the tasks are known and no preemption is needed.
- I/O-based environments, such as a print queue or disk access queue, in which the requests are processed on a first-come, first-serve basis.
- Embedded or low-resource systems, where overhead has to be kept to a minimum.
- Educational because it allows learners to understand the main concepts of CPU scheduling without having to deal with priorities or time slices.
While FCFS is not suitable for modern multitasking environments, it remains relevant in scenarios prioritizing sequence over efficiency.
FCFS Scheduling Algorithm
Here, we will discuss the FCFS Scheduling algorithm in step by step manner.
- Start the program and enter the amount of total number of processes to schedule.
- For each process: Enter the burst time, which is the time that the process would take to execute. Suppose that all processes arrive at time 0 (or are initially in the ready queue at time 0) and that these processes will be executed in FIFO order. For the first process, we set the waiting time to zero, so the process will begin executing right away.
- Considering all other processes (i.e., the second to last): Compute its waiting time by adding all previous process burst times. It is the waiting time required by the current process before attaining CPU time.
- For each process: Turnaround time is computed as the summation of its burst time and waiting time. Turnaround time = Burst time+ Waiting time
- Compute the total waiting time and total turnaround time by summing the individual values for all processes.
- Calculate the average waiting time by dividing the total waiting time by the number of processes.
- Calculate the average turnaround time by dividing the total turnaround time by the number of processes.
- Present the results: Process ID Burst time Waiting time Turnaround time Terminate the program.
- Enter the burst time, which is the time that the process would take to execute.
- Suppose that all processes arrive at time 0 (or are initially in the ready queue at time 0) and that these processes will be executed in FIFO order.
- For the first process, we set the waiting time to zero, so the process will begin executing right away.
- Compute its waiting time by adding all previous process burst times.
- It is the waiting time required by the current process before attaining CPU time.
- Turnaround time is computed as the summation of its burst time and waiting time.
- Turnaround time = Burst time+ Waiting time
- Process ID
- Burst time
- Waiting time
- Turnaround time
- Terminate the program.
FCFS Scheduling Example in C
Let's consider an example to demonstrate the First-Come, First-Served (FCFS) Scheduling in the C programming language.
Example
#include <stdio.h>
int main() { //main function
int n, i;
int burst_time[20], waiting_time[20], turnaround_time[20];
int total_wt = 0, total_tat = 0;
// Step 1: Get the number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
// Step 2: Input burst time for each process
printf("Enter burst time for each process:\n");
for(i = 0; i < n; i++) {
printf("P[%d]: ", i + 1);
scanf("%d", &burst_time[i]);
}
// Step 3: Waiting time for the first process is always 0
waiting_time[0] = 0;
// Step 4: Calculate the waiting time for each process
for(i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}
// Step 5: Calculate turnaround time and accumulate totals
for(i = 0; i < n; i++) {
turnaround_time[i] = waiting_time[i] + burst_time[i];
total_wt += waiting_time[i];
total_tat += turnaround_time[i];
}
// Step 6: Print process info
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], waiting_time[i], turnaround_time[i]);
}
// Step 7: Print averages
printf("\nAverage Waiting Time: %.2f", (float)total_wt / n);
printf("\nAverage Turnaround Time: %.2f\n", (float)total_tat / n);
return 0;
}
Output:
Enter the number of processes: 4
Enter burst time for each process:
P[1]: 5
P[2]: 3
P[3]: 8
P[4]: 6
Process Burst Time Waiting Time Turnaround Time
P[1] 5 0 5
P[2] 3 5 8
P[3] 8 8 16
P[4] 6 16 22
Average Waiting Time: 7.25
Average Turnaround Time: 12.75
Explanation:
In this illustration, we showcase the First-Come, First-Served (FCFS) scheduling algorithm. Initially, it requests the user to provide the burst durations of various processes, then proceeds to determine the waiting time and turnaround time for each process. The initial process starts with a waiting time of 0, while subsequent processes increment their waiting times based on the preceding burst durations. Ultimately, it presents the statistics for each individual process and calculates the average waiting and turnaround times.
Characteristics of FCFS
Several characteristics of FCFS scheduling in C are as follows:
- Order-based Execution: The execution of processes is done on the order of entry.
- Non-Preemptive: A process keeps running until it is finished.
- Fair: There are no preferred processes, and service is exclusively based on arrival order.
- May cause starvation: Long-duration jobs can cause starvation of the shorter jobs (called the convoy effect).
- Predictable: It is deterministic and is applicable in real-time systems where it is important.
Benefits of FCFS Scheduling
Several benefits of FCFS Scheduling in C are as follows:
- Easy to Use: FCFS has a queue-like structure, which means it is one of the simplest scheduling algorithms to code and comprehend.
- Fairness: All processes will be considered equal: to be scheduled on a first-come-first-serve basis with no favourites or preferences.
- No Starvation: No process is delayed forever, as with priority-based algorithms. Processes are eventually executed to completion.
- Predictability: Its deterministic character is advantageous in applications to systems in which behaviour is comparatively well understood and can be readily modelled.
Drawbacks of FCFS Scheduling
Several drawbacks of FCFS Scheduling in C are as follows:
- Poor Average Waiting Time: The execution of longer processes, which are processed first, may cause the subsequent smaller jobs to wait longer (Convoy Effect) and thus lower the responsiveness of the system.
- Convoy Effect: A heavy (long burst time) process is capable of holding up all processes behind it in the queue.
- Not suitable in Time Sensitive Systems: FCFS does not allow preemption, so it is not an ideal operating system used in real-time systems where both responsiveness and timeliness are key factors.
- Low CPU Utilisation: When one process is doing an I/O, the CPU may remain idle despite the existence of other ready processes in the queue.
Real-World Applications of FCFS Scheduling
Several real-world applications of FCFS Scheduling in C are as follows:
- Print Spoolers: In the case of several print jobs being sent to a printer, they are placed in a queue and are printed accordingly. FCFS is a fair scheduling method that does not skip jobs.
- Bank Queue Systems: In the case of banks or ATMs, customers are served in the exact order they arrive. Nobody gets to go out of the queue, and this is where FCFS comes in naturally.
- Disk Scheduling (Simple Systems): FCFS may be employed to process read/write requests sequentially in basic disk I/O systems, but when more efficiency is demanded, other algorithms can be selected in the cases of high-performance systems.
- Call Center or Help Desk Ticketing: Calls or support requests are addressed on a first-come, first-served basis, so support is rendered to the earliest customers, and nobody receives unfair delays.
Conclusion
In summary, First-Come-First-Served (FCFS) scheduling may not be the most optimal algorithm, but it is transparent, impartial, and consistent. Consequently, it plays an essential role in the realm of operating systems. Whether we are engaged in crafting the scheduler blueprint for an embedded system or simulating CPU performance, FCFS is crucial for understanding process management.
The provided C program demonstrates the efficient implementation of the First-Come, First-Served (FCFS) scheduling algorithm and calculates essential performance metrics crucial in real-world scenarios. While not ideal for time-critical applications, FCFS remains a fundamental concept with practical significance in both theoretical and applied contexts.
FCFS Scheduling Program FAQs
FCFS scheduling stands out from other CPU scheduling algorithms due to its simplicity and straightforwardness in handling processes. Unlike other algorithms that prioritize certain criteria like priority levels or burst times, FCFS simply arranges processes in the order they arrive, executing them one by one without any reordering based on characteristics such as priority or length of burst.
In C programming, the FCFS (First-Come, First-Served) scheduling technique is a non-preemptive method that processes tasks in the sequence they arrive, regardless of their burst time or priority. Its straightforward nature simplifies implementation and comprehension. Nonetheless, it may result in suboptimal resource allocation and extended waiting periods, particularly when shorter tasks are positioned after longer ones, a scenario referred to as the "convoy effect."
In C, how does the First Come First Serve (FCFS) scheduling algorithm manage processes that have identical arrival times?
When multiple processes arrive at the same time, the First-Come-First-Serve (FCFS) scheduling algorithm handles them based on their initial order of arrival. It does not consider factors like burst time or priority for internal tie-breaking. The process that appears first in the list will be executed first. While this approach ensures fairness, it can lead to suboptimal turnaround times in certain scenarios.
FCFS (First-Come, First-Served) scheduling is not appropriate for real-time or interactive systems due to its inability to prioritize tasks based on their urgency or importance. In real-time systems, tasks often have strict deadlines that need to be met, and FCFS does not consider the time sensitivity of tasks. Similarly, in interactive systems where user responsiveness is crucial, FCFS may lead to long waiting times for high-priority tasks if they are queued behind multiple lower-priority tasks. This lack of prioritization in FCFS scheduling can result in inefficient use of system resources and failure to meet critical time constraints in real-time or interactive environments.
In C programming, the First-Come-First-Served (FCFS) scheduling algorithm does not support preemption, leading to potential delays in handling urgent tasks promptly. This can result in shorter or critical tasks being delayed while waiting for a long job to complete, impacting user experience and causing system delays. Real-time systems necessitate efficient and responsive scheduling algorithms, limiting the practical application of FCFS to simpler or background batch systems.
Can First-Come-First-Served (FCFS) scheduling be employed in scenarios where processes arrive at varying times?
Yes, First-Come-First-Serve (FCFS) scheduling algorithm can manage varying arrival times effectively by arranging processes in order of arrival before execution. Estimating waiting time accurately would be challenging with unorganized data. Once sorted, waiting and turnaround times can be computed as usual. The complexity of implementation increases when considering arrival times, making FCFS more closely resemble real-world task processing scenarios.
5) What effect does First-Come, First-Served (FCFS) scheduling have on CPU utilization and throughput in the C programming language?
FCFS may lead to inefficient CPU utilization, especially when lengthy processes are stuck waiting for I/O. The linear execution of processes can result in unnecessary CPU idle time. Shorter tasks can get stuck behind longer ones, decreasing overall throughput. Its sequential nature limits system performance under heavy loads. More dynamic algorithms would be better suited for high-throughput operations.