Fifo algorithm principle and fifo replacement algorithm

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.

Principle of fifo algorithm

In the computer, the first in first out queue is a traditional sequential execution method. The first incoming instruction is completed and retired, and then the second instruction is executed (the instruction is the program code of the computer in response to the user operation, for the user. It is transparent). As shown in Figure 1, when the CPU does not respond to all instructions within a certain period of time, the instruction will be arranged in the FIFO queue. For example, the instruction No. 0 enters the queue first, followed by the instruction No. 1, the instruction No. 2... when the CPU After completing the current instruction, the instruction No. 0 will be executed from the queue first. At this time, the No. 1 instruction will take over the position of the No. 0 instruction. Similarly, the No. 2 instruction and the No. 3 instruction will move forward one position, so that the explanation Are you clear?

Fifo algorithm principle and fifo replacement algorithm

Figure 1 FIFO queue

FIFO is the simplest in the queuing mechanism. There are FIFO queues on each interface. On the surface, the FIFO queue does not provide any QoS (Quality of Service) guarantee. Even many people think that FIFO is not a strict one. Queue technology, in fact, FIFO is the basis of other queues, FIFO will also affect the key indicators of QoS: packet discard, delay, jitter. Since there is only one queue, it is naturally not necessary to consider how to classify the traffic with complicated traffic, and it does not need to consider how to get and get the next message, and because the message is taken in order, the FIFO does not need to reorder the message. Simplifying these implementations actually increases the guarantee of message delay.

The FIFO is concerned with the queue length problem, and the queue length will affect the delay, jitter, and packet loss rate. Because the queue length is limited and may be filled, this involves the discarding principle of the mechanism. A common discarding principle is called the Tail Drop mechanism. Simply put, if the queue is full, the subsequent incoming packets are discarded, and there is no mechanism to ensure that subsequent packets can squeeze out the packets already in the queue. In this mechanism, if a long queue length is defined, the queue is not easy to fill up, and the number of discarded packets is small. However, if the queue length is too long, there will be a delay, and the delay is generally The increase will cause the jitter to increase as well. If a shorter queue is defined, the delay problem can be solved, but the message that occurs with Tail Drop becomes much larger.

First in first out (FIFO) permutation algorithm

This is the earliest 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 need to put a page that has been loaded into memory, link it into a queue in order, and set a pointer, called a replacement pointer, so that it always points to the oldest page. However, the algorithm is not compatible with the actual running of the process, because some pages are frequently accessed during the process. For example, pages containing global variables, common functions, routines, etc., the FIFO algorithm does not guarantee that these pages will not be eliminated.

Here, we only need to set up a FIFO queue. The page that first enters the memory is first converted.

For example: suppose the system allocates three physical blocks for a process and considers the following page number reference string:

7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1

The result is:

7

7 0

7 0 1

0 1 2

1 2 0

2 0 3

2 3 0

3 0 4

0 4 2

4 2 3

2 3 0

2 0 3

0 3 2

3 2 1

3 1 2

1 2 0

2 0 1

0 1 7

1 7 0

7 0 1

First in first out (FIFO) permutation algorithm simulation source code

[java] view plain copy/**

* FIFO conversion algorithm

* @author Administrator

*

*/

Public class FIFO {

/**

* The number of memory blocks

*/

Public staTIc final int N = 3;

/**

* Memory block array

*/

Object[] array = new Object[N];

Private int size;

/**

* Memory is not empty or not

* @return

*/

Public boolean isEmpty() {

If(0 == size)

Return true;

Else

Return false;

}

Public/**

* Memory is not empty

* @return

*/ boolean isFulled() {

If(size 》= N)

Return true;

Else

Return false;

}

/**

* Number of elements (page frames)

* @return

*/

Public int size() {

Return size;

}

/**

* Find the position of the element o in the array

* @param o

* @return

*/

Public int indexOfElement(Object o) {

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

If(o == array[i]) {

Return i;

}

}

Return -1;

}

/*public void push(Object o) {

Node p = new Node(o);

//Node p2 = head;

P.next = head;

Head = p;

}*/

/**

* Page conversion

* @param obj

*/

Public Object trans(Object obj){

Object e = null;

Int t = 0;

If(indexOfElement(obj) != -1) {

t = indexOfElement(obj);

For(int i=t; i"size-1; i++) {

Array[i] = array[i+1];

}

Array[size-1] = obj;

} else {

If(!isFulled()){

Array[size] = obj;

Size ++;

} else {

For(int i=0; i"size-1; i++) {

Array[i] = array[i+1];

}

Array[size-1] = obj;

}

}

If( -1 == t) {

Return null;

} else {

Return array[t];

}

}

/**

* Output each data in the memory area

*/

Public void showMemoryBlock() {

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

System.out.print(array[i] + " ");

}

}

/**

* Clear queue (page frame)

*/

Public void clear(){

}

/**

* @param args

*/

Public staTIc void main(String[] args) {

Integer iter[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};

FIFO fifo = new FIFO();

For(int i=0; i"iter.length; i++) {

Fifo.trans(iter[i]);

fifo.showMemoryBlock();

System.out.println();

}

}

}

Engine Maintenance Tool

Cummins Filter Wrench,Cummins Engine Repair Kit,Cummins Water Pump Tool,Cummins Cylinder Liner Press Fit Repair Tool

Chongqing LDJM Engine Parts Center , https://www.ckcummins.com