Background • Program must be brought into memory and placed within a process for it to be run. Storage Allocation • Input queue – collection of processes on the disk that are waiting to be brought into memory to run the program. Operating System • User programs go through several steps before Hebrew University being run. Spring 2007 1 2 Binding of Instructions and Binding of Instructions and Data to Memory Data to Memory • Compile time: If memory location known in advance, • Address binding of instructions and data to absolute code can be generated; memory addresses can happen at three • Must recompile code if starting location changes. different stages: • Load time: Must generate relocatable code if memory location is not known at compile time. – Compile time • Execution time: Binding delayed until run time if the – Load time process can be moved during its execution from one – Execution time memory segment to another. Need hardware support for address maps (e.g., base and limit registers ). 3 4 Multi-step Processing of a Division of Responsibility User Program • Compiler : generates one object file for each source code file containing information for that file. Information is incomplete, since each source file generally uses some things defined in other source files. • Linker/Loader : combines all of the object files for one program into a single object file, which is complete and self-sufficient. • Operating system : loads object files into memory, allows several different processes to share memory at once, provides facilities for processes to get more memory after they have started running. • Run-time library : provides dynamic allocation routines, such as malloc and free in C. 5 6 1
Logical v. Physical Address Space Memory Management Unit (MMU) • The concept of a logical address space that is • Hardware device that maps virtual to physical bound to a separate physical address space is address. central to proper memory management. • In MMU scheme, the value in the relocation – Logical address – generated by the CPU; also referred register is added to every address generated by to as virtual address . a user process at the time it is sent to memory. – Physical address – address seen by the memory unit. • The user program deals with logical addresses; • Logical and physical addresses are the same in it never sees the real physical addresses. compile and load time; logical (virtual) and physical addresses differ in execution time. 7 8 Dynamic Relocation using a Contiguous Allocation Relocation Register • Single partition allocation (one process) • Relocation register scheme used to protect user processes from each other, and from changing operating system code and data. • Relocation register contains value of smallest physical address; • Limit register contains range of logical addresses – each logical address must be less than the limit register. 9 10 Hardware for Relocation Hardware for Relocation and Limit Registers and Limit Registers 11 12 2
Contiguous Allocation Contiguous Allocation • Multiple partition allocation (multiple processes) • Hole – block of available memory; holes of various size are scattered throughout memory. • When a process arrives, it is allocated memory from a hole large enough to accommodate it. • Operating system maintains information about: – a) allocated partitions – b) free partitions (hole) 13 14 Dynamic Storage Allocation So Which One? Problem • All could leave many small and useless holes. • Algorithms differ in how they manage the free holes: – Assuming 1K blocks: • Best fit: search the whole list on each allocation, • Best-Fit sometimes performs better: Assume holes of choose hole that comes closest to matching the needs 20K and 15K, requests for 12K followed by 16K can of the allocation, save the excess for later. During be satisfied only by best-fit. release operations, merge adjacent free blocks. • But First-Fit can also perform better: Assume holes of • First fit: scan for the first hole that is large enough. 20K and 15K, requests for 12K, followed by 14K, and 7K, can be satisfied only by first-fit. Also merge on releases. • In practice (based on trace-driven simulation ) • Worse fit: select the largest block, produces the – First-Fit is usually better than Best-Fit largest leftover hole. – First-Fit and Best-Fit are better than Worst Fit 15 16 Fragmentation Fragmentation • External Fragmentation – total memory space • Reduce external fragmentation by compaction exists to satisfy a request, but it is not – Shuffle memory contents to place all free memory contiguous. together in one large block. • Compaction is possible only if relocation is dynamic, and is done at execution time. • Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. 17 18 3
Segmentation User's View of a Program • Memory management scheme that supports user view of memory. • A program is a collection of segments. A segment is a logical unit such as: – main program – procedure, – function, – object, – local variables, global variables, – common block, – stack, – symbol table, arrays 19 20 Logical View of Segmentation Segmentation Architecture • Logical address consists of a two-tople: – <segment-number, offset> • Segment table – maps two dimensional physical addresses; each table entry has: – base – contains the starting physical address where the segments reside in memory. – limit – specifies the length of the segment. 21 22 Segmentation Architecture Segmentation Architecture • Relocation. • Segment table base register (STBR) - points to – dynamic the segment table’s location in memory. – by segment table • Segment table length register (STLR) - • Sharing indicates number of segments used by a – shared segments program; – same segment number • Segment number s is legal if s < STLR. • Allocation – first fit/best fit – external fragmentation 23 24 4
Protection Segmentation Hardware • Protection. With each entry in segment table associate: – validation bit = 0 � illegal segment – read/write/execute privileges • Protection bits associated with segments; code sharing occurs at segment level. • Since segments vary in length, memory allocation is a dynamic storage allocation problem. 25 26 The User Program Segmentation Example • Information stored in memory is used in many different ways. Some possible classifications are: – Role in Programming Language: • Instructions (operations and the operands in the operations). • Variables (change as the program runs: locals, globals, parameters, dynamic storage). • Constants (used as operands, but that never changes: pi for example). – Changeability: • Read-only: (code, constants) . • Read & write: (variables) . 27 28 Variables and Locations Variable Lifetime • Can we allocate all the required memory to our • lifetime of a variable - the period of time during program in advanced (compilation time)? execution in which the variable has storage – It is in general impossible to know at compile time how allocated for it. many variables are going to be created at run time – Global variables: Lifetime is entire runtime of • recursive procedures containing local variables program. • dynamic variables – Local variables (variables declared in a procedure): • We cannot solve all the allocations at compile time. Lifetime is during activation of procedure. • Do we want to allocate the required memory in – User-allocated variables (aka dynamic variables - advanced? variables created with new/malloc and destroyed – We want to be efficient in the management of the memory: with delete/free ): Lifetime is from user allocation to We only want to allocate storage for a variable when user de-allocation needed, and we want to de-allocate when possible. 29 30 5
Storage Allocation Policies Static Allocation (for globals only) • We have the following storage allocation • Done at compile time policies: • Lifetime = entire runtime of program – Static Allocation – Dynamic Allocation of local variables • Advantage: efficient execution time – Dynamic Allocation of user-allocated variables 31 32 Dynamic Allocation of local Dynamic Allocation of user-allocated variables variables • Done at run time • Done at run time • Lifetimes = duration of procedure activation • Lifetimes = until the user deletes it (or until it is garbage-collected) • Advantage: efficient storage use • Advantage: permits creation of dynamic – Two Methods: structures, like lists, trees, etc. • Stack Allocation (all variables allocated within the procedure scope) • Heap Allocation (unless declared 'static') • Heap Allocation 33 34 Memory Management The Segments • The memory of the machine is divided into three parts: – A space for the globals and the code of the program – The stack, for the locals – The heap, for the dynamic variables • The division between 1 and 2 is only logical: physically the locals and the code are (usually) placed at the base of the stack. 35 36 6
Recommend
More recommend