5 Memory Access in Program Execution The instruction execution cycle: The CPU fetches an instruction from main memory, according to the value of the program counter. The instruction may require other main memory locations to be accessed, for loading or storing contents of registers. The memory unit is responsible for accessing main memory. This unit doesn t know or care about what the data being accessed are actually used for. Zhiyi Huang (Otago) COSC243 Lecture 20 5 / 29

6 Getting a Program into Main Memory Memory management deals with how to get processes into main memory, and the organisation of that memory. This isn t a trivial task. The memory unit accesses es in primary memory. (E.g ) Source code just refers to symbols. (Variables: e.g. count.) (Procedures: e.g. get_next_item.) Symbols need to be converted into es. Technically, we talk of symbols being bound to es. Zhiyi Huang (Otago) COSC243 Lecture 20 6 / 29

7 Address Binding Symbols can be converted into es at several points: Compile time. Compilers can create: absolute code: if it s known where the process will reside in memory. (E.g. MS-DOS.COM programs.) relocatable code: if this is not known. Link time. Programs are often designed as separate modules which are compiled separately, but make reference to each other. These must be linked, so that their variables are correctly bound. Load time. When the process is loaded into main memory. Run time. If a process can be moved during execution from one place in memory to another. Zhiyi Huang (Otago) COSC243 Lecture 20 7 / 29

9 Dynamic Linking and Loading When a source program is compiled, the code for the functions provided by the language (e.g. Java libraries) needs to be incorporated into the object program. In static linking, this is done before the program is executed. In dynamic linking, it is only done at run-time. A stub containing a pointer is included in place of the code for each routine. When the stub is referenced, the pointer is initialised: - if the routine is already loaded, use it; - otherwise load the routine. Advantages of dynamic linking? Saving on memory space. (don t need multiple copies of the same routines). Saving on load time. (only need to load a routine once). Ease of updating libraries (e.g. to fix bugs, change versions). Zhiyi Huang (Otago) COSC243 Lecture 20 9 / 29

10 Logical and Physical Addresses Some terminology: A logical is one referred to in a piece of executable code (e.g. to refer to a variable or a location). It is used in a process space. The CPU executes instructions involving logical es. Logical is also called virtual. A physical is the which is actually sent to the memory unit. In compile-time and load-time binding, logical es are used. At run-time, something in the computer has to map between logical and physical es. Zhiyi Huang (Otago) COSC243 Lecture / 29

11 The Memory-Management Unit The logical space of a process runs from 0 to the memory limit of the process. Physical space runs from 0 to the size of main memory. The memory management unit maps between the two. It takes a logical generated by the CPU, and adds a number N to compute the physical. The number is held in a relocation register. relocation register CPU logical N + physical memory Memory management unit To move a process in memory, just change the relocation register. Zhiyi Huang (Otago) COSC243 Lecture / 29

12 Run-Time Binding and Multitasking Run-time binding is useful for saving on memory: Only loading modules if they re needed. Allowing several processes to share one copy of a module. There are also many benefits that relate to a multitasking scenario. Until now, our multitasking scenario has involved switching between processes resident in main memory. But note: while a process is waiting for the CPU, there s no reason for it to be in main memory. Zhiyi Huang (Otago) COSC243 Lecture / 29

13 Swapping Terminology: processes are swapped in and out of main memory to/from a backing store. O/S swap out swap in P1 P2 main memory backing store Swapping is done by the memory manager, which is a module of the kernel. The memory manager needs to work in synch with the CPU scheduler. Zhiyi Huang (Otago) COSC243 Lecture / 29

14 Memory Allocation and Protection Lecture on data sharing: processes are not allowed to access each other s space (except under special circumstances). Q: How is this rule implemented? A: Each process is associated with a relocation register and also a limit register. logical limit register < yes relocation register + physical CPU no TRAP: ing error main memory Zhiyi Huang (Otago) COSC243 Lecture / 29

15 Allocating Memory for Several Processes How to organise memory allocation for many processes? A simple method: divide memory into a number of fixed-size partitions. Problems? Processes can be different sizes. Presumably the partitions would have to be as big as the biggest process, which would make for huge redundancy. A more complex method: Each free region of memory is termed a hole. When a process arrives, we find a hole big enough to put it into. If the hole is bigger than the process, we keep a record of the new (smaller) hole. Zhiyi Huang (Otago) COSC243 Lecture / 29

