Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP755 Advanced Operating Systems

Similar presentations


Presentation on theme: "COMP755 Advanced Operating Systems"— Presentation transcript:

1 COMP755 Advanced Operating Systems
Memory Management COMP755 Advanced Operating Systems COMP650 Advanced Operating Systems

2 Purpose of Memory Manager
Find a place in RAM for programs and data. OS Memory Manager allocates RAM to programs and OS tasks and data. User level memory management is done to allocate memory for new objects. COMP650 Advanced Operating Systems

3 Logical Address Formation
Compiler data and instruction segments addresses relative to the start of function stack addresses relative to stack pointer Linker external references resolved all object file addresses are adjusted. addresses relative to start of the program COMP650 Advanced Operating Systems

4 Program Memory Organization
Heap Stack Global data Program instructions COMP650 Advanced Operating Systems

5 Logical to Physical Translation
Most programs are created with zero as the lowest address in the program. All programs cannot be loaded at physical RAM address zero. Thus the program addresses have to be translated into hardware RAM addresses. COMP650 Advanced Operating Systems

6 Simple Relocation and Protection
For fixed and dynamic partitioning, the hardware can be designed with a base address register and a limit register. Every memory access is checked to ensure the address is not greater than the limit register value. If it is greater, an addressing interrupt is generated. The logical program address is added to base address value to get the RAM address COMP650 Advanced Operating Systems

7 Address Computation Address Limit Address Error Interrupt
Program Address Physical Address Base Address COMP650 Advanced Operating Systems

8 Putting Programs in Memory
Base Address Programs are located in any convenient contiguous part of memory. When a program is to execute, the base register is loaded with the start address of the program. The program effective address from each memory reference is added to the value in the base register to create the physical memory location. COMP375

9 Base Address with Limits
Putting Programs in Memory Base Address with Limits Base Address Register 45000 123 45000 Program address 123 is located in RAM at the real physical address 45123 55000 COMP375

10 Putting Programs in Memory
Limit Register The limit register contains the highest address of the program. The memory system checks each memory reference to make sure it is not greater than the value in the limit register. An addressing exception interrupt occurs if the address is too large. COMP375

11 Base Address Translation
Putting Programs in Memory Base Address Translation Base Address Register 45000 12345 45000 Limit Register 10000 55000 Program address is too large COMP375

12 Fixed Partitioning Memory is divided into fixed size areas.
Programs are loaded into these partitions. Often there were different sized partitions and partitions for different classes of jobs. Ancient operating systems such as OS/MFT or IBM DOS/VSE (for mainframes, not the PC) use fixed partitioning. from IBM System/360 Operating System Introduction COMP650 Advanced Operating Systems

13 Fixed Partitions Fixed partitions are easy to implement.
A program fits in a partition or it doesn’t. Internal fragmentation is high. No external fragmentation. Unable to run large programs when RAM is available. OS 2 MB 2 MB 4MB COMP650 Advanced Operating Systems

14 Dynamic Partitioning Memory is allocated in varying sized blocks based on the size of the request. Memory is allocated when programs are loaded and released when the program terminates. Possibly high external fragmentation. COMP650 Advanced Operating Systems

15 Dynamic Partition Allocation Algorithms
First Fit Best Fit Worst Fit Next Fit Last Fit Buddy system COMP650 Advanced Operating Systems

16 Allocation Example Assume there is 1600 units of memory to allocate.
Memory is allocated in 100 unit chunks. Programs W, X, Y and Z are initially loaded in memory as follows. COMP650 Advanced Operating Systems

17 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

18 Allocation Example time program size action 1 A 200 allocate 2 B 300 4
100 3 Y release 5 D 6 7 E 8 F COMP650 Advanced Operating Systems

19 First Fit Allocate the memory requests in the previous table in the specified order. COMP650 Advanced Operating Systems

20 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

21 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

22 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

23 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 C 100 A 000 COMP650 Advanced Operating Systems

24 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 400 300 200 C 100 A 000 COMP650 Advanced Operating Systems

25 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 400 300 D 200 C 100 A 000 COMP650 Advanced Operating Systems

26 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 400 300 D 200 C 100 000 COMP650 Advanced Operating Systems

27 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 E 500 400 300 D 200 C 100 000 COMP650 Advanced Operating Systems

28 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 E 500 400 300 D 200 C 100 000 F COMP650 Advanced Operating Systems

29 Example Allocation - Best Fit
Allocate the same requests using the Best Fit algorithm. COMP650 Advanced Operating Systems

30 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

31 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 A 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

32 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 A 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

33 1500 1400 W 1300 B 1200 1100 1000 Z 900 C 800 X 700 A 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

34 1500 1400 W 1300 B 1200 1100 1000 Z 900 C 800 X 700 A 600 500 400 300 200 100 000 COMP650 Advanced Operating Systems

35 1500 D 1400 W 1300 B 1200 1100 1000 Z 900 C 800 X 700 A 600 500 400 300 200 100 000 COMP650 Advanced Operating Systems

36 1500 D 1400 W 1300 B 1200 1100 1000 Z 900 C 800 X 700 600 500 400 300 200 100 000 COMP650 Advanced Operating Systems

37 1500 D 1400 W 1300 B 1200 1100 1000 Z 900 C 800 X 700 600 500 400 300 200 E 100 000 COMP650 Advanced Operating Systems

38 1500 D 1400 W 1300 B 1200 1100 1000 Z 900 C 800 X 700 600 500 400 300 F 200 E 100 000 COMP650 Advanced Operating Systems

39 Example Allocation - Next Fit
Allocate the same requests using the Next Fit algorithm. We need to keep track of the last allocation location. COMP650 Advanced Operating Systems

40 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

41 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

42 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

43 1500 C 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

44 1500 C 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 400 300 200 100 A 000 COMP650 Advanced Operating Systems

45 1500 C 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 400 300 200 D 100 A 000 COMP650 Advanced Operating Systems

46 1500 C 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 400 300 200 D 100 000 COMP650 Advanced Operating Systems

47 1500 C 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 E 400 300 200 D 100 000 COMP650 Advanced Operating Systems

48 1500 C 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 F 500 E 400 300 200 D 100 000 COMP650 Advanced Operating Systems

49 Example Allocation - Worst Fit
Allocate the same memory using the Worst Fit algorithm COMP650 Advanced Operating Systems

50 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

51 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

52 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 A 000 COMP650 Advanced Operating Systems

53 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 C 100 A 000 COMP650 Advanced Operating Systems

54 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 400 300 200 C 100 A 000 COMP650 Advanced Operating Systems

55 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 400 300 D 200 C 100 A 000 COMP650 Advanced Operating Systems

56 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 500 400 300 D 200 C 100 000 COMP650 Advanced Operating Systems

57 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 E 500 400 300 D 200 C 100 000 COMP650 Advanced Operating Systems

58 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 E 500 400 300 D 200 C 100 000 F COMP650 Advanced Operating Systems

59 Example Allocation Buddy System
Allocate the same memory requests using the Buddy System Note that the memory is divided by the powers of two. COMP650 Advanced Operating Systems

60 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

61 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 A 600 500 Y 400 300 200 100 000 COMP650 Advanced Operating Systems

62 1500 1400 W 1300 1200 1100 1000 Z 900 800 X 700 A 600 500 Y 400 300 200 B 100 000 COMP650 Advanced Operating Systems

63 1500 1400 W 1300 1200 1100 1000 Z 900 C 800 X 700 A 600 500 Y 400 300 200 B 100 000 COMP650 Advanced Operating Systems

64 1500 1400 W 1300 1200 1100 1000 Z 900 C 800 X 700 A 600 500 400 300 200 B 100 000 COMP650 Advanced Operating Systems

65 1500 D 1400 W 1300 1200 1100 1000 Z 900 C 800 X 700 A 600 500 400 300 200 B 100 000 COMP650 Advanced Operating Systems

66 1500 D 1400 W 1300 1200 1100 1000 Z 900 C 800 X 700 600 500 400 300 200 B 100 000 COMP650 Advanced Operating Systems

67 1500 D 1400 W 1300 1200 1100 1000 Z 900 C 800 X 700 600 E 500 400 300 200 B 100 000 COMP650 Advanced Operating Systems

68 1500 D 1400 W 1300 1200 1100 F 1000 Z 900 C 800 X 700 600 E 500 400 300 200 B 100 000 COMP650 Advanced Operating Systems

69 Algorithm Commonality
Sort the list of free space by: Memory address (First Fit) Increasing size (Best Fit) Decreasing size (Worst Fit) Increasing time since last use (Last Fit) The free list must be efficient for release as well as allocation. COMP650 Advanced Operating Systems

70 Microsoft Windows and Linux use
First fit Next fit Best fit None of the above COMP650 Advanced Operating Systems

71 Small Systems Small simple computers, such as embedded devices and wireless sensors, still use simple memory allocation. Handheld devices without mass storage use these memory allocation algorithms Most modern computers use virtual memory. COMP650 Advanced Operating Systems

72 Virtual Memory Fragmentation Even the best dynamic memory allocation scheme causes external fragmentation. As the amount of RAM in a system increases, the difficulty in allocating memory decreases. As the average size of programs increases the difficulty in allocating memory increases. COMP755

73 Moving Programs with Dynamic Memory Allocation
Virtual Memory Moving Programs with Dynamic Memory Allocation If memory becomes fragmented, it can be possible to move programs to reduce fragmentation. Programs must be suspended to move them. It takes CPU time to move a program in memory. COMP755

74 Virtual Memory 1500 1400 W 1300 B 1200 1100 1000 Z 900 800 X 700 600 E 500 400 300 D 200 C 100 000 F COMP755

75 Virtual Memory 1500 1400 1300 1200 1100 W 1000 B 900 800 700 Z 600 X 500 E 400 300 200 D 100 C 000 F COMP755

76 Dynamic Memory Challenges
Virtual Memory Dynamic Memory Challenges Some memory areas may be allocated for a long time, possibly fragmenting memory. Certain programs are hard to move. Real time programs that may need to run immediately. Shared memory requires suspended all programs using it. COMP755

77 Virtual Memory Paged Memory RAM is divided into fixed sized pages or frames. Pages are usually between ½K and 8K (always a power of 2). Programs are divided into fixed sized pages and stored in the RAM page frames Program pages can be stored anywhere in any order. A page table is used to map program addresses to physical addresses. COMP755

78 Pages in RAM RAM Page Table 1 2 3 4 1 4 3 C 7 1 2 3 4 5 6 7 8 9 A B C
Virtual Memory Pages in RAM RAM 1 2 3 4 5 6 7 8 9 A B C D E F Page Table 1 2 3 4 1 4 3 C 7 COMP755

79 Program Address Translation
Virtual Memory Program Address Translation COMP755

80 Virtual Memory Sizes The length of a virtual address is log2(size of addressable memory) The length of the offset portion of the address is the log2(size of a page) The page number is all bits left of the offset. The number of page table entries is the programs size / page size. For 32 bit addresses with 4K pages (12 bits) the page number is 20 bits. A 1 MB program will have 256 page table entries. COMP755

81 Combining Paging and Segmentation
Virtual Memory Combining Paging and Segmentation An addressing system can use both paging and segmentation. Large segments can be composed of pages. Allows simple memory allocation and logical program division. Segment Page Offset COMP755

82 Intel Segment Addresses
Virtual Memory Intel Segment Addresses 10 bits bits bits Segment Page Offset Segment table Page table Physical Addr COMP755

83 Sharing Memory Often it is advantageous to share memory.
Virtual Memory Sharing Memory Often it is advantageous to share memory. Multiple users running the same program. Programs communicating with shared data The segment tables of different users can point to the same shared memory location. Depending upon the program address used, a user might access private segments or shared segments. COMP755

84 OS and Hardware Virtual memory implementation requires action from the hardware and OS. User programs do not have to do anything. A large number of page faults can degrade program performance. Programs with localized memory access will perform better than programs using a wide range of addresses.

85 Virtual Memory Challenge
The big decisions for the OS in handling virtual memory are: How many pages should each program have? What pages should be replaced when memory fills?

86 Resident Set Management
More on Virtual Memory Resident Set Management Should all programs have an equal number of pages? If there are N programs running, should each program have 1/N of the total RAM? When a program gets a page fault and needs another page, should the new page replace only a page in that user’s program or can it replace any page from any program? COMP755

87 Resident Set Management
More on Virtual Memory Resident Set Management Fixed or variable number of pages per process. Pages replaced globally or within a process. Working Set Algorithm looks at the number of pages recently accessed to determine how many pages a program should have. COMP755

88 More on Virtual Memory Allocating Pages If free pages are available, it is usually advantageous to give more pages to a program. When few or no free pages are available, giving pages to one program means taking pages from another. Taking pages from another program will likely increase that program’s page fault rate. The goal is to reduce the total number of page faults in the system. COMP755

89 More on Virtual Memory COMP755

90 Which program should have more pages in RAM?
Virtual Memory Which program should have more pages in RAM? Program A is a large program in a tight loop. Program B is smaller multiplying matrices. A B COMP755

91 Page Fault Frequency Algorithm
More on Virtual Memory Page Fault Frequency Algorithm If a program is generating more page faults than some limit, give the program more pages in RAM. If the OS needs to take pages from a program, remove them from a program that is generating less than some minimum number of page faults. COMP755

92 More on Virtual Memory COMP755

93 More on Virtual Memory Working Set Strategy For each program, the OS keeps resident all pages that have been accessed in the last Δ time units. Increasing the parameter Δ increases the number of pages. Usually implemented so that at a page fault the system keeps those pages that have been accesses in Δ time units. COMP755

94 More on Virtual Memory Thrashing A system is said to be thrashing when it is spending so much of its resources maintaining the virtual memory that little actual work gets accomplished. As the number of running programs increases, each program gets fewer pages in RAM. With fewer pages in RAM, each program creates more page faults. COMP755

95 Replacement Policy What page should be overwritten?
More on Virtual Memory Replacement Policy What page should be overwritten? Optimal - requires looking forward in time. Useful for evaluating other algorithms. LRU - works well. FIFO - denies the concept of locality Clock - used by Mac OS. Page Buffering - System keeps a list of available and released pages. COMP755

96 Multiprogramming Level
More on Virtual Memory Multiprogramming Level How many programs can run simultaneously? Swap out idle programs COMP755

97 Locked Pages Some pages cannot be paged out.
More on Virtual Memory Locked Pages Some pages cannot be paged out. Certain parts of the OS must always be in RAM (If the memory manager gets paged out, what will page it back in.) If a user is doing I/O to or from a page, that page must stay in RAM until the I/O is complete. The I/O controller won’t know that another program is using the RAM. COMP755


Download ppt "COMP755 Advanced Operating Systems"

Similar presentations


Ads by Google