memory management - gadjah mada...

44
Sunu Wibirama Memory Management 9th Week Operating System - TIF 206 Department of Electrical Engineering and Information Technology Faculty of Engineering Universitas Gadjah Mada, Indonesia Copyright 2011

Upload: others

Post on 16-Mar-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Sunu Wibirama

Memory Management9th Week

Operating System - TIF 206Department of Electrical Engineeringand Information TechnologyFaculty of EngineeringUniversitas Gadjah Mada, Indonesia

Copyright ⓒ 2011

Page 2: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Before we start...Do you really believe that CPU’s clock speed

defines how fast the CPU is?

Page 3: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Steve has his own words...

Page 4: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

THE MEGAHERTZ MYTH

Page 5: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Outlines• Memory management:

• Introduction

• Logical and Physical Addresses

• Swapping

• Paging

• Segmentation

• Memory Sharing

Page 6: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

References

Abraham Silberchatz, Peter Baer Galvin, Greg Gagne, “Operating System Concepts with Java 6th Edition“, Wiley International Publisher : Chapter 9

Abraham Silberchatz, Peter Baer Galvin, Greg Gagne, “Operating System Principles 7th Edition“, Wiley International Publisher : Chapter 8

Google and Youtube (very useful!)

Page 7: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Basic Memory Hierarchy

Page 8: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Type of Memory

Volatile : requires power to maintain the stored information, also known as temporary memory (Random Access Memory / RAM)

