Presentation is loading. Please wait.

Presentation is loading. Please wait.

WEEK 5 LINKING AND LOADING MEMORY MANAGEMENT PAGING AND SEGMENTATION Operating Systems CS3013 / CS502.

Similar presentations


Presentation on theme: "WEEK 5 LINKING AND LOADING MEMORY MANAGEMENT PAGING AND SEGMENTATION Operating Systems CS3013 / CS502."— Presentation transcript:

1 WEEK 5 LINKING AND LOADING MEMORY MANAGEMENT PAGING AND SEGMENTATION Operating Systems CS3013 / CS502

2 Agenda 2 Linking and Loading Memory Management Paging and Segmentation

3 Objectives 3 Understand Linking and Loading Differentiate Static and Dynamic Linking

4 Executable Files 4 Every OS expects executable files in a specific format  Header Info  Code locations  Data locations  Code & Data  Symbol Table  List of names of things defined in your program and where they are located within your program.  List of names of things defined elsewhere that are used by your program, and where they are used.

5 Example 5 Symbols defined in the program and used elsewhere  main Symbols defined elsewhere and used in the program  printf #include int main () { printf (“hello, world\n”) }

6 Example 6 Symbols defined in the program and used elsewhere  main Symbols defined elsewhere and used in the program  printf  errno #include Extern int errno; int main () { printf (“hello, world\n”) }

7 Linking and Loading 7 Linking: Combining a set of programs, including library routines, to create a loadable image  Resolving symbols defined within the set  Listing symbols needing to be resolved by loader Loading: Copying the loadable image into memory, connecting it with any other programs already loaded, and updating addresses as needed  (in all systems) kernel image is special (own format)

8 Source 8 Binding is the act of connecting names to addresses Most compilers produce relocatable object code  Addresses relative to zero The linker combines multiple object files and library modules into a single executable file  Addresses also relative to zero The Loader reads the executable file  Allocates memory  Maps addresses within file to memory addresses  Resolves names of dynamic library items Source (.c,.cc) Object (.o) Executable In-memory Image Compiler Linker Loader Other Objects (.o) Dynamic libraries (.dll) Static libraries (.a)

9 Static Linking 9 Printf.c Printf.oMain.o Main.c a.out Memory Static Library Linker gcc ar loader

10 Classic Unix 10 Linker lives inside of cc or gcc command Loader is part of exec system call Executable image contains all object and library modules needed by program Entire image is loaded at once Every image contains its own copy of common library routines Every loaded program contain duplicate copy of library routines

11 Dynamic Linking 11 Routine is not loaded until it is called Better memory-space utilization; unused routines are never loaded. Useful when large amounts of code are needed to handle infrequently occurring cases.

12 Linker-Assisted Dynamic Linking 12 For function call to a dynamic function  Call is indirect through a link table  Each link table entry is initialized with address of small stub of code to locate and load module.  When loaded, loader replaces link table entry with address of loaded function  When unloaded, loader restores table entry with stub address  Works only for function calls, not static data

13 Linker-Assisted Dynamic Linking 13 Your program void main () { printf (…); } Link table Stub void load() { … load(“IOLib”); … }

14 Linker-Assisted Dynamic Linking 14 Your program void main () { printf (…); } Link table IOLib read() {…} printf() {…} scanf() {…}

15 Shared Library 15 Observation – “everyone” links to standard libraries (libc.a, etc.) These consume space in  every executable image  every process memory at runtime Would it be possible to share the common libraries?

16 Shared Library 16 Libraries designated as “shared” .so,.dll, etc.  Supported by corresponding “.a” libraries containing symbol information Linker sets up symbols to be resolved at runtime Loader: Is library already in memory?  If yes, map into new process space  If not, load and then map

17 Run Time Dynamic Linking 17 Printf.c Printf.oMain.o Main.c a.out Memory Dynamic Library Linker gcc ar loader Run-time Loader

18 Dynamic Linking 18 Complete linking postponed until execution time. Stub used to locate the appropriate memory-resident library routine. Stub replaces itself with the address of the routine, and executes the routine. Operating system needs to check if routine is in address space of process Dynamic linking is particularly useful for libraries.

19 Dynamic Shared Library 19 Static shared libraries requires address space pre- allocation Dynamic shared libraries – address binding at runtime  Code must be position independent  At runtime, references are resolved as  Library_relative_address + library_base_address

20 Linker 20 Linker – key part of OS – not in kernel  Combines object files and libraries into a “standard” format that the OS loader can interpret  Resolves references and does static relocation of addresses  Creates information for loader to complete binding process  Supports dynamic shared libraries

21 Loader 21 An integral part of the OS Resolves addresses and symbols that could not be resolved at link-time May be small or large  Small: Classic Unix  Large: Linux, Windows XP, etc. May be invoke explicitly or implicitly Explicitly by stub or by program itself Implicitly as part of exec

