Operating Systems (2INC0) 2017/18

Similar documents
CS420: Operating Systems

CS370 Operating Systems

Main Memory (Part I)

Chapter 8: Memory Management. Operating System Concepts with Java 8 th Edition

Operating Systems (2INC0) 2017/18

Memory Management (1) Memory Management

Memory Management (1) Memory Management. CPU vs. memory. No.8. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Background. Contiguous Memory Allocation

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

12: Memory Management

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

The Memory Management Unit. Operating Systems. Autumn CS4023

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

Chapter 8: Main Memory

CS307: Operating Systems

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Chapter 8: Memory-Management Strategies

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Operating systems. Part 1. Module 11 Main memory introduction. Tami Sorgente 1

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

CSE325 Principles of Operating Systems. Memory. David P. Duggan. March 6, 2010

6 - Main Memory EECE 315 (101) ECE UBC 2013 W2

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Chapter 8: Main Memory

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 8: Memory- Management Strategies

Chapter 8: Memory Management. Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

Part Three - Memory Management. Chapter 8: Memory-Management Strategies

8.1 Background. Part Four - Memory Management. Chapter 8: Memory-Management Management Strategies. Chapter 8: Memory Management

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

Chapter 8: Main Memory

UNIT III MEMORY MANAGEMENT

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

Preview. Memory Management

CSE 421/521 - Operating Systems Fall Lecture - XII Main Memory Management. Tevfik Koşar. University at Buffalo. October 18 th, 2012.

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

Lecture 7. Memory Management

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapters 9 & 10: Memory Management and Virtual Memory

Memory Management. CSCI 315 Operating Systems Design Department of Computer Science

Chapter 9: Memory Management. Background

Roadmap. Tevfik Koşar. CSC Operating Systems Spring Lecture - XII Main Memory - II. Louisiana State University

Chapter 8: Main Memory

Memory Management. Contents: Memory Management. How to generate code? Background

Chapter 8: Memory Management

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Chapter 9 Real Memory Organization and Management

Chapter 9 Real Memory Organization and Management

CS399 New Beginnings. Jonathan Walpole

CS307 Operating Systems Main Memory

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Memory Management. CSE 2431: Introduction to Operating Systems Reading: , [OSC]

Chapter 8: Memory Management Strategies

Memory Management. Memory Management

Module 8: Memory Management

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Chapter 8: Memory Management

Module 9: Memory Management. Background. Binding of Instructions and Data to Memory

Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation

CS418 Operating Systems

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 8: MEMORY

Chapter 8 Memory Management

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Basic Memory Management

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

CS 3733 Operating Systems:

Chapter 8 Main Memory

I.-C. Lin, Assistant Professor. Textbook: Operating System Principles 7ed CHAPTER 8: MEMORY

CS Operating Systems

CS Operating Systems

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Memory Management. Jo, Heeseung

MEMORY MANAGEMENT. Jo, Heeseung

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

Requirements, Partitioning, paging, and segmentation

Goals of Memory Management

Principles of Operating Systems

Chapter 9 Memory Management

Main Memory. CISC3595, Spring 2015 X. Zhang Fordham University

Main Memory. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & APPS 1

Lecture 8 Memory Management Strategies (chapter 8)

Introduction to Operating Systems

Memory Management. Dr. Yingwu Zhu

MEMORY MANAGEMENT: Real Storage. Unit IV

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

15 Sharing Main Memory Segmentation and Paging

Chapter 7 Memory Management

Last class: Today: Deadlocks. Memory Management

Memory Management. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

Module 8: Memory Management

8: Memory Management

Operating Systems Memory Management. Mathieu Delalandre University of Tours, Tours city, France

3. Memory Management

Process. Memory Management

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Transcription:

Operating Systems (2INC0) 2017/18 Memory Management (09) Dr. Courtesy of Dr. I. Radovanovic, Dr. R. Mak (figures from Bic & Shaw) System Architecture and Networking Group

