Professional Documents
Culture Documents
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
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.
Page number & corresponding page frame memory address. Page numbers are sequential (Page 0, Page 1 )
Example:
1.
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
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
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
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
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
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 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.
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
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
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
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
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
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
30
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
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
2.
3.
34
Example1:
35
Example2:
36
37
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
Job Table lists every job in process (one for whole system). Segment Map Table lists details about each segment (one for each job).
3.
Page Map Table lists details about every page (one for each segment).
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
Overhead required for the extra tables Time required to reference segment table and page table. Logical benefits of segmentation. Physical benefits of paging
42
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
45
46
47
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
50