22 Agenda 22 Linking and Loading Memory Management Paging and Segmentation

23 Objectives 23 Understand Different Memory Management Strategies Differentiate Internal and External Fragmentation

24 Simple Memory Management 24 One process in the memory  Uses the entire memory available  Each program needs I/O driver User Program RAM I/O Drivers

25 Simple Memory Management 25 Small, protected OS “Mono-processing” User Program User Program User Program OS Device Drivers OS RAM ROM

26 Memory Management with Fixed Partitions 26 Unequal Queues Waste Large Partitions Partition 1 OS Partition 2 Partition 3 Partition 4 Partition 1 OS Partition 2 Partition 3 Partition 4 200k 300k 500k 900k

27 Physical Memory 27 OS Kernel Process 1 Process 2 Process 3 Empty x00000000 xFFFFFFFF Physical Address Space

28 Process 2 Terminates 28 OS Kernel Process 1 Empty Process 3 Empty x00000000 Physical Address Space xFFFFFFFF

29 Problem 29 What happens when Process 4 comes along and requires space larger than the largest empty partition? OS Kernel Process 1 Empty Process 3 Empty Process 4

30 Solution 30 Virtual Address: an address used by the program that is translated by computer into a physical address each time it is used  Also called Logical Address When the program utters 0x00105C, … … the machine accesses 0x01605C

31 Implementation 31 Base and Limit registers  Base automatically added to all addresses  Limit checked on all memory references  Introduced in minicomputers of early 1970s Loaded by OS at each context switch logical address Limit Reg error no Base Reg + yes physical address Physical Memory CPU <

32 Physical Memory 32 OS Kernel Process 1 Empty Process 3 Empty x00000000 Physical Address Space xFFFFFFFF Base Limit

33 Advantage 33 No relocation of program addresses at load time  All addresses relative to zero! Built-in protection provided by Limit  No physical protection per page or block Fast execution  Addition and limit check at hardware speeds within each instruction Fast context switch  Need only change base and limit registers Partition can be suspended and moved at any time  Process is unaware of change  Potentially expensive for large processes due to copy costs!

34 34 OS Kernel Process 1 Process 3 Process 4 x00000000 Physical Address Space xFFFFFFFF Base Limit

35 Memory Allocation Strategy 35 First Fit  First big enough hole Best Fit  Smallest hole that is big enough Worst Fit  Largest hole

36 Challenge – Memory Allocation 36 How should we partition the physical memory for processes?

37 Fixed-sized Partitions 37 Fixed Partitions – divide memory into equal sized pieces (except for OS)  Degree of multiprogramming = number of partitions  Simple policy to implement  All processes must fit into partition space  Find any free partition and load the process Problem – what is the “right” partition size?  Process size is limited  Internal Fragmentation – unused memory in a partition that is not available to other processes

38 Internal Fragmentation 38 “Empty” space for each process OS Program Data Stack Room for growth Allocated to Process

39 Variable-sized Partitions 39 Idea: remove “wasted” memory that is not needed in each partition  Eliminating internal fragmentation Memory is dynamically divided into partitions based on process needs Definition:  Hole: a block of free or available memory  Holes are scattered throughout physical memory New process is allocated memory from hole large enough to fit it

40 External Fragmentation 40 Total memory space exists to satisfy request but it is not contiguous OS Kernel Process 1 Empty Process 3 Empty 150k 100k Process 4 ? 200k

41 Analysis of External Fragmentation 41 Assumption  First-fit allocation strategy  System at equilibrium N allocated blocks ½ N blocks lost to fragmentation Fifty-percent rule

42 Compaction 42 OS Kernel Process 1 Process 3 Process 4 Process 2 OS Kernel Process 1 Process 3 Process 2 OS Kernel Process 1 Process 3 Process 2 (a)(b) 50k 75k 100k 125k

43 Solutions? 43 Minimize external fragmentation  Large Blocks  But internal fragmentation Tradeoff  Sacrifice some internal fragmentation for reduced external fragmentation  Paging!

44 Agenda 44 Linking and Loading Memory Management Paging and Segmentation

45 Objectives 45 Understand Paging Mechanism Translate Virtual Address Using Paging Analyze Different Requirements for Paging Differentiate Different Paging Strategies Understand Segmentation

46 Memory Management 46 Logical address vs. Physical address Memory Management Unit (MMU)  Set of registers and mechanisms to translate virtual addresses to physical addresses Processes (and processors) see virtual addresses  Virtual address space is same for all processes, usually 0 based  Virtual address spaces are protected from other processes MMU and devices see physical addresses Processor MMU MemoryI/O Devices Logical Addresses Physical Addresses

