OPERATING SYSTEMS MEMORY MANAGEMENT

Jerry Breecher
8: Memory Management 1

OPERATING SYSTEM Memory Management
What Is In This Chapter?
Just as processes share the CPU, they also share physical memory. This chapter is about mechanisms for doing that sharing.

8: Memory Management

2

MEMORY MANAGEMENT
Just as processes share the CPU, they also share physical memory. This section is about mechanisms for doing that sharing. EXAMPLE OF MEMORY USAGE: Calculation of an effective address
Fetch from instruction Use index offset

Example: ( Here index is a pointer to an address ) loop:

load add store inc skip_equal branch loop ... continue ....

register, index 42, register register, index index index, final_address

8: Memory Management

3

logical (virtual) and physical addresses differ in execution-time address-binding scheme 8: Memory Management 4 . • Logical address – generated by the CPU.MEMORY MANAGEMENT Definitions • The concept of a logical address space that is bound to a separate physical address space is central to proper memory management. also referred to as virtual address • Physical address – address seen by the memory unit • Logical and physical addresses are the same in compile-time and loadtime address-binding schemes.

Otherwise compiler produces relocatable code. then absolute code is generated. Programs need real memory in which to reside. Data used within compiled source is offset within object module. If it’s known where the program will reside.MEMORY MANAGEMENT Relocatable Binding Definitions Means that the program image can reside anywhere in physical memory. Can find best physical location. Binds relocatable to physical. Means flexible virtual mapping. • This binding can be done at compile/link time. Converts symbolic to relocatable. Compiler: Load: Execution: 8: Memory Management 5 . When is the location of that real memory determined? • This is called mapping logical to physical addresses. The code can be moved around during execution.

Binds relocatable to physical. Can be done at load time. Can be done at run time.MEMORY MANAGEMENT This binding can be done at compile/link time. The next example shows how a compiler and linker actually determine the locations of these effective addresses. Data used within compiled source is offset within object module. Implies that the code can be moved around during execution. Converts symbolic to relocatable. Binding Logical To Physical Source Compiler Object Other Objects Linker Executable Loader In-memory Image Libraries 8: Memory Management 6 .

8} 9 10 11 voidb() 12 { 13 printf( "Hello. from 'b'\n" ). 7 b(). from main\n" ). 14 } 8: Memory Management 7 .MEMORY MANAGEMENT Binding Logical To Physical 4 void main() 5 { 6 printf( "Hello.

%r1 L%0. .-20(%sp) 64(%sp).%r2 8. get code start area . .%r1 -192(%r1). get code start area call printf calc. String loc. .%r2 %r0(%r2) -64(%sp). call b store return addr %r2. main() .31.n nop stw ldo bl depi ldo bl addi ldw bv ldo Binding Logical To Physical .%sp 0x000000C0.%r1 0. return from b b() 8: Memory Management 8 .%r1) %r2.%r26 -84(%sp).%r1 0x00000000(%sr7.%r2 32.%r1.%r1 0x000000E0.2.MEMORY MANAGEMENT ASSEMBLY LANGUAGE LISTING 000000B0: 000000B4 000000B8 000000BC 000000C0 000000C4 000000C8 000000CC 000000D0 000000D4 000000D8 000000DC 6BC23FD9 37DE0080 E8200000 D4201C1E 34213E81 E8400028 B43A0040 E8400040 6BC23FD9 4BC23F59 E840C000 37DE3F81 stw ldo bl depi ldo bl addi bl stw ldw bv ldo bl addil be. return from main STUB(S) FROM LINE 6 000000E0: E8200000 000000E4 28200000 000000E8: E020E002 000000EC 000000F0: 000000F4: 000000F8 000000FC 00000100 00000104 00000108 0000010C 00000110 00000114 08000240 6BC23FD9 37DE0080 E8200000 D4201C1E 34213E01 E85F1FAD B43A0010 4BC23F59 E840C000 37DE3F81 0x000000E8. get current addr=BC .%r1.%r1 0x000000E0.-20(%sp 64(%sp).31.-20(%sp) -84(%sp). .%sp void .%r2 %r2.%sp .2.%r2 %r0(%r2) -64(%sp).%r26 0x000000F4. get current addr=F8 . call printf .%r1 0.%sp 0x00000100.%r1 -256(%r1).

H e l 6F2C2066 . . . o . 48656C6C .%r26 E8400040 bl 0x000020F4. r o m 6D61696E .-20(%sp) 4BC23F59 ldw -84(%sp).%r1 E020E772 be. 726F6D20 . .n 0x000003B8(%sr7.%r1.%r1) 08000240 nop 8: Memory Management 9 .%sp E8200000 bl 0x000020E8.MEMORY MANAGEMENT 00002000 00002004 00002008 0000200C 00002010 00002014 00002018 0000201C 00002020 00002024 000020B0 000020B4 000020B8 000020BC 000020C0 000020C4 000020C8 000020CC 000020D0 000020D4 000020D8 000020DC 000020E0 000020E4 000020E8 000020EC Binding Logical To Physical . stub 28203000 addil L%6144. . . . @ l f .-20(%sp) . b . o . r o m 620A0001 .%r1 34213E81 ldo -192(%r1). 08000240 . H e l 6F2C2066 . main 37DE0080 ldo 64(%sp).2.%sp E8200000 bl 0x000020C0. .%r2 B43A0040 addi 32.%r2 6BC23FD9 stw %r2.31. m a i 6BC23FD9 stw %r2.%r2 E840C000 bv %r0(%r2) 37DE3F81 ldo -64(%sp). 726F6D20 .%r1 D4201C1E depi 0.%r1 . 48656C6C . l f n EXECUTABLE IS DISASSEMBLED HERE 0009000F .%r1 E84017AC bl 0x00003CA0.

-100(%sp) L%-26624.%sp 0x00003CE8.n %r2.%r19 %r24.2.%r19.-104(%sp) 212(%r1).%r1 D4201C1E depi 0.-20(%sp) 64(%sp).-20(%sp) 37DE0080 ldo 64(%sp).%r2 %r0.-112(%sp) -8188(%r19).%r26 4BC23F59 ldw -84(%sp).%r1) .%r1 0x00000428(%sr7.%r25 %r23.%r19 104.%r2 %r0(%r2) -64(%sp).%r1 34213E01 ldo -256(%r1).%r2 B43A0010 addi 8.%r24 -84(%sp).%r23 0x00004110.%sp 6BC23FD9 37DE0080 6BDA3F39 2B7CFFFF 6BD93F31 343301A8 6BD83F29 37D93F39 6BD73F21 4A730009 B67700D0 E8400878 08000258 4BC23F59 E840C000 37DE3F81 E8200000 E020E852 stw ldo stw addil stw ldo stw ldo stw ldw addi bl copy ldw bv ldo bl be.%r1 E840172C bl 0x00003CA0.%r1.%sp E8200000 bl 0x00002100.-108(%sp) -100(%sp). b EXECUTABLE IS DISASSEMBLED HERE 6BC23FD9 stw %r2.%r2 E840C000 bv %r0(%r2) 37DE3F81 ldo -64(%sp).%sp %r26. printf 8: Memory Management 10 .MEMORY MANAGEMENT 000020F0 000020F4 000020F8 000020FC 00002100 00002104 00002108 0000210C 00002110 00002114 00003CA0 00003CA4 00003CA8 00003CAC 00003CB0 00003CB4 00003CB8 00003CBC 00003CC0 00003CC4 00003CC8 00003CCC 00003CD0 00003CD4 00003CD8 00003CDC 00003CE0 00003CE8 Binding Logical To Physical .%dp %r25.31.

unused routine is never loaded. Operating system needed to check if routine is in processes’ memory address. used to locate the appropriate memory-resident library routine. and executes the routine.implemented through program design. Useful when large amounts of code are needed to handle infrequently occurring cases. stub. No special support from the OS is required . Usually requires hardware support. Dynamic Linking + + + + + Linking postponed until execution time.MEMORY MANAGEMENT Dynamic loading + + + + More Definitions Routine is not loaded until it is called Better memory-space utilization. Dynamic linking is particularly useful for libraries. Stub replaces itself with the address of the routine. 8: Memory Management 11 Memory Management . Small piece of code. Performs the above operations.

Partition is given to a process when it' s scheduled. DEFINITION OF PARTITIONS: Division of physical memory into fixed sized regions. the operating system has none. This is the simplest form of memory management. real time/dedicated systems. 8: Memory Management 12 . no overhead.MEMORY MANAGEMENT BARE MACHINE: SINGLE PARTITION ALLOCATION No protection.) The number of partitions determines the level of multiprogramming. lower ) base / limit. (Allows addresses spaces to be distinct = one user can' t muck with another user. Used by hardware diagnostics. Protection around each partition determined by bounds ( upper. These limits are done in hardware. or the system. no utilities. logical == physical User can have complete control. by system boot code. Commensurably.

start user at high end and move back. SINGLE PARTITION ALLOCATION Usually in low memory where interrupt vectors are placed. 8: Memory Management 13 . OR use fence as base register that requires address binding at execution time. If monitor can change in size. User program starts at fence -> fixed for duration of execution. If user generated address < fence.MEMORY MANAGEMENT RESIDENT MONITOR: Primitive Operating System. Must check each memory reference against fence ( fixed or variable ) in hardware or register. But only works for static-sized monitor. then illegal. Concept of "mapping addresses” shown on next slide. Add base register to every generated user address. Isolate user from physical address space using logical address space. Then user code has fence address built in.

MEMORY MANAGEMENT Limit Register SINGLE PARTITION ALLOCATION Relocation Register Yes CPU Logical Address < No + Physical Address MEMORY 8: Memory Management 14 .

Can determine memory size required ( either user specified or "automatically" ). and partition availability. load a program THEN it can compete for CPU time.) Sequence of events: In an empty memory slot. Upon job completion. (This is a combination of short/medium term scheduling. 8: Memory Management 15 . JOB SCHEDULING Must take into account who wants to run. the memory needs. the partition becomes available.MEMORY MANAGEMENT CONTIGUOUS ALLOCATION All pages for a process are allocated together in one chunk.

space allocated based on table.DYNAMIC STORAGE MEMORY MANAGEMENT CONTIGUOUS ALLOCATION (Variable sized holes in memory allocated on need.buddy system. ALLOCATION PRODUCES HOLES OS OS OS process 1 process 1 process 1 process 2 process 3 Process 2 Terminates process 3 Process 4 Starts process 4 process 3 8: Memory Management 16 . Adjacent freed space merged to get largest holes .) Operating System keeps table of this memory .

allocate the first hole that' s big enough. worst fit has lowest memory utilization. This occurs when there are many small pieces of free memory.allocate smallest hole that' s big enough. Best fit . allocated in what chunk size? Want to also avoid internal fragmentation.) Avoid small holes (external fragmentation).allocate largest hole. 8: Memory Management 17 . This is when memory is handed out in some fixed way (power of 2 for instance) and requesting program doesn' t use it all. Worst fit . (First fit is fastest. What should be the minimum size allocated.MEMORY MANAGEMENT CONTIGUOUS ALLOCATION HOW DO YOU ALLOCATE MEMORY TO NEW PROCESSES? First fit .

8: Memory Management 18 . This is because the memory is constantly being handed cycled between the process and free. What are the pros and cons of each of these? LONG TERM SCHEDULING There' s little or no internal fragmentation (the process uses the memory given to it the size given to it will be a page.MEMORY MANAGEMENT If a job doesn' t fit in memory. the scheduler can wait for memory skip to next job and see if it fits.) But there can be a great deal of external fragmentation.

COMPACTION Only possible if programs linked with dynamic relocation (base and limit. But if dynamic. code/data must return to same place. can reenter at more advantageous memory.) There are many ways to move programs in memory.MEMORY MANAGEMENT Trying to move free memory to one large block. OS P1 OS P1 P2 P3 P2 P3 8: Memory Management 19 OS P1 P3 P2 . Swapping: if using static relocation.

Internal fragmentation. Divide physical memory into fixed-sized blocks called frames (size is power of 2. Keep track of all free frames. To run a program of size n pages.MEMORY MANAGEMENT • PAGING New Concept!! • • • • • • Logical address space of a process can be noncontiguous. Set up a page table to translate logical to physical addresses. between 512 bytes and 8192 bytes). Divide logical memory into blocks of same size called pages. process is allocated physical memory whenever that memory is available and the program needs it. 8: Memory Management 20 . need to find n free frames and load program.

4096 bytes = 2^12 – it requires 12 bits to contain the Page offset p d 8: Memory Management 21 . • Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit.MEMORY MANAGEMENT Address Translation Scheme PAGING Address generated by the 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.

MEMORY MANAGEMENT Frames = physical blocks Pages = logical blocks PAGING Permits a program' s memory to be physically noncontiguous so it can be allocated from wherever available. This avoids fragmentation and compaction. 8: Memory Management 22 . Size of frames/pages is defined by hardware (power of 2 to ease calculations) HARDWARE An address is determined by: page number ( index into table ) + offset ---> mapping into ---> base address ( from table ) + offset.

32-byte memory with 4-byte pages 0a 1b 2c 3d 4e 5f 6g 7h 8I 9j 10 k 11 l 12 m 13 n 14 o 15 p Logical Memory PAGING 0 4 I j k l m n o p 8 0 1 2 3 5 6 1 2 12 16 20 a b c d e f g h Page Table 24 Physical Memory 28 8: Memory Management 23 .MEMORY MANAGEMENT Paging Example .

anyway? • Could use dedicated registers (OK only with small tables. PAGING IMPLEMENTATION OF THE PAGE TABLE TLB = Translation Lookaside Buffer 8: Memory Management 24 . WHO says how big a page is.) • Could use a register pointing to table in memory (slow access.) • Cache or associative memory • (TLB = Translation Lookaside Buffer): • simultaneous search is fast and uses only a few registers.MEMORY MANAGEMENT • A 32 bit machine can address 4 gigabytes which is 4 million pages (at 1024 bytes/page).

8: Memory Management 25 . 20 nanoseconds to access processor cache and bring it into TLB for next time. Calculate time of access: hit = 1 search + 1 memory reference miss = 1 search + 1 mem reference(of page table) + 1 mem reference.98% with 100 registers add entry if not found Effective access time = %fast * time_fast + %slow * PAGING time_slow Relevant times: 2 nanoseconds to search associative memory – the TLB.MEMORY MANAGEMENT IMPLEMENTATION OF THE PAGE TABLE Issues include: key and value hit rate 90 .

MEMORY MANAGEMENT SHARED PAGES Data occupying one physical page. (NO write-able data mixed with code. Useful for common code must be write protected.) Extremely useful for read/write communication between processes. but pointed to by multiple logical pages. PAGING 8: Memory Management 26 .

page table entries. Use hash table to limit the search to one .MEMORY MANAGEMENT INVERTED PAGE TABLE: One entry for each real page of memory. 8: Memory Management PAGING 27 .or at most a few . Entry consists of the virtual address of the page stored in that real memory location. Essential when you need to do work on the page and must find out what process owns it. with information about the process that owns that page.

then to which logical page in which process. •The operating system keeps a frame containing descriptions of physical pages. valid bits. ADDRESS MAPPING: •Allows physical memory larger than logical memory. execute.MEMORY MANAGEMENT PROTECTION: PAGING •Bits associated with page tables. 8: Memory Management 28 . •Valid bit says page isn’t in address space. •Write to a write-protected page causes a fault. if allocated. •Useful on 32 bit machines with more than 32-bit addressable words of memory. Touching an invalid page causes a fault. •Can have read. write.

PAGING 8: Memory Management 29 .MEMORY MANAGEMENT MULTILEVEL PAGE TABLE A means of using page tables for large address spaces.

We really don' t care at what address a segment is located. This view is more useful than thinking of a linear array of words. Typical segments include global variables procedure call stack code for each function local variables for each large data structures Logical address = segment name ( number ) + offset Memory is addressed by both segment and offset. 8: Memory Management 30 .MEMORY MANAGEMENT USER'S VIEW OF MEMORY Segmentation A programmer views a process consisting of unordered segments with various purposes.

MEMORY MANAGEMENT Segment Table Limit S Logical Address D Base Segmentation HARDWARE -. CPU Yes < No + Physical Address MEMORY 8: Memory Management 31 .Must map a dyad (segment / offset) into one-dimensional address.

Limit 1000 400 400 1100 1000 Segmentation 1 2 0 3 4 0 1 2 3 4 Base 1400 6300 4300 3200 4700 1 4 2 3 Logical Address Space 8: Memory Management Physical Memory 32 .MEMORY MANAGEMENT HARDWARE base / limit pairs in a segment table.

for instance a common library of subroutines. Can use compaction since segments are relocatable. etc. FRAGMENTATION Use variable allocation segment lengths vary. 8: Memory Management 33 .MEMORY MANAGEMENT PROTECTION AND SHARING Addresses are associated with a logical unit (like data. a segment has an attribute called "shareable". Smaller segments means less fragmentation. Can do bounds checking on arrays Sharing specified at a logical level. Can share some code but not all . since Segmentation Again have issue of fragmentation.) so protection is easy. code.

MEMORY MANAGEMENT Segmentation PAGED SEGMENTATION Combination segmentation. of paging and address = frame at ( page table base for segment + offset into page table ) + offset into memory Look at example of Intel architecture. 8: Memory Management 34 .

associating logical with physical memory. This subject is at the very heart of what every operating system must do today. 8: Memory Management 35 .MEMORY MANAGEMENT WRAPUP We’ve looked at how to do paging .

Sign up to vote on this title
UsefulNot useful