Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 11-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11.

Similar presentations


Presentation on theme: "Slide 11-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11."— Presentation transcript:

1 Slide 11-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11

2 Slide 11-2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 11 Memory Management

3 Slide 11-3 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 The External View of the Memory Manager Hardware Application Program Application Program File MgrDevice MgrMemory Mgr Process Mgr UNIX File MgrDevice MgrMemory Mgr Process Mgr Windows VMQuery() VirtualFree() VirtualLock() ZeroMemory() VirtualAlloc() sbrk() exec() getrlimit() shmalloc()

4 Slide 11-4 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Memory Manager Requirements –Minimize executable memory access time –Maximize executable memory size –Executable memory must be cost-effective Today’s memory manager: –Allocates primary memory to processes –Maps process address space to primary memory –Minimizes access time using cost-effective memory configuration –May use static or dynamic techniques

5 Slide 11-5 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Storage Hierarchies Less Frequently Used Information More Frequently Used Information

6 Slide 11-6 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 The Basic Memory Hierarchy CPU Registers Primary Memory (Executable Memory) e.g. RAM Secondary Memory e.g. Disk or Tape More Frequently Used Information Less Frequently Used Information

7 Slide 11-7 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Contemporary Memory Hierarchy & Dynamic Loading CPU Registers “Main” Memory Rotating Magnetic Memory Optical Memory Sequentially Accessed Memory L1 Cache Memory L2 Cache Memory Secondary Primary (Executable) Larger storage Faster access

8 Slide 11-8 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Exploiting the Hierarchy Upward moves are (usually) copy operations –Require allocation in upper memory –Image exists in both higher & lower memories Updates are first applied to upper memory Downward move is (usually) destructive –Destroy image in upper memory –Update image in lower memory Place frequently-used info high, infrequently-used info low in the hierarchy Reconfigure as process changes phases

9 Slide 11-9 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Background

10 Slide 11-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Binding of instructions and Data to Memory

11 Slide 11-11 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Address Binding CNTD Source program references data and instructions using identifiers (e.g. variable names etc.) Compiler translates source program into object module. A collection of object modules is combined using a link editor to produce an absolute module. –Can compiler/linker generate physical addresses? –Actual physical addresses are not known yet. Why Not? –Link editor produce re-locatable code: all addresses are relative to memory address 0 ==> compile-time binding

12 Slide 11-12 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Address Binding CNTD When module is loaded in memory for execution the loader, knowing where module is to be loaded in memory, can set the addresses at load time to produce the executable image of the module with physical addresses ==> load time binding – What if module is swapped out of memory? Dynamic Binding: binding is delayed until execution time. At load time, each identifier is associated a relative address. Physical address is computed at execution time by hardware.

13 Slide 11-13 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Address Space vs Primary Memory Mapped to object other than memory Process Address Space Hardware Primary Memory

14 Slide 11-14 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Creating an Executable Program Source code Source code Compile time: Translate elements Load time: Allocate primary memory Adjust addresses in address space Copy address space from secondary to primary memory Loader Process address space Primary memory C Reloc Object code Reloc Object code Link Edit Link Edit Library code Library code Other objects Other objects Secondary memory Link time: Combine elements

15 Slide 11-15 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 A Sample Code Segment... static int gVar;... int proc_a(int arg){... gVar = 7; put_record(gVar);... }

16 Slide 11-16 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 The Relocatable Object module Code Segment Relative AddressGenerated Code 0000...... 0008entryproc_a... 0220load=7, R1 0224storeR1, 0036 0228push0036 0232call‘put_record’... 0400External reference table... 0404‘put_record’0232... 0500External definition table... 0540‘proc_a’0008... 0600(symbol table)... 0799(last location in the code segment) Data Segment Relative AddressGenerated variable space... 0036[Space for gVar variable]... 0049(last location in the data segment)

17 Slide 11-17 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 The Absolute Program Code Segment Relative AddressGenerated Code 0000(Other modules)... 1008entryproc_a... 1220load=7, R1 1224storeR1, 0136 1228push1036 1232call2334... 1399(End of proc_a)... (Other modules) 2334entryput_record... 2670(optional symbol table)... 2999(last location in the code segment) Data Segment Relative AddressGenerated variable space... 0136[Space for gVar variable]... 1000(last location in the data segment)

18 Slide 11-18 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 The Program Loaded at Location 4000 Relative AddressGenerated Code 0000(Other process’s programs) 4000(Other modules)... 5008entryproc_a... 5036[Space for gVar variable]... 5220load=7, R1 5224storeR1, 7136 5228push5036 5232call6334... 5399(End of proc_a)... (Other modules) 6334entryput_record... 6670(optional symbol table)... 6999(last location in the code segment) 7000(first location in the data segment)... 7136[Space for gVar variable]... 8000(Other process’s programs)

19 Slide 11-19 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 C-Style Memory Layout Text Segment Initialized Part Data Segment Initialized Part Data Segment Uninitialized Part Data Segment Uninitialized Part Data Segment Heap Storage Stack Segment Environment Variables, … Environment Variables, …

