Chapter 6: Memory Management

feastcanadianΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 7 χρόνια και 8 μήνες)

211 εμφανίσεις

Chapter
6
: Memory Management


Memory management is an OS activity which controls
distribution of memory among processes. The part of the
OS that manages memory is called the memory manager.


The main jobs are:



To keep track of used and free memory



To allocate and de
-
allocate memory for processes



To manage swapping between main memory and disk.


Background

Memory management systems rely on the
following strategies:



Memory allocation (partitioning)



Swapping



Segmentation



Virtual memory

Memory allocation


Monoprogramming:



The simplest possible memory management scheme is to have just
one process in memory at a time and to allow that process to use all
memory. This approach is not used any more even in home
computers. This approach can be as:


Device Drivers are loaded in ROM,


the rest of the OS is loaded in part of the RAM and


the user program uses the entire remaining RAM.




On computers with multiple users, monoprogramming is not used,
because different users should be able to run processes
simultaneously. This requires having more than one process in
memory (i.e. multiprogramming), thus another approach for memory
management is multiprogramming.

Memory allocation


Multiprogramming


with fixed partitioning:



With this strategy, the memory is divided into N (typically unequal)
portions called “partitions”. When a job arrives, it can be inserted into
the smallest partition large enough to hold it.



Basically, we might have a single job queue served by all the
partitions or one input queue for each partition.

multiple queues

single queue

Memory allocation




Two special registers called
Base

and
Limit

are usually
used per partition for protection.



Fixed partitioning method has two main problems:



How to determine the number of partitions and size of
each partition?


Fragmentation

(both internal and external) may occur!

Memory allocation


Multiprogramming


with variable partitioning:



In this method, we keep a table indicating used/free areas in
memory. Initially, whole memory is free and it is considered as
one large block.



When a new process arrives, we search for a block of free
memory, large enough for that process.



The number, location and size of the partitions vary
dynamically as process come and go.

Variable partitioning
-

example

A is moved into main memory, then process B, then process C, then A
terminates or is swapped out, then D comes in, then B goes out, then E
comes in.

If a block is freed, we try to merge it with its neighbors if they are also free.

Selection of blocks


First
-
Fit:
Allocate the first free block that is large enough for the
new process (very fast).



Best
-
Fit:
Allocate the smallest block among those that are large
enough fort he new process. We have to search the entire list.
However, this algorithm produces the smallest left
-
over block.



Worst
-
Fit:
Allocate the largest block among those that are large
enough fort he new process. Again, we have to search the list. This
algorithm produces the largest left
-
over block.


There are three main algorithms for searching the list of free blocks for a
specific amount of memory:

Example

Consider the example given on the
right. Now, assume that P4 = 3K
arrives. The block that is going to be
selected depends on the algorithm
used.

Example

before

insertion

Relative performance



Simulation studies show that Worst
-
fit is the worst and in
some cases, First
-
fit has better performance than Best
-
fit.



Generally, variable partitioning results in better memory
utilization than fixed partitioning; causes lesser
fragmentation; and serves large or small all partitions
equally well.

Fragmentation


The algorithms described above suffer from external fragmentation.
As processes as loaded and removed from memory, it is broken into
small regions. External fragmentation occurs when enough memory
space exists but it is not contiguous.


P3

P2

P1

OS

1000

800

600

250

100

Assume that a new process P4 arrives

requiring 250 bytes. Although there are

350 bytes free space, this request cannot

be satisfied. This is called

external fragmentation

of memory

Fragmentation


Assume that a new process arrives requiring 18,598
bytes. If the requested block is allocated exactly, there
will be a hole of 2 bytes. The memory space required
to keep this hole in the table used for indicating the
available and used regions of memory will be larger
than the hole itself.


The general approach is to allocate very small holes as
a part of larger request. The difference between the
allocated and requested memory space is called
internal fragmentation
.


P3

OS

a hole of

18,600 bytes

Compaction


One possible solution to external fragmentation problem is
compaction. The main aim is to shuffle the memory contents to
place all free memory together in one block.


P3

P2

P1

OS

1000

800

600

250

100

P3

P2

P1

OS

1000

450

100

50

50

650