04 ch02 understandos memory1
TRANSCRIPT
-
8/20/2019 04 Ch02 UnderstandOS Memory1
1/54
Chapter 2Memory Management:
Early Systems
Understanding Operating Systems,Fourth Edition
-
8/20/2019 04 Ch02 UnderstandOS Memory1
2/54
2
Memory Management:Memory Management: Early SystemsEarly Systems
“Memory is second only to processes in
importance (and in intensity) with which its
managed by the OS” Tanenbaum.
-
8/20/2019 04 Ch02 UnderstandOS Memory1
3/54
3
Memory Management
Ideally programmers wantmemory 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, mediumprice main memory
Gigabytes of slow, cheap diskstorage
Translate these speeds tohuman scale
-
8/20/2019 04 Ch02 UnderstandOS Memory1
4/54
4
Memory Management
• Memory Management means
– Being aware of what parts of the memory are in useand which parts are not
– Allocating memory to processes when they request it
and de-allocating memory when a process releasesit
– 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’
-
8/20/2019 04 Ch02 UnderstandOS Memory1
5/54
5
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems
– Fixed partitions
– Dynamic partitions – Relocatable dynamic partitions
-
8/20/2019 04 Ch02 UnderstandOS Memory1
6/54
6
SingleSingle--UserUser
Contiguous SchemeContiguous 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
Oneie BIOS
Embedded
System
DOS
-
8/20/2019 04 Ch02 UnderstandOS Memory1
7/54
7
Memory Protection inMemory Protection in SingleSingle--User ContiguousUser Contiguous
SchemeScheme• To properly implement this scheme
a boundary register is needed – Special purpose H/W must be
designed into CPUs
– the contents of the register canonly be modified by specialprivileged instruction
• When a process referencesmemory the system checks if its
outside its boundary – Inside: then service the request
– Outside: Error message andterminate program
• Of course the user program must
access the OS from time to time toperform I/O for example – Does this through system calls
– The system detects the call andswitches to ‘kernel mode’ or‘executive mode’
boundary register
CPU
0x123
0x123
-
8/20/2019 04 Ch02 UnderstandOS Memory1
8/54
8
SingleSingle--User Contiguous SchemeUser Contiguous Scheme(continued)(continued)
• Disadvantages of Single-User ContiguousScheme:
– 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 theCPU .... When a single process is doing I/O the CPU
is inactive
-
8/20/2019 04 Ch02 UnderstandOS Memory1
9/54
9
The secret isThe secret is
multiprogramming can improve theutilization of the CPU
But now the problem is :
how do we to organize the available memory so allthe programs can reside there ?
-
8/20/2019 04 Ch02 UnderstandOS Memory1
10/54
10
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems
– Fixed partitions
– Dynamic partitions – Relocatable dynamic partitions
-
8/20/2019 04 Ch02 UnderstandOS Memory1
11/54
11
Fixed PartitionsFixed Partitions• Main memory is partitioned; one partition/job
– Partitions can be different sizes
• The partition sizes remain static unless and until computersystem 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
12/54
12
Fixed PartitionsFixed Partitions (continued)(continued)
Table 2.1: A simplified fixed partition memory table with the
free partition shaded
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:
-
8/20/2019 04 Ch02 UnderstandOS Memory1
13/54
13
Fixed PartitionsFixed Partitions (continued)(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.
-
8/20/2019 04 Ch02 UnderstandOS Memory1
14/54
14
Fixed PartitionsFixed Partitions (continued)(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 preciselocation 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
15/54
15
Fixed PartitionsFixed Partitions (continued)(continued)
• Disadvantages: – Requires entire program to be stored contiguously
– Jobs are allocated space on the basis of firstavailable partition of required size
– Works well only if all of the jobs are of the same sizeor if the sizes are known ahead of time
– Arbitrary partition sizes lead to undesired results
• Too small a partition size results in large jobs havingto wait such that their ‘turnaround time’ is extended
• Too large a partition size results in memory waste or‘internal fragmentation’
-
8/20/2019 04 Ch02 UnderstandOS Memory1
16/54
16
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems
– Fixed partitions
– Dynamic partitions – Relocatable dynamic partitions
-
8/20/2019 04 Ch02 UnderstandOS Memory1
17/54
17
Dynamic PartitionsDynamic Partitions
• Dynamic Partitions: Jobs are given only as muchmemory 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 thefirst jobs are loaded
•
Subsequent allocation leads to memory waste or‘external fragmentation’
-
8/20/2019 04 Ch02 UnderstandOS Memory1
18/54
18
Dynamic PartitionsDynamic Partitions (continued)(continued)
Figure 2.2: Main memory use during dynamic partition allocation
-
8/20/2019 04 Ch02 UnderstandOS Memory1
19/54
19
Dynamic PartitionsDynamic Partitions (continued)(continued)
Figure 2.2 (continued): Main memory use during dynamic partition allocation
5 K
10 K
20K
35K of memory available but
Job 8 still has to wait!
-
8/20/2019 04 Ch02 UnderstandOS Memory1
20/54
20
Where to put the jobsWhere to put the jobs
• With Dynamic memory allocation we give jobs justenough room
• Easy for first job
• What happens when we have lots of jobs allarriving one after the other and all with different
requirements
• We need some sort of allocation algorithm
-
8/20/2019 04 Ch02 UnderstandOS Memory1
21/54
21
BestBest--FitFit andand FirstFirst--FitFit Allocation 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
22/54
-
8/20/2019 04 Ch02 UnderstandOS Memory1
23/54
23
FirstFirst--Fit AllocationFit Allocation
Figure 2.3: An example of a first-fit free scheme
-
8/20/2019 04 Ch02 UnderstandOS Memory1
24/54
24
BestBest--FitFit
Figure 2.4: An example of a best-fit free scheme
?
-
8/20/2019 04 Ch02 UnderstandOS Memory1
25/54
25
A Note: The Memory Manager A Note: The Memory Manager
In the following slides we assume that the Memory Manager keeps twolists,
– 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 cancontain values or be empty.
– For simplicity the lists are often merged in the diagrams that follow
-
8/20/2019 04 Ch02 UnderstandOS Memory1
26/54
26
Free Memory List
FirstFirst
--Fit AlgorithmFit Algorithm
A new Jobrequires
a 200 size
block
After Job is placed inmemory the list of
addresses where free
space is available
changes
.........The variousaddresses
where we
can getfree space.
Please note that 4075 + 105 = 4180
( so the space (1045) up to 5225
must be filled with jobs)
Where
can we
put theJob?
-
8/20/2019 04 Ch02 UnderstandOS Memory1
27/54
27
BestBest--Fit AlgorithmFit 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
28/54
28
BestBest--Fit AlgorithmFit Algorithm A Jobrequires200 spaces
Where will it beplaced?
-
8/20/2019 04 Ch02 UnderstandOS Memory1
29/54
29
More Allocation SchemesMore Allocation Schemes
• Hypothetical allocation schemes: – Next-fit: Starts searching from last allocated block, for the nextavailable 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
.
-
8/20/2019 04 Ch02 UnderstandOS Memory1
30/54
30
Another way to do it? Another way to do it?
• Alternative search strategy – Search the entire input queue looking for the largest job that fitsinto 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
31/54
31
When Jobs finishWhen Jobs finish
• How do we reclaim space when jobs finish?
-
8/20/2019 04 Ch02 UnderstandOS Memory1
32/54
32
DeallocationDeallocation
• 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 0indicating free and 1 indicating busy—may be used
-
8/20/2019 04 Ch02 UnderstandOS Memory1
33/54
33
DeallocationDeallocation (continued)(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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
34/54
34
Case 1: Joining Two Free BlocksCase 1: Joining Two Free Blocks
This has to be de-allocated. It ‘buts-up’ to the
next highest free memory location (althoughnot to the free space below it... there is
probably another process in the green area).
Join
-
8/20/2019 04 Ch02 UnderstandOS Memory1
35/54
35
– The free memory list must bechanged to reflect starting
address of the new free block• In the example, 7600
– this was the addressof the first instructionof the job that just
released this block – Memory block size for the new
free space must be changedto show its new size—that is,the combined total of the two
free partitions• In the example, (200 + 5)
Case 1: Joining Two Free BlocksCase 1: Joining Two Free Blocks
-
8/20/2019 04 Ch02 UnderstandOS Memory1
36/54
36
Case 2: Joining Three Free BlocksCase 2: Joining Three Free Blocks
This has to be de-allocated
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.
-
8/20/2019 04 Ch02 UnderstandOS Memory1
37/54
37
Case 2: Joining Three Free BlocksCase 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— whichwas the smallest beginningaddress
– Sizes of the three free partitions mustbe combined
• In the example, (20 + 20 + 205)
– Because location 7600 in our list hasbeen combined the pointer in that list
location must be changed to null toindicate that it no longer points to freememory space, hence ‘null’.
-
8/20/2019 04 Ch02 UnderstandOS Memory1
38/54
38
Case 3:Case 3: DeallocatingDeallocating an Isolated Blockan Isolated Block
• Space to be deallocated is
isolated from other free
areas
1. System learns that the
memory block to be
released is ‘tight’ between
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
1
2
3
4
-
8/20/2019 04 Ch02 UnderstandOS Memory1
39/54
39
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems
– Fixed partitions
– Dynamic partitions
– Relocatable dynamic partitions
-
8/20/2019 04 Ch02 UnderstandOS Memory1
40/54
40
Problems so far Problems so far
• The fixed and dynamic memory managementsystems 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
41/54
41
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
• Relocatable Dynamic Partitions: – Memory Manager relocates programs to gathertogether all of the empty blocks
– Compact the empty blocks to make one block of
memory large enough to accommodate some or allof the jobs waiting to get in
• Sometimes called garbage collection
– Its not easy
-
8/20/2019 04 Ch02 UnderstandOS Memory1
42/54
42
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(continued)
• Compaction: Reclaiming fragmented sections ofthe 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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
43/54
43
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(continued)
Figure 2.5: An assembly language program that performs
a simple incremental operation
-
8/20/2019 04 Ch02 UnderstandOS Memory1
44/54
44
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(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 ‘
-
8/20/2019 04 Ch02 UnderstandOS Memory1
45/54
45
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(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?
-
8/20/2019 04 Ch02 UnderstandOS Memory1
46/54
46
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
47/54
47
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(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 theprogram’s relocation register is zero
-
8/20/2019 04 Ch02 UnderstandOS Memory1
48/54
48
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(continued)
Figure 2.7: Three snapshots of memory before and after
compaction
-
8/20/2019 04 Ch02 UnderstandOS Memory1
49/54
49
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
50/54
50
RelocatableRelocatable Dynamic PartitionsDynamic Partitions
(continued)(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
-
8/20/2019 04 Ch02 UnderstandOS Memory1
51/54
51
SummarySummary
• Four memory management techniques were usedin early systems: single-user systems, fixedpartitions, dynamic partitions, and relocatabledynamic partitions
• Memory waste in dynamic partitions iscomparatively small as compared to fixed partitions
• First-fit is faster in making allocation but leads tomemory waste
• Best-fit makes the best use of memory space butslower in making allocation
-
8/20/2019 04 Ch02 UnderstandOS Memory1
52/54
52
Summary (continued)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 largestpartitions in memory
-
8/20/2019 04 Ch02 UnderstandOS Memory1
53/54
53
Objectives of this chapter wereObjectives of this chapter were
You will be able to describe:
• The basic functionality of the three memoryallocation schemes presented in this chapter: fixedpartitions, 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 adynamic partition system
-
8/20/2019 04 Ch02 UnderstandOS Memory1
54/54
54
Objectives (continued)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