Sei sulla pagina 1di 11

TERM PAPER

CSE-211
Topic: Simulation of page replacement algorithm

Submitted to Amitabh

submitted by Piyush kr mishra 11007887 RK1001B42

ACKNOWLEDGEMENT

I take this opportunity to present my votes of thanks to all those guidepost who really acted as lightening pillars to enlighten our way throughout this project that has led to successful and satisfactory completion of this study.

I am really grateful to our HOD for providing me with an opportunity to undertake this project in this university and providing me with all the facilities. I am highly thankful to my subject teacher Mr Amitabh for his active support, valuable time and advice, wholehearted guidance, sincere cooperation and pains-taking involvement during the study and in completing the assignment of preparing the said project within the time stipulated.

Lastly, I am thankful to all those, particularly the various friends , who have been instrumental in creating proper, healthy and conductive environment and including new and fresh innovative ideas for me during the project, their help, it would have been extremely difficult for me to prepare the project in a time bound framework.

Contents
1 Abstract 2 Page replacement algorithms 2.1 Optimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Least Recently Used (LRU) . . . . . . . . . . . . . . . . . . 2.3 First In First Out (FIFO) . . . . . . . . . . . . . . . . . . . . . 2.4 Second Chance . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Aging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6 Not Recently Used (NRU) . . . . . . . . . . . . . . . . . . . . . 3 Simulation of page replacement algorithms 3.1 How to simulate . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 MMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.2 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.3 Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.4 Simulate . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 What to simulate . . . . . . . . . . . . . . . . . . . . . . . 3.2.1 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.2 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.3 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.4 Algorithm Simulator . . . . . . . . . . . . . . . . . 3.3 Implementation of Standard Algorithms

4. Implementing a new algorithm in the simulator

5. Bibliography

1 Abstract
This is a facharbeit about the simulation of page replacement algorithms. This means that it focuses mainly on the simulation, not on the page replacement algorithms themselves. Basic knowledge of memory management systems is presumed.

2 Page replacement algorithms


When a page fault occurs, the operating system has to choose a page to remove from memory to make room for the page that has to be brought in. If the page to be removed has been modified while in memory, it must be rewritten to the disk to bring the disk copy up to date. If, however, the page has not been changed (e.g., it contains program text), the disk copy is already up to date, so no rewrite is needed. The page to be read in just overwrites the page being evicted. While it would be possible to pick a random page to evict at each page fault, system performance is much better if a page that is not heavily used is chosen. If a heavily used page is removed, it will probably have to be brought back in quickly, resulting in extra overhead. Much work has been done on the subject of page replacement algorithms, both theoretical and experimental. Below we will describe some of the most important algorithms. It is worth noting that the problem of page replacement occurs in other areas of computer design as well. For example, most computers have one or more memory caches consisting of recently used 32-byte or 64-byte memory blocks. When the cache is full, some block has to be chosen for removal. This problem is precisely the same as page replacement except on a shorter time scale (it has to be done in a few nanoseconds, not milliseconds as with page replacement). The reason for the shorter time scale is that cache block misses are satisfied from main memory, which has no seek time and no rotational latency. A second example is in a Web server. The server can keep a certain number of heavily used Web pages in its memory cache. However, when the memory cache is full and a new page is referenced, a decision has to be made which Web page to evict. The considerations are similar to pages of virtual memory, except for the fact that the Web pages are never modified in the cache, so there is always a fresh copy on disk. In a virtual memory system, pages in main memory may be either clean or dirty. This section gives a very short overview of the page replacement algorithms which can be simulated with the scripts and their implementation on real hardware. 2.1 Optimal The best possible page replacement algorithm is easy to describe but impossible to implement. It goes like this. At the moment that a page fault occurs, some set of pages is in memory. One of these pages will be referenced on the very next instruction (the page containing that instruction). Other pages may not be referenced until 10, 100, or perhaps 1000 instructions later. Each page can be labelled with the number of instructions that will be executed before that page is first referenced. The optimal page algorithm simply says that the page with the highest label should be removed. If one page will not be used for 8 million instructions and another page will not be

