Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real Memory Management

Similar presentations


Presentation on theme: "Real Memory Management"— Presentation transcript:

1 Real Memory Management
Operating Systems Real Memory Management

2 Real Memory Management
Background Memory Management Requirements Fixed/Static Partitioning Variable/Dynamic Partitioning Simple/Basic Paging Simple/Basic Segmentation Segmentation with Paging

3 Background Program must be brought into main memory and placed within a process representing it, in order for it to be run. Memory management is the task carried out by the OS and hardware to accommodate multiple processes in main memory. User programs go through several steps before being able to run. This multi-step processing of the program invokes the appropriate utility and generates the required module at each step (see next slides).

4 Multi-step Processing of a User Program

5 Steps for loading a process in memory
Source file1 Source file2 Source file3 Object module1 Object module2 Object module3 Linker Assembler Or Compiler Executable Binary file (Load module) Loader Process image in Memory

6 Object Module Public names are usable by other object modules.
External names are defined in other object modules: Includes the list of instructions having these names as operands. Relocation dictionary: Has the list of instructions who’s operands are addresses (since they are relocatable). Only code and data will be loaded in physical memory: The rest is used by the linker and then removed. The stack is allocated only at load time. End of module Relocation dictionary Data Machine code External names table Public names table Module identification

7 Object Modules Initially, each object module has its own address space. All addresses are relative to the beginning of the module.

8 Linking Static The linker uses tables in object modules to link modules into a single linear addressable space. The new addresses are addresses relative to the beginning of the load module. BRANCH TO 200 MOVE P TO X CALL 400 BRANCH TO 600 MOVE R TO X CALL 0 100 200 300 400 500 600 700 800 900 Object module B Object module A Load Module (from Linker)

9 Dynamic Linking The linking of some external modules is done after the creation of the load module (executable file). Load-time dynamic linking: The load module contains references to external modules which are resolved at load time. Run-time dynamic linking: references to external modules are resolved when a call is made to a procedure defined in the external module. unused procedure is never loaded. Process starts faster.

10 Program vs. Memory sizes
What to do when program size is larger than the amount of memory/partition (that exists or can be) allocated to it? There are two basic solutions within real memory management: Overlays Dynamic Linking (Libraries - DLLs)

11 Overlays Keep in memory only the overlay (those instructions and data that are) needed at any given phase/time. Overlays can be used only for programs that fit this model multi-pass programs like compilers. Overlays are designed/implemented by programmer. Needs an overlay driver. No special support needed from operating system, but program design of overlays structure is complex.

12 Overlays for a Two-Pass Assembler
Symbol table Common Routine Overlay Driver 20 K 30 K 10 K Total memory needed, if Overlay is not used is 200 K ( ) K Total memory needed is 140K ( ) K Pass 1 70 K Pass 2 80 K

13 Dynamic Linking Dynamic linking is useful when large amounts of code are needed to handle infrequently occurring cases. Routine is not loaded unless/until it is called. Better memory-space utilization; unused routine is never loaded.

14 Dynamics of Dynamic Linking
Linking postponed until execution time. Small piece of code, stub, used to locate the appropriate memory-resident library routine. Stub replaces itself with the address of the routine, and executes the routine. OS needed to check if routine is in processes’ memory address. Dynamic linking is particularly useful for shared/common libraries -– here full OS support is needed.

15 Advantages of Dynamic Linking
Executable files can use another version of the external module without the need of being modified. Each process is linked to the same external module. Saves disk space. The external module needs to be loaded in main memory only once. Processes can share code and save memory. Examples: Windows: external modules are .DLL files. Unix: external modules are .SO files (shared library).

16 Memory Management Requirements
If only a few processes can be kept in main memory, then much of the time all processes will be waiting for I/O and the CPU will be idle. Hence, memory needs to be allocated efficiently in order to pack as many processes into memory as possible. Need additional support for: Relocation Protection Sharing Logical Organization Physical Organization Next

17 Memory Management Requirements (1)
Relocation: Programmer don’t know where the program will be placed in memory when it is executed. A process may be (often) relocated in main memory due to swapping/compaction: Swapping enables the OS to have a larger pool of ready-to-execute processes. Compaction enables the OS to have a larger contiguous memory to place programs in. Back

18 Memory Management Requirements (2)
Protection: Processes should not be able to reference memory locations in another process without permission. Impossible to check addresses in programs at compile/load-time since the program could be relocated. Address references must be checked at execution-time by hardware. Back

19 Memory Management Requirements (3)
Sharing: must allow several processes to access a common portion of main memory without compromising protection: Better to allow each process to access the same copy of a shared module rather than have their own separate copy. Cooperating processes may need to share access to the same data structure. Back

20 Memory Management Requirements (4)
Logical Organization: Users write programs in modules with different characteristics: instruction modules are execute-only. data modules are either read-only or read/write. some modules are private and others are public. To effectively deal with user programs, the OS and hardware should support a basic form of a module to provide the required protection and sharing. Back

21 Memory Management Requirements (5)
Physical Organization: External memory is the long term store for programs and data while main memory holds programs and data currently in use. Moving information between these two levels of the memory hierarchy is a major concern of memory management - it is highly inefficient to leave this responsibility to the application programmer. Back

22 The need for Relocation
Because of need for process swapping and memory compaction, a process may occupy different main memory locations during its lifetime. Consequently, physical memory references (addresses) by a process cannot always be fixed. This problem is solved by distinguishing between logical address and physical address.

23 Address Types A physical (absolute) address is a physical location in main memory. A logical (virtual) address is a reference to a memory location that is independent of the physical organization of memory. Compilers produce code in which all memory references are logical addresses. A relative address is an example of logical address in which the address is expressed as a location relative to some known point in the program (the beginning).