Agenda Reminder: OS & resources Memory hierarchy Address binding Memory management techniques Examples from early systems 2

Agenda Reminder: OS & resources Memory hierarchy Address binding Memory management techniques Examples from early systems 3

REMINDER: OS as resource manager Resource: Anything that is needed for a process to run Main Memory (MM) Space on a disk CPU OS role: create resource abstractions (hide HW details) e.g. files for disk space, processes for memory use, threads for CPU use. OS role: manage resource sharing space-sharing vs time-sharing provides a mechanism to isolate (protect) resources to manage sharing. 4

REMINDER: Logical OS Organization OS kernel Process and thread manager Memory manager File manager Device manager Hardware Processor(s) Main memory Devices All modules interact to coordinate their activities. Examples: For virtual memory: Process Man. Memory Man. Coordinate scheduling activity with memory allocation For performance improvement: File Man. Memory Man. Information from storage device read prior to request by a thread 5

REMINDER: Process execution Program must be brought (from disk) into MM and be placed within a process address space (needs protection). Addresses can start from zero then (abstract). CPU can access MM and registers (storage types) directly register access in one CPU clock (or less) MM access can take many cycles 6

User/programmer perspective A good memory is one that is non-volatile is private for a program (protected) has infinite capacity has zero access time is simple to use is cheap Such a memory clearly does not exist, but a hierarchical hardware organization combined with virtualization can help! We discuss virtual memory in the next lecture 7

Agenda Reminder: OS & resources Memory hierarchy Address binding Memory management techniques Examples from early systems 8

Analogy: Office storage hierarchy Office storage hierarchy Desktop File folder File cabinet Warehouse Upper-layers: info more frequently used Lower layers: info less frequently used 9

Memory hierarchy CPU register memory fast access (1 clk cycle) but limited in size CPU Registers Primary (executable memory) direct access (a few clk cycles), relatively larger Secondary memory (storage devices) I/O operations required for access, very cheap / very large, data stored for a longer period of time, access speed is slow (3 orders of magnitude slower than CPU register memory) More Frequently Used Information Primary Memory (Main Memory) e.g. RAM Secondary Memory e.g. Disk, Tape, Solid State 10

Contemporary memory hierarchy & dynamic loading CPU Registers Larger storage Secondary Primary (Executable) L1 Cache Memory L2 Cache Memory \ Main Memory Solid State Memory Rotating Magnetic Memory Optical Memory Faster access Sequentially Accessed Memory 11

OS exploiting the hierarchy Place frequently-used info high in the hierarchy infrequently-used info low in the hierarchy Updates are first applied to upper memory. Upward moves are (usually) copy operations Image exists in both higher & lower memories Downward moves are (usually) destructive Usually to save space in upper memory. Destroy image in upper memory Update image in lower memory avoid when the copy in lower memory is still consistent! 12

Memory manager functional requirements Classical memory managers provide Abstraction MM (virtually) appears to be larger than the physical machine mem. Memory as a large array of contiguously addressed bytes Abstract set of logical addresses to reference physical memory Allocation Allocate primary memory to processes as requested Isolation Enable mutually exclusive use of memory by processes Memory Sharing Enable memory to be shared by many processes 13

Memory manager performance requirements Minimum MM access time access is very often Maximum MM size Minimum cost 14

Agenda Reminder: OS & resources Memory hierarchy Address binding Memory management techniques Examples from early systems 15

Simple approach: Bring one executing process to MM Hardware Main OS OS OS Memory P1 P2 FREE FREE Possible update to secondary memory FREE CPU time CPU time P1 P2 time 16

Address space abstraction Instead of a set of physical primary memory addresses, processes are allocated a set of logical primary memory addresses process address space Memory manager has to bind logical primary memory addresses to physical primary memory locations Process Address Space Hardware Main (Primary) Memory 17

