Memory Management

Document Sample
Memory Management Powered By Docstoc
					Chapter 7: Memory Management

        CS 472 Operating Systems
   Indiana University – Purdue University
                Fort Wayne


                                            1
Memory management
 In a uniprogramming system, memory is divided
 into one partition for the OS and another partition
 for the user program
 In a multiprogramming system, the user area is
 subdivided so each process has its own partition
 This allows multiple processes to run concurrently
 A strategy for doing this is known as memory
 management


                                                  2
Memory management
 Memory should be efficiently allocated so that a
 maximum number of processes can coexist
 We want to have at least one process in the ready
 queue when the processor becomes available
 Some requirements
   Ability to “relocate” a program
   Each partition should be protected
   Processes should be able to establish shared
    memory areas

                                                  3
Review of addressing                              absolute
                                                  address

 Relative address
   an offset from the load                         load
   address                                         address

                             relative
 Absolute address            address    program



   address of a physical
   memory location
   used, for example, with
   memory-mapped I/O


                                                      4
Early systems
 Compiled programs in early systems used only
 absolute addresses
 The load address needed to be specified at compile
 time
 Data areas were allocated within the load module
 If the program was loaded elsewhere, references
 would be off
 Hard to combine separately compiled program units

                                               5
Later systems with relative addressing
 A“linker” program is introduced to combine
 separately compiled program units
 The linker needs to modify relative addresses of
 each separate unit to reflect the load address of the
 first unit
 Linker output is an .exe file
 Absolute addresses (say, for memory-mapped I/O)
 are not changed

                                                  6
                        myProgram.exe
         load address




                          unit1.obj


Linker
                                        modified
                          unit2.obj     relative
                                        addresses




                          unit3.obj




                                                7
Protection
 A process should not be able to reference
 memory locations in another process without
 permission
 Hardware support needed for protection
 Not possible to check dynamically calculated
 addresses at compile time
 All addresses must be checked at run time


                                                8
   Hardware support for relative
   addressing
                               interrupt




relative
address         +             compare            absolute
                                                 address




           base register    bounds register




           load address    load address + size


                                                            9
Hardware and relative addressing . . .
 Allows a program to be built out of separately
 compiled units
 “Bounds register” provides protection to other
 users
 Allows the program to be swapped out of
 memory and then swapped back in at a different
 load address
   This is called “dynamic relocation”


                                              10
Memory management techniques
 Fixed partitioning
 Dynamic partitioning
 Buddy system
 Simple paging
 Simple segmentation
 Virtual memory with paging          In order of
                                     increasing
                                    sophistication
 Virtual memory with segmentation

                                                 11
Fixed partitioning                     memory



 The number and size of              operating system


 partitions is fixed
 The sizes can be the same or
 different
   A program must use “overlays”
   if too big
 “Internal fragmentation”
 results if program too small
   This is wasted space at the end
   of a partition
   See next slide
                                                        12
Fixed partitioning                 operating system
 Placement algorithm for              process 2
 partitions of mixed size
   Feed all new processes from
   a single queue
                                      process 1
   For the next process,
   allocate the smallest
   available partition that will
   hold the process
                                      process 3




                fragmentation
                                                      13
                                  memory

Dynamic partitioning              operating
                                  system
 No fixed number of partitions
                                  process 1
 Partition size = process size
   no internal fragmentation      process 2


 “External fragmentation”
                                   process 3
   As processes come and go,
   many scattered small holes
   grow between processes
                                   process 4
   See next slide
                                    External
                                 fragmentation


                                                 14
                                          memory

Dynamic partitioning                      operating
                                          system
 “Compaction” needed
                                          process 5
    time consuming                        process 6
    requires dynamic relocation
                                          process 2
 Placement algorithm choices
    best-fit, first-fit, and next-fit      process 7

 First-fit is simplest and best
 Best-fit leads to smaller fragments
    These are usually too small to use     process 4


                                            External
                                         fragmentation


                                                         15
Buddy system
 A compromise between the rigidity of fixed
 partitioning and the complexity and overhead of
 dynamic partitioning
 Allocates blocks of size 2k
 Tries to allow a variety of block sizes while
 avoiding excess internal fragmentation




                                                   16
Buddy system
 Initial memory is one block of 2U bytes
 For a memory request of size s, find the
 smallest i such that 2i-1< s ≤ 2i
 Successively divide blocks in half until size 2i is
 reached
 Allocate one block, save the “buddy”
 Maintain a list of “holes” of each size
 When two buddies of size 2i become available,
 merge them into a hole of size 2i+1

                                                       17
18
19
Simple paging
 Partition memory into “page frames” of fixed
 equal size
 Divide each process into “pages” of the same
 size as page frames
 Any page may be loaded into any page frame
 “Simple” means all pages of a process must be
 loaded into page frames when the process is
 started

                                                 20
Simple paging
 The OS maintains a page table for each process
 Table consists of “page table entries” (PTEs)
 Entry n in the table is the PTE of page n
 The PTE of page n gives the frame number
 where the page is loaded
 Internal fragmentation exists only on the last
 page of each process
 No external fragmentation

                                              21
Assignment of pages to free page frames




                                     22
Assignment of pages to free page frames




                                     23
Corresponding page tables




                            24
Address translation
 Must be done automatically by hardware
 Uses the page tables
 Each logical address consists of two parts
   page number
   offset within the page

              logical address
      page number           offset




                                              25
Address translation
 Consider a logical address of n+m bits
   leftmost n bits are the page number
   rightmost m bits are the offset
 Use the page number as an index into the
 process page table
 Obtain the bits of the frame number from the PTE
 Append the offset to the frame number and obtain
 the physical address that corresponds to the
 logical address

                                               26
27
Simple segmentation
 Similar to simple paging except programmer
 modularizes process into segments s0, s1, s2, . . .
 Segments may be of different sizes (smaller than a
 maximum size)
 Each segment is loaded into a dynamic partition
 Each segment has a load address and a length
 “Simple” means that all segments must be loaded
 into partitions when the process starts
 External fragmentation exists

                                                 28
Address translation
 The OS maintains a “segment table” for each
 process
 Address translation must be done automatically by
 hardware
 Each logical address consists of two parts
   segment number
   offset within the segment
 If the offset exceeds the segment length, the
 process is terminated with a memory violation

                                                 29
Address translation




                      30
Appendix 7A
 Good review of linking and loading
 See pages 338 - 344




                                      31

				
About if any file u wil find copyright contact me it will be remove in 3 to 4 buisnees days. add me on sanjaydudeja007@gmail.com or visit http://www.ohotech.com/