used for 6 million instructions, removing the former pushes the page fault that will fetch it back as far into the future as possible. The only problem with this algorithm is that it is unrealizable. At the time of the page fault, the operating system has no way of knowing when each of the pages will be referenced next. Still, by running a program on a simulator and keeping track of all page references, it is possible to implement optimal page replacement on the second run by using the page reference information collected during the first run. In this way it is possible to compare the performance of realizable algorithms with the best possible one. If an operating system achieves a performance of, say, only 1 percent worse than the optimal algorithm, effort spent in looking for a better algorithm will yield at most a 1 percent improvement. To avoid any possible confusion, it should be made clear that this log of page references refers only to the one program just measured and then with only one specific input. The page replacement algorithm derived from it is thus specific to that one program and input data. Although this method is useful for evaluating page replacement algorithms, it is of no use in practical systems. Below we will study algorithms that are useful on real systems. 2.2 Least Recently Used (LRU) The least recently used" (LRU) algorithm replaces the page which has not been accessed for the longest time. It performs very well, but it is difficult to implement. There are several implementations of it: 1. Every time a page is read, it is moved to the head of the list. The next page to replace is the one at the tail of the list. It is very hard to perform this task so fast that it does not slow down the whole system, because memory accesses occur very frequently. 2. We use a bit matrix consisting of n * n bits, where n is the number of physical pages in memory If there are many pages, the bit matrix grows very big. 3. Every time a page is read, the current time is stored in a variable of this page. The next page to swap out is the one with the lowest time of last access. This is hardly implementable because the timer needs more than 32 bit to be fine-granular enough and finding the page with the lowest access time is very expensive. 2.3 First In First Out (FIFO) The FIFO (first-in-first-out) algorithm is extremely simple: It removes the page with the earliest creation time. This can be implemented using a list: New pages are inserted at the head of the list, and the page at the tail is swapped out. Another implementation is using a ring (usually referred to as \clock"): Every time a page has to be replaced, the page the pointer points at is swapped out and at the same place the new page is swapped in. After this, the pointer moves to the next page. The FIFO algorithm's performance is rather bad. To illustrate how this works, consider a supermarket that has enough shelves to display exactly k different products. One day, some company introduces a new convenience food instant, freeze-dried, organic yogurt that can be reconstituted in a microwave oven. It is an immediate success, so our finite supermarket has to get rid of one old product in order to stock it. One possibility is to find the product that the supermarket has been stocking the longest (i.e., something it began selling 120 years ago) and get rid of it on the grounds that no one is interested any more. In effect, the supermarket maintains a linked list of all the products it currently sells in the order they were introduced. The new one goes on the back of

the list; the one at the front of the list is dropped. As a page replacement algorithm, the same idea is applicable. The operating system maintains a list of all pages currently in memory, with the page at the head of the list the oldest one and the page at the tail the most recent arrival. On a page fault, the page at the head is removed and the new page added to the tail of the list. When applied to stores, FIFO might remove mustache wax, but it might also remove flour, salt, or butter. When applied to computers the same problem arises. For this reason, FIFO in its pure form is rarely used.

2.4 Second Chance The second-chance algorithm is very similar to FIFO. However, it interferes with the accessing process: Every page has, in addition to its \dirty bit", a \referenced bit" (r-bit). Every time a page is accessed, the r-bit is set. The replacement process works like FIFO, except that when a page's r-bit is set, instead of replacing it, the r-bit is unset, the page is moved to the list's tail (or the pointer moves to the next page, resp.) and the next page is examined. Second Chances performs better than FIFO, but it is still far from optimal. 2.5 Aging The aging algorithm is somewhat tricky: It uses a bit field of w bits for each page in order to track its accessing profile. Every time a page is read, the first (i.e. most significant) bit of the page's bit field is set. Every n instructions all pages' bit fields are right-shifted by one bit. The next page to replace is the one with the lowest (numerical) value of its bit field. If there are several pages having the same value, an arbitrary page is chosen. The aging algorithm works very well in many cases, and sometimes even better than LRU, because it looks behind the last access. It furthermore is rather easy to implement, because there are no expensive actions to perform when reading a page. However, finding the page with the lowest bit field value usually takes some time. Thus, it might be necessary to predetermine the next page to be swapped out in background.