24 Relocation Scheme Relative address is the most frequent type of logical address used in program modules (i.e., executable files). Relocatable modules are loaded in main memory with all memory references left in relative form. Physical addresses are calculated “on the fly” as the instructions are executed. For adequate performance, the translation from relative to physical address must by done by hardware.

25 Memory-Management Unit (MMU)
Hardware device that maps logical/virtual address to real/physical address. In relocation scheme, the value in the relocation register is added to every logical address generated by a user process at the time it is sent to memory. The process deals with logical/virtual addresses; it never sees the real/physical addresses.

26 CPU, MMU and Memory The CPU sends logical addresses to the memory CPU
CPU package Memory Disk Controller Bus The MMU sends Physical addresses to the memory

27 Dynamic relocation using a relocation register
+ MMU CPU Memory 14000 Physical address 14346 Logical address 346

28 Hardware Support for Relocation and Limit Registers
CPU Limit register Relocation register Memory + Physical address < Logical address yes no Trap; addressing error

29 Address-binding of Instructions/Data
Address-binding of instructions and data to memory addresses can happen at three different stages: Compile-time: If memory location is known a priori, absolute code can be generated; must recompile code if the starting location changes. Load-time: Must generate relative code if memory location is not known at compile-time; loading maps relative code to absolute code by adding start location. Execution-time: Binding delayed until run-time if the process can be relocated (i.e., relocatable code) during its execution from one place to another. Need hardware support for address maps (e.g., base and limit registers).

30 Logical vs. Physical Address Space
The concept of a logical address space of a program that is bound to a physical address space is central to proper memory management. Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme.

31 Logical and Physical Address Spaces
Process 2 Base reg =500K Length =350K Process 2’s logical address space 350 K 0 K The computers Physical address space 850 K 500 K 200 K 0 K Process 1 Base reg =200K Length =300K Process 1’s logical address space 300 K 0 K

32 Example Hardware for Address Translation
Adder Relative address Base register Code Data stack Process image in memory Comparator absolute address Limit register Interrupt to OS

33 Dynamics of hardware translation of addresses
When a process is assigned to the running state, a relocation/base register gets loaded with the starting physical address of the process. A limit/bounds register gets loaded with the process’s ending physical address. When a relative addresses is encountered, it is added with the content of the base register to obtain the physical address which is compared with the content of the limit/bounds register. This provides hardware protection: each process can only access memory within its process image.

34 Contiguous Allocation
An executing process must be loaded entirely in main memory (if overlays are not used). Main memory is usually split into two (memory split) or more (memory division) partitions: Resident operating system, usually held in low memory partition with interrupt vector. User processes then held in high memory partitions. Relocation-register scheme is used to protect user processes from each other, and from changing OS code and data.

35 Unequal-size Partition
Fixed Partitioning Operating system 8 M Equal size Partition 8 M Operating system 2M 4M 6M 12 M 16 M Unequal-size Partition Partition main memory into a set of non-overlapping memory regions called partitions. Fixed partitions can be of equal or unequal sizes. Leftover space in partition, after program assignment, is called internal fragmentation.

36 Placement Algorithm with Partitions
Equal-size partitions: If there is an available partition, a process can be loaded into that partition - because all partitions are of equal size, it does not matter which partition is used. internal fragmentation may occur. If all partitions are occupied by blocked processes, choose one process to swap out to make room for the new process. Unequal-size partitions, use of multiple queues: assign each process to the smallest partition within which it will fit. increases the level of multiprogramming at the expense of internal fragmentation.

37 Dynamics of Fixed Partitioning
Any process whose size is less than or equal to a partition size can be loaded into the partition. If all partitions are occupied, the OS can swap a process out of a partition. A program may be too large to fit in a partition. The programmer must design the program with overlays.

38 Comments on Fixed Partitioning
Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This can cause internal fragmentation. Unequal-size partitions lessens these problems but they still remain ... Equal-size partitions was used in early IBM’s OS/MFT (Multiprogramming with a Fixed number of Tasks).

39 Variable Partitioning
When a process arrives, it is allocated memory from a hole large enough to accommodate it. Hole – block of available memory; holes of various sizes are scattered throughout memory. Operating system maintains information about: a) allocated partitions b) free partitions (holes) OS OS OS OS process 5 process 5 process 5 process 5 process 9 process 9 process 8 process 10 process 2 process 2 process 2 process 2

40 Managing allocated and free partitions
Example: memory with 5 processes and 3 holes: tick marks show allocation units. shaded regions are free.

41 Variable Partitioning: example (1)
A hole of 64K is left after loading 3 processes: not enough room for another process. Eventually each process is blocked. The OS swaps out process 2 to bring in process 4.

42 Variable Partitioning: example (2)
Another hole of 96K is created. Eventually each process is blocked. The OS swaps out process 1 to bring in again process 2 and another hole of 96K is created ...

43 Internal/External Fragmentation
There are really two types of fragmentation: Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, that is not being used. External Fragmentation – total memory space exists to satisfy a size n request, but that memory is not contiguous.

44 Reducing External Fragmentation
Reduce external fragmentation by doing compaction: Shuffle memory contents to place all free memory together in one large block (or possibly a few large ones). Compaction is possible only if relocation is dynamic, and is done at execution time. I/O execution problem: Lock job in memory while it is involved in I/O. Do I/O only into OS buffers.

45 Comments on Variable Partitioning
Partitions are of variable length and number. Each process is allocated exactly as much memory as it requires. Eventually holes are formed in main memory. This can cause external fragmentation. Must use compaction to shift processes so they are contiguous; all free memory is in one block. Used in IBM’s OS/MVT (Multiprogramming with a Variable number of Tasks).


Download ppt "Real Memory Management"

Similar presentations


Ads by Google