12
Swap Space and Other Memory Management Issues Operatin g Systems: Internals and Design Principle s

Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Embed Size (px)

Citation preview

Page 1: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Swap Space and Other Memory

Management Issues

Operating

Systems:

Internals and

Design Principl

es

Page 2: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

What is Swap Space? The virtual address space of programs is often bigger than

the physical address space of the computers they will run on For example, the virtual address space of a program running on

a 32-bit machine will probably be 232 (Actually 231 since half is set aside for the kernel)

Virtual memory systems support processes that are bigger than main memory by using disk space as backup storage.

Swap space is a file, disk partition, or separate disk used originally to store suspended processes and today, in virtual memory systems, to store all or part of the virtual address space of currently executing processes.

Page 3: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Swap Space Use Executable files contain executable code and initialized data.

Stack, heap, other dynamic elements aren’t included.

Some systems store the entire process image, including executable code, in swap space; others page code in and out from the executable files and use swap space for stack, heap, etc.

Some systems initialize swap space for a process when the process is created, others allocate space only when pages are evicted from memory.

The amount of swap space needed by a system depends on a number of elements, including size of physical memory, virtual address space, etc.

Page 4: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Swap Space & the File System

Two approaches:

Swap space as a large file in the file system

Swap space as a separate partition

Swap space that’s part of the normal file system is easy to implement (just use existing file system functions) but the overhead of going through the directory system and disk allocation structures to swap pages in and out is a drag on system efficiency.

If swap space is created as a separate partition, no file system is used. Instead, a special swap manager handles space allocation and page accesses

Page 5: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Swap Space Management Swap-space management focuses on speed rather than efficiency of

disk usage. Swap space is accessed more frequently than the file system. Internal fragmentation may be created, but swap space objects have a short

life span (just while program is running), whereas files may exist for years.

Consequently the space will be reclaimed in a few fractions of a second. Some versions of Linux support either separate swap areas or swap

files. A swap file can provide adequate speed if space is allocated to

processes as a contiguous set of locations Using a separate disk for swapping improves performance by

supporting concurrent access as well as by having more efficient allocation strategies.

Page 6: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Virtual Memory Review

Organization of virtual address space Page table size/structure/access Address translation (virtual/logical to physical)

Page 7: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Virtual Memory Review

Organization of program virtual address spaces (how executable binary files are organized):

Code, usually located at some fixed (virtual) address, often 0.

Data segment: contains non-stack, non-heap variables

When a process is created from the program the stack is allocated at the high end of virtual memory; it can grow towards lower addresses

Heap space is usually created at the same time and is at lower

address space than the stack. They grow toward each other.

Thus information is not stored consecutively; there are blocks of virtual memory that are empty

Page 8: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Page Table Issues Consider a simple, one-level page table. Size of page table is based on the maximum logical size of a

process: one entry for each possible virtual page Not on the physical size of memory Not on the number of pages in the program (which is not known in

advance anyway, since stack size and heap size vary from one

execution to another) The logical page # selects the appropriate page table entry Typical contents of each entry:

valid (or present) bit tells if the page is in memory Dirty bit tells if page has been modified Reference bit tells if page has been referenced (may be unset & reset) Protection bits (read only, etc)

Page 9: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Address Translation

• Page size (and frame size) is defined by the hardware• Always a power of 2• Simplifies address translation by making it easy to

divide the address into a page number and a page offset

• Formulas: given that the size of the logical address space is 2m and the page size is 2n then

• Page number (p) = the (m-n) high-order bits of the address

• Page offset (d) = the n low-order bits of the address• Recall that address translation is done entirely by the

hardware, using information in the page table, as long as the page is in memory

• Logical address p#d becomes physical address f#d (f=frame,p=page)

• Only if there is a page fault does the OS become involved.

Page 10: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Example

• Suppose parameters of the logical address space are m = 5 and n = 2; so

• p = m – n = 3 → 8 pages (2p)• d = n → 4, indicating 4 addresses/page

• Suppose parameters of the physical address space are m = 4 and n = 2

• p = 2 → 4 pages (2p)• d = n → 4 addresses/page

• The page table must have 8 entries, even though there are only 4 pages of physical memory. Otherwise, you could not handle programs that are larger than memory.

• For example, suppose a process generated an address on logical page 5, such as 21

Page 11: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Index Valid bit

Frame #

0 0 --- 1 0 --- 2 1 33 0  ---4 0 --- 5 1 06 1 17 0 --- 

Frame Content0 Page 51 Page 62 -----3 Page 2

Physical Memory

Page Table

(Red areas are not part of either page table or physical memory)

If there weren’t 8 entries in the page table an address such as 21 (101012) could not be translated. But we can divide the address into two parts: 101|01 to get p = 1012, and d = 012 or p = 5, d = 2.

Page 12: Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles

Example