Binding: Virtual and physical address Decouple two types of addresses addresses (names) used by a process to identify objects addresses used by memory to identify storage locations Objects in different processes can have the same name (logical address), but they must be stored at different physical locations. address (name) processor Process A Process B 0=Δ 1= 2= object 0=Δ 1= 2= memory 0 1 2 3 4 5 6 Δ Δ 18

Binding at which time? Static, i.e., during translation of program source to executable load module, or upon activation of a process (loading) at programming time at compile time at link time at load time Dynamic, i.e., during execution of the instructions of a running process (as late as possible). 19

Static binding Compile time compiler outputs object modules with isolated addresses Link time linker resolves external object references load module (addresses) stored in a file in secondary memory Instructions Data Stack Load time latest possible static binding scheme Reloc Object code Library code Other objects Relative + symbolic address Compiler Linker L o a d e r Symbolic address Relative address Absolute address Secondary memory Source code Load module Primary memory Process address space 20

Static binding Compile time binding Physical addresses corresponding to program instructions and data are determined at compile time Program must run at these physical memory locations (not relocatable) Link time binding Allows external references to be resolved for larger programs with multiple object modules (resulting in a load module) Load time binding Physical address binding upon loading into MM for execution (still not dynamically relocatable) 21

Program transformation (static binding) f( ): function call 0 0 100 function f 1000 1100 function f int i 20 120 1120 i = f( ) store 20 store 120 compiler linker loader branch f branch 0 store 1120 branch 1000 source module object module load module (secondary memory) load module (main memory) 22

Dynamic binding immediately before executing an instruction translation using an address map physical_address = address_map (logical_address) hardware support for efficiency processor logical address address_map physical address main memory data transfer (read or write) relocation register processor logical address + physical address main memory data transfer (read or write) 23

Program transformation (dynamic binding) 0 0 100 function f 1000 1100 function f int i 20 120 1120 i = f( ) store 20 store 120 compiler linker loader branch f branch 0 store 120 branch 0 source module object module load module (secondary memory) load module (main memory) address_map execution 24

Agenda Reminder: OS & resources Memory hierarchy Address binding Memory management techniques Examples from early systems 25

Early systems Single-programmed configuration Multi-programmed configurations Fixed partitions Dynamic partitions Relocatable dynamic partitions 26

Single-programmed configuration Program loaded in its entirety into MM and allocated as much contiguous MM space as needed If a program does not fit in the memory, it cannot be executed. No multiprogramming support. Hardware needed OS OS OS register to store base address accumulator to track size of program as it is loaded into memory (limit) base + limit can be used to protect kernel space against the process. P1 FREE FREE P2 FREE Once in MM, program stays there until execution ends. 27

Early systems Single-programmed configuration Multi-programmed configurations Fixed partitions Dynamic partitions Relocatable dynamic partitions 28

Fixed partitions Divide the memory available for user programs into fixed partitions 1 process/partition partitions vary in size to accommodate various programs (fixed at OS initialization) Issue: protection of the task s memory space Disadvantage: Entire program is stored in MM until the end of execution Problem: Internal fragmentation Goal: Choosing the right partition size Too small long turnaround time (doesn t fit in any partition in the worst case) Too big wasted memory Partition allocation scheme needed (first-fit example on the next slide) 29

Fixed partition -an example- Process List : P1 30K P2 50K P3 30K P4 25K Note: Process 3 has to wait even though 70K of contiguous free space is available (actually 70K+25K available). Original State 100K Partition 1 After Process Entry Process 1 (30K) wasted memory Partition 1 25K 25K 50K Partition 2 Partition 3 Partition 4 Process 4 (25K) Partition 2 Partition 3 Process 2 (50K) Partition 4 30

Early systems Single-programmed configuration Multi-programmed configurations Fixed partitions Dynamic partitions allocation deallocation Relocatable dynamic partitions 31

Dynamic partitions Partition size not fixed but determined based on the process size. Advantage: no memory waste due to internal fragmentation Disadvantage: Memory waste not solved completely Memory utilization deteriorates as new processes enter and old processes exit the system external fragmentation Programs still have to be stored contiguously. 32