2.6 Not Recently Used (NRU) In order to allow the operating system to collect useful statistics about which pages are being used and which ones are not, most computers with virtual memory have two status bits associated with each page. R is set whenever the page is referenced (read or written). M is set when the page is written to (i.e., modified). The bits are contained in each page table entry, as shown in Fig. 4-0. It is important to realize that these bits must be updated on every memory reference, so it is essential that they be set by the hardware. Once a bit has been set to 1, it stays 1 until the operating system resets it to 0 in software. If the hardware does not have these bits, they can be simulated as follows. When a process is started up, all of its page table entries are marked as not in memory. As soon as any page is referenced, a page fault will occur. The operating system then sets the R bit (in its internal tables), changes the page table entry to point to the correct page, with mode READ ONLY, and restarts the instruction. If the page is subsequently written on, another page fault will occur, allowing the operating system to set the M bit and change the pages mode to READ/WRITE.

The R and M bits can be used to build a simple paging algorithm as follows. When a process is started up, both page bits for all its pages are set to 0 by the operating system. Periodically (e.g., on each clock interrupt), the R bit is cleared, to distinguish pages that have not been referenced recently from those that have been. When a page fault occurs, the operating system inspects all the pages and divides them into four categories based on the current values of their R and M bits: Class 0: not referenced, not modified. Class 1: not referenced, modified. Class 2: referenced, not modified. Class 3: referenced, modified. Although class 1 pages seem, at first glance, impossible, they occur when a class 3 page has its R bit cleared by a clock interrupt. Clock interrupts do not clear the M bit because this information is needed to know whether the page has to be rewritten to disk or not. Clearing R but not M leads to a class 1 page. The NRU (Not Recently Used) algorithm removes a page at random from the lowest numbered nonempty class. Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in at least one clock tick (typically 20 msec) than a clean page that is in heavy use. The main attraction of NRU is that it is easy to understand, moderately efficient to implement, and gives a performance that, while certainly not optimal, may be adequate.

3 Simulation of page replacement algorithms


Now we start with the really interesting part: The simulation of page replacement algorithms. 3.1 How to simulate The scripts to simulate the page replacement algorithms are written in Python. They have been tested with Python-2.2.3, but other versions might work as well. There are four modules, mms, algorithms, access and simulate, and demo, a demonstration script. 3.1.1 MMS The module mms contains a generic (abstract) class MMS, which represents a memory management system. All page replacement algorithms are classes derived from MMS. MMS' constructor takes one parameter, the number of physical pages. (The number of virtual pages is unlimited.) The method read reads a page. It takes one parameter, the virtual page number. If the page is already contained in memory, it does nothing. (Of course, this behaviour may be changed by derived classes.) If the page is not contained in memory, the method page fault is called. If the memory is full, page fault calls swap out, which is not defined in MMS and has to be implemented by derived classes. After this, page fault calls create, which creates a new page in memory. Derived classes might implement additional behaviour for create. The method output is simply used for outputting the virtual page numbers in a human-readable format. By default, it outputs the next page to swap out last, however this behaviour might be undesirable for some non-stack algorithms like Second Chance, which should overwrite output for better convenience. Every time a page is swapped in (or out), page fault increments swap in (or swap out, resp.), so that you can measure the algorithms' performances.

