c language implementation fifo algorithm and code

C language is a general-purpose computer programming language that is widely used. The C language is designed to provide a programming language that can be easily compiled, processed with low-level memory, generated with a small amount of machine code, and run without any runtime environment support.

Although the C language provides many low-level processing functions, it still maintains good cross-platform features. The C language program written in a standard specification can be compiled on many computer platforms, even including some embedded processors ( MCU) and supercomputer and other operating platforms.

In the 1980s, in order to avoid differences in the C grammar used by developers, the National Bureau of Standards developed a complete set of international standard grammar for C, called ANSI C, as the initial standard for C.

The abbreviation of First Input First Output, first in first out queue, this is a traditional sequential execution method, the first entry instruction is completed and retired, followed by the second instruction.

FIFO (First Input First Output), that is, the first in first out queue. After shopping at the supermarket, we will carry our full shopping cart to the checkout counter at the end of the checkout team, watching the customers in front of us leave. This is a FIFO mechanism where customers who queue first leave for checkout.

c language implementation fifo algorithm and code

c language implementation fifo algorithm and code

In the operating system, when the program is running, if the page to be accessed is no longer in memory and needs to be loaded into memory, but the memory has no free space, in order to ensure that the process can run normally, the system must be from the memory. Bring up a page of programs or data to the redemption area of ​​the disk. But which page is called out must be determined according to a certain algorithm. In general, the algorithm that chooses to swap out pages is called Page-Replacement Algorithms. The quality of the replacement algorithm will directly affect the performance of the system.

1) First In First Out (FIFO) page replacement algorithm

The algorithm always eliminates the first page that enters memory, that is, selects the page with the longest resident time in memory to be eliminated. The algorithm is simple to implement, just put a process into memory, arrange it into a queue in order, and set a pointer, called a replacement pointer, so that he can always point to the oldest page. However, the algorithm and process are not compatible with the actual operation rules, and the efficiency is the worst.

2) The longest used (LRU) algorithm

The LRU algorithm makes decisions based on the usage of the page after it is loaded into memory. It is to use the "recent past" as an approximation of the "recent future", so it is to eliminate the most recently unused pages. The algorithm assigns an access field to each page to record the time t experienced by a page since it was last accessed. When a page is eliminated, the largest t value in the existing page is selected, and the most recent one is used. The page is eliminated.

167#include "stdio.h"

#define PAGES 12 /*Page Reference Pages*/

#define M 3 /*The number of physical blocks currently assigned to the changed job*/

//#define M 4

/* page reference string */

Int page[PAGES] = {4,3,2,1,4,3,5,4,3,2,1,5};

Int rel[M][PAGES]; /*Store result array */

/*Memory physics block structure*/

Typedef struct{

Int pnum; /* The page number stored in the block */

Int tm; /* The time elapsed since the last call in time*/

}PBlock;

/* Initialize the physical block array */

Void init(PBlock *pb)

{

Int i,j;

//pb = (PBlock*)malloc(sizeof(PBlock)*M);

For(i=0;i"M;i++){

Pb[i].pnum = -1;

Pb[i].tm = -1;

For(j=0;j“PAGES;j++){

Rel[i][j] = -1;

}

}

}

/*Print result array*/

Void printRelArr(int rel[M][PAGES])

{

Int i,j;

For(i=0;i"M;i++){

For(j=0;j“PAGES;j++){

If(rel[i][j]==-1)

Printf("_");

Else

Printf("%d",rel[i][j]);

}

Printf("");

}

}

/*Print one-dimensional array*/

Void printArr1(int *arr,int n)

{

Int i;

For(i=0;i"n;i++){

Printf("%d",arr[i]);

}

Printf("");

}

/*Check if the page with page number num is in the memory block, there is a return 1*/

Int in_mem(int num,PBlock *pb,int m)

{

Int i;

Int b = 0;

For(i=0;i“m;i++){

If(pb[i].pnum == num){

b = 1;

Break;

}

}

Return b;

}

/* FIFO algorithm implementation, no need to consider time * /

Int fifo(PBlock *pb,int m)

{

Int lps=0; /*pages missing*/

Double lpp; /* page fault rate*/

Int p = 0; /* replace pointer */

Int index =0; /*page number index*/

While(index "PAGES){

If(!in_mem(page[index],pb,M)){ //If the page is not in a physical block

Pb[p].pnum = page[index]; /* put the page into a physical block*/

p = (p+1)%M; /* replace pointer movement */

Lps++; /* but also times 1*/

For(int i=0;i"M;i++){

Rel[i][index] = pb[i].pnum;

}

}

Index++;

}

