FIFO scheduling algorithm and LRU algorithm

Source: Internet
Author: User

I. Theory

FIFO: Advanced first-out scheduling algorithm

LRU: The most recent unused scheduling algorithm

Both are cache scheduling algorithms, which are often used as memory page replacement algorithms.

Make an analogy to help you understand. You have a lot of books, say 10000. Because you have so many books, you can only put them in the basement. You don't read in the basement while you are reading, you read in the study. Every time you want to read, you have to go to the basement to find the book you want to read, and then put it back on the desk before you start looking. There is, there are some books you will look over and over, today, maybe a few days to see again. In short, you don't know which book you will need to read someday. Your teacher will give you a list of books every day after class, so that you can go back to see which book at night. (Assuming that the book your teacher wants you to read is in your basement) running the basement is of course a hassle, so you want the books you often read better on your desk. But your study's desk can only have 10 books at the same time (this is hypothetical AH). So, here's the problem. What's the best thing to say on the desk? The best thing to say here is that you try to run down the basement to find a book. In order to solve this problem, people have invented a lot of algorithms.

Among them, the more common is the above two kinds: FIFO algorithm and LRU algorithm.

FIFO algorithm:

Quite simply, I piled up 10 books on my desk in a pile of time. The placement time here means that the book was put on my desk for a few days. Every time I want to read a book, I first find it on the desk, I can read it directly. After reading, put back the original position can be, do not disturb the order. If there is no book on the desk that I want to read, go to the basement to find it. After finding it, I put the longest book on my desk (that is, the bottom of the book) back to the basement. Then put the book I need to see today on the top of the book heap.

LRU algorithm:

It is not difficult, I put 10 books on the desk in accordance with the reading time piled into a pile. The reading time here means that the last time I read this book was a few days ago. Every time I want to read a book, I first find it on the desk, I can read it directly. After reading it, put it on top of the pile of books. If there is no book on the desk that I want to read, go to the basement to find it. After I found it, I put it back in the basement for the longest unread book on my desk, which is the bottom of the book. Then put the book I need to see today on the top of the book heap. This analogy, I believe you can see it. The basement corresponds to the memory, the desk corresponds to the cache, the book corresponds to the page.

Two. Number of pages missing

In a system that uses page-based virtual storage management, there is a user job, which sequentially accesses a sequence of pages that is 1,2,3,4,1,2,5,1,2,3,4,5. Assuming that the number of pages assigned to the job is 3 and the page is not loaded at the initial time of the job, how many pages are broken by the FIFO scheduling algorithm, What is the number of fault pages generated by the LRU scheduling algorithm?
FIFO algorithm: (first in primary out), FIFO, generally see this kind of thinking, the first thought of the data structure should be a queue, but we better here is the vector, because the paging process needs to traverse the queue to check whether the page already exists, when the algorithm's storage structure is a queue or stack, But the implementation of the process needs to traverse the whole column or the entire stack of content, it is best to use vector, this is the "point of the sword" question 25 to my inspiration. It should be very simple to give an access sequence simulation algorithm, in order to save time, the following only gives the problem calculation steps, the code will be re-fill.
Access sequence: 1,2,3,4,1,2,5,1,2,3,4,5
First-in-a-number memory, memory structure: 3 2 1 Pages: 3
4 into memory, 1 out, Memory structure: 4 3 2 Number of pages: 4
1 into memory, 2 out, memory structure: 1 4 3 Number of pages: 5
2 into memory, 3 out, memory structure: 2 1 4 Number of pages: 6
5 into memory, 4 out, memory structure: 5 2 1 Number of pages: 7
1 exists, memory structure does not change
2 exists, memory structure does not change
3 into memory, 1 out, Memory structure: 3 5 2 Number of pages: 8
4 into memory, 2 out, Memory structure: 4 3 5 Number of pages: 9
5 exists, memory structure does not change
9 pages, page break rate = page Break number/Total Visits = 9/12


LRU algorithm: Least Recently Used (Least recently used), first look at a downward page process
Access sequence: 1,2,3,4,1,2,5,1,2,3,4,5
First-in-a-number memory, memory structure: 3 2 1 Pages: 3
4 into memory, 1 out, Memory structure: 4 3 2 Number of pages: 4
1 into memory, 2 out, memory structure: 1 4 3 Number of pages: 5
2 into memory, 3 out, memory structure: 2 1 4 Number of pages: 6
5 into memory, 4 out, memory structure: 5 2 1 Number of pages: 7
There's no difference between this step and the FIFO.
1 into memory, because there is 1 in memory, so there is no fault, but because 1 has recently been visited, so to change its location,
Make it the last one to be eliminated, memory structure: 1 5 2
2 into memory, no missing pages interrupt, but memory location to change, memory structure: 2 1 5
3 into memory, 5 out, Memory structure: 3 2 1 Number of pages: 8
4 into memory, 1 out, Memory structure: 4 3 2 Number of pages: 9
5 into memory, 2 out, memory structure: 5 4 3 Number of pages: 10
10 pages, page break rate: 10/12

FIFO scheduling algorithm and LRU algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.