Module 5 Memory Management PDF

Summary

These notes cover memory management concepts, including direct access to memory, hardware memory protection, and different types of address binding. The material appears to be part of a Computer Science module, likely at an undergraduate level. It details how programs interact with memory, moving from logical to physical addresses.

Full Transcript

Module 5 Memory Management Module 5 Memory  Memory consists of a large array of bytes, each with its own address.  The CPU fetches instructions from memory according to the value of the program counter.  The memory unit sees only a str...

Module 5 Memory Management Module 5 Memory  Memory consists of a large array of bytes, each with its own address.  The CPU fetches instructions from memory according to the value of the program counter.  The memory unit sees only a stream of memory addresses; it does not know how they are generated or what they are for. 22/09/24 2 Module 5 Direct Access to Memory and Registers  The C P U can directly access only two types of storage: main memory and registers built into the processing cores.  Any data or instruction being used must be in one of these locations. If it is not, the data must be moved into memory for the C P U to access.  Registers are extremely fast (accessible in one clock cycle), but memory access is slower as it requires communication over the memory bus. So, cache is used. 3 Module 5 Hardware Memory Protection  For proper operation, it’s essential to protect the operating system from unauthorized access by user processes and to prevent user processes from accessing each other's memory.  This protection is enforced by hardware using mechanisms like base and limit registers.  Each process has a separate memory space with a range of legal addresses that the process may access. 22/09/24 4 Module 5 Hardware Memory Protection  Base register holds the smallest legal memory address for a process.  Limit register specifies the range of legal addresses.  The CPU compares every address generated in u ser mode against these registers.  Any illegal access resu lts in a trap to the operating system, wh i c h t re a t s t h e a t te m p t a s a fa t a l e r ro r. 22/09/24 5 Module 5 Hardware Memory Protection 22/09/24 6 Module 5 Hardware Memory Protection  Base Address Register: The starting memory address of a process in main memory. It defines the lowest legal memory address that the process can access.  Limit Register: Specifies the size of the process or the range of addresses the process is allowed to access. The value in the limit register is typically the length of the addressable memory region for that process, starting from the base address.  The base address tells where the process begins in memory.  The limit tells how m u c h memory (in terms of size) the process can access, thus defining the upper boundary of accessible memory as base + limit 22/09/24 7 Module 5 Hardware Memory Protection 22/09/24 8 Module 5 User and Kernel Mode  The base and limit registers can be loaded only by the operating system, which uses a special privileged instruction.  Protection mechanisms ensure that user processes cannot access kernel memory or manipulate hardware directly.  Only the operating system can modify the value of base and limit registers, as this operation can only be performed in kernel mode. User programs cannot change these register values. 22/09/24 9 Module 5 User and Kernel Mode  The operating system, executing in kernel mode, is given unrestricted access to both operating-system memory and users' memory 22/09/24 10 Module 5 Address binding - Process Execution  Program s are stored as binary executable files on disk and need to be loaded into memory to run.  The process accesses instru ction s and data from memory during execution.  When process terminates, and its memory is reclaimed for use by other processes. 22/09/24 11 Module 5 Address Binding  Address B inding refers to the process of m appin g logical (virtual) addresses to physical addresses in memory.  It occurs in different stages during a program's lifecycle: compile-time, load-time, and execution-time.  The m ain goal of address binding is to ensu re that when a program accesses m em ory, it can find the right data or instruction in physical memory. 22/09/24 12 Module 5 Address Binding  In most cases, a user program goes through several steps - Addresses maybe represented in different ways:  In a source code, symbolic addresses are identifiers for variables, functions, classes, and other constructs.  A compiler typically binds these symbolic addresses to relocatable addresses  The linker or loader in turn binds the relocatable addresses to absolute addresses. 22/09/24 13 Module 5 Address Binding  The binding of instructions and data to memory addresses can be done at any step along the way:  Compile time. If you know at compile time where the process will reside in memory, then absolute code can be generated.  Load time. If it is not known at compile time where the process will reside in memory, then the compiler must generate relocatable code.  Execution time. If the process can be moved during its execution from one memory segment to another, then binding must be delayed until run time. 22/09/24 14 Module 5 Address Binding 22/09/24 15 Module 5 Address Binding - Compile-Time Address Binding  Address B inding refers to m appin g sym bolic or relocatable addresses in the program to actual memory addresses  The compiler determ ines where a progra m will reside in memory.  The addresses in the program are set during compilation, and if the program’s memory location needs to change later (e.g., if the program is loaded into a different memory location), it must be recompiled. 22/09/24 16 Module 5 Address Binding - Compile-Time Address Binding  Example  Suppose a small program is always loaded into memory startin g at address 1000. The compiler generates m ach in e code that expects the program to start at this address.  If the program is loaded elsewh ere (e. g. , address 2000), the code will not work u n less it is recom piled to reflect this new memory location. 22/09/24 17 Module 5 Address Binding - Load-Time Address Binding  The exact memory location where the program will be loaded is not known at compile time.  The linker or loader binds addresses when the program is loaded into memory. The addresses are relocatable, meaning they can adjust based on where the program is loaded in memory. 22/09/24 18 Module 5 Address Binding - Load-Time Address Binding Example  The program is compiled with relocatable addresses, which say, "I need memory starting from some location." If the program is loaded into address 3000, the loader adjusts all addresses to work from that starting point.  If the program is later loaded into address 5000, the loader adjusts the addresses accordingly, but no recompilation is needed. 22/09/24 19 Module 5 Address Binding - Execution-Time Address Binding  The program can be moved in memory during its execution (e.g., due to swapping or paging).  The binding happens dynamically at run time, meaning the operating system keeps track of where the program is at any given time and translates addresses on-the-fly. 22/09/24 20 Module 5 Address Binding - Execution-Time Address Binding  Example  The program starts at address 4000, but during execution, the operatin g system moves part of it to address 7000 (perhaps due to memory management or multitasking).  Every time the program tries to access a memory location, the operatin g system tran slates its logical addresses to the current physical address in memory. 22/09/24 21 Module 5 Address Binding - Examples  Example  When you write code like int x = 5;, the compiler assigns an address to x, say 1000.  If the compiler knows that the program will always be loaded startin g at memory location 4000, it can assign x a fixed address of 5000 (4000 + 1000).  This is ? 22/09/24 22 Module 5 Address Binding - Execution-Time Address Binding  Example  Now, imagine the program might not always start at 4000. It could start at any available memory address.  The program is compiled with relocatable addresses. For example, x might be stored at offset 1000 relative to the program's starting address.  When the program is loaded into memory (e.g., starting at 8000), the loader will adjust the address of x to 9000 (8000 + 1000). 22/09/24 23 Module 5 Address Binding - Execution-Time Address Binding  Example  If you r progra m is moved to another part of memory (e. g. , beca u se of paging or swappin g), the operatin g system will adjust the memory addresses dynamically during execution.  For exam ple, if the O S moves the program to 12000, the address of x will be updated to 13000 (12000 + 1000) while the program is running. 22/09/24 24 Module 5 Logical Address Space  A logical address is an address generated by the CPU during program execution.  It is the address used by a program to reference memory locations.  This address is visible to the user or programmer and is often seen in the program as symbolic references (like variables, arrays, etc.).  Logical addresses are part of the logical address space (also known as the virtual address space), which is the set of all possible addresses that a program can generate  The logical address does not correspond directly to an actual location in physical memory. It is translated into a physical address by the memory management unit (MMU) via address translation techniques like paging or segmentation. 22/09/24 25 Module 5 Logical Address Space  Why Use Logical Addresses?  Logical addresses provide an extra layer of abstraction. This allows program s to run with ou t needing to know exactly where their data is stored in physical memory.  It also gives flexibility to the operatin g system, allowing it to move program s arou nd in memory with ou t the program s needing to know. 22/09/24 26 Module 5 Physical Address Space  A physical address is the actual address in the main memory (RAM).  It represen ts the real location of data or instruc tions in physic al memory hardware.  This address is not visible to the user or programmer and is only used by the hardware (the memory management unit, C P U, and OS).  Physical addresses are part of the physical address space, which is the set of all physical memory locations in the hardware.  The physical address is obtained by translating th e logical address using the memory management unit (MMU). 22/09/24 27 Module 5 Physical Address Space  When a program is run n in g, the C P U generates a logical address (e. g. , 0x005A). This address is not the actual location in physical memory.  The M M U tran slates this logical address to a physical address (e. g. , 0xF12345), which is where the data is stored in the RAM. 22/09/24 28 Module 5 Memory Management Unit (MMU)  The M M U is a special hardware component in the computer that helps translate the virtual (logical) addresses used by a program into physical addresses that point to actual locations in the computer’s memory (RAM).  Think of the M M U as a translator that maps the addresses a program uses into real memory addresses behind the scenes 22/09/24 29 Module 5 Memory Management Unit (MMU)  When a program is running, it doesn’t directly use the physical addresses where data is stored in memory. Instead, it uses virtual addresses, which are more flexible and can be different from the physical addresses.  The MMU converts these virtual addresses to physical addresses so that the C P U can find the correct location in the actual memory. 22/09/24 30 Module 5 Memory Management Unit (MMU)  For example, the base register is now called a relocation register.  The relocation register is a special part of the MMU that holds a fixed offset (a number). This offset helps the M M U shift (or relocate) the virtual address to the correct physical address.  This offset is the difference between where the program thinks it’s running and where it’s actually located in physical memory. 22/09/24 31 Module 5 Memory Management Unit (MMU)  Relocation Register - Example  Imagine a process starts its virtu al address at 0, b u t in real memory, it starts at physical address 5000.  The relocation register will hold the value 5000.  So, if the program asks for data at virtu al address 20, the M M U adds 5000 to this, m akin g the actu al physical address 5020. 22/09/24 32 Module 5 Memory Management Unit (MMU)  Relocation Register - Example 22/09/24 33 Module 5 Static Loading  When a program is about to run, static loading means that everything the program needs (code, data, etc.) is loaded into memory before it starts.  The memory locations for all components are set in advance, and they don’t change while the program is running. Pros: Fast access to everything since it’s already in memory. Cons: This can waste memory if not all components are actually used during the program’s run. Example: Imagine opening a big book. You copy the entire book onto your desk even if you might only read a few pages. 22/09/24 34 Module 5 Static Linking  Static linking happens when the necessary libraries are added directly into the program’s file when it is created.  The final program file contains everything it needs to run on its own, so it doesn’t need to find any external libraries at runtime.  Pros: It makes the program independent and portable  Cons: The file becomes larger, and if many programs use the same library, each will have its own copy, leading to redundancy. 22/09/24 35 Module 5 Dynamic Linking  With dynamic linking, the program does not include the libraries in its final file. Instead, it connects to these libraries while running.  This allows many programs to share a single copy of a library, saving memory and reducing file size.  Pros: Saves space and avoids redundancy.  Cons: The program depends on these libraries being available on the system during runtime 22/09/24 36 Module 5 Dynamic Loading  In dynamic loading, the program loads components into memory only when they are needed during execution. It doesn’t load everything upfront.  With dynamic loading, a routine is not loaded until it is called. All routines are kept on disk in a relocatable load format.  Then the relocatable linking loader is called to load the desired routine into memory and to update the program's address tables to reflect this change. 22/09/24 37 Module 5 Dynamic Loading  The advantage of dynamic loading is that a routine is loaded only when it is needed.  Dynamic loading does not require special support from the operating system.  Better memory management, as unused parts don’t take up space.  It ca n slow down the program slightly if it has to load new components while running. 22/09/24 38

Use Quizgecko on...
Browser
Browser