×

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:

  1. Assume there is a ticket counter. People arrive at the counter, grab their tickets, and leave.
  2. People form a line (queue) to go to the ticket booth in an ordered way.
  3. The individual who enters the queue first will receive the ticket and exit first.
  4. The next person in line will receive the tickets after the one in front of them.
  5. As a result, the person who arrives last in the queue (line) will receive the tickets last. 
  6. 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?

  1. 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.
  2. Data Structures: Certain data structures, such as "queue" and its variants, process data using the FIFO method.
  3. 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:

FIFO Example in the C Language

Related Topics

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.