16 Algorithms for Choosing a Hole There are different methods First-fit: allocate the first hole you find that s big enough. Best-fit: find the hole that leaves the smallest leftover hole. Worst-fit: find the hole that leaves the biggest leftover hole. Advantages/disadvantages of each? First-fit: quickest (because you don t have to search every hole), but you mightn t allocate the optimal-sized hole. Best-fit: you have to search the whole set of holes each time. A disadvantage is that you end up creating very small holes. Worst-fit: again, you have to do an exhaustive search. You minimise the problem of creating small holes, but you might not use the large holes as effectively as you could; you might put small processes into them which mean that there are no holes large enough for a subsequent large process. Zhiyi Huang (Otago) COSC243 Lecture / 29

17 External Fragmentation If memory is broken into many holes, there might be enough memory in total to fit a particular process, but not all in one place. This situation is called external fragmentation. P1 P0 holes process memory Worst-case external fragmentation: a hole between every pair of processes. Zhiyi Huang (Otago) COSC243 Lecture / 29

18 Internal Fragmentation The operating system has to keep a table of all the currently available holes in memory. Hole Memory region H 1 H 2... There s a large overhead in keeping track of lots of very small holes. P1 new hole? process hole To avoid creating tiny holes, we sometimes allocate a process more memory than it needs. Unused memory within a process is called internal fragmentation. Zhiyi Huang (Otago) COSC243 Lecture / 29

19 Noncontiguous Memory Allocation The allocation methods described so far have all required the memory allocated for a process to be contiguous. Obviously, noncontiguous allocation has advantages. One way of providing noncontiguous allocation is through paging. In paging, a process logical space is broken into fixed-size units called pages, and main memory is broken into units of the same size, called frames. PAGES FRAMES CPU logical memory physical memory Zhiyi Huang (Otago) COSC243 Lecture / 29

20 Paging In a paged memory allocation scheme, the logical es generated by the CPU are broken by special hardware into two components: a page number (identifying a page), and an offset (an within that page). CPU logical p off f page table page P0 P1 P2 P3 frame F1 F4 F3 F6 physical off F0 F1 F2 F3 F4 F5 F6 memory A page table indicates for each page which frame it s stored in. Paging allows a process to be stored noncontigously in memory. Paging is a way of implementing run-time binding. Zhiyi Huang (Otago) COSC243 Lecture / 29

21 An example Consider the following scenario: Pages (and frames) are 10 bytes in size. There are 70 bytes of main memory in total. Process P1 has a logical space of 40 bytes, and a page table which looks like this: page frame Q: What physical es would be accessed by the following stream of logical es? 0, 11, 25, 31 10, 41, 35, 61 Zhiyi Huang (Otago) COSC243 Lecture / 29

26 Paging: some questions What happens to external fragmentation in a paging scheme? A: this scheme eliminates it. What happens to internal fragmentation? A: each process has to be allocated a discrete number of pages. So there s going to be internal fragmentation when a process doesn t fit exactly. It ll be on average half a page per process, assuming process size is independent of page size. So how big should pages be? A: small, but not so small that the disk I/O and housekeeping become too great. How many page tables do we need? A: one for each process. How does the O/S keep track of a process page table? A: It keeps a copy in its PCB. (Remember I said that the PCB contains memory management information...) Zhiyi Huang (Otago) COSC243 Lecture / 29

27 Implementing Paging The fastest/most expensive way of implementing pages is to store the page table in a set of special-purpose registers. But this isn t feasible if the page table is big (as it normally is). The alternative is to store the page table in main memory. But this could slow things down hugely: to access an in memory, we now need two or more memory accesses. The solution is to keep a cache of page table entries that have been used recently, in a special set of parallel-access registers called associative registers, aka. Translation Look-aside Buffer (TLB). Zhiyi Huang (Otago) COSC243 Lecture / 29

28 Segmentation In the paging scheme above, it s the hardware that partitions a CPU-generated into pages. But there are some reasons for allowing a process to partition its own space. subroutine SQRT stack main program symbol table logical space A segmentation allocation scheme supports this view of memory. Zhiyi Huang (Otago) COSC243 Lecture / 29

29 Implementing Segmentation In a segmentation scheme, a logical is a pair of < segment_number, offset >. These are mapped onto physical es by the segmentation hardware, using a segment table. Each entry in the table has a segment base (the starting physical ) and a segment limit (the size of the segment). Segments can be stored noncontiguously. Like the page table, the segment table can either be put into fast registers or main memory. Zhiyi Huang (Otago) COSC243 Lecture / 29

30 Advantages / disadvantages of segmentation? Advantages: makes protection and sharing easy to deal with (as items in a segment have the same protection status). You just need one protection bit per segment. it just makes it easier to write machine code. (And thus to write compilers.) Disadvantages: external fragmentation is back. But with less of a vengeance. NB: clever schemes (e.g. MULTICS) use paging and segmentation. The idea is that a memory reference is a segment base plus segment offset, where the segment offset is itself broken into two components: a page number and a page offset. There is a question about the MULTICS scheme in Tutorial 11B Zhiyi Huang (Otago) COSC243 Lecture / 29

