Docsity

Memory Management in Operating Systems: Lecture Notes by Shivakant Mishra - Prof. Shivakan, Study notes of Operating Systems

Partial preview of the text

Download Memory Management in Operating Systems: Lecture Notes by Shivakant Mishra - Prof. Shivakan and more Study notes Operating Systems in PDF only on Docsity!

CSCI 3753

Operating Systems

Memory Management

Lecture Notes By Shivakant Mishra

Computer Science, CU-Boulder

Last Update: 03/09/

Introduction

•^

Memory is an important shared resource in acomputing systems^ – Shared among all processes^ – Used by processes to store their state (code, heap,

stack)

– Used for execution of processes (CPU registers, …) – Can be used for interprocess communication and

synchronization

– Store information that can outlive the lifetime of a

process

The External View of the Memory Manager

Application ApplicationProgramProgram

VMQuery()

VirtualFree()

VirtualLock()

ZeroMemory()

VirtualAlloc()

sbrk() exec()

getrlimit() shmalloc() System Calls

File Mgr

Device Mgr

Memory Mgr

Process Mgr

UNIX

File Mgr

Device Mgr

Memory Mgr

Process Mgr

Windows

Hardware

Memory manager functions

•^

Requirements^ – Minimize executable memory access time^ – Maximize executable memory size^ – Executable memory must be cost-effective

•^

Today’s memory manager:^ – Allocates primary memory to processes^ – Maps process address space to primary memory^ – Minimizes access time using cost-effective memory

configuration

– May use static or dynamic techniques

The Basic Memory Hierarchy^ CPU Registers

Primary Memory

(Executable Memory)

e.g. RAM

Secondary Memory

e.g. Disk or Tape

More Frequently

Used Information

Less Frequently

Used Information

Contemporary Memory Hierarchy &

Dynamic Loading

CPU Registers “Main” Memory

Rotating Magnetic Memory

Optical Memory

Sequentially Accessed Memory

L1 Cache MemoryL2 Cache Memory

Primary^ Secondary

(Executable)

Larger storage

Faster access

Address Space vs Primary Memory

Hardware Primary Memory

Process Address Space

Mapped to objectother than memory

Creating an Executable Program • Source code

• Stored in secondary storage

•^

Compile: Translate elements^ • External references (e.g. library calls) are unresolved^ • Relocatable object code

•^

Link: Combine elements^ • Resolve external references^ • Addresses: Relative (starting at address 0)^ • Loadable object module: stored in secondary storage

•^

Load:^ • Allocate primary memory^ • Adjust addresses in address space^ • Copy address space from secondary to primary memory

A Sample Code Segment

... static int gVar; ... int proc_a(int arg){

... gVar = 7; put_record(gVar); ...

The Relocatable Object module

Code Segment Relative Address

Generated Code

entry

proc_a

load

R

store

R1,

push

call

‘put_record’

External

reference

table

‘put_record’

External definition table

‘proc_a’ 0008

(symbol table)

(last location in the code segment)

Data Segment Relative Address

Generated variable space

[Space for gVar

variable]

(last location in the data segment)

The Program Loaded at Location 4000

Relative Address

Generated Code

(Other process’s programs)

(Other modules)

entry

proc_a

[Space for gVar

variable]

load

R

store

R1,

push

call

(End of proc_a)

(Other modules)

entry

put_record

(optional symbol table)

(last location in the code segment)

(first location in

the data segment)

[Space for gVar

variable]

(Other process’s programs)

Memory Allocation

• Loader

– OS needs to allocate memory to process

• Two approaches

– Fixed partition

• Memory is divided into N fixed size regions

– Variable partition

• Number, location and size of memory regions vary

dynamically, as processes come and go.

-^

Two problems with fixed partitions –^

Internal fragmentation: Memory in a regionthat a process does not need (

r-x

) is wasted.

-^

Memory requirements of a process may growover time (dynamic memory). What if thememory requirements exceed the allocatedregion size?

OperatingSystemRegion 0Region 1Region 2^ Region 3

N

N

N

N

pi

pi

needs n

unitsi

ni