3.1.2 Algorithms The module algorithms contain from mms.MMS derived classes which implement various page replacement algorithms. The classes FIFO, Second Chance and LRU are selfexplanatory. Optimal needs to get a list of virtual page numbers which will we accessed, either as second parameter of the constructor or as assignment to the a list attribute. The algorithm will only be optimal if the order of read pages is the same as in a list. Aging may get the bit field width and shifting frequency as second and third constructor-parameter or as parameters to the method configure. (\Shifting frequency" means the number of readinstructions between the shifts.) Defaults for both are provided. NRU is derived from Aging and uses a bit field length of 1, regardless of its configuration. 3.1.3 Access The module access provides helper functions to simulate memory accessing behavior of real programs. Some general notes for all functions in this module: mem is a list of virtual page numbers representing the whole allocated memory. ws is a list of virtual page numbers representing a program's working set and may be contained in mem. probab is a probability in the range of 0 to 1. rand is a random number generator, e.g. the module random or an instance of random. Random. The function access(mem, ws, rand, probab) returns a page. With a probability of probab, the returned page is contained in ws. Otherwise, it is contained in mem but not in ws. Makealist(mem, ws, rand, probab, length) returns a list of length pages to access. Basically, it works like access, except that exactly round((1 - probab) * length) pages are contained in mem but not in ws and all other pages are contained in ws, so that random inaccuracies are avoided. wsmake(mem, rand, size) returns a working set of size pages which are all contained in mem. wsmove(mem, ws, rand) removes one page from ws and adds a di_erent, randomly chosen page from mem to ws. wsextend(mem, ws, rand) adds a random page from mem to ws. wsshrink(ws, rand) removes a random page from ws. None of the four ws-functions touch mem, and, when adding a page to ws, they ensure that no page is contained more than once in ws. 3.1.4 Simulate The module simulate has only one function, simulate (mms, a list, skip=0), which invokes mms.read for all pages in a list. It returns the ratio of page faults to accesses, leaving the first skip accesses out of consideration. skip defaults to 0.

3.2 What to simulate


This section deals with what exactly to simulate. 3.2.1 Hardware If the hardware is still being designed, an important point is how to support the algorithm. E.g., without the ability to perform any action after reading a page, there is not much more left than FIFO. If the hardware is able to reorder elements of a list at every read-instruction, it is possible to implement LRU. If there are not many pages, it might be a good idea to choose the bit-matrix implementation of LRU or to supply automatically shifting bit-fields for every page in order to support Aging. 3.2.2 Software Now we can start creating profiles of the programs which will run on the system. E.g., we could generate access lists by tracking a (real) program's memory access behaviour.

Or we could analyse the source of the programs in order to answer questions like these: Does the program have a working set at all? (If it does not, we simply do not need to care of a decent page replacement algorithm.) If it does, how does the working set change over time? Does it have a constant size? Does it move? And so on. Based on these parameters, we can create access lists which approximate the program's memory accessing behaviour using the access module.

3.2.3 Algorithms Based on the previously defined hardware environment, we select algorithms which can run in that environment, or we even implement some more complex algorithms specific to our hardware. Then we simulate the algorithms using the simulate module.

3.2.4 Algorithm Simulator 3.2.4.1 Overall Design. The algorithm simulator component gives a base class for any page replacement algorithm using which new algorithms can be easily added in the system. The simulator drives the algorithms based on the base class by giving them information about every memory access. The decisions made by the algorithm are communicated back to the simulator which uses them to collect the statistics. 3.2.4.2 Implementation Details. The simulator is designed in Java and has a both GUI based interface and a command line interface. The simulator consists of the following important classes: (1) Virtual Page: This class represents a virtual page in memory. It stores things like last access time, the reference bit and a reference to the physical page(an object of type Physical Page)in which this virtual page resides if it has not been paged out. (2) Physical Page: This class represents a physical page in memory. It stores things like last access time, reference bit and the reference to the virtual page that it holds. (3) Memory Map: This class represents the memory of the system. It contains a list of Physical Page and Virtual Page and provides methods to access them based on the address. The memory map is initialized using a configuration file which specifies the number of physical pages and the pages present in the memory when starting the program. The physical pages for which no information is provided are assumed to be free. (4) Page Replacement Algorithm: This class is the base class for any page replacement algorithm which is to be tested on the simulator. The child class can implement its functionality by specifying the following .The action to be taken when a page is to be accessed .The action to be taken in case of a page fault .Actions to be performed periodically .The configuration parameters to be specified.

(5) Page Replacement Simulator: This class is the actual simulator which drives the algorithms by providing it the memory access information in a step by step fashion. The simulator searches for the algorithms in algos directory present in the home directory of this system and load all the classes from there. The simulator has GUI interface which allows the user to specify the configuration file, the trace file and the algorithm to be used. The algorithm can either be run step by step or continuously by the user. After the run of the algorithm is completed, the simulator shows statistics like total number of page faults, number of page faults per page, number of accesses per page. (6) Cmd Page Replacement Simulator: This class is the command line version of Page Replacement Simulator and takes the configuration file, trace file and algorithm as arguments.

3.3 Implementation of Standard Algorithms


The following standard algorithms have been implemented for comparing their performance on traces of various programs: (1) FIFO Algorithm (FIFO): The pages are paged out in FIFO order (2) LRU Algorithm (LRU): The virtual page which has been least recently used is paged out (3) LFU Algorithm (LFU): The virtual page which has been used least number of times is paged out. (4) MRU Algorithm (MRU): The virtual page which has been used most recently is paged out. (5) MFU Algorithm (MFU): The page which is used most frequently is paged out. (6) Random Algorithm (RND): The page to be paged out is chosen randomly from the given set of pages. (7) Optimal Algorithm (OPT): The page which would be used last in the future amongst the pages residing in memory is replaced. (8) Clock Algorithm (CLK): It is one of the classical LRU approximation schemes. In this algorithm, each virtual page maintains a reference bit which is set whenever a page is accessed. The algorithm considers the list of pages as a circular list and iterates over them using a clock hand. If it finds a page with reference bit unset, it pages out that page otherwise it unsets the reference bit (9) Two Clock Hand Algorithm (TCLK): It is an adaptation of the single clock hand algorithm in which there is a second hand which unsets the bit.

4. IMPLEMENTING A NEW ALGORITHM IN THE SIMULATOR


For implementing a new page replacement algorithm using our simulator, one needs to extend the Page Replacement Algorithm class. More specifically, the following functions can be overridden: (1) void init(long numVirtualPages, long numPhysicalPages, String configFile, String pageRefFile) throws IOException: This function is used to initialize the memory map to be used by the algorithm. The configuration file is needed by the memory map for initialization. The page reference _le may be used for some algorithms which try to look into the future memory accesses like the optimal algorithm but any practical algorithm should avoid using the page reference file by itself. An implementation of the init function is provided in the base Page Replacement Algorithm class which is sufficient for most of the algorithms. Here

we purposefully chose to initialize the memory map in the algorithm so that if any algorithm requires some additional information to be stored with the virtual or physical pages, it can extend the Virtual Page or Physical Page class respectively and use them to initialize the memory map. (2) void step(long VirtualPageNumber, int type): This function is used by the simulator to drive the algorithm. On every memory access, this function is invoked with appropriate page number and type values. In this function, the algorithm _rst checks if the virtual page being accessed is already present in the memory. If it is present, then some relevant statistics for that page and other pages in memory is updated. If the page is not present, then we first try to find a free physical page to bring the virtual page in memory. If there is no free page is present, then the algorithm chooses a victim page to page out using the selectVictimPage function. Here again a basic implementation is provided in the PageReplacementAlgorithm class. The user can override this function if some other work needs to be done at every step. (3) PhysicalPage selectVictimPage(): This function actually determines the page replacement policy and must be implemented by every algorithm. Here the algorithm selects a victim page using the memory map and returns the physical page which holds that victim virtual page. (4) void periodicFunction(): This function provides the algorithm to do some task periodically. This function is executed every period number of clock ticks where each clock tick refers to a memory access. (5) void setPeriodicity(int period): This function is used to set the period for the invocation of the periodic- Function. (6) void configure(Frame owner): This function can be used to set some parameters for the algorithms like the difference between two clock hands in case of two clock hand algorithm. This functionality is available only through the GUI interface. The page replacement algorithm should create a new frame to take input from the user.

5 Bibliography
[Tanenbaum, 2001] Tanenbaum, A. S., Modern Operating Systems, Upper Saddle River 2001 DHAMDHERE, D. 2002. Operating Systems - A Concept-based Approach. Tata McGrawHill. STALLINGS, W. 2000. Operating Systems. Prentice Hall of India.

Potrebbero piacerti anche