Presentation is loading. Please wait.

Presentation is loading. Please wait.

Virtual Memory Management

Similar presentations


Presentation on theme: "Virtual Memory Management"— Presentation transcript:

1 Virtual Memory Management
Sometimes the size of the free space in the main memory is smaller than the process that wants to come in. In this case, the idea of the virtual memory becomes necessary for using. The virtual memory is a back store memory that contains the total process. This process will be divided into either pages or segments. Then , only the parts ( pages or segments) that are really needed for execution, be swapped into the main memory. While, the parts that are not needed at the moment be kept in the virtual memory. Also the parts that become inactive in the main memory might be swapped out to the virtual memory if necessary. Figure in slide no. 2 shows the idea of the virtual memory. It is clear that the virtual memory is very suitable for multiprocesses system. That is because even if the main memory is not enough to contain all the processes, parts of these processes can be swapped in the main memory and be executed with each other.

2 The principle of the virtual memory

3 Virtual memory paging In the previous chapter ( Main Memory Management ) , when we talked about the paging, we said that the process is divided into pages and then all these pages ( all the process) is loaded in the main memory. Of course, it is not necessary that these pages be in contiguous places, as explained. However, with pages, there must be a page table to translate the logical address into real address. The same technique ( page table) will be used here with virtual memory system but now, it will be more complicated. That is because some and not all pages of the process ( as needed ) will be in the main memory. So a new bit will be added to each record in the page table of the process as shown in figure of slide no. 4. This bit which is called valid/invalid or sometimes present/absent, is used to express if the page in the main memory or not.

4 The use of the page table with virtual memory paging system

5 Dynamic address translation in virtual memory with paging system
The translation of the logical address to the real address happens without the user feels that. In other words, the user feels his process is be executed as if all the process is in the main memory. While in fact, there is unseen efforts done by the OS to check the status of the demande page if it is in the main memory or still in the virtual memory. However, if the page is in the main memory then the situation is normal and the execution continues. But if the page is not in the main memory till that moment, then a situation called page fault occurs. That page fault will lead to the OS to do what is necessary to bring the demanded page from the virtual memory to the main memory. These activities are shown in the figure of the slide no. 6, which can be explained as below; According to the page no., the OS will search the page table to find the record of this page and check the present/absent bit. If the page is not in the main memory then a page fault trap occurs to tell the OS about the absent of the demanded page. The OS will go to the virtual memory to insure that the page is in the virtual memory and to prepare a free frame in the main memory. The OS will bring the demanded page into that free frame. The OS will updates the information of that page in the page table ( the present/absent bit and the frame no. where the page is loaded. Re-execute the instruction again with no page fault because the demanded page becomes in the main memory now.

6 The steps of managing the page fault trap

7 Page Replacement Algorithms
As we said when page fault occurs, the OS must start to bring the demanded page from the virtual memory to the main memory. That means a free frame must be provided to put the page inside it. When a free frame is found , this operation is called placement. Now, what happens if the memory is full and no free frame exists ?. In this case, the operation is called replacement. That is because a page must be swapped from the main memory to the virtual memory to make a free frame, so the OS can swap the new demanded page in it. This procedure is shown in the figure of slide no. 8. There are 4 steps to do that after the page fault occurs. 1) a victim page is selected from between the pages in the main memory , to be swapped out. This is done by one of the replacement algorithms ( studied later). 2) Updating the information of the victim page in the page table from present to absent ( valid to invalid). 3) Swapping the demanded page ( the new page) from the virtual memory in the emptied frame in the main memory. 4) Updating the information of the new page in the page table to indicate that it is now in the main memory ( from absent to present).

8 Page Replacement Steps

9 First in First out Page Replacement Algorithm
This algorithm is the most simple one. This algorithm will candidate the oldest page with respect to the entering to the main memory for swapping out when necessary. Of course this can be done using linked list structure arranges the pages from the first in page to the last in page. Figure in the slide no. 10 shows an example for the FIFO algorithm that implemented on the following page reference string. Also we assume that there are only three free frames at the beginning. From the figure we find that 15 page faults occur through the execution of the above page reference string. Three main problems will face this algorithm, these are; 1) The number of page faults is too high comparing with other algorithms 2) This algorithm may candidate a page for replacement that is heavily used , and this will cause this page to demand return back to the main memory after a short while. 3) A belady ‘s anomaly may occur which means that the number of page fault will increase directly proportional to the number of free frames.

10 First in First out page replacement algorithm

11 Optimal Page Replacement Algorithm
This algorithm is the best comparing with the other replacement algorithms. That is because , it produces the minimum number of page faults when used for replacement. However, this algorithm can be implemented theoretically only since the OS needs to know in advance the page reference string. This is almost very difficult because no one can guess the future history of the order of pages that will referenced. This algorithm selects for replacement, the page with the most distant usage in the future. In other words, this algorithm looks to the future use of the pages when trade-off to choose one of them for replacement. . Figure in the slide no. 12 shows an example for the Optimal algorithm for the same page reference string that used with FIFO algorithm. Also we assume that there are 3 free frames at the beginning of testing the algorithm. We see from the figure that only 9 page faults occur while this number of page faults was 15 for the same testing condition.

12 Optimal page replacement algorithm

13 Least Recently Used Page Replacement Algorithm [ LRU]
As we see that the Optimal algorithm is not possible practically, because we need to look to the future usage of the pages. So, the alternative is to look to the times of the pages usage in the past. This algorithm selects for replacement the page with the oldest usage. Although, this algorithm is a possible solution, but it is very expensive and time consuming. One of the methods to implement this algorithm is to use a large register ( say 64 bit) as a counter. This counter will increment with each memory reference. Also we have to add a field ( same size as the register ) to each page entry in the page table. When any page is referenced, the counter value at that moment will be copied in the entry of that page in the page table. Now when a page fault occurs, the OS will select the page with lower counter value, since this page will be the oldest page with respect to the usage. Figure in the slide no. 14 shows the same previous example but now with the LRU algorithm . As we see, this algorithm will cause 12 page fault and this better than the algorithm FIFO which caused 15 page fault, but it is less efficient than the optimal algorithm which caused 9 page fault.

14 Least Recently Used page replacement algorithm

15 Not Recently Used Page Replacement Algorithm [ NRU]
We said that the FIFO algorithm is simple but not efficient , while the LRU algorithm is efficient but very expensive as we saw.So, An alternative algorithm is suggested as a balancing between the simplicity, the cost and the efficiency. This alternative is called Not Recently Used algorithm ( NRU). This algorithm depends upon 2 bits to recognize between the pages in the main memory. The first bit which is called referenced bit (R), becomes 1 when a read or write operation is done on the page. The second bit which is called modified bit (M), becomes 1 when write operation is done on the page. This two bits-system is used to implement the NRU algorithm as follows; two bits field for representing (R ) and (M) will be added to each page entry in the page table. Now, when the process starts, the page table will also be initialized with values of zero for (R) and (M) bits. The OS will modify these bits according to the type of the reference ( read or write) that occurs within each page through the process life. The question is how the NRU algorithm will use these two bits to select a page for replacement when a page fault occurs. In fact the OS will classify the pages according to the values of these two bits into 4 classes as below; class 0: not referenced [ R=0] and not modified [M=0] class 1: not referenced [ R=0] and modified [ M=1] class 2: referenced [ R=1] and not Modified [ M=0] class 3: referenced [ R=1] and Modified [ M=1]

16 Steps of page selection for replacement:
1) The page from the lowest class value, will be candidate for replacement if needed. 2) Of course if there are more than one page in the lowest class then a page randomly is selected from between them for replacement. 3) If the lowest class is empty, then the selection will be from between the pages in the next class. The problem of this method is the accumulation of the pages in the class 2 and 3, with the time of the process execution. To solve this problem, the bit (R ) will be reset for all pages in the page table from time to time ( say each 10 msec). This operation will help to recognize between the used and the unused pages within the next 10 msec. Some students may ask , how the class 1 occurs !! , in other words how the page be unreferenced and modified at the same time [ R=0 and M=1]?. This question is logical because the page is modified only when it is used. The answer is very simple , R became 0 because of the reset operation that happens each 10 msec as explained above. That means, the pages in class 3 will classified as class 1 after each 10 msec. The figure in the slide no. 17 gives an example to explain the operation of this method. The NRU algorithm seems to be simple because its operation depends on only two bits which is easy to be managed. And also, it is fairly efficient. So, this algorithm is attractive to be used as a page replacement technique.

17 R M R M R M 1 1 Pg0 Pg1 Pg2 . Pgn Pg0 Pg1 Pg2 . Pgn Pg0 Pg1 Pg2 . Pgn At time = 10 msec Reset bit R At time = 0 At time = x The page map table in different times using NRU algorithm for replacement