Printf ("The number of page faults obtained by the FIFO algorithm is %d", lps);

Lpp = (double)lps/PAGES;

Printf ("FIFO algorithm page fault rate is %0.4lf", lpp);

Printf ("page number sequence is:");

printArr1(page,PAGES);

Printf ("Results are listed as:");

printRelArr(rel);

Return 0;

}

/*Get the oldest block*/

Int getP(PBlock *pb,int p)

{

Int i;

Bool out = true; //

For(i=0;i"M;i++){

If(pb[i].tm == -1){

p = i;

Out = false;

Break;

}

}

If(out){

For(i=0;i"M;i++){

If(pb[i].tm"pb[p].tm)

p = i;

}

}

Return p;

}

Int getEQnum(int num,PBlock *pb)

{

Int i;

Int in = -1;

For(i=0;i"M;i++){

If(pb[i].pnum == num){

In = i;

Break;

}

}

Return in;

}

/*LRU algorithm*/

Void lru(PBlock *pb,int m)

{

Int lps=0; /*pages missing*/

Double lpp; /* page fault rate*/

Int p = 0; /* replace pointer */

Int index =0; /*page number index*/

While(index "PAGES){

If(!in_mem(page[index],pb,m)){ /*If the page is not in the physical block */

p = getP(pb,p);

Pb[p].pnum = page[index];

Pb[p].tm = 0;

Lps++;

For(int i=0;i"M;i++){

Rel[i][index] = pb[i].pnum;

}

}else{ /* If the page is in a physical block */

Int in = getEQnum(page[index],pb); /*Get the index of the page in the physical block*/

Pb[in].tm = 0;

}

Int i;

For(i=0;i"M;i++){

If(i!=p&&pb[i].tm!=-1){

Pb[i].tm++;

}

}

Index++;

}

Printf ("LRU algorithm gets the number of page faults %d", lps);

Lpp = 1.0*lps/PAGES;

Printf ("LRU algorithm page missing rate: %0.4lf", lpp);

Printf ("page number sequence is:");

printArr1(page,PAGES);

Printf ("LRU result array is:");

printRelArr(rel);

}

Int main()

{

//printArr(rel);

PBlock pb[M];

Init(pb);

Fifo(pb,M);

Init(pb);

Lru(pb,M);

Return 0;

}

FIFO C language implementation

/*----------------- FIFO algorithm-------------------*/ /* Algorithm Description: Elimination first Go to the physical block page */ /* ---------writen by Xu Zhuozhuo----------*/

C++ code example:

#include 《iostream》

#define Num_Ref 20 //The maximum number of characters in the quoted string using namespace std; int main() {

Int num_ref; //The number of characters in the user int Phy_Blk[3];

//The number of physical blocks is 3 int ref_arr[Num_Ref];

/ / Store the user string

Cout ""-----First In First Out-----" "endl" "endl; do {

Cout ""Please input the number of reference chars:" "endl; cin" num_ref;

/ / User's number of characters } while (num_ref "Num_Ref);

Cout ""Please input the queue of reference chars:" "endl;

For(int i=0;i"num_ref;i++) //Enter a reference string

Cin 》》ref_arr[i];

For(int j=0;j"3;j++) // physical block array initialization

Phy_Blk[j]=ref_arr[j];

/ / FIFO, and output this replacement result

Cout "endl" "Display the replacement procedure:";

Cout "endl" "---------------------------------" "endl;

For(int loop=3;loop“num_ref;loop++)

{

Phy_Blk[0]=Phy_Blk[1];

Phy_Blk[1]=Phy_Blk[2];

Phy_Blk[2]=ref_arr[loop];

Cout "endl" "[" ""loop-2" "". Physical block array content: ";

For(int inloop=0;inloop"3;inloop++)

Cout "Phy_Blk[inloop] "" ";

}

Return 0;

}

Leather USB Flash Disk

High-end USB 3.0 Flash Pen Drive leather Storage Card Disk 8gb 16GB 32gb 64gb 128gb Pen drive.

Customize:
1) Do Customer's logo on USB Flash Drive.
Method: digital Print, Silk screen Print, Laser engraving, doming etc.
2) Make USB flash drive with Customer's design.
3) Preload Erasable data or unerasable data.
4) Do Auto-Play. Such as playing your vedio or open your website while connecting to the computer.
5) Make Customer design Package or Lanyard.


Leather Usb Flash Disk,Usb Memory Stick Housing,Rectangle Leather Usb Flash Drive,Rectangle Leather Custom Pendrivde

MICROBITS TECHNOLOGY LIMITED , https://www.hkmicrobits.com