Non-volatile : retains the stored information even when not powered (Read-Only Memory, Flash Memory, Magnetic Storage (HDD, etc)

Page 9: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

memory management

OS must fit multiple process in memory

Memory needs to be allocated to ensure a reasonable supply of ready processes so that CPU is never idle

Memory needs to be subdivided to accommodate multiple process

Memory management is an optimization task under constraints

Page 10: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Important terms

Page 11: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Important terms

Relocation of address references:translating memory references to physical address

Page 12: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Important terms

Relocation of address references:translating memory references to physical address

Protection of memory spaces:forbid cross-process references

Page 13: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Important terms

Relocation of address references:translating memory references to physical address

Protection of memory spaces:forbid cross-process references

Sharing of memory spaces:allow several process to access a common memory area

Page 14: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Important terms

Relocation of address references:translating memory references to physical address

Protection of memory spaces:forbid cross-process references

Sharing of memory spaces:allow several process to access a common memory area

Logical organization (of programs):programs are broken up into independent modules

Page 15: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Important terms

Relocation of address references:translating memory references to physical address

Protection of memory spaces:forbid cross-process references

Sharing of memory spaces:allow several process to access a common memory area

Logical organization (of programs):programs are broken up into independent modules

Physical organization (of memory):fit multiple programs and modules in memory area

Page 16: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Memory Address Space• Each process has a separate

memory space

• Two registers provide address protection between processes:

• Base register: smallest legal address space (also called relocation register)

• Limit register: size of the legal range

Page 17: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

HW Address protection

CPU hardware compares every address generated in user mode with the registers

Any attempt to access other process’ memory will be trapped and causes a fatal error

Page 18: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Various Error messages

Blue Screen of Death (Windows)

Kernel Panic(FreeBSD)

Kernel Panic(Mac)

Page 19: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Address bindingAddresses in a source program are generally symbolic- ex.: int count;

A compiler binds these symbolic addresses to relocatable addresses- ex.: 100 bytes from the beginning of this module

The linkage editor or loader will in turn bind the relocatable addresses to absolute addresses- ex: 74014

Each binding is mapping from one address space to another

Page 20: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Binding of instructions & data to memory

Address binding of instructions and data to memory addresses can happen at three different stages:

1. Compile time: if memory location known, we can generate absolute code, but we must recompile the code if starting location change. 2. Load Time: we can generate relocatable code if memory location is not known at compile time; need only reload if starting address changes.3. Execution Time: binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address mapping

Page 21: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Dynamic loading v.s. Dynamic linking

Dynamic Loading:

Routine in a program is not loaded until it is called.

Can handle large amount of code

User is fully responsible to maintain the code in order to be loaded dynamically

Dynamic Linking:

Linking is done in execution time

Using ‘stub’, to locate appropriate memory-resident library (shared-library concept)

Requires help from operating system to manage protection of library

Page 22: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Logical v.s. Physical Address Space

Address generated by CPU is referred as logical address

Address seen by the memory is seen as physical address

Compile time and load time methods generate identical logical and physical addresses

Execution time method generates different logical and physical addresses- Logical address referred as virtual address

Page 23: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Memory Management Unit (MMU)

Hardware device that maps virtual to physical address

In MMU scheme, the value in the relocation register (formerly known as base register) is added to every address generated by a user process at the time it is sent to memory

The user program deals with logical addresses, it never sees the real physical address

Page 24: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

swappingA process must be in memory to be executed

However, it can be swapped temporarily out of memory to a backing store, and brought back for continued execution

If address binding is done at compiling or load time, the process cannot be easily moved to a different memory space

If execution time binding is used the process can be swapped into different memory space. Why?

Page 25: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

swappingA process must be in memory to be executed

However, it can be swapped temporarily out of memory to a backing store, and brought back for continued execution

If address binding is done at compiling or load time, the process cannot be easily moved to a different memory space

If execution time binding is used the process can be swapped into different memory space. Why?

Because the physical addresses are computed during execution time

Page 26: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

swapping

Page 27: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

swap-SPACE management

If system runs out of swap space, it may be forced to abort processes or may crash entirely

Overestimation of swap space is important, ex: Linux suggests swap space 2x RAM

Location: inside normal file system (Windows) or in separate disk partition (Unix-based OS).

Swap file in Windows OS

Swap file in Linux OS

Page 28: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

swap-SPACE management

Inside file system: a large file, but inefficient since we need extra time to navigate the directory structure and disk allocation data structure. Moreover, disk is fragmented.

Separate disk partition: now file system or directory structure inside it. Here, swap space is accessed more frequently. The system optimizes the speed for data processing.

When handle large data transfer with swapping mode turned ON, we prefer Unix-based operating system instead of Windows

Page 29: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Your ‘friend’ did it!

Page 30: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Your ‘friend’ did it!

Page 31: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Google uses Linux clusters for its data center.

The widely-used term is parallel computing

Page 32: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without
Page 33: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

paging

Memory management scheme that permits the physical address space of a process to be noncontiguous.

User submit one address, the rest will be handled by hardware. Recently, OS can take care of it

Logical address should be ordered in specific way

Physical memory is broken into fixed-sized blocks called frames.

Logical memory is broken into blocks of the same size called pages.

Page 34: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Advantages of paging

Allow noncontiguous memory allocation for a process

I/O processes are more efficient if the transferred data are larger

User’s view of memory: one single spaceActual physical memory: scattered Easier for programmer!

Page 35: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

hardware of paging

page number! page offset!

p! d!

Page 36: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Example of Paging

Page 37: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

When you code...Have you ever think the order

of where your part of program is stored in memory?

Page 38: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Obviously, I don’t care about the order ofwhere the memory stores the part of my program

Page 39: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

segmentationIn common usage, user doesn’t see memory as a linear array of bytes.

User sees memory as a collection of variable-sized segments, without ordering the the logical address.

Segmentation is a memory-management scheme that supports this user view of memory.

User can define segment number and its offset: <segment-number, offset>

In paging, user only can submit one address and HW / OS will handle the rest

Page 40: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

see the difference?

PAGING SEGMENTATION

Page 41: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Hardware of Segmentation

d = offset to the segment

Page 42: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Memory Allocation in Linux

free -t -m

htop

Page 43: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

Try it by yourself !

Page 44: Memory Management - Gadjah Mada Universityte.ugm.ac.id/~wibirama/tif206/02/week09/09_memory_management.pdf · User sees memory as a collection of variable-sized segments, without

THANK YOU