47 Paging 47 Logical address space noncontiguous; process gets memory wherever available  Divide physical memory into fixed-size blocks  Size is a power of 2, between 512 and 8192 bytes  Called Frames  Divide logical memory into fixed-size blocks  Called Pages Address generated by CPU divided into:  Page number (p) – index to page table  Page table contains base address of each page in physical memory (frame)  Page offset (d) – offset into page/frame

48 Paging 48 page frame 0 page frame 1 page frame 2 page frame Y … page frame 3 physical memory offset physical address F(PFN)page frame # page table offset logical address virtual page #

49 Paging Example 49 Page size 4 bytes Memory size 32 bytes (8 pages) Page 0 0 1 2 3 Page Table Logical Memory Physical Memory 0 1 2 3 4 5 6 7 Page 2 Page 3 Page 1 1 4 3 7 Page 3 Page 2 Page 0

50 Paging Example 50 Page 0 Page 1 Page 2 Page 3 000 010 001 011 100 110 101 111 000 010 001 011 100 110 101 111 001011 0001 11 1000 1110 Page Table PageFrame Offset

51 Paging 51 Address space 2 m Page offset 2 n Page number 2 m-n Not: not losing any bytes pd Page numberPage offset m-n bitsn bits

52 Paging Example 52 Consider  Physical memory = 128 bytes  Physical address space = 8 frames How many bits in an address? How many bits for page number? How many bits for page offset? Can a logical address space have only 2 pages? How big would the page table be?

53 Another Paging Example 53 Consider:  8 bits in an address  3 bits for the frame/page number How many bytes (words) of physical memory? How many frames are there? How many bytes is a page? How many bits for page offset? If a process’ page table is 12 bits, how many logical pages does it have?

54 Page Table Example (7 bits) 54 0 1 2 3 4 5 6 7 Page 1 (A) Page 1 (B) Page 0 (B) Page 0 (A) pd Page numberPage offset m-n = 3n = 4 Page 0 Page 1 Process A Page 0 Page 1 Process B 0 1 1 4 0 1 3 7 Page Table

55 Paging Tradeoffs 55 Advantage  No external fragmentation (no compaction)  Relocation (now pages, before were processes) Disadvantage  Internal fragmentation  Consider: 2048 byte pages, 72,766 byte proc 35 pages + 1086 bytes = 962 bytes Average : ½ page per process Small pages  Overhead Page table / process (context switch + space) Lookup (especially if page to disk)

56 Implementation of Page Table 56 How would you implement page tables in operating systems?

57 Implementation of Page Table 57 Page table kept in main memory Page Table Base Register (PTBR) Page Table Length 2 memory accesses per data access  Solution? Page 0 Page 1 0 1 Logical Memory Page Table PTBR Physical Memory 37 3 7

58 Associative Registers 58 pd Logical Address Page number frame number Associative Registers fd f miss hit Physical Memory

59 Associative Register Performance 59 Hit Ratio – percentage of times that a page number is found in associative registers Hit time = reg. time + mem. time Miss time = reg. time + mem. time * 2 Effective access time = hit ratio * hit time + miss ratio * miss time Example:  80% hit ratio, reg. time = 20 ns, mem. time = 100 ns

60 Protection 60 Protection bits with each frame Store in page table Expand to more permissions Page 0 0 1 2 3 Page Table Logical Memory Physical Memory 0 1 2 3 Page 2 Page 1 1 0 3 0 Page 2 Page 0 v v v i

61 Large Address Spaces 61 Typical logical address spaces:  4 GBytes => 2 32 address bits (4-byte address) Typical page size:  4 Kbytes = 2 12 bits Page table may have:  2 32 / 2 12 = 2 20 = 1million entries Each entry 3 bytes => 3MB per process! Do not want that all in RAM Solution? Page the page table  Multilevel paging

62 Multilevel Paging 62 Outer Page Table Logical Memory Inner Page Tables... page number p1 page offset d 10 12 p2 10 Page 0 Page 2 Page 1 …

63 Inverted Page Table 63 pdpid p Search di Physical Memory

64 View of Memory 64 Paging lost users’ view of memory

65 Segmentation 65 Logical address: Segment table - maps two-dimensional user defined address into one-dimensional physical address  base - starting physical location  limit - length of segment Hardware support  Segment Table Base Register  Segment Table Length Register

66 Segmentation 66 segment 0 segment 1 segment 2 segment 3 segment 4 physical memory segment # + virtual address <? raise protection fault no yes offset baselimit Segment register table Index to segment register table Physical Address

67 Segmentation 67 Protection. With each entry in segment table associate:  validation bit = 0  illegal segment  read/write/execute privileges Protection bits associated with segments Since segments vary in length, memory management becomes a dynamic storage-allocation problem Still have external fragmentation of memory


Download ppt "WEEK 5 LINKING AND LOADING MEMORY MANAGEMENT PAGING AND SEGMENTATION Operating Systems CS3013 / CS502."

Similar presentations


Ads by Google