FCFS Program in C
“A scheduling technique called First-Come, First-Served (FCFS) is used in computers and operating systems to decide the sequence of activities or processes.” It is a non-preemptive method; thus, once a process is launched, it will run until it is finished or willingly releases the CPU.
Following a straightforward queue-based method, The processes are carried out by FCFS in the order they are received. The first process to arrive is performed, and subsequent processes are appended to the end of the queue. The process at the head of the queue is chosen and carried out entirely as soon as the CPU is available.
The FCFS method's simplicity and ease of application are two benefits. It is simple to comprehend and does not need heuristics or complicated computations. FCFS also assures fairness since procedures are carried out in the order they are received, eliminating any possibility of prejudice or prioritization.
FCFS does, however, have certain restrictions. It experiences the "convoy effect," in which the early arrival of a lengthy process delays the completion of following shorter processes. When lengthy processes arrive first, this results in subpar system performance and reaction time.
The First-Come, First-Served (FCFS) scheduling technique in C is explained below, point by point:
- A straightforward scheduling method called FCFS executes tasks in the order they are received. The procedure that comes first is the one that gets carried out first.
- When implementing the FCFS algorithm in C, the processes might be represented by an array or a linked list. A structure having properties like the process ID, arrival time, burst time, etc., may be used to define each process.
- Sort the processes in ascending order according to their arrival timings. Thus, the process with the earliest arrival time will always be carried out first.
- Determine how long it takes for each step to complete. The process's burst time and waiting time are added to determine turnaround time.
- By adding together and dividing by the total number of processes, you can determine the average waiting and turnaround times.
- Display each process step's waiting times, turnaround times, and average timings before concluding.
Significance of First-Come, First-Serve
- Simplicity
- Fairness
- Predictability
- Resource Sharing
- Low Overhead
Simplicity:
One of the most accessible scheduling techniques to use is FCFS. It is simple to comprehend and program since it has a queue-based architecture. In instances when elaborate scheduling algorithms are superfluous or unfeasible, their simplicity is beneficial.
Fairness:
By carrying out procedures in the order they are received, FCFS assures fairness. No process is given preferential treatment depending on its attributes or priority; all processes can utilize the CPU. This fairness may be acceptable in some circumstances where an egalitarian attitude is sought.
Predictability:
Because processes are rigidly carried out in the order they come, FCFS offers predictable behavior. In real-time systems or contexts where the execution order is essential for effective operation, this predictability might be helpful. The exact determination of the execution sequence made possible by FCFS enables more effective planning and coordination.
Resource Sharing:
When numerous people or entities share a single resource, such as a printer or a network connection, FCFS might be helpful. Each user or organization can request and use the shared resource depending on their arrival time by adhering to FCFS, guaranteeing everyone an equal opportunity to use the resource.
Low Overhead:
FCFS has very little computational or decisional overhead. A process begins running and continues uninterruptedly without context changes or breaks until it is finished. This minimal overhead might be advantageous when effective resource utilization is not a top priority or in systems with limited computing capability.
It's crucial to remember that while FCFS offers advantages, there might be better options for scheduling in some circumstances. In some circumstances, the convoy effect might be problematic, which causes longer processes to lag behind shorter ones. Such circumstances may call for more sophisticated scheduling algorithms that take execution times or process priority into account.
Algorithm for FCFS in C
Step 1: Start by declaring and defining the necessary variables:
Step 2: Accept the number of processes from the user and initialize the arrays
Step 3: Calculate the waiting time for each process
Step 4: Calculate the turnaround time for each process
Step 5: Determine the avg wait time and avg turnaround time.
Step 6: Display the results
FCFS Program in C:
Certainly! Here is a straightforward First-Come, First-Served (FCFS) program written in C.
Code:
#include <stdio.h>
void calculate waiting time(int processIDs[], int n, int burstTimes[], int waitingTimes[])
{
waitingTimes[0] = 0;
for (int i = 1; i < n; i++)
waitingTimes[i] = burstTimes[i - 1] + waitingTimes[i - 1];
}
void calculateTurnaroundTime(int processIDs[], int n, int burstTimes[], int waitingTimes[], int turnaroundTimes[])
{
for (int i = 0; i < n; i++)
turnaroundTimes[i] = burstTimes[i] + waitingTimes[i];
}
void calculateAverageTime(int processIDs[], int n, int burstTimes[])
{
int waitingTimes[n], turnaroundTimes[n], totalWaitingTime = 0, totalTurnaroundTime = 0;
calculateWaitingTime(processIDs, n, burstTimes, waitingTimes);
calculateTurnaroundTime(processIDs, n, burstTimes, waitingTimes, turnaroundTimes);
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
totalWaitingTime += waitingTimes[i];
totalTurnaroundTime += turnaroundTimes[i];
printf("%d\t%d\t\t%d\t\t%d\n", processIDs[i], burstTimes[i], waitingTimes[i], turnaroundTimes[i]);
}
float avgWaitingTime = (float)totalWaitingTime / n;
float avgTurnaroundTime = (float)totalTurnaroundTime / n;
printf("\nAverage Waiting Time: %.2f", avgWaitingTime);
printf("\nAverage Turnaround Time: %.2f", avgTurnaroundTime);
}
int main()
{
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processIDs[n];
int burstTimes[n];
printf("Enter the burst time for each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &burstTimes[i]);
processIDs[i] = i + 1;
}
calculate average time(processIDs, n, burstTimes);
return 0;
}
Output:
![FCFS Program in C](https://static.tutorialandexample.com/c/fcfs-program-in-c1.png)