0% found this document useful (0 votes)
51 views

National University of Computer & Emerging Sciences: Introduction To Memory Management

The document discusses memory management in computer systems. It explains that a memory manager is responsible for allocating and de-allocating memory and swapping between main memory and disk. It also discusses requirements like relocation, protection, and sharing. Relocation and protection require that memory addresses be translated to physical addresses using base and limit registers to allow processes to be loaded anywhere in memory without conflicting. Memory is divided into partitions with base and limit registers defining the accessible range for each process.

Uploaded by

Ali Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

National University of Computer & Emerging Sciences: Introduction To Memory Management

The document discusses memory management in computer systems. It explains that a memory manager is responsible for allocating and de-allocating memory and swapping between main memory and disk. It also discusses requirements like relocation, protection, and sharing. Relocation and protection require that memory addresses be translated to physical addresses using base and limit registers to allow processes to be loaded anywhere in memory without conflicting. Memory is divided into partitions with base and limit registers defining the accessible range for each process.

Uploaded by

Ali Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 33 - Introduction to Memory Management

National University of
Computer & Emerging Sciences

Lecture 33
Introduction to Memory Management

Dr. Ehtesham Zahoor

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Background
• Program must be brought (from disk) into memory and
placed within a process for it to be run

• A programmer will like to have memory


– Infinitely large
– Infinitely fast Registers
– Non volatile
• However, we have Memory Hierarchy: Cache

Main Memory
Disk
2
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Memory Manager

• The part of the OS that manages the Memory


Hierarchy
– Which part of memory is in use and which is not in use
– Allocate memory
– De-allocate memory
– Swapping between main memory and disks

3
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Memory Management Requirements

• Speed is not the only issue


– Relocation
– Protection
– Sharing
– Physical memory organisation
– ….

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Requirements: Relocation

• The programmer does not know where the


program will be placed in memory when it
is executed,
– it may be swapped to disk and return to main
memory at a different location (relocated)

• Memory references must be translated to


the actual physical memory address

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Relocation and Protection 800K


Partition 4
700K
• Different jobs will run at different addresses
Partition 3
• Suppose, the first instruction is
– call a procedure at absolute address 10 within the 400K
binary file
• If the process is loaded in 1st partition Partition 2

200K
Partition 1
Operating 100K
System
0

This call will jump inside the Operating system

6
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Relocation and Protection 800K


Partition 4
700K
• Solution:
• If the program is loaded in 1st partition, then Partition 3

– Call 100k + 10
• If the program is loaded in 2nd partition, then 400K

– Call 200k + 10 Partition 2


• So on… 200K
• This is called Relocation problem Partition 1
Operating 100K
System
0

7
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Relocation and Protection 800K


Partition 4
700K
• Solution:
Partition 3
– Add an offset to each address in the program
– The offset depends on the partition, e.g.,
400K
• if the Program is loaded in partition 1, add
100k to every address Partition 2
• if the Program is loaded in partition 2, add 200K
200k to every address Partition 1
Operating 100K
System
0
•A program can still generate an address that jumps
in the OS or other user’s code
•We need to Protect the code
8
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Requirements: Protection

• Processes should not be able to reference


memory locations in another process
without permission
• Impossible to check absolute addresses at
compile time
• Must be checked at run time

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Relocation and Protection


• Solution:
– Base and Limit Registers
• When a process is scheduled
– Base Register is loaded with the starting
address of the Partition
– Limit Register is loaded with the length of
the Partition

• Before referring to memory


– Add the base register contents to
generated memory address
– Also check against the Limit register for
protection

10
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management
Relocation and Protection
Addition and comparison has to be performed on every address
Use a hardware module

Absolute
Physical Address
Interrupt 800K
Partition 4
700K

Limit Register Comparator


Partition 3

400K
Partition 2
Base Register Adder
200K
Partition 1
Memory Management Unit Operating 100K
(MMU) System
Relative
Logical Address
Address 0

11
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Memory Management/Organization

• In good old days, memory management was


trivial as there was only one program
– No OS, eveything is responsibility of user program

• For the later uniprogramming systems, memory was