20 Slide 11-20 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Program and Process Address Spaces Absolute Program Address Space 0 4GB 3GB Process Address Space Supervisor Process Address Space User Process Address Space Primary Memory

21 Slide 11-21 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Multiprogramming Memory Support Operating System Process 1 Process 3 Process 0 Process 1 Unused In Use 0 A B C D E F G H R0R0 R1R1 R2R2 R3R3 R4R4

22 Slide 11-22 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Static Memory Allocation Operating System Process 3 Process 0 Process 2 Process 1 Unused In Use Issue: Need a mechanism/policy for loading p i ’s address space into primary memory pipi

23 Slide 11-23 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Fixed-Partition Memory Mechanism Operating System Region 3 Region 2 Region 1 Region 0N0N0 N1N1 N2N2 N3N3 pipi p i needs n i units nini

24 Slide 11-24 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Fixed-Partition Memory Best-Fit Operating System Region 3 Region 2 Region 1 Region 0N0N0 N1N1 N2N2 N3N3 pipi Internal Fragmentation Loader must adjust every address in the absolute module when placed in memory

25 Slide 11-25 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Fixed-Partition Memory Worst-Fit Operating System Region 3 Region 2 Region 1 Region 0N0N0 N1N1 N2N2 N3N3 pipi

26 Slide 11-26 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Fixed-Partition Memory First-Fit Operating System Region 3 Region 2 Region 1 Region 0N0N0 N1N1 N2N2 N3N3 pipi

27 Slide 11-27 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Fixed-Partition Memory Next-Fit Operating System Region 3 Region 2 Region 1 Region 0N0N0 N1N1 N2N2 N3N3 pipi P i+1

28 Slide 11-28 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Variable Partition Memory Operating System Operating System Process 0 Process 6 Process 2 Process 5 Process 4 Compaction moves program in memory Operating System Process 0 Process 6 Process 2 Process 5 Process 4 External fragmentation Operating System Process 0 Process 1 Process 2 Process 3 Process 4 Loader adjusts every address in every absolute module when placed in memory

29 Slide 11-29 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Cost of Moving Programs load R1, 0x02010 3F013010 Program loaded at 0x01000 3F016010 Program loaded at 0x04000 Must run loader over program again! Consider dynamic techniques

30 Slide 11-30 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Moving an Executable Image Executable Program Executable Program Loader Executable Image Executable Image Executable Image Executable Image 02000 06000

31 Slide 11-31 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Dynamic Memory Allocation Could use dynamically allocated memory Process wants to change the size of its address space –Smaller  Creates an external fragment –Larger  May have to move/relocate the program Allocate “holes” in memory according to –Best- /Worst- / First- /Next-fit

32 Slide 11-32 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Dynamic Address Binding All addresses to data and instructions are made relative to beginning of the module i.e. as if process address space starts at physical address 0. Hardware provides a base register (also called a relocation register). When process is switched to CPU the base register is loaded with the initial address of the module. At run time:

33 Slide 11-33 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Dynamic Address Relocation CPU 0x02010 0x10000 + MAR load R1, 0x02010 0x12010 Program loaded at 0x10000  Relocation Register = 0x10000 Program loaded at 0x04000  Relocation Register = 0x04000 Relocation Register Relative Address We never have to change the load module addresses!

34 Slide 11-34 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Multiple Segment Relocation Registers CPU Relative Address Code Register + MAR 0x12010 Stack Register Data Register

35 Slide 11-35 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Runtime Bound Checking CPU Relative Address Relocation Register + MAR Limit Register <  Interrupt Bound checking is inexpensive to add Provides excellent memory protection

36 Slide 11-36 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Special Case: Swapping Special case of dynamic memory allocation Suppose there is high demand for executable memory Equitable policy might be to time-multiplex processes into the memory (also space-mux) Means that process can have its address space unloaded when it still needs memory –Usually only happens when it is blocked

37 Slide 11-37 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Swapping Image for p j Image for p i Swap p i out Swap p j in

38 Slide 11-38 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Sharing a Portion of the Address Space Process 1 Process 2 Address Space for Process 2 Address Space for Process 1 Primary Memory

39 Slide 11-39 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Figure 11 ‑ 26: Multiple Segments Relocation Limit Relocation Limit CPU Executing Process 1 CPU Executing Process 2 Primary Memory Shared Private to Process 1 Private to Process 2

40 Slide 11-40 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11 Memory Mgmt Strategies Fixed-Partition used only in batch systems Variable-Partition used everywhere (except in virtual memory) Swapping systems –Popularized in timesharing –Relies on dynamic address relocation –Now dated Dynamic Loading (Virtual Memory) –Exploit the memory hierarchy –Paging -- mainstream in contemporary systems –Segmentation -- the future


Download ppt "Slide 11-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 11."

Similar presentations


Ads by Google