You are on page 1of 50

MEMORY MANAGEMENT: VIRTUAL MEMORY

Prepared by: EJ Antonio

Introduction:
In a very simple operating system, each process is given its own segment of RAM for its own use. Though this system is straightforward and easy to implement, it brings up some issues.

How does the operating system decide how much memory to allocate to each process? What if the total memory required by active processes exceeds the amount of RAM available? How can the system prevent a process from reading or altering the memory supposedly reserved for other processes?

To address these issues, and to address several others that we'll discuss later, modern operating systems use virtual memory..
2

Basics

The idea of virtual memory is to create a virtual address space that doesn't correspond to actual addresses in RAM. The system stores the official copy of memory on disk 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 Frames The system also maintains a page table, stored in RAM, which is an array of entries, one for each page, storing information about the page.

TERMS

Input Queue Collection of jobs waiting to be brought into the Memory Unit for execution Logical Address Address generated by the CPU Physical Address Address generated by the MU Dynamic Loading Loading of job/process is stopped until called Dynamic Linking Linking is postponed until executed Swapping Transferring of data from MU into temporary storage for the use of different process/job
5

Virtual Memory Schemes

Types of Virtual Memory Schemes:

Paging Demand Paging Segmentation Segmentation / Demand Paging Allocation

Paged Memory Allocation

Before a job is loaded into memory, it is divided into parts called pages that will be loaded into memory locations called page frames. Paged memory allocation is based on the concept of dividing each incoming job into pages of equal size. Sectors The sections of a Virtual Memory (disk) Page Frames Sections of main memory are called page frames. The scheme works quite efficiently when the pages, sectors, and page frames are all the same size. The exact size (the number of bytes that can be stored in each of them) is usually determined by the disks sector size.
7

Paging Requirements
1. Job Table (JT) - 2 entries for each active job.

Job number and job size Dynamic 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.

Example:
1.

Job 1 = 350 bytes long Page size = 100 bytes

Displacement / Offset

The displacement, or offset, of a byte is the factor used to locate that byte within its page frame. It is a relative factor. In the simplified example, bytes 0, 100, 200, and 300 are the first bytes for pages 0, 1, 2, and 3, respectively, so each has a displacement of zero. Likewise, if the operating system needs to access byte 214, it can first go to page 2 and then go to byte 14 (the fifteenth line). The first byte of each page has a displacement of zero, and the last byte, has a displacement of 99. So once the operating system finds the right page, it can access the correct bytes using its relative position within its page
10

11

Address Calculation

The operating system uses an algorithm to calculate the page and displacement; it is a simple arithmetic calculation. To find the address of a given program instruction, the byte number is divided by the page size, keeping the remainder as an integer. The resulting quotient is the page number, and the remainder is the displacement within that page. When it is set up as a long division problem, it looks like this:

12

Example:
1. For example, if we use 100 bytes as the page size, find the page number and the displacement of byte 214.

13

Example:
2. Lets try another example with a more common page size of 256 bytes. Say we are seeking the location of byte 384.

14

Locating a Line in the Memory Address Resolution / Address Translation


Procedure: 1. Determine the page no. and the displacement by the algorithm discussed. 2. Refer to jobs PMT to find the corresponding page frame. 3. Get the address of the beginning of the page frame by multiplying 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

Example:
1. Page size = 512 bytes Page Frame size = 512 bytes Find the exact location of line byte 518. Page Map Table:

16

Pros and Cons of Paged Memory Allocation

Allows jobs to be allocated in non-contiguous memory locations.

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

DEMAND PAGING

Bring a page into memory only when it is needed, so less I/O & memory needed.

Faster response.

Takes advantage that programs are written sequentially so not 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 even though only a fraction of table is actually used.

18

DEMAND PAGING

Demand paging made virtual memory widely available.

Can give appearance of an almost-infinite or nonfinite amount of physical memory.

Requires use of a high-speed direct access storage device that can work directly with CPU.
How and when the pages are passed (or swapped) depends on predefined policies that determine when to make room for needed pages and how to do so.

19

Table Requirements in Demand Paging

Job Table. Page Map Table (with 3 new fields).


1. 2. 3.

Determines if requested page is already in memory. Determines if page contents have been modified. Determines if the page has been referenced recently.

Used to determine which pages should remain in main memory and which should be swapped out.

Page 0 1 2 3

20

Status Bit 1 1 1 1

Referenced Bit 1 0 0 1

Modified Bit 1 0 0 0

Page Frame 5 9 7 12

Memory Map Table.

Page 0 1 2 3

Status bit 1 1 1 1

Referenced bit 1 0 0 1

Modified bit 1 0 0 0

Page frame 5 9 7 12

21

Example:

22

Trashing | Page Fault

Trashing an excessive amount of page swapping back and forth between main memory and secondary storage. Operation becomes inefficient. Caused when a page is removed from memory but is called back shortly thereafter. Can occur across jobs, when a large number of jobs are vying for a relatively few number of free pages. Can happen within a job (e.g., in loops that cross page boundaries). Page fault a failure to find a page in memory.
23

Page Replacement Policies Policy that selects page to be removed is crucial to system efficiency.