Dynamic partitions -an example- First-fit allocation scheme Job List J1 10k J2 15k J3 20k J4 50k Operating System Job 1(10k) Job 2(15k) Job 3(20k) 10k 20k 35k 55k Job 1 ends Operating System Job 2(15k) Job 3(20k) 10k 20k 35k 55k Job 5 (5k) arrives Operating System Job 5(5k) Job 2(15k) Job 3(20k) 10k 15k 20k 35k 55k Job 4(50k) Job 4 ends Job 6 (30k) arrives Job 6(30k) 85k 105k 105k 105k Initial job allocation (a) After Job 1 and Job 4 have finished (b) After Job 5 and Job 6 have entered (c) 33

Dynamic partitions -an example- First-fit allocation scheme (cont d) Operating System Job 5(5k) 10k 15k 20k Operating System Job 5(5k) 10k 15k 20k Job 3 ends Job 2(15k) 35k 55k Job 7 (10k) arrives Job 2(15k) Job 7(10k) 35k 45k 55k Job 8 has to wait Job 6(30k) 85k 105k After Job 3 has finished (d) Job 8 (30k) arrives Job 6(30k) After Job 7 has entered (e) 85k 105k Note: Job 8 has to wait even though there is 35K of free memory space available 34

Dynamic partition -allocation schemes- Allocation schemes issues: decrease time, increase utilization First-fit Allocate the first partition that is big enough Advantage: Faster in making the allocation Disadvantage: Waste of memory Best-fit Allocate the smallest partition fitting the requirements Advantage: Produces the smallest leftover partition Makes best use of memory when it is possible to allocate a new job Disadvantage: Takes more time Question: How to organize free/busy partition list in both cases? 35

First-fit allocation -an example- P1 10K location organization P2 20K P3 30K* P4 10K Memory Memory Process Process location block size number size Status Fragmentation 10240 30K P1 10K Busy 20K 40960 15K P4 10K Busy 5K 56320 50K P2 20K Busy 30K 107520 20K Free Total Available: 115K Total Used: 40K 36

Best-fit allocation -an example- P1 10K size organization P2 20K P3 30K P4 10K Memory Memory Process Process location block size number size Status Fragmentation 40960 15K P1 10K Busy 5K 107520 20K P2 20K Busy None 10240 30K P3 30K Busy None 56230 50K P4 10K Busy 40K Total Available: 115K Total Used: 70K 37

Exercise The table of free memory spaces is given below. Show how this table will look after allocating a process that occupies 200 spaces using the Best-Fit algorithm. Before request After request Beginning address Memory block size Beginning address Memory block size 4075 105 4075 105 5225 5 5225 5 6785 600 6785 600 7560 20 7560 20 7600 205 *7800 5 10250 4050 10250 4050 15125 230 15125 230 24500 1000 24500 1000 Best-Fit is not the best for dynamic partitions: Excessive memory fragmentation due to smaller free space - sliver 38

Allocation schemes First-Fit: clusters small holes at the top Next-Fit (rotating First-Fit): start each search at the point where the previous search stopped (faster search, slightly worse memory utilization than First-Fit) Best-Fit: worst performance due to excessive fragmentation Worst-Fit: always uses the largest available partition similar performance to first-fit and next-fit Optimized schemes statistics on average process size etc. needed 39

Early systems Single-programmed configuration Multi-programmed configurations Fixed partitions Dynamic partitions allocation deallocation Relocatable dynamic partitions 40

Release of memory space -deallocation- Occurs when process terminates or becomes suspended Deallocation for fixed partitions is simple Memory Manager resets status of memory block to free. Deallocation for dynamic partitions tries to combine free areas of memory whenever possible Is the block adjacent to another free block? If yes, combine the 2 blocks Busy Free Busy Is the block between 2 free blocks? If yes, combine those 3 blocks Free Busy Free Is the block isolated from other free blocks? If yes, make a new table entry Busy Busy Busy 41

