Chapter 8: Main Memory
ef: Operating System Concepts with Java – 7 th Edition,
Prof. Dr. Hanafy M. Ismail
Chapter 8: Memory Management
Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation
Ref: Operating System Concepts with Java – 7 th Edition ,
8.2
Prof. Dr. Hanafy M. Ismail
Objectives
To provide a detailed description of various ways of organizing memory hardware To discuss various memory-management techniques, including paging and segmentation
Ref: Operating System Concepts with Java – 7 th Edition ,
8.3
Prof. Dr. Hanafy M. Ismail
Background
Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage CPU can access directly Register access in one CPU clock (or less) Main memory access can take many cycles of the CPU clock to complete; Cache (fast memory) sits between main memory and CPU registers Not only are we concerned with relative speed of accessing physical memory, but we also must ensure correct operation has to protect the operating system from access by user processes and to protect user processes from one another. This protection must be provided by the hardware. Protection of memory required to ensure correct operation.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.4
Prof. Dr. Hanafy M. Ismail
Background
We need to ensure that each process has a separate memory space. A range of legal addresses is determined for each process. We can provide this protection by using two registers: Base register holds the smallest legal physical memory address. Limit register specifies the size of the range. Protection of memory space is accomplished by having the CPU hardware compare every address generated in the user mode with the registers
Ref: Operating System Concepts with Java – 7 th Edition ,
8.5
Prof. Dr. Hanafy M. Ismail
Base and Limit Registers ď °
A pair of base and limit registers define the logical address space
Ref: Operating System Concepts with Java – 7 th Edition ,
8.6
Prof. Dr. Hanafy M. Ismail
Binding of Instructions and Data to Memory
Address binding of instructions and data to memory addresses can happen at three different stages Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes Load time: Must generate relocatable code if memory location is not known at compile time Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers)
Ref: Operating System Concepts with Java – 7 th Edition ,
8.7
Prof. Dr. Hanafy M. Ismail
Logical vs. Physical Address Space
The concept of a logical address space that is bound to a separate physical address space is central to proper memory management Logical address – generated by the CPU; also referred to as virtual address Physical address – address seen by the memory unit
Ref: Operating System Concepts with Java – 7 th Edition ,
8.8
Prof. Dr. Hanafy M. Ismail
Memory-Management Unit (MMU)
Hardware device that maps at run-time virtual to physical address. Different methods can be used to accomplish such mapping. (discussed later) In a simple MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory The user program deals with logical addresses; it never sees the real physical addresses
Ref: Operating System Concepts with Java – 7 th Edition ,
8.9
Prof. Dr. Hanafy M. Ismail
Dynamic relocation using a relocation register
Ref: Operating System Concepts with Java – 7 th Edition ,
8.10
Prof. Dr. Hanafy M. Ismail
Dynamic Loading
Routine is not loaded until it is called. All routines are kept on disk in a relocatable load format. The main program is loaded into memory and is executed. When a routine needs to call another routine, the first routine first checks whether the other routine has been loaded. If not, the relocatable linking loader is called to load the desired routine into memory and update the program’s address tables to reflect this change Advantage: Better memory-space utilization; unused routine is never loaded This method is useful when large amounts of code are needed to handle infrequently occurring cases, such as error routines. No special support from the operating system is required ; implemented through program design
Ref: Operating System Concepts with Java – 7 th Edition ,
8.11
Prof. Dr. Hanafy M. Ismail
Swapping
A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution. For example, assume multiprogramming environment with RR CPU scheduling algorithm. The quantum must be large enough to allow reasonable amounts of computing to be done between swaps. Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images
Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed
Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped.
Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows) System maintains a ready queue of ready-to-run processes which have memory images on the backing store or in memory.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.12
Prof. Dr. Hanafy M. Ismail
Schematic View of Swapping
Ref: Operating System Concepts with Java – 7 th Edition ,
8.13
Prof. Dr. Hanafy M. Ismail
Contiguous Allocation
Main memory usually into two partitions: Resident operating system, usually held in low memory with interrupt vector User processes then held in high memory
Relocation registers used to protect user processes from each other, and from changing operating-system code and data Base register contains value of smallest physical address Limit register contains range of logical addresses – each logical address must be less than the limit register MMU maps logical address dynamically
Ref: Operating System Concepts with Java – 7 th Edition ,
8.14
Prof. Dr. Hanafy M. Ismail
HW address protection with base and limit registers
Ref: Operating System Concepts with Java – 7 th Edition ,
8.15
Prof. Dr. Hanafy M. Ismail
Memory Allocation: Fixed-sized allocation
Multiple-partition allocation Hole – block of available memory; holes of various size are scattered throughout memory When a process arrives, it is allocated memory from a hole large enough to accommodate it Operating system maintains information about: a) allocated partitions b) free partitions (hole) OS
OS
OS
OS
process 5
process 5
process 5
process 5
process 9
process 9
process 8 process 2
process 10 process 2
process 2
Ref: Operating System Concepts with Java – 7 th Edition ,
8.16
process 2
Prof. Dr. Hanafy M. Ismail
Dynamic Storage-Allocation Problem How to satisfy a request of size n from a list of free holes
First-fit: Allocate the first hole that is big enough Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size Worst-fit: Allocate the largest hole; must also search entire list
First-fit and best-fit better than worst-fit in terms of speed and storage utilization
Ref: Operating System Concepts with Java – 7 th Edition ,
8.17
Prof. Dr. Hanafy M. Ismail
Multiprogramming with Fixed Partitions
Divide memory into n (possibly unequal) partitions. Problem: Fragmentation Total space may be enough for a new process, but it’s “fragmented” into parts
0k 4k Pid: 405
16k Pid: 21
64k
Free Space
128k Ref: Operating System Concepts with Java – 7 th Edition ,
8.18
Prof. Dr. Hanafy M. Ismail
Example ď °
Given memory holes (i.e., unused memory blocks) of 100K, 500K, 200K, 300K and 600K (in address order) as shown below, how would each of the first-fit, next-fit, best-fit and worst-fit algorithms allocate memory requests for 310K, 80K, 350K and 230K (in this order)? The shaded areas are used/allocated regions and are not available. You should clearly write down the size of each memory block and indicate its status (i.e., allocated or free).
Ref: Operating System Concepts with Java – 7 th Edition ,
8.19
Prof. Dr. Hanafy M. Ismail
Example Solution
i. First Fit
Ref: Operating System Concepts with Java – 7 th Edition ,
8.20
Prof. Dr. Hanafy M. Ismail
Example solution
Ref: Operating System Concepts with Java – 7 th Edition ,
8.21
Prof. Dr. Hanafy M. Ismail
Fragmentation
External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Reduce external fragmentation by compaction Shuffle memory contents to place all free memory together in one large block Compaction is possible only if relocation is dynamic, and is done at execution time
Ref: Operating System Concepts with Java – 7 th Edition ,
8.22
Prof. Dr. Hanafy M. Ismail
Paging
Paging is a memory-management scheme that permits the physical address space of a process to be noncontiguous. Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes and 8,192 bytes (16 MB)) Divide logical memory (backing store) into blocks of same size called pages Keep track of all free frames To run a program of size n pages, need to find n free memory frames and load program pages from the backing store. Set up a page table to translate logical to physical addresses If the size of logical address space 2m and page size 2n then the higher order m - n bits of the logical address designate the page number Internal fragmentation
Ref: Operating System Concepts with Java – 7 th Edition ,
8.23
Prof. Dr. Hanafy M. Ismail
Address Translation Scheme
Address generated by CPU is divided into: Page number (p) – used as an index into a page table which contains base address of each page in physical memory Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit page number
page offset
p
d
m-n
n
Ref: Operating System Concepts with Java – 7 th Edition ,
8.24
Prof. Dr. Hanafy M. Ismail
Paging Hardware
Ref: Operating System Concepts with Java – 7 th Edition ,
8.25
Prof. Dr. Hanafy M. Ismail
Paging Model of Logical and Physical Memory
Ref: Operating System Concepts with Java – 7 th Edition ,
8.26
Prof. Dr. Hanafy M. Ismail
Paging Example
32-byte memory and 4-byte pages Ref: Operating System Concepts with Java – 7 th Edition ,
8.27
Prof. Dr. Hanafy M. Ismail
Free Frames
Before allocation
After allocation
Ref: Operating System Concepts with Java – 7 th Edition ,
8.28
Prof. Dr. Hanafy M. Ismail
Shared Pages
Shared code One copy of read-only code shared among processes (i.e., text editors, compilers, window systems). Shared code must appear in same location in the logical address space of all processes
Private code and data Each process keeps a separate copy of the code and data The pages for the private code and data can appear anywhere in the logical address space
Ref: Operating System Concepts with Java – 7 th Edition ,
8.29
Prof. Dr. Hanafy M. Ismail
Shared Pages Example
Ref: Operating System Concepts with Java – 7 th Edition ,
8.30
Prof. Dr. Hanafy M. Ismail
Problem ď °
A paging system uses 16-bit address and 4K pages. The following shows the page tables of two running processes, Process 1 and Process 2. Translate the logical addresses in the table below to their corresponding physical addresses, and fill the table entries with your answers.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.31
Prof. Dr. Hanafy M. Ismail
Problem solution
Answer: Consider the logical address 11034 generated by process 1. Since page size is 4K = 4096, logical address 11034 is in page 2 = 11034/4096, and the offset is 11034 - 2× 4096 = 2842. From process 1’s page table, page 2 is in page frame 1, and, hence, the corresponding physical address is 1× 4096 + 2842 = 6938. The second logical address 12345 is translated the same way with process 2’s page table
Ref: Operating System Concepts with Java – 7 th Edition ,
8.32
Prof. Dr. Hanafy M. Ismail
Structure of the Page Table
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
Ref: Operating System Concepts with Java – 7 th Edition ,
8.33
Prof. Dr. Hanafy M. Ismail
Hierarchical Page Tables
Break up the logical address space into multiple page tables
A simple technique is a two-level page table
Ref: Operating System Concepts with Java – 7 th Edition ,
8.34
Prof. Dr. Hanafy M. Ismail
Two-Level Page-Table Scheme
Ref: Operating System Concepts with Java – 7 th Edition ,
8.35
Prof. Dr. Hanafy M. Ismail
Two-Level Paging Example
A logical address (on 32-bit machine with 1K page size) is divided into: a page number consisting of 22 bits a page offset consisting of 10 bits Since the page table is paged, the page number is further divided into: a 12-bit page number a 10-bit page offset Thus, a logical address is as follows: page number pi 12
page offset
p2
d
10
10
where pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table
Ref: Operating System Concepts with Java – 7 th Edition ,
8.36
Prof. Dr. Hanafy M. Ismail
Address-Translation Scheme
Ref: Operating System Concepts with Java – 7 th Edition ,
8.37
Prof. Dr. Hanafy M. Ismail
Hashed Page Tables
Common in address spaces > 32 bits
The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location (collision).
Virtual page numbers are compared in this chain searching for a match. If a match is found, the corresponding physical frame is extracted.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.38
Prof. Dr. Hanafy M. Ismail
Hashed Page Table
Ref: Operating System Concepts with Java – 7 th Edition ,
8.39
Prof. Dr. Hanafy M. Ismail
Segmentation
Memory-management scheme that supports user view of memory A program is a collection of segments. A segment is a logical unit such as: main program, procedure, function, method, object, local variables, global variables, common block, stack, symbol table, arrays
Ref: Operating System Concepts with Java – 7 th Edition ,
8.40
Prof. Dr. Hanafy M. Ismail
User’s View of a Program
Ref: Operating System Concepts with Java – 7 th Edition ,
8.41
Prof. Dr. Hanafy M. Ismail
Logical View of Segmentation
1 4
1 2
2
3 4
3
user space
physical memory space
Ref: Operating System Concepts with Java – 7 th Edition ,
8.42
Prof. Dr. Hanafy M. Ismail
Segmentation Architecture
Logical address consists of a two tuple: <segment-number, offset>, Segment table – maps two-dimensional physical addresses; each table entry has: base – contains the starting physical address where the segments reside in memory limit – specifies the length of the segment Segment-table base register (STBR) points to the segment table’s location in memory Segment-table length register (STLR) indicates number of segments used by a program; segment number s is legal if s < STLR
Ref: Operating System Concepts with Java – 7 th Edition ,
8.43
Prof. Dr. Hanafy M. Ismail
Segmentation Architecture (Cont.)
Protection With each entry in segment table associate: validation bit = 0 ⇒ illegal segment read/write/execute privileges Protection bits associated with segments; code sharing occurs at segment level Since segments vary in length, memory allocation is a dynamic storage-allocation problem A segmentation example is shown in the following diagram
Ref: Operating System Concepts with Java – 7 th Edition ,
8.44
Prof. Dr. Hanafy M. Ismail
Segmentation Hardware
Ref: Operating System Concepts with Java â&#x20AC;&#x201C; 7 th Edition ,
8.45
Prof. Dr. Hanafy M. Ismail
Example of Segmentation
Ref: Operating System Concepts with Java â&#x20AC;&#x201C; 7 th Edition ,
8.46
Prof. Dr. Hanafy M. Ismail
What is binding? Answer: Setting the actual addresses used by each non-relocatable address of a program. What is dynamic loading? Answer: Loading a routine only when it is called and not yet in memory; the routine will not be loaded into memory until first time it is called. What is the advantage of dynamic loading? Answer: Routines that are never used are never loaded; thus more free memory. What is a relocation register? Answer: A base register used to give the lowest physical address for a process. What is swapping? Answer: Copying a process from memory to disk to allow space for other processes. List ways of reducing the context switch time. Minimize the amount of memory of a process to be swapped. Increase the speed of the disk used for swapped-out processes. Overlap swapping and program execution.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.47
Prof. Dr. Hanafy M. Ismail
What is external fragmentation? Answer: Wasted space due to empty partitions being too small for current processes. What are variable partitions? Answer: Partitions that can be moved in location, and can be changed in number. What is compaction? Why use it? Answer: Movement of processes to eliminate small partitions (holes). It allows smaller partitions to form fewer larger ones, to allow larger processes to run. What is paging? Answer: Splitting program up into a group of fixed-equal-sized partitions, allowing the parts to be non-contiguous. What is a frame? Answer: Fixed-size block of physical memory, each block of same size as page.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.48
Prof. Dr. Hanafy M. Ismail
What is contained in the page table? Answer: Base address of each frame, and corresponding page number. How are the page numbers and offset numbers obtained? Answer: Logical address is split into two parts: right-hand bits give the offset numbers, and left-hand bits give the page number. Describe the page-to-frame translation. Answer: Logical address is split into page offset and page number. Scan page table for page number; corresponding entry is the frame number, which is combined with page offset to give physical address. How many frames are needed for each page? Answer: One. How much fragmentation occurs with paging? Which type? Answer: On the average, one-half of last page in each job; this is internal fragmentation. In what order are the frames assigned? Answer: In the order of the free frames list.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.49
Prof. Dr. Hanafy M. Ismail
List advantages of paging. Sharing common code. Reducing fragmentation. What table does the operating system use to keep track of frame allocations? Answer: Frame table. What is segmentation? Answer: Breaking program up into its logical segments, and allocating space for these segments into memory separately. The segments may be of variable length, and need not be allocated contiguously. What is the logical address space? What is the physical address space? Collection of segments or pages; each segment has a name and length, and is numbered. Each page is numbered, and has same length as any other, but is not named. Actual memory regions. What advantages does segmentation have over paging? Can associate protection with critical segments, without protecting other items. Can share code and data with other users, tightly, so that only the desired portions are shared.
Ref: Operating System Concepts with Java – 7 th Edition ,
8.50
Prof. Dr. Hanafy M. Ismail
Define external and internal fragments. Consider the following memory management schemes: fixed-size partitions, variable-size partitions, and paging. Which sachems have external fragments, and which schemes have internal fragments? Why? Answer: An external fragment is an unused memory block between two allocated (i.e., used) ones. An internal fragment is an unused memory area within an allocated memory block. Fixed-size partitions and paging do not have external fragments because all partitions and page frames are pre-allocated with fixed sizes. However, they may have internal fragments since a process may not use all the allocated space. Variable-size partitions do not have internal fragment; but, they have external fragments. Note that even though the variable-size partition scheme may allocate a bit more memory than requested, say to fit the boundary alignment requirement, we still consider its allocation being exact
Ref: Operating System Concepts with Java – 7 th Edition ,
8.51
Prof. Dr. Hanafy M. Ismail
End of Chapter 8
ef: Operating System Concepts with Java â&#x20AC;&#x201C; 7 th Edition,
Prof. Dr. Hanafy M. Ismail