Sei sulla pagina 1di 25

AFRICA NAZARENE UNIVERSITY

Department of Computer Science

CSC 302
Operating
Systems
PART THREE: MEMORY MANAGEMENT:
Lecture12: Basic Memory Management:
Lecture 12:

Scope:
Introduction to memory management.
Basic memory management options.
Relocation and protection.
Goals:
Examine the basic principles and strategies
applied in memory management.
Discuss the relocation and protection problems
and some solutions to them

Memory Management 2
Introduction

Ideally what is desired is an infinitely large,


inexpensive, fast memory that is also
nonvolatile.
Unfortunately technology does not provide
for such.
The memory hierarchy is based on
Storage capacity
Speed and
Cost.

Memory Management 3
Introduction
Lower in the hierarchy, storage capacities have
lesser speed and cost.
Consequently, most computers have a memory
hierarchy, with
a small amount of very fast, inexpensive, volatile cache
memory,
some number of megabytes of medium-speed, medium-
price volatile main memory (RAM) and
hundreds or thousands of megabytes of slow, cheap, non-
volatile disk storage.
It is the work of the operating systems to co-ordinate
how these memories are used.

Memory Management 4
Memory hierarchy

Memory Management 5
memory manager

This is the part of the operating system that


manages the memory hierarchy.
Its role is to keep track of which parts of
memory are in use and which parts are not in
use,
to allocate memory to processes when they need
it and de-allocate it when they are done.
It also manages swapping between main
memory and disk when main memory is not
enough to hold all the processes.
Memory Management 6
Memory management
Memory management is one of the most important
services provided by the operating system.
An operating system has five major responsibilities
for managing memory:
Process isolation The operating system should prevent
independent processes from interfering with the data and
code segments of each other.
Automatic allocation and management - Programs
should be dynamically allocated across the memory
depending on the availability and this may or may not be
contiguous.

Memory Management 7
Modular programming support - The programmers
should be able to define program modules and also be able
to dynamically create/destroy/alter the size of modules.
Protection and access control - Different programs
should be able to co-operate in sharing some memory
space. It is important to ensure that such sharing is
controlled and processes should not be able to
indiscriminately access the memory allocated to other
processes.
Long-term storage - Users and applications may require
means for storing information for extended periods of time.
This is implemented using a file system.

Memory Management 8
Basic memory
management:
Memory management systems can be
divided into two classes:
those that move processes back and forth
between main memory and disk during execution
(swapping and paging) and
those that do not.
Swapping and paging are caused by lack of
sufficient main memory to hold all the
programs at once.

Memory Management 9
Mono-programming without
swapping or paging:
The simplest possible memory management
scheme is to run one program at a time,
sharing the memory between the program
and the operating system.
In this scheme, there are three variations:
The operating system may be at the bottom of
memory in RAM, or
It may be at the top in ROM, or
Device drivers may at the top in ROM and the rest
of the system in RAM.

Memory Management 10
The later model is common with small MS-DOS
systems.
On IBM PCs, the portion of the system in ROM is
called the BIOS (Basic Input Output System).
System characteristics:
Only one process can be running.
As soon as the user types a command, the operating
system copies the requested program from disk to memory
and executes it.
When the operating system receives another command, it
loads a new program into memory and overwrites the
previous one.
Mono programming is common with small
computers with simple operating systems.
Memory Management 11
Memory Management 12
Multiprogramming with fixed
partitions:
It is desirable to have multiple processes
running at once.
On timesharing systems, having multiple
processes in memory at the same time
means that when one process is blocked
waiting for I/O to finish, another one can use
the CPU.

Memory Management 13
One way of achieving multiprogramming is to
divide memory into unequal partitions.
Partitioning can be done manually when the
system is started up.
When a job is started, it is put in the input
queue for the smallest partition that is
large enough to hold it.
Since partitions are fixed, any unused
space in the partition is lost.

Memory Management 14
Memory Management 15
Disadvantage:
The queue for a large partition may be empty
while several jobs are kept waiting for a smaller
partition.
An alternative is to maintain a single queue:
Whenever a partition becomes free, the job
closest to the front of the queue that fits in it could
be loaded into the empty partition and run.
Problem:
Wastes large partition on small job

Memory Management 16
It is undesirable to waste a large partition on
a small job
Thus, a different strategy;
search the whole input queue whenever a
partition becomes free and pick the largest job
that fits it.
Problem:
This algorithm discriminates against small jobs as
being unworthy of having a whole partition,
whereas, it is desirable to give small jobs the best
service and not the worst.
Memory Management 17
A solution is to have at least one small
partition around.
Another approach is to have a rule stating
that a job that is eligible to run may not be
skipped over more than k times.
Each time it is skipped over it is given one point.
When it has gained k points, it may not be skipped
again.

Memory Management 18
Relocation and protection:

Multiprogramming introduces relocation and


protection.
Different jobs will now have to be run at
different addresses.
When a program is linked (main program,
user-written procedures and library
procedures are combined into a single
address space), the linker must know at what
address the program will start in memory.
Memory Management 19
The relocation problem
Suppose the first instruction is a call to a procedure
at absolute address 100 within the binary file
produced by the linker.
If this program is loaded in partition 1, that
instruction will jump to absolute address 100, which
is inside the operating system.
What is needed is a call to 100K + 100.
If the program is loaded into partition 2, it must be carried
out as call to 200K + 100.
This problem is known as the relocation problem.

Memory Management 20
Solutions to the relocation
problem
Modify the instructions as the program is loaded into
memory.
Programs loaded into partition 1 have 100K added to each
address; programs loaded into partition 2 have 200K added
to addresses.
However:- Relocation during loading does not
solve the protection problem.
A malicious program can always construct a new
instruction and jump to it.
In multi-user systems, it is undesirable to let processes
read and write memory belonging to other users.

Memory Management 21
Solution to protection
problem:
One solution is to divide memory into blocks
of 2K bytes and assign a 4-bit protection
code to each block.
The PSW contained a 4-bit key.
The hardware traps any attempt by a running
process to access memory whose protection code
differed from the PSW key.
It is only the operating system that was allowed to
change this key and not user processes.

Memory Management 22
Another solution to both the relocation and
protection problems is to equip the machine with two
special hardware registers, called the base and the
limit registers.
When a process is loaded the base and limit registers are
loaded with address of the start of its partition and length of
the partition respectively.
Every memory address generated automatically has the
base register contents added to it before being sent to
memory.
Addresses are also checked against the limit registers to
make sure that they do not attempt to address memory
outside the current partition.
Hardware protects the base and limit registers from user
programs.

Memory Management 23
With batch systems, organizing memory into fixed
partitions is simple and effective.
Each job is loaded into a partition when it gets to the head
of the queue.
It stays in memory until it is finished.
With timesharing systems, the situation is different.
Sometimes there is not enough main memory to hold all
the currently active processes,
so excess processes must be kept on disk and brought in
to run dynamically.

Memory Management 24
Two general approaches to memory
management can be used depending on the
available hardware.
The simplest strategy, called swapping, consists
of bringing each process in its entirety, running it
for a while then putting it back to disk.
The other strategy, called virtual memory, allows
programs to run even when they are only partially
in main memory.

Memory Management 25

Potrebbero piacerti anche