Chapter 9: Memory Management Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging 9.1 Background Program must be brought into memory and placed within a process for it

Background Memory Management Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage CPU can access directly Register access

OS - Memory Management Memory management is the functionality of an operating system which handles or manages primary memory. Memory management keeps track of each and every memory location either it is

Memory Management Goals of Memory Management Allocate available memory efficiently to multiple processes Main functions Allocate memory to processes when needed Keep track of what memory is used and what

OPERATING SYSTEM - MEMORY MANAGEMENT http://www.tutorialspoint.com/operating_system/os_memory_management.htm Copyright tutorialspoint.com Memory management is the functionality of an operating system which

Lecture 4: Memory Management Background Program must be brought into memory and placed within a process for it to be run Input queue collection of processes on the disk that are waiting to be brought into

CHAPTER 8 Although many systems are demand paged (discussed in Chapter 12), there are still many that are not, and in many cases the simpler memory-management strategies may be better, especially for small

Basic Memory Management Basic Memory Management CS 256/456 Dept. of Computer Science, University of Rochester Program must be brought into memory and placed within a process for it to be run. Mono-ming

4 Main Memory Management 4.1 Introduction A large part of the OS s responsibility is organising main memory for processes. GOAL pack as many processes into memory as possible so that the processor will

Virtual and Physical Addresses Physical addresses are provided by the hardware: one physical address space per machine; valid addresses are usually between 0 and some machinespecific maximum; not all addresses

Contiguous Allocation Main memory usually into two partitions: Resident operating system, usually held in low memory with interrupt vector. User processes then held in high memory. Memory Management --

The Deadlock Problem Deadlocks A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example System has 2 tape drives. P 1 and P 2 each

CS 31: Intro to Systems Virtual Memory Kevin Webb Swarthmore College April 7, 2015 Reading Quiz Memory Abstraction goal: make every process think it has the same memory layout. MUCH simpler for compiler

Chapter 2 Memory Management: Early Systems Understanding Operating Systems, Fourth Edition Memory Management: Early Systems Memory is second only to processes in importance (and in intensity) with which

OPERATING SYSTEMS MEMORY MANAGEMENT Jerry Breecher 8: Memory Management 1 OPERATING SYSTEM Memory Management What Is In This Chapter? Just as processes share the CPU, they also share physical memory. This

Memory Management Prof. P.C.P. Bhatt P.C.P Bhat OS/M4/V1/2004 1 What is a Von-Neumann Architecture? Von Neumann computing requires a program to reside in main memory to run. Motivation The main motivation

Memory If we define memory as a place where data is stored there are many levels of memory: Processor registers Primary (or main) memory RAM Secondary memory slower and more permanent disks Tertiary memory

Chapter 8: Main Memory 1. Background Typical instruction/execution cycle Fetches an instruction from memory Decode Operand fetched from memory Result stored back in memory Memory units see ONLY a stream

Module 4: Memory Management The von Neumann principle for the design and operation of computers requires that a program has to be primary memory resident to execute. Also, a user requires to revisit his

True / False Questions: Chapter 7 Memory Management 1. T / F In a multiprogramming system, main memory is divided into multiple sections: one for the operating system (resident monitor, kernel) and one

An Introduction to Memory Management: appendix Copyright : University of Illinois CS 241 Staff 1 Memory partitioning Nowadays memory management is based on a sophisticated technique known as (paged) virtual

Memory Management Is the task carried out by the OS and hardware to accommodate multiple processes in main memory If only a few processes can be kept in main memory, then much of the time all processes

Virtual Memory COMP375 Computer Architecture and Organization You never know when you're making a memory. Rickie Lee Jones Design Project The project is due 1:00pm (start of class) on Monday, October 19,

OPERATING SYSTEMS INTRODUCTION TO MEMORY MANAGEMENT 1 8 Memory Management In a multiprogramming system, in order to share the processor, a number of processes must be kept in. Memory management is achieved

Memory Management 1 Memory Management Multitasking without memory management is like having a party in a closet. Charles Petzold. Programming Windows 3.1 Programs expand to fill the memory that holds them.

Lecture Outline Operating Systems Objectives Describe the functions and layers of an operating system List the resources allocated by the operating system and describe the allocation process Explain how

CHAPTER 8 : VIRTUAL MEMORY Introduction In simple paging and segmentation, we realised that we can : Load program pages or segments into frames that may not necessarily be contigous. Make memory address