memory management - gadjah mada...
TRANSCRIPT
Sunu Wibirama
Memory Management9th Week
Operating System - TIF 206Department of Electrical Engineeringand Information TechnologyFaculty of EngineeringUniversitas Gadjah Mada, Indonesia
Copyright ⓒ 2011
Before we start...Do you really believe that CPU’s clock speed
defines how fast the CPU is?
Steve has his own words...
THE MEGAHERTZ MYTH
Outlines• Memory management:
• Introduction
• Logical and Physical Addresses
• Swapping
• Paging
• Segmentation
• Memory Sharing
References
Abraham Silberchatz, Peter Baer Galvin, Greg Gagne, “Operating System Concepts with Java 6th Edition“, Wiley International Publisher : Chapter 9
Abraham Silberchatz, Peter Baer Galvin, Greg Gagne, “Operating System Principles 7th Edition“, Wiley International Publisher : Chapter 8
Google and Youtube (very useful!)
Basic Memory Hierarchy
Type of Memory
Volatile : requires power to maintain the stored information, also known as temporary memory (Random Access Memory / RAM)
Non-volatile : retains the stored information even when not powered (Read-Only Memory, Flash Memory, Magnetic Storage (HDD, etc)
memory management
OS must fit multiple process in memory
Memory needs to be allocated to ensure a reasonable supply of ready processes so that CPU is never idle
Memory needs to be subdivided to accommodate multiple process
Memory management is an optimization task under constraints
Important terms
Important terms
Relocation of address references:translating memory references to physical address
Important terms
Relocation of address references:translating memory references to physical address
Protection of memory spaces:forbid cross-process references
Important terms
Relocation of address references:translating memory references to physical address
Protection of memory spaces:forbid cross-process references
Sharing of memory spaces:allow several process to access a common memory area
Important terms
Relocation of address references:translating memory references to physical address
Protection of memory spaces:forbid cross-process references
Sharing of memory spaces:allow several process to access a common memory area
Logical organization (of programs):programs are broken up into independent modules
Important terms
Relocation of address references:translating memory references to physical address
Protection of memory spaces:forbid cross-process references
Sharing of memory spaces:allow several process to access a common memory area
Logical organization (of programs):programs are broken up into independent modules
Physical organization (of memory):fit multiple programs and modules in memory area
Memory Address Space• Each process has a separate
memory space
• Two registers provide address protection between processes:
• Base register: smallest legal address space (also called relocation register)
• Limit register: size of the legal range
HW Address protection
CPU hardware compares every address generated in user mode with the registers
Any attempt to access other process’ memory will be trapped and causes a fatal error
Various Error messages
Blue Screen of Death (Windows)
Kernel Panic(FreeBSD)
Kernel Panic(Mac)
Address bindingAddresses in a source program are generally symbolic- ex.: int count;
A compiler binds these symbolic addresses to relocatable addresses- ex.: 100 bytes from the beginning of this module
The linkage editor or loader will in turn bind the relocatable addresses to absolute addresses- ex: 74014
Each binding is mapping from one address space to another
Binding of instructions & data to memory
Address binding of instructions and data to memory addresses can happen at three different stages:
1. Compile time: if memory location known, we can generate absolute code, but we must recompile the code if starting location change. 2. Load Time: we can generate relocatable code if memory location is not known at compile time; need only reload if starting address changes.3. 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 mapping
Dynamic loading v.s. Dynamic linking
Dynamic Loading:
Routine in a program is not loaded until it is called.
Can handle large amount of code
User is fully responsible to maintain the code in order to be loaded dynamically
Dynamic Linking:
Linking is done in execution time
Using ‘stub’, to locate appropriate memory-resident library (shared-library concept)
Requires help from operating system to manage protection of library
Logical v.s. Physical Address Space
Address generated by CPU is referred as logical address
Address seen by the memory is seen as physical address
Compile time and load time methods generate identical logical and physical addresses
Execution time method generates different logical and physical addresses- Logical address referred as virtual address
Memory Management Unit (MMU)
Hardware device that maps virtual to physical address
In MMU scheme, the value in the relocation register (formerly known as base 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 address
swappingA process must be in memory to be executed
However, it can be swapped temporarily out of memory to a backing store, and brought back for continued execution
If address binding is done at compiling or load time, the process cannot be easily moved to a different memory space
If execution time binding is used the process can be swapped into different memory space. Why?
swappingA process must be in memory to be executed
However, it can be swapped temporarily out of memory to a backing store, and brought back for continued execution
If address binding is done at compiling or load time, the process cannot be easily moved to a different memory space
If execution time binding is used the process can be swapped into different memory space. Why?
Because the physical addresses are computed during execution time
swapping
swap-SPACE management
If system runs out of swap space, it may be forced to abort processes or may crash entirely
Overestimation of swap space is important, ex: Linux suggests swap space 2x RAM
Location: inside normal file system (Windows) or in separate disk partition (Unix-based OS).
Swap file in Windows OS
Swap file in Linux OS
swap-SPACE management
Inside file system: a large file, but inefficient since we need extra time to navigate the directory structure and disk allocation data structure. Moreover, disk is fragmented.
Separate disk partition: now file system or directory structure inside it. Here, swap space is accessed more frequently. The system optimizes the speed for data processing.
When handle large data transfer with swapping mode turned ON, we prefer Unix-based operating system instead of Windows
Your ‘friend’ did it!
Your ‘friend’ did it!
Google uses Linux clusters for its data center.
The widely-used term is parallel computing
paging
Memory management scheme that permits the physical address space of a process to be noncontiguous.
User submit one address, the rest will be handled by hardware. Recently, OS can take care of it
Logical address should be ordered in specific way
Physical memory is broken into fixed-sized blocks called frames.
Logical memory is broken into blocks of the same size called pages.
Advantages of paging
Allow noncontiguous memory allocation for a process
I/O processes are more efficient if the transferred data are larger
User’s view of memory: one single spaceActual physical memory: scattered Easier for programmer!
hardware of paging
page number! page offset!
p! d!
Example of Paging
When you code...Have you ever think the order
of where your part of program is stored in memory?
Obviously, I don’t care about the order ofwhere the memory stores the part of my program
segmentationIn common usage, user doesn’t see memory as a linear array of bytes.
User sees memory as a collection of variable-sized segments, without ordering the the logical address.
Segmentation is a memory-management scheme that supports this user view of memory.
User can define segment number and its offset: <segment-number, offset>
In paging, user only can submit one address and HW / OS will handle the rest
see the difference?
PAGING SEGMENTATION
Hardware of Segmentation
d = offset to the segment
Memory Allocation in Linux
free -t -m
htop
Try it by yourself !
THANK YOU