shared between
– A User program
– Operating system

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Uni-programming
• Run just one program at a time User
• Memory is shared between Program
– A User program
– Operating system
Operating
•The user types a command on the prompt
System
•The OS
Copies the program from the disk to memory
Executes the program
After execution, prompt is available again
•The new program is copied into RAM, overwriting the previous one 13
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Multi-programming systems

• Keep more than one process in memory


– Benefits?

• How to organize memory to achieve this?

• One basic approach is to divide memory


into n (usually unequal partitions)

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Multiprogramming with Fixed Partitions

•If the job size < Partition size, memory is wasted


•When a job arrives
its put into the input queue of

The smallest partition large enough to hold the job

15
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management
Multiprogramming with Fixed Partitions

 Disadvantage of Multiple Queues:


 When a large partition is empty
 And queues for small partition is
full
 Small jobs have to wait, even
though plenty of memory is free
 Alternative: Maintain a single
Queue

16
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Multiprogramming with Fixed Partitions


800K
 Whenever a partition becomes free, Partition 4
a job is selected 700K
 Closest to the front of the queue
Partition 3
 Smaller than the partition size
 Undesirable to waste a large
partition for smaller job 400K

 Search the queue, find the largest Partition 2


job that fits is
200K
 Unfair for smaller jobs Partition 1
 A job may not be skipped more than Operating 100K
System
k times 0

17
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management
Internal Fragmentation
Memory that is internal to a partition but not being used
800K
B 4
Partition
Internal Fragmentation 700K

A
Partition 3

Internal Fragmentation
400K

Partition 2

200K
Partition 1
Operating 100K
System 0
18

FAST, National University of Computer and Emerging Sciences, Islamabad


Lecture 33 - Introduction to Memory Management

Swapping
• If not enough space in memory for all the currently active
processes
• Excess processes must be kept on disk
– Fully  Swapping
– Partially  Virtual Memory
• Example of Swapping
– Round Robin Scheduling
• When the quantum expires
– Swap out the currently running process
– Swap in another process to freed memory space

19
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Swapping

• Another Example:
– Priority based scheduling
• If a higher priority process arrives
– Swap out a lower priority process
– Swap in the higher priority process
• When the higher priority process exits
– Swap in the lower priority process

20
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Swapping

The number, size


C and location of the
partition is decided
B D dynamically.
B
A
C A A
D
Operating
System

D is of Higher Priority, A is of Lower Priority!!!


B exits now, Swap in A 21
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management
Swapping
Its possible to combine all the holes into one big hole
This is called COMPACTION
EXTERNAL
E FRAGMENTATION
Memory External to all
partitions is Fragmented
C Enough total memory exists
to satisfy the request
But is not Contiguous
A
E Swapping has
D created Holes
Operating
System
D exits now
No Space for E !!!
Now E has enough space 22
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Compaction
• Compaction involves CPU overhead
• If addresses are not generated relative to the partition location:
– Then Compaction is not possible
• How much memory should be allocated for a process?
– If all the memory is allocated statically in a program
– Then, OS knows exactly the amount of memory to be allocated:
executable code + variables
• However, if memory is allocated dynamically (say using new)

23
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Memory allocation for Processes


• Problem may occur whenever a process tries to grow
• If a hole is adjacent to the growing process then it can be
allowed to grow
• If another process is adjacent to the growing process, then
– The growing process should be moved to a larger hole
– If a larger hole is not available, then, one or more processes should
be swapped out
– If a process cannot be swapped out (say, there is not enough
space on disk
• The growing process has to wait
• Or should be killed!!!

24
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Memory allocation for Processes


• If most of the processes grow as they run,
• It is better to allocate a little extra memory for a process

Room for growth


B Actual in use

Room for growth


A Actual in use
Operating
System
25
FAST, National University of Computer and Emerging Sciences, Islamabad
Lecture 33 - Introduction to Memory Management

Memory allocation for Processes


• If processes have two growing segments
– Data
• (as heap for dynamically allocated variables)
– Stack B-Stack
Room for growth
B-Data
•The memory can be used by either of the B-Program
two segments
•If it runs out the process either
Has to be moved to a larger
hole
A-Stack
Or swapped out Room for growth
Or Killed A-Data
A-Program
Operating System
26

FAST, National University of Computer and Emerging Sciences, Islamabad

You might also like