Selection of algorithm is critical.

Types:


24

First-in first-out (FIFO) policy Least-recently-used (LRU) policy Most recently used (MRU) policy Least frequently used (LFU) policy Optimal Policy Hardware Matrix LRU

Page Replacement Policies First-In First-Out (FIFO)

The best page to be replaced when the main memory is full is the first page that was allocated in the memory.

Example: Reference String: ABC AED CAB ECB Three (3) Page Frame Allocation
A B C A E D C A B E C B

25

Page Replacement Policies Least Recently Used (LRU)

In this policy, the least recently used page is the page to be removed or displaced from the memory, when the memory is full.

Example: Reference String: ABC AED CAB ECB Three (3) Page Frame Allocation
A B C A E D C A B E C B

26

Page Replacement Policies Most Recently Used (MRU)

In this policy, the most recently used page is the page to be removed or displaced from the memory, when the memory is full.

Example: Reference String: ABC AED CAB ECB Three (3) Page Frame Allocation
A B C A E D C A B E C B

27

Page Replacement Policies Least Frequently Used (LFU)

This technique uses counter, the least frequently used page is the page to be replaced when the main memory is full

Example: Reference String: ABC AED CAB ECB Three (3) Page Frame Allocation
A B C A E D C A B E C B

28

Page Replacement Policies Optimal Policy

The Optimal policy selects for replacement the page that will not be used for longest period of time. Impossible to implement (need to know the future) but serves as a standard to compare with the other algorithms.

Example: Reference String: ABC AED CAB ECB Three (3) Page Frame Allocation

29

Page Replacement Policies Hardware Matrix Implementation (LRU)


Example: Pages are referenced in the order 0, 1, 2, 3, 2, 1, 0, 3, 2, 3 Four (4) Page Frame Allocation

30

Segmented Memory Allocation

Based on common practice by programmers of structuring their programs in modules (logical groupings of code). A segment is a logical unit such as: main program, subroutine, procedure, function, local variables, global variables, common block, stack, symbol table, or array. Main memory is not divided into page frames because size of each segment is different. Memory is allocated dynamically.

31

32

Segment Map Table (SMT)

When a program is compiled, segments are set up according 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

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

Example1:

Segment Map Table track for each segment for Job1.

35

Example2:

Use segmentation to allocate each segment in the MM.

36

Pros and Cons of Segmentation

Need for Compaction. Memory is allocated dynamically. External fragmentation.

37

Segmented/Demand Paged Memory Allocation

It is a combination of segmentation and demand paging, and it offers the logical benefits of segmentation, as well as the physical benefits of paging. This allocation scheme doesnt keep each segment as a single contiguous unit but subdivides it into pages of equal size, smaller than most segments, and more easily manipulated than whole segments. Therefore, many of the problems of segmentation (compaction, external fragmentation, and secondary storage handling) are removed because the pages are of fixed length.

38

Tables used in S/DP Memory Allocation


1. 2.

Job Table lists every job in process (one for whole system). 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 frames in main memory (one for whole system).

39

Memory Location

To access a location in memory, the system must locate the address, which is composed of three entries: segment number, page number within that segment, and displacement within that page. It is a three-dimensional addressing scheme: SEGMENT_NO. & PAGE_NO. & DISPLACEMENT

40

Example;

41

Pros & Cons of S/DP Memory Allocation

Overhead required for the extra tables Time required to reference segment table and page table. Logical benefits of segmentation. Physical benefits of paging

42

Virtual Memory (VM)

Even though only a portion of each program is stored in memory, virtual memory gives appearance that programs are being completely loaded in main memory during their entire processing time. Shared programs and subroutines are loaded on demand, reducing storage requirements of main memory. VM is implemented through demand paging and segmentation schemes.
43

Virtual Paging

versus

Virtual Segmentation
VIRTUAL MEMORY WITH SEGMENTATION

VIRTUAL MEMORY WITH PAGING

Allows internal fragmentation within page frames


Doesnt allow external fragmentation Programs are divided into equal-sized pages Absolute address calculated using page number and displacement Requires PMT
44

Doesnt allow internal fragmentation


Allows external fragmentation Programs are divided into unequalsized segments Absolute address calculated using segment number and displacement Requires SMT

The Virtual Page Number through Examples


For some examples of this, provided are some virtual addresses with the page size of the system. Then, tell what the page is, and what the page offset is. 1.Virtual address 0x000A502F, with page size of 4K bytes.

45

The Virtual Page Number through Examples


2.Virtual address 0x000A502F, with page size of 2K bytes.

46

The Virtual Page Number through Examples


3.Virtual address 0x009B18A0, with page size of 4K bytes.

47

The Virtual Page Number through Examples


4.Virtual address 0x009B18A0, with page size of 2K bytes.

48

Advantage of VM


49

Works well in a multiprogramming environment because most 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 and eliminates internal fragmentation when used with segmentation. Allows a program to be loaded multiple times occupying a different memory location each time. Allows the sharing of code and data. Facilitates dynamic linking of program segments.

Disadvantage of VM

Increased processor hardware costs. Increased software complexity to prevent thrashing.

50

You might also like