24191 os virtualmemory final
TRANSCRIPT
-
8/12/2019 24191 OS VirtualMemory Final
1/50
MEMORY MANAGEMENT:
VIRTUAL MEMORYPrepared by: EJ Antonio
1
-
8/12/2019 24191 OS VirtualMemory Final
2/50
Introduction:
In a very simple operating system, each process is given itsown segment of RAM for its own use. Though this systemis straightforward and easy to implement, it brings upsome issues.
How does the operating system decide how much memory toallocate to each process?What if the total memory required by active processesexceeds the amount of RAM available?How can the system prevent a process from reading oraltering the memory supposedly reserved for other processes?
To address these issues, and to address several others thatwe'll discuss later, modern operating systems use virtualmemory ..
2
-
8/12/2019 24191 OS VirtualMemory Final
3/50
Basics
The idea of virtual memory is to create a virtual addressspace that doesn't correspond to actual addresses inRAM. The system stores the official copy of memory ondisk and caches only the most frequently used data in
RAM.VM is divided into equal pages . (Usually 4K in size)MM is divided into equal Page FramesThe system also maintains a page table , stored in RAM,which is an array of entries, one for each page, storinginformation about the page.
3
-
8/12/2019 24191 OS VirtualMemory Final
4/50
4
-
8/12/2019 24191 OS VirtualMemory Final
5/50
TERMSInput Queue
Collection of jobs waiting to be brought into the Memory Unit forexecution
Logical AddressAddress generated by the CPU
Physical AddressAddress generated by the MU
Dynamic LoadingLoading of job/process is stopped until called
Dynamic LinkingLinking is postponed until executed
SwappingTransferring of data from MU into temporary storage for the use of differentprocess/job
5
-
8/12/2019 24191 OS VirtualMemory Final
6/50
Virtual Memory Schemes
Types of Virtual Memory Schemes:PagingDemand PagingSegmentation
Segmentation / Demand Paging Allocation
6
-
8/12/2019 24191 OS VirtualMemory Final
7/50
Paged Memory AllocationBefore a job is loaded into memory, it is divided into parts called pages that will be loaded into memory locations called pageframes.Paged memory allocation is based on the concept of dividing eachincoming job into pages of equal size.
SectorsThe sections of a Virtual Memory (disk)
Page FramesSections of main memory are called page frames.
The scheme works quite efficiently when the pages, sectors, andpage frames are all the same size. The exact size (the number ofbytes that can be stored in each of them) is usually determined bythe disks sector size.
7
-
8/12/2019 24191 OS VirtualMemory Final
8/50
Paging Requirements
1.Job Table (JT) - 2 entries for each active job. Job number and job sizeDynamic grows/shrinks as jobs loaded/completed.
2.Page Map Table (PMT) - 1 entry per page.Page number & corresponding page frame memory address.Page numbers are sequential (Page 0, Page 1 )
3.Memory Map Table (MMT) - 1 entry for each page frame.Location & free/busy status.
8
-
8/12/2019 24191 OS VirtualMemory Final
9/50
Example:
1. Job 1 = 350 bytes longPage size = 100 bytes
9
-
8/12/2019 24191 OS VirtualMemory Final
10/50
Displacement / Offset
The displacement , or offset , of a byte is the factor used tolocate that byte within its page frame. It is a relative factor.In the simplified example, bytes 0, 100, 200, and 300 are thefirst bytes for pages 0, 1, 2, and 3, respectively, so each has adisplacement of zero.Likewise, if the operating system needs to access byte 214, itcan first go to page 2 and then go to byte 14 (the fifteenthline).The first byte of each page has a displacement of zero, and thelast byte, has a displacement of 99. So once the operatingsystem finds the right page, it can access the correct bytesusing its relative position within its page
10
-
8/12/2019 24191 OS VirtualMemory Final
11/50
11
-
8/12/2019 24191 OS VirtualMemory Final
12/50
Address CalculationThe operating system uses an algorithm to calculate the page anddisplacement; it is a simple arithmetic calculation.To find the address of a given program instruction, the bytenumber is divided by the page size, keeping the remainder as aninteger.The resulting quotient is the page number, and the remainder isthe displacement within that page. When it is set up as a longdivision problem, it looks like this:
12
-
8/12/2019 24191 OS VirtualMemory Final
13/50
Example:
1. For example, if we use 100 bytes as the page size, find thepage number and the displacement of byte 214.
13
-
8/12/2019 24191 OS VirtualMemory Final
14/50
Example:
2. Lets try another example with a more common pagesize of 256 bytes. Say we are seeking the location of byte384.
14
-
8/12/2019 24191 OS VirtualMemory Final
15/50
Locating a Line in the Memory Address Resolution / Address TranslationProcedure:
1. Determine the page no. and the displacement by thealgorithm discussed.
2. Refer to jobs PMT to find the corresponding pageframe.
3. Get the address of the beginning of the page frame bymultiplying the page frame no. by the page frame size.ADDR_PAGE_FRAME = PAGE_FRAME_NUM * PAGE_SIZE
4. Finally add the displacement to the starting address of
the page frame.INSTR_ADDR_IN_MEM = ADDR_PAGE_FRAME + DISPL
15
-
8/12/2019 24191 OS VirtualMemory Final
16/50
Example:
1. Page size = 512 bytesPage Frame size = 512 bytesFind the exact location of line byte 518.
Page Map Table:
16
-
8/12/2019 24191 OS VirtualMemory Final
17/50
Pros and Cons of Paged Memory Allocation
Allows jobs to be allocated in non-contiguous memorylocations.Memory used more efficiently; more jobs can fit.
Size of page is crucial (not too small, not too large).
Increased overhead occurs.Reduces, but does not eliminate, internal fragmentation.
17
-
8/12/2019 24191 OS VirtualMemory Final
18/50
DEMAND PAGINGBring a page into memory only when it is needed, so lessI/O & memory needed.
Faster response.
Takes advantage that programs are written sequentially sonot all pages are necessary at once. For example:
User-written error handling modules.Mutually exclusive modules.Certain program options are either mutually exclusive or not
always accessible.Many tables assigned fixed amount of address space eventhough only a fraction of table is actually used.
18
-
8/12/2019 24191 OS VirtualMemory Final
19/50
DEMAND PAGING
Demand paging made virtual memory widely available.Can give appearance of an almost-infinite or nonfinite amountof physical memory.
Requires use of a high-speed direct access storage devicethat can work directly with CPU.
How and when the pages are passed (or swapped)depends on predefined policies that determine when tomake room for needed pages and how to do so.
19
-
8/12/2019 24191 OS VirtualMemory Final
20/50
Table Requirements in Demand Paging Job Table.Page Map Table (with 3 new fields).1. Determines if requested page is already in memory.2. Determines if page contents have been modified.3. Determines if the page has been referenced recently.
Used to determine which pages should remain in main memory andwhich should be swapped out.
Memory Map Table.
20
Page StatusBit
ReferencedBit
ModifiedBit
PageFrame
0 1 1 1 5
1 1 0 0 92 1 0 0 7
3 1 1 0 12
-
8/12/2019 24191 OS VirtualMemory Final
21/50
21
Page Statusbit
Referencedbit
Modifiedbit
Page frame
0 1 1 1 5
1 1 0 0 9
2 1 0 0 7
3 1 1 0 12
-
8/12/2019 24191 OS VirtualMemory Final
22/50
Example:
22
-
8/12/2019 24191 OS VirtualMemory Final
23/50
Trashing | Page FaultTrashing an excessive amount of page swapping back andforth between main memory and secondary storage.
Operation becomes inefficient.Caused when a page is removed from memory but is calledback shortly thereafter.Can occur across jobs, when a large number of jobs arevying for a relatively few number of free pages.Can happen within a job (e.g., in loops that cross pageboundaries).
Page fault a failure to find a page in memory.
23
-
8/12/2019 24191 OS VirtualMemory Final
24/50
Page Replacement Policies
Policy that selects page to be removed iscrucial to system efficiency.Selection of algorithm is critical.
Types:First-in first-out (FIFO) policyLeast-recently-used (LRU) policyMost recently used (MRU) policy
Least frequently used (LFU) policyOptimal PolicyHardware Matrix LRU
24
-
8/12/2019 24191 OS VirtualMemory Final
25/50
Page Replacement Policies
First-In First-Out (FIFO)The best page to be replaced when the main memory isfull is the first page that was allocated in the memory.
Example: Reference String: ABC AED CAB ECBThree (3) Page Frame Allocation
25
A B C A E D C A B E C B
-
8/12/2019 24191 OS VirtualMemory Final
26/50
Page Replacement Policies
Least Recently Used (LRU)In this policy, the least recently used page is the page tobe removed or displaced from the memory, when thememory is full.
Example: Reference String: ABC AED CAB ECBThree (3) Page Frame Allocation
26
A B C A E D C A B E C B
-
8/12/2019 24191 OS VirtualMemory Final
27/50
Page Replacement Policies
Most Recently Used (MRU)In this policy, the most recently used page is the page tobe removed or displaced from the memory, when thememory is full.
Example: Reference String: ABC AED CAB ECBThree (3) Page Frame Allocation
27
A B C A E D C A B E C B
-
8/12/2019 24191 OS VirtualMemory Final
28/50
Page Replacement Policies
Least Frequently Used (LFU)This technique uses counter, the least frequently usedpage is the page to be replaced when the main memory isfull
Example: Reference String: ABC AED CAB ECBThree (3) Page Frame Allocation
28
A B C A E D C A B E C B
-
8/12/2019 24191 OS VirtualMemory Final
29/50
Page Replacement Policies
Optimal PolicyThe Optimal policy selects for replacement the page that will not beused for longest period of time.Impossible to implement (need to know the future) but serves as astandard to compare with the other algorithms.
Example: Reference String: ABC AED CAB ECBThree (3) Page Frame Allocation
29
A B C A E D C A B E C B
-
8/12/2019 24191 OS VirtualMemory Final
30/50
Page Replacement Policies
Hardware Matrix Implementation (LRU)Example: Pages are referenced in the order 0, 1, 2, 3, 2, 1, 0, 3, 2, 3Four (4) Page Frame Allocation
30
-
8/12/2019 24191 OS VirtualMemory Final
31/50
Segmented Memory AllocationBased on common practice by programmers of structuringtheir programs in modules ( logical groupings of code).
A segment is a logical unit such as: main program,subroutine, procedure, function, local variables, globalvariables, common block, stack, symbol table, or array.
Main memory is not divided into page frames because sizeof each segment is different.
Memory is allocated dynamically.
31
-
8/12/2019 24191 OS VirtualMemory Final
32/50
32
-
8/12/2019 24191 OS VirtualMemory Final
33/50
Segment Map Table (SMT)When a program is compiled, segments are set upaccording to programs structural modules.
Each segment is numbered and a
Segment Map Table (SMT)is generated for each job.
Contains segment numbers,their lengths, access rights, status, and(when each is loaded into memory)its location in memory.
33
-
8/12/2019 24191 OS VirtualMemory Final
34/50
Tables Used in Segmentation
Memory Manager needs to track segments in memory:1. Job Table (JT)
Lists every job in process (one for whole system).
2. Segment Map Table (SMT)
Lists details about each segment (one for each job).
3. Memory Map Table (MMT)Monitors allocation of main memory (one for whole system).
34
-
8/12/2019 24191 OS VirtualMemory Final
35/50
Example1:Segment Map Table track for each segment for Job1.
35
-
8/12/2019 24191 OS VirtualMemory Final
36/50
Example2:Use segmentation to allocate each segment in the MM.
36
-
8/12/2019 24191 OS VirtualMemory Final
37/50
Pros and Cons of SegmentationNeed for Compaction.Memory is allocated dynamically.External fragmentation.
37
-
8/12/2019 24191 OS VirtualMemory Final
38/50
Segmented/Demand Paged Memory Allocation
It is a combination of segmentation and demand paging,and it offers the logical benefits of segmentation, as wellas the physical benefits of paging.This allocation scheme doesnt keep each segment as a
single contiguous unit but subdivides it into pages of equalsize, smaller than most segments, and more easilymanipulated than whole segments. Therefore, many of theproblems of segmentation (compaction, external
fragmentation, and secondary storage handling) areremoved because the pages are of fixed length.
38
-
8/12/2019 24191 OS VirtualMemory Final
39/50
Tables used in S/DP Memory Allocation1. Job Table lists every job in process (one for whole
system).2. Segment Map Table lists details about each segment (one
for each job).
E.g., protection data, access data.3. Page Map Table lists details about every page (one for
each segment).E.g., status, modified, and referenced bits .
4. Memory Map Table monitors allocation of page framesin main memory (one for whole system).
39
-
8/12/2019 24191 OS VirtualMemory Final
40/50
Memory LocationTo access a location in memory, the system must locatethe address, which is composed of three entries: segmentnumber, page number within that segment, anddisplacement within that page. It is a three-dimensionaladdressing scheme:SEGMENT_NO. & PAGE_NO. & DISPLACEMENT
40
-
8/12/2019 24191 OS VirtualMemory Final
41/50
Example;
41
-
8/12/2019 24191 OS VirtualMemory Final
42/50
Pros & Cons of S/DP Memory AllocationOverhead required for the extra tablesTime required to reference segment table and page table.Logical benefits of segmentation.Physical benefits of paging
42
-
8/12/2019 24191 OS VirtualMemory Final
43/50
Virtual Memory (VM)Even though only a portion of each program is stored inmemory, virtual memory gives appearance that programsare being completely loaded in main memory during theirentire processing time.
Shared programs and subroutines are loaded ondemand, reducing storage requirements of mainmemory.
VM is implemented through demand paging andsegmentation schemes.
43
-
8/12/2019 24191 OS VirtualMemory Final
44/50
Virtual Paging versus Virtual SegmentationVIRTUAL MEMORY WITH
PAGINGVIRTUAL MEMORY WITH
SEGMENTATION
Allows internal fragmentationwithin page frames
Doesnt allow internalfragmentation
Doesnt allow externalfragmentation Allows external fragmentation
Programs are divided intoequal-sized pages
Programs are divided into unequal-sized segments
Absolute address calculatedusing page number anddisplacement
Absolute address calculated usingsegment number and displacement
Requires PMT Requires SMT
44
The Virtual Page Number through
-
8/12/2019 24191 OS VirtualMemory Final
45/50
The Virtual Page Number throughExamples
For some examples of this, provided are some virtualaddresses with the page size of the system. Then, tell whatthe page is, and what the page offset is.
1. Virtual address 0x000A502F, with page size of 4K bytes.
45
The Virtual Page Number through
-
8/12/2019 24191 OS VirtualMemory Final
46/50
The Virtual Page Number throughExamples2. Virtual address 0x000A502F, with page size of 2K bytes.
46
The Virtual Page Number through
-
8/12/2019 24191 OS VirtualMemory Final
47/50
The Virtual Page Number throughExamples3. Virtual address 0x009B18A0, with page size of 4K bytes.
47
The Virtual Page Number through
-
8/12/2019 24191 OS VirtualMemory Final
48/50
The Virtual Page Number throughExamples4. Virtual address 0x009B18A0, with page size of 2K bytes.
48
-
8/12/2019 24191 OS VirtualMemory Final
49/50
Advantage of VMWorks well in a multiprogramming environment becausemost programs spend a lot of time waiting.
Jobs size is no longer restricted to the size of main memory(or the free space within main memory).Memory is used more efficiently.
Allows an unlimited amount of multiprogramming.Eliminates external fragmentation when used with paging andeliminates internal fragmentation when used withsegmentation.
Allows a program to be loaded multiple times occupying adifferent memory location each time.Allows the sharing of code and data.Facilitates dynamic linking of program segments.
49
-
8/12/2019 24191 OS VirtualMemory Final
50/50
Disadvantage of VMIncreased processor hardware costs.
Increased software complexity to prevent thrashing.