18 Segmentation with virtual memory
As we said there is main difference between the segmentation without and with virtual memory. Without virtual memory, the OS will load in the main memory all the segments of the process ( not necessary be contiguous).While with virtual memory at any moment, the OS will load in the main memory only the segments that are needed for execution. However, the use of segmentation means the need for a segment map table for each process. The figure below shows a typical entry (record) of the segment map table. As we see, this entry consists of the following fields: The base of the segment which represents the starting address in the main memory The limit of the segment which represents the length of the segment to protect the security of the segment from the unauthorized use The P bit which indicates if the segment is present in the main memory or not The M bit which indicates if the segment has been modified or not

19 Dynamic address translation in virtual memory with segmentation system
The dynamic address translation with virtual segmentation system is shown in the figure of the slide no. 20. This mechanism is done under the control of the OS and can be explained as below: The logical (virtual) address of the instruction to be executed consists of two parts. The first part is the segment no. In fact, this segment no. is given to the segment by the OS and it is used as index in the segment table. The second part represents the offset address within the segment. The segment no. will be used as index in the segment table to find the related entry After reaching the related entry, the present bit p will be tested. This bit is not mentioned in the figure for simplicity. Now if the bit p is 1 that means the segment is in the main memory and the base address is found in the segment table, otherwise a segment fault occurs to bring the wanted segment from the virtual memory to a suitable space in the main memory. The base address of that suitable space is written in the segment table. At the end the base address is read from the segment table and be added to the offset from the logical address. The summation value of the addresses will represent the real address of the executed instruction in the main memory

20 Dynamic address translation using segmentation with virtual memory

21 The problems of segmentation method with virtual memory: These problems can be summarized as below:
Segments are of different sizes, so the OS needs more complicated procedure to find the suitable space when there is needed to bring a segment in the main memory. For instance, OS can use First-Fit or Best-Fit technique to find the suitable space. On segment fault, some times swapping out one segment is not enough, so more than one segment might be selected for replacement. If no suitable space is available, then all the external fragments might be merged together into on big useful fragment. This can be done by compaction which is time consuming as we said.

22 Virtual Memory with Segmentation-Paging Method
First of all, segmentation-paging can be used with and without virtual memory as was the case with pure segmentation and pure paging method. However, the important question here is about the benefits of merging between segmentation and paging. The answer is simple, since by merging we will gain the advantages of paging and also the advantages of segmentation. To remember , we will summarize these advantages here: Advantages of paging: Simple placement and replacement because the pages are of same size No external fragmentation but only internal fragmentation which is in the worst case will be less than a page size. Advantages of segmentation The logical division of the process by the owner will improve the efficiency of execution and will reduce the swapping operations. That is clear because the owner is the most person that knows his process’s structure. The sharing parts in the addressing space of the process is more secured because the owner will recognize between the shared and the unshared segment when he divides the process. This hybrid system will be implemented by cooperation between the process owner and the operating system. The owner will divide the process logically into segments while the OS will divide each segment physically into pages.

23 The dynamic Address translation will be done on two levels as shown in the figure of slide no. 24. On the first level, segment table will be built for each process with entries equal to the number of segments. On the second level, a page table will be built for each segment, so that the entries of that page table will be equal to the number of pages in that segment. In the following, we will explain the steps of the address translation; The logical address consists of two parts. The first part is the segment number and the second part is the segment offset. The segment offset itself consists of two parts which is the page number and the page offset. From the logical address, the OS will start with the segment number. This segment number will be used as index in the segment table to reach the related entry. In this entry, the OS will read a pointer to the page table that belongs to the wanted segment After using the pointer from step 2, the OS will reach the related page table. In this step the OS will get the segment offset from the logical address, which as we said it consists of page number and page offset. 4) The page number from step 3 will be used as index in the page table to reach the related entry. When the OS reaches that entry, It checks the P bit ( present/absent bit) to know whether that page is in the main memory or not ( this bit is not mentioned in the figure for simplicity). So here there are two possibilities a) if the page is in the main memory ( bit p =1) then the OS will read the frame number from the page table b) if the page is not in the main memory ( bit p = 0) then a page fault occurs and the OS will try to bring the wanted page from the virtual memory to a free frame ( as described previously). 5) Last step, the OS will add the frame number to the page offset to achieve the real address in the main memory

24 Dynamic address translation using hybrid segmentation-paging method with virtual memory

25 The disadvantages of the segmentation-paging method:
Although, this method is better than the pure segmentation or pure paging method, but it the same time it suffers from the complexity of the dynamic address translation as we saw. And since this addressing translation occurs with each instruction execution, so the complexity of the procedure will result in a time consuming condition.

26


Download ppt "Virtual Memory Management"

Similar presentations


Ads by Google