Principle of fifo algorithm and fifo replacement algorithm

Author: PoisonApple Reprinted  from http://www.elecfans.com/emb/604190.html

The abbreviation of First Input First Output, first-in-first-out queue, which is a traditional sequential execution method. The first-entered instruction is completed and retired first, and then the second instruction is executed.

  FIFO (First Input First Output), that is, a first-in, first-out queue. After shopping in the supermarket, we will carry our full shopping cart to the checkout counter and stand at the end of the checkout line, watching the customers in front leave one by one. This is a first-in, first-out mechanism, and customers who queue up first check out first and leave.

  Principle of fifo algorithm

  In computers, FIFO is a traditional sequential execution method. The first instruction is completed and retired, and then the second instruction is executed (the instruction is the program code that the computer responds to the user's operation, and for the user is transparent). As shown in Figure 1, when the CPU is too late to respond to all the commands in a certain period of time, the commands will be arranged in the FIFO queue. For example, the 0th command enters the queue first, followed by the 1st command, the 2nd command... When the CPU After the current instruction is completed, the No. 0 instruction will be taken out of the queue and executed first. At this time, the No. 1 instruction will take over the position of the No. 0 instruction. Similarly, the No. 2 instruction, No. 3 instruction... will move forward one position, so explain Are you clear?

  Principle of fifo algorithm and fifo replacement algorithm

  Figure 1 FIFO queue

  FIFO is the simplest queue mechanism. There is a FIFO queue on each interface. On the surface, the FIFO queue does not provide any QoS (Quality of Service, quality of service) guarantee. Many people even think that FIFO is not strictly a service. FIFO is the foundation of other queues, and FIFO also affects the key indicators for measuring QoS: packet discarding, delay, and jitter. Since there is only one queue, there is no need to consider how to perform complex traffic classification of packets, nor how to take the next packet and how much to take. Moreover, because the packets are taken in order, the FIFO does not need to reorder the packets. Simplifying these implementations actually improves the guarantee of packet delay.

  What FIFO cares about is the queue length, which affects the delay, jitter, and packet loss rate. Because the queue length is limited, it may be filled, which involves the discarding principle of the mechanism. A common drop principle is called the Tail Drop mechanism. Simply put, if the queue is full, subsequent incoming packets will be discarded, and there is no mechanism to ensure that subsequent packets can squeeze out packets already in the queue. In this mechanism, if a longer queue length is defined, the queue is not easy to fill up, and fewer packets are discarded. However, if the queue length is too long, there will be a delay problem. In general, the delay An increase in , results in an increase in jitter as well. If a shorter queue is defined, the delay problem can be solved, but the number of Tail Drop packets will increase.

  First in first out (FIFO) permutation algorithm

  This is the earliest permutation algorithm. The algorithm always eliminates the page that enters the memory first, that is, selects the page that has been in memory for the longest time to be eliminated. The algorithm is simple to implement. It only needs to link the pages that a process has transferred into memory into a queue in order, and set a pointer, called the replacement pointer, so that it always points to the oldest page. However, this algorithm is not suitable for the actual operation of the process, because in the process, some pages are frequently accessed, for example, pages containing global variables, common functions, routines, etc., the FIFO algorithm cannot guarantee that these pages will not be eliminated.

  Here, we only need to set up a FIFO queue. The page that goes into memory first is translated first.

  For example: Suppose the system allocates three physical blocks to a process, and consider the following reference string of page numbers:

  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) replacement 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;

  /**

  * 内存块数组

  */

  Object[] array = new Object[N];

  private int size;

  /**

  * 内存是非空为否

  * @return

  */

  public boolean isEmpty() {

  if(0 == size)

  return true;

  else

  return false;

  }

  public/**

  * 内存是非空满

  * @return

  */ boolean isFulled() {

  if(size 》= N)

  return true;

  else

  return false;

  }

  /**

  * 元素(页框)的个数

  * @return

  */

  public int size() {

  return size;

  }

  /**

  * 查找元素o在数组中的位置

  * @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;

  }*/

  /**

  * 页面转换

  * @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 the queue (page frame)

  */

  public void clear(){

  }

  /**

  * @param args

  */

  public staTIc void main(String[] args) {

  The integer path = {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();

  }

  }

  }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325203351&siteId=291194637