FIFO Example in the C Language
FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is handled last.
It is a way of arranging the processing of a data structure (usually a data buffer) in which the initial entry, or "head," of the queue is handled first.
FIFO Real-life Example:
Consider the following series of points:
- Assume there is a ticket counter. People arrive at the counter, grab their tickets, and leave.
- People form a line (queue) to go to the ticket booth in an ordered way.
- The individual who enters the queue first will receive the ticket and exit first.
- The next person in line will receive the tickets after the one in front of them.
- As a result, the person who arrives last in the queue (line) will receive the tickets last.
- Hence, the first person in line (queue) receives the ticket first, and the last person in line (queue) receives the ticket last.
This is known as the "First-In-First-Out" (FIFO) method.
The queue has two ends, one at the front and one at the back.
Many programs are built on the “First-In-First-Out” (FIFO) principle.
Where does FIFO come into play?
- Communication and Networking: FIFOs are used in computer networks to store data packets that are ready to travel to their next destination via the communication network's bridges, switches, and routers.
- Data Structures: Certain data structures, such as "queue" and its variants, process data using the FIFO method.
- Disk Scheduling: Disk controllers can utilize the First-In-First-Out (FIFO) method as a disk scheduling technique to decide the order in which disk I/O requests should be handled.
Syntax for FIFO
A function call, such as "mkfifo," is used to create a "First-In-First-Out" (FIFO) file.
int mkfifo(const char *file_pathname, mode_t method);
mkfifo() creates a First-In-First-Out (FIFO) special file with the name "file_pathname," where "method" determines the permissions of the FIFO. It is updated in the normal way by the process's umask: the permissions of the generated file are "method & umask."
Using First-In-First-Out (FIFO) in the C Programming Language:
Because named pipes (FIFO) are files, we can utilize all of the system methods associated with them, such as "open," "read," "write," and "close."
Algorithm:
The following represents the FIFO Page Replacement Algorithm.
Step 1: Start
Step 2: Begin to browse the pages.
Step 3: If the memory has fewer pages, the capacity is reduced; otherwise, the process proceeds to Step 5.
Step 4: Push pages into the queue (line) one at a time till the queue is full or all the page requests are satisfied.
Step 5: Do nothing if the existing page is still in memory.
Step 6: Otherwise, pop the first page from the line (queue) because it was placed first.
Step 7: Replace the first page in the string with the existing page.
Step 8: Increase the number of page faults. (Increment)
Step 9: Stop
Example
This following program code represents the most basic page replacement approach, in which the OS (operating system) system keeps all pages in a queue. The first pages are maintained at the beginning, while the last pages are kept at the end. When a page fault occurs, the front pages are deleted first, followed by the pages in demand.
/* C program for FIFO page replacement algorithm */
#include <stdio.h>
int main(){
int Incoming_Stream[] = {10, 20, 30, 40, 50};
int page_Faults = 0;
int frame = 5;
int a, b, s, pages;
pages = sizeof(Incoming_Stream)/sizeof(Incoming_Stream[0]);
printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3 \t Frame 4 \t Frame 5");
int temp[frame];
for(a = 0; a < frame; a++){
temp[a] = -1;
}
for(a = 0; a < pages; a++){
s = 0;
for(b = 0; b < frame; b++){
if(Incoming_Stream[a] == temp[b]){
s++;
page_Faults--;
}
}
page_Faults++;
if((page_Faults <= frame) && (s == 0)){
temp[a] = Incoming_Stream[a];
}
else if(s == 0){
temp[(page_Faults - 1) % frame] = Incoming_Stream[a];
}
printf("\n");
printf("%d\t\t\t",Incoming_Stream[a]);
for(b = 0; b < frame; b++){
if(temp[b] != -1){
printf(" %d\t\t\t", temp[b]);
}
else{
printf(" - \t\t\t");
}
}
}
printf("\nTotal Number of Page Faults:%d\n", page_Faults);
return 0;
}
Output:
