×

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

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

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.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 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.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.