Early Systems

advertisement
Chapter 2
Memory Management:
Early Systems
Understanding Operating Systems,
Fourth Edition
Memory Management: Early Systems
“Memory is second only to processes in
importance (and in intensity) with which its
managed by the OS” Tanenbaum.
2
Memory Management
Ideally programmers want
memory that is
large
fast
non volatile
Memory management done by
OS memory manager
Special purpose hardware
Memory hierarchy
small amount of fast,
expensive memory – cache
some medium-speed, medium
price main memory
Gigabytes of slow, cheap disk
storage
Translate these speeds to
human scale
3
Memory Management
• Memory Management means
– Being aware of what parts of the memory are in use
and which parts are not
– Allocating memory to processes when they request it
and de-allocating memory when a process releases
it
– Moving data from memory to disc, when the physical
capacity becomes full, and vice versa.
– Stopping programs interfering with memory that
‘doesn’t belong to them’
4
Memory Management: Early Systems
• Types of memory allocation schemes:
–
–
–
–
Single-user systems
Fixed partitions
Dynamic partitions
Relocatable dynamic partitions
5
Single-User
Contiguous Scheme
•
Program is loaded in its entirety into memory and allocated as
much contiguous space in memory as it needs
– Jobs processed sequentially in single-user systems
– Requires minimal work by the Memory Manager
• Register to store the base address
• Accumulator to keep track of the program size
ie BIOS
One
Embedded
System
DOS
6
Memory Protection in Single-User Contiguous
Scheme
•
To properly implement this scheme
a boundary register is needed
CPU
– Special purpose H/W must be
designed into CPUs
– the contents of the register can
only be modified by special
privileged instruction
•
When a process references
memory the system checks if its
outside its boundary
– Inside: then service the request
– Outside: Error message and
terminate program
•
boundary register
0x123
0x123
Of course the user program must
access the OS from time to time to
perform I/O for example
– Does this through system calls
– The system detects the call and
switches to ‘kernel mode’ or
‘executive mode’
7
Single-User Contiguous Scheme
(continued)
• Disadvantages of Single-User Contiguous
Scheme:
– Doesn’t support multiprogramming
• Mono-programming is unacceptable for modern user
• Modern users will not suffer the delay of swapping
programs in/out to the hard disk
– Not cost effective
• Multiprogramming makes more effective use of the
CPU .... When a single process is doing I/O the CPU
is inactive
8
The secret is
multiprogramming can improve the
utilization of the CPU
But now the problem is :
how do we to organize the available memory so all
the programs can reside there ?
9
Memory Management: Early Systems
• Types of memory allocation schemes:
–
–
–
–
Single-user systems
Fixed partitions
Dynamic partitions
Relocatable dynamic partitions
10
Fixed Partitions
• Main memory is partitioned; one partition/job
– Partitions can be different sizes
• The partition sizes remain static unless and until computer
system is shut down, reconfigured, and restarted
– Must remember where each space is in memory
• Also requires protection of the job’s memory space
– Requires matching job size with partition size
– Allows multiprogramming
11
Fixed Partitions (continued)
To allocate memory spaces to jobs, the operating system’s
Memory Manager must keep some sort of table.
A very simple example is shown below:
Table 2.1: A simplified fixed partition memory table with the
free partition shaded
12
Fixed Partitions (continued)
• Job1 is allocated first space
large enough to accommodate it
• Job 2 is allocated next
available space large enough
to accommodate it
•Job 3 ?
•No partition big enough to
accommodate it even though
70K of free space is
available in Partition 1 where
Job 1 occupies only 30K of
the 100K available but
unfortunately Job 1 owns all
of this partition.
13
Fixed Partitions (continued)
• Programmers
– In the earliest multi-programming systems the programmer
translated the job using an absolute assemble or compiler
• This meant that the process had a pre-defined precise
location defined for it in memory
• The process could only run in that space
• If that partition was occupied (while others were free)
then this resulted in inefficiencies.
– To overcome this problem programmers created (more
complex) relocating compilers, assemblers, and loaders.
• These programs produced a relocatable program that
could run in any available (big enough) partition
14
Fixed Partitions (continued)
• Disadvantages:
– Requires entire program to be stored contiguously
– Jobs are allocated space on the basis of first
available partition of required size
– Works well only if all of the jobs are of the same size
or if the sizes are known ahead of time
– Arbitrary partition sizes lead to undesired results
• Too small a partition size results in large jobs having
to wait such that their ‘turnaround time’ is extended
• Too large a partition size results in memory waste or
‘internal fragmentation’
15
Memory Management: Early Systems
• Types of memory allocation schemes:
–
–
–
–
Single-user systems
Fixed partitions
Dynamic partitions
Relocatable dynamic partitions
16
Dynamic Partitions
• Dynamic Partitions: Jobs are given only as much
memory as they request when they are loaded
– Available memory is kept in contiguous blocks
– Memory waste is comparatively small
• Disadvantages:
– At first its great and it fully utilizes memory when the
first jobs are loaded
•
Subsequent allocation leads to memory waste or
‘external fragmentation’
17
Dynamic Partitions (continued)
Figure 2.2: Main memory use during dynamic partition allocation
18
Dynamic Partitions (continued)
5K
10 K
35K of memory available but
Job 8 still has to wait!
20K
Figure 2.2 (continued): Main memory use during dynamic partition allocation
19
Where to put the jobs
• With Dynamic memory allocation we give jobs just
enough room
• Easy for first job
• What happens when we have lots of jobs all
arriving one after the other and all with different
requirements
• We need some sort of allocation algorithm
20
Best-Fit and First-Fit Allocation
• Free partitions are allocated on the following basis:
– First-fit memory allocation: First partition fitting the
requirements
• Leads to fast allocation of memory space
– Best-fit memory allocation: Smallest partition
fitting the requirements
• Results in least wasted space
• Internal fragmentation reduced but not eliminated
21
Best-Fit Versus First-Fit Allocation
• First-fit memory allocation:
– Advantage: Faster in making allocation
– Disadvantage: Leads to memory waste
• Best-fit memory allocation
– Advantage: Makes the best use of memory space
– Disadvantage: Slower in making allocation
22
First-Fit Allocation
Figure 2.3: An example of a first-fit free scheme
23
Best-Fit
?
Figure 2.4: An example of a best-fit free scheme
24
A Note: The Memory Manager
In the following slides we assume that the Memory Manager keeps two
lists,
– one for free memory (LHS) and
– one for busy memory blocks (RHS)
– Assume the two lists are of a fixed size and the elements in the list can
contain values or be empty.
– For simplicity the lists are often merged in the diagrams that follow
25
First-Fit Algorithm
A new Job
requires
a 200 size
block Free Memory List
The various
addresses
where we
can get
free space.
.........
Where
can we
put the
Job?
Please note that 4075 + 105 = 4180
( so the space (1045) up to 5225
must be filled with jobs)
After Job is placed in
memory the list of
addresses where free
space is available
changes
26
Best-Fit Algorithm
• Algorithm for Best-Fit:
– Goal: find the smallest memory block into which the
job will fit
– NB: Entire table must be searched before allocation
27
A Job
requires
200 spaces
Best-Fit Algorithm
Where will it be
placed?
28
More Allocation Schemes
• Hypothetical allocation schemes:
– Next-fit: Starts searching from last allocated block, for the next
available block when a new job arrives
– Worst-fit: Allocates the largest free available block to the new
job
• Opposite of best-fit
• Good way to explore the theory of memory allocation; might
not be the best choice for an actual system
.
29
Another way to do it?
• Alternative search strategy
– Search the entire input queue looking for the largest job that fits
into the partition
– Do not waste a large partition on a small job
•
but smaller jobs are discriminated against
• ☺ Have at least one small partition or ensure that small jobs
only get skipped a certain number of times
30
When Jobs finish
• How do we reclaim space when jobs finish?
31
Deallocation
• Deallocation: Freeing an allocated memory space
– For fixed-partition system: ☺
• Straightforward process. When job completes,
Memory Manager resets the status of the job’s
memory block to “free”
• Any code—for example, binary values with 0
indicating free and 1 indicating busy—may be used
32
Deallocation (continued)
• For dynamic-partition system:
– Algorithm tries to combine free areas of memory
whenever possible. This makes it more complex.
– The system must prepare for three possible cases:
• Case 1: When the block to be deallocated is adjacent
to another free block
• Case 2: When the block to be deallocated is between
two free blocks
• Case 3: When the block to be deallocated is isolated
from other free blocks
33
Case 1: Joining Two Free Blocks
Join
This has to be de-allocated. It ‘buts-up’ to the
next highest free memory location (although
not to the free space below it... there is
probably another process in the green area).
34
Case 1: Joining Two Free Blocks
– The free memory list must be
changed to reflect starting
address of the new free block
• In the example, 7600
– this was the address
of the first instruction
of the job that just
released this block
– Memory block size for the new
free space must be changed
to show its new size—that is,
the combined total of the two
free partitions
• In the example, (200 + 5)
35
Case 2: Joining Three Free Blocks
Now notice that the memory we will free is butted-up to free memory above and below it. We’ll end up
with three free spaces. These can be joined together.
This has to be de-allocated
36
Case 2: Joining Three Free Blocks.
•
Deallocated memory space is
between two free memory blocks
– Change list to reflect the starting
address of the new free block
• In the example, 7560— which
was the smallest beginning
address
– Sizes of the three free partitions must
be combined
• In the example, (20 + 20 + 205)
– Because location 7600 in our list has
been combined the pointer in that list
location must be changed to null to
indicate that it no longer points to free
memory space, hence ‘null’.
37
Case 3: Deallocating an Isolated Block
1
•
Space to be deallocated is
isolated from other free
areas
2
1. System learns that the
memory block to be
released is ‘tight’ between 3
two other busy areas
2. Null entry in the busy list
3. Must search the free
memory list for a null entry
4. And point that list element
to the new free area
4
38
Memory Management: Early Systems
• Types of memory allocation schemes:
–
–
–
–
Single-user systems
Fixed partitions
Dynamic partitions
Relocatable dynamic partitions
39
Problems so far
• The fixed and dynamic memory management
systems described so far share some unacceptable
fragmentation characteristics that must be resolved
• We’ll likely end up with lots of slivers of un-used
memory
40
Relocatable Dynamic Partitions
• Relocatable Dynamic Partitions:
– Memory Manager relocates programs to gather
together all of the empty blocks
– Compact the empty blocks to make one block of
memory large enough to accommodate some or all
of the jobs waiting to get in
• Sometimes called garbage collection
– Its not easy
41
Relocatable Dynamic Partitions
(continued)
• Compaction: Reclaiming fragmented sections of
the memory space
– Every program in memory must be relocated so they
are contiguous
– So every address so must be relocated
– Every reference to an address within the program
must be updated
– Data must not be harmed
42
Relocatable Dynamic Partitions
(continued)
Figure 2.5: An assembly language program that performs
a simple incremental operation
43
Relocatable Dynamic Partitions
(continued)
The assembly language program (RHS) after it has been
processed by the assembler appear on LHS. Notice the
memory locations are flagged by apostrophe ‘
44
Relocatable Dynamic Partitions
(continued)
• Compaction issues:
– What goes on behind the scenes when relocation
and compaction take place?
– What keeps track of how far each job has moved
from its original storage area?
– What lists have to be updated?
45
Relocatable Dynamic Partitions
(continued)
• What lists have to be updated?
– Free list must show the partition for the new block of
free memory
– Busy list must show the new locations for all of the
jobs already in process that were relocated
– Each job will have a new address except for those
that were already at the lowest memory locations
46
Relocatable Dynamic Partitions
(continued)
• Special-purpose registers are used for relocation:
– Bounds register
• Stores highest location accessible by each program
– Relocation register
• Contains the value that must be added to each
address referenced in the program so it will be able to
access the correct memory addresses after relocation
• If the program isn’t relocated, the value stored in the
program’s relocation register is zero
47
Relocatable Dynamic Partitions
(continued)
Figure 2.7: Three snapshots of memory before and after
compaction
48
Relocatable Dynamic Partitions
(continued)
Figure 2.8: Contents of relocation register and close-up of
Job 4 memory area (a) before relocation and
(b) after relocation and compaction
49
Relocatable Dynamic Partitions
(continued)
• Compacting and relocating optimizes the use of
memory and thus improves throughput
• Options for when and how often it should be done:
– When a certain percentage of memory is busy
– When there are jobs waiting to get in
– After a prescribed amount of time has elapsed
Goal: Optimize processing time and memory use while
keeping overhead as low as possible
50
Summary
• Four memory management techniques were used
in early systems: single-user systems, fixed
partitions, dynamic partitions, and relocatable
dynamic partitions
• Memory waste in dynamic partitions is
comparatively small as compared to fixed partitions
• First-fit is faster in making allocation but leads to
memory waste
• Best-fit makes the best use of memory space but
slower in making allocation
51
Summary (continued)
• Compacting and relocating optimizes the use of
memory and thus improves throughput
• All techniques require that the entire program
must:
– Be loaded into memory
– Be stored contiguously
– Remain in memory until the job is completed
• Each technique puts severe restrictions on the size
of the jobs: can only be as large as the largest
partitions in memory
52
Objectives of this chapter were
You will be able to describe:
• The basic functionality of the three memory
allocation schemes presented in this chapter: fixed
partitions, dynamic partitions, relocatable dynamic
partitions
• Best-fit memory allocation as well as first-fit
memory allocation schemes
• How a memory list keeps track of available memory
• The importance of deallocation of memory in a
dynamic partition system
53
Objectives (continued)
You should be able to describe:
• The importance of the bounds register in memory
allocation schemes
• The role of compaction and how it improves
memory allocation efficiency
54
Download