Deallocation -an example- Before Deallocation After Deallocation Beginning Memory address block size Status Beginning address Memory block size Status 4075 105 Free 4075 105 Free Deallocate 5225 5 Free 5225 5 Free 6785 600 Free 6785 600 Free 7560 40 Free 7560 245 Free (7600) (200) (Busy) 1 * (null) *7800 5 Free 10250 4050 Free 10250 4050 Free 15125 230 Free 15125 230 Free 24500 1000 Free 24500 1000 Free 42

Early systems Single-programmed configuration Multi-programmed configurations Fixed partitions Dynamic partitions Relocatable dynamic partitions compaction swapping 43

Relocatable dynamic partitions Solves problem of both internal and external fragmentation relocation and compaction avoids memory waste makes insufficient memory problems less frequent OS memory manager relocates programs to gather all empty blocks and compacts them to make 1 big free memory block. 44

Compaction steps Relocate every program in memory so that they re contiguous. Adjust every address, and every reference to an address, within each program to account for programs new locations in memory. Must leave alone all other values within the program (e.g., data values). Question: When should compaction be done? When a certain percent of memory becomes busy (e.g. 75%), When there are processes pending, or After a prescribed amount of time Note: Compaction cannot be done if address binding is static. 45

Compaction -an example- Memory before and after compaction Job 6 (84k) arrives Operating System Job 1 (8k) Job 4 (32k) Job 2 (16k) 10k 18k 30k 62k 92k 108k Operating System Job 1 (8k) Job 4 (32k) Job 2 (16k) Job 5 (48k) 10k 18k 50k 66k 114k Now Job 6 can be accommodated Operating System Job 1 (8k) Job 4 (32k) Job 2 (16k) Job 5 (48k) 10k 18k 50k 66k 114k Job 5 (48k) Job 6 (84k) 156k 210k Initial memory layout (a) 210k Memory layout after compaction (b) 198k 210k Memory layout after Job 6 has been loaded (c) 46

Memory compaction (strategies) Assume that a block of size 10 is needed. 2 3 4 3 2 5 3 2 2 3 2 11 11 5 20 5 5 5 9 9 9 (a) (a) initial state; (c) partial compaction; (b) (c) (b) complete compaction (d) minimal data movement (d) 47

Memory compaction (remarks) Overhead: very costly requires each word of memory to be read from and written into memory May take several seconds Contemporary systems Virtual Memory techniques (paging, segmentation) instead of compaction 48

Relocation Q: How to keep track of where each process is with respect to its original location? A: Special-purpose registers Relocation register Contains value to be added to each address referenced in the program Bounds register Stores highest (lowest) location of memory accessible by each program 49

Relocation register -an example- Job 4 loaded at memory location 30K (30720) It requires memory block of 32K (32768) At memory location 31744 an instruction: LOAD 4, ANSWER Answer is the value 37 Operating System Job 1 (8k) 10k 18k Operating System Job 1 (8k) 10k 18k 30k 19456 Load 4,53248 31744 53248 Load 4,53248 37 Job 4 (32k) 62k 40960 37 Job 2 (16k) Job 4 (32k) 50k 66k Relocation register 00000 Job 2 (16k) 92k Relocation register -12288 Job 5 (48k) ( a ) Go back 30-18=12K ( b ) 50

Swapping When? a program must be loaded into MM and there is not enough room move something else to secondary storage to make room. How? OS selects a process to swap-out and replaces it with another process from the secondary storage swap-out only those parts of process space that are not already on disk typically code is not modified benefits from hardware support that registers (keeps track of) modification Where to (on secondary memory)? either to an arbitrary file (introduces file management overhead) or to a special partition on disk, the swap space more efficient (low-level I/O) 51

Summary Single-user configuration Fixed partitions Dynamic partitions Relocatable Dynamic partitions Comments: All require that the entire program be loaded into memory be stored contiguously remain in memory until a process is completed Severe restrictions to the process size 52