Memory management basics (1) Requirements (1) Operating Systems Part of E1.9 - Principles of Computers and Software Engineering Lecture 7: Memory Management I Memory management intends to satisfy the following requirements: Relocation Memory is shared among several processes; exact number and memory location of each cannot be known in advance to programmer Processes might need to be swapped in and out of main memory A process might need be relocated address binding an important issue. Protection Processes should not be able to access (memory) resources held by other processes Hardware is crucial for providing this. E1.9 - Operating systems (Lect 7) 3 Objectives To present the fundamentals of memory management Address binding Memory allocation Fixed Partitioning Dynamic Partitioning Paging Segmentation Memory management basics (1) Requirements (2) Sharing If needed, processes should be able to access the same portion of main memory for communication Multiple processes executing the same program: keep only one copy of the program in memory Logical organization Ability to handle modularity in the construction of programs: ability to link libraries and resolve symbolic references Physical organization Memory hierarchies (from limited, expensive, fast-access memory to extensive, cheap, slow access memory) discussed earlier in E1.9 Ability to move data through the hierarchy. E1.9 - Operating systems (Lect 7) 2 E1.9 - Operating systems (Lect 7) 4
Address binding (1) From program to process the tools Compiler: translates a high-level language (Pascal, C ) into machine instructions Assembler: translates humanreadable machine instructions into machine-readable instructions that can be executed by the processor Linker: combines different modules of machine instructions (e.g. libraries) into a single sequence of machine instructions Loader: loads machine instructions from file into memory [Silberschatz et al 2001] E1.9 - Operating systems (Lect 7) 5 Address binding (3) At execution time Logical- vs. Physical address space Logical addresses (as generated by CPU) vs. physical addresses (as seen by the memory unit) Execution-time address binding requires hardware support, in particular a relocation register Value of relocation register is added to every address generated by the user process CPU Logical address 122 Relocation register 1000 physical address 1122 The concept of binding a logical-address space to a separate physical address space is central to memory management Memory E1.9 - Operating systems (Lect 7) 7 Address binding (2) When Binding of instructions and data to memory addresses can be done at any step of the previous sequence: Address binding (4) Memory protection Use of a limit register to protect the OS from user processes and user processes from each other Compile time: Absolute code can be generated at this stage, if it is known in advance where will the process reside in memory (e.g. MSDOS.com programs) Load time: Relocatable code can be generated if it not known in advance where will the code reside. Memory addresses are relative (to the beginning of the program), replaced by absolute ones during loading. Execution time: performed if a process can be moved during its execution from one memory segment to another (e.g. swapping) --- requires special hardware CPU Logical address 122 Trap: addressing error No < + Relocation register 1000 physical address 1122 Memory E1.9 - Operating systems (Lect 7) 6 E1.9 - Operating systems (Lect 7) 8
Memory allocation (1) Fixed Partitioning (1) One of the simplest methods for memory allocation is to divide the available memory (after allocating space for the OS) into several fixed-sized partitions Simplest case: one partition; only one process is allowed in memory (along with the OS). Simple but inefficient Degree of multiprogramming bound by the number of partitions OS keeps track of free partitions When a process is created, OS allocates a partition large enough to accommodate it When a process is terminated, OS frees the partition that was allocated to the process Memory allocation (2) Dynamic Partitioning (1) Partitioning can also be dynamic Can divide into variable number of partitions with variable size At the start, entire memory forms a single partition, a hole. When a process is created, OS allocates it the exact memory it requested (if it is available). As processes are created and deleted, corresponding partitions are deleted and created dynamically. Free memory space is broken into several pieces Suffers from external fragmentation E1.9 - Operating systems (Lect 7) 9 E1.9 - Operating systems (Lect 7) 11 Memory allocation (1) Fixed Partitioning (2) Partitions can be equal- or unequal-sized A process occupies exactly one partition If process is smaller than the allocated partition, remaining memory of the partition is wasted This is known as internal fragmentation If process is larger, it cannot run Two partitions Process B Wasted Space (internal fragmentation) Memory allocation (2) Dynamic Partitioning (2) Process D Process K Process E External fragmentation example: there is sufficient memory (in total) for process F, but it is scattered in unusable little parts Process F (waiting for available memory) Free memory holes E1.9 - Operating systems (Lect 7) 10 E1.9 - Operating systems (Lect 7) 12
Memory allocation (2) Dynamic Partitioning (3) The following methods are used to allocate memory to processes First-fit: allocate the first hole that is big enough fast allocation method Best-fit: allocate the smallest hole that is big enough. Requires a search through the entire list of holes. Tends to produce lots of remaining tiny fragments. Worst-fit: allocate the largest hole that is available. (rationale: after allocating the request, the remainder of that hole might still be usable). Also requires search through entire list of holes E1.9 - Operating systems (Lect 7) 13 Paging (1) Paging permits the physical address space of a process to be non-contiguous Physical memory is broken into fixed-sized blocks called frames Logical memory is broken into blocks (of the same size as frames) called pages. Addresses generated by the CPU are now divided into two parts: a page number (p) and a page offset (d) Page number is used as an index into a page table E1.9 - Operating systems (Lect 7) 15 Memory allocation (2) Fragmentation and compaction (4) A solution to the problem of external fragmentation is compaction. Involves reshuffling of the memory contents to place all free memory together in one large block Requires OS to be capable of dynamic relocation Paging (2) Page table contains the base address of each page in physical memory Base address is combined with the page offset to define the physical memory address Process B Compaction Process B Process G Process G Free space E1.9 - Operating systems (Lect 7) 14 E1.9 - Operating systems (Lect 7) [Silberschatz, 2001] 16
Paging (3) When a process is to be executed, its size expressed in pages is examined Frames are allocated (one page per frame) by examining the frame table (a data structure maintained by the OS with the details of the memory available, allocated etc). First page of process is placed into one of the allocated frames; frame number is placed in the page table entry that corresponds to the page number Next page is loaded in the next allocated frame, and so on Paging hardware support Page table lookup can be speeded up through the provision of small, fast-lookup, hardware cache: translation look-aside buffer (TLB) that stores part of the page table Note: user views memory as one single contiguous space but the user s program might be scattered throughout physical memory E1.9 - Operating systems (Lect 7) 17 [Silberschatz, 2001] E1.9 - Operating systems (Lect 7) 19 Paging (4) Advantages: We have no external fragmentation Any free frame can be allocated to a process that needs it Paging shared pages Two or more processes can execute the same code (kept in shared pages) keeping separate data pages Disadvantages: Internal fragmentation still possible frames are allocated as units E1.9 - Operating systems (Lect 7) 18 E1.9 - Operating systems (Lect 7) [Silberschatz, 2001] 20
Memory allocation (4) Segmentation (1) Instead of partitioning the logical address space into pages (paging), we can divide it into logical segments Each segment has a name and a length associated with it. Different segments can have different lengths User specifies addresses as pairs: <logical segment offset>. E.g arrays of data Memory allocation (4) Segmentation (3) Advantages: Supports user s view of memory segments are constructed according to user semantics Protection and sharing easily implementable Note difference with paging: user is now aware of segments in paging, user specified a single address which was then partitioned by the hardware into a page number and an offset Disadvantages: External fragmentation possible E1.9 - Operating systems (Lect 7) 21 E1.9 - Operating systems (Lect 7) 23 Memory allocation (4) Segmentation (2) Segment table is used to translate logical address into physical address Summary Memory management fundamentals Flexible memory management requires dynamic relocation capabilities Simple memory allocation techniques include fixed and dynamic partitioning; susceptible to internal/external fragmentation Paging allows for non-contiguous allocation of memory Segmentation allows for a user s view of memory partitioning [Silberschatz, 2001] E1.9 - Operating systems (Lect 7) 22 E1.9 - Operating systems (Lect 7) 24
Recommended Reading Read chapter 9 of Silberschatz s book Read chapter 4, sections 4.1 & 4.2 of Tanenbaum s book Read chapter 7 of Stalling s book E1.9 - Operating systems (Lect 7) 25