Date post: | 17-Mar-2018 |
Category: |
Documents |
Upload: | dinhnguyet |
View: | 215 times |
Download: | 2 times |
Chapter 2Memory Management:
Early Systems
Understanding Operating Systems, Fourth Edition
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.
3
Memory ManagementIdeally programmers want memory that is
largefastnon volatile
Memory management done byOS memory managerSpecial purpose hardware
Memory hierarchy small amount of fast, expensive memory – cache some medium-speed, medium price main memoryGigabytes of slow, cheap disk storage
Translate these speeds to human scale
4
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’
5
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems– Fixed partitions– Dynamic partitions– Relocatable dynamic partitions
6
SingleSingle--User User 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
7
Memory Protection in Memory Protection in SingleSingle--User Contiguous User 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 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• 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’
boundary register
CPU
0x123
0x123
8
SingleSingle--User Contiguous SchemeUser Contiguous Scheme(continued)(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
9
The secret isThe 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 ?
10
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems– Fixed partitions– Dynamic partitions– Relocatable dynamic partitions
11
Fixed PartitionsFixed 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
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’sMemory Manager must keep some sort of table. A very simple example is shown below:
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.
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 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
15
Fixed PartitionsFixed Partitions (continued)(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’
16
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems– Fixed partitions– Dynamic partitions– Relocatable dynamic partitions
17
Dynamic PartitionsDynamic 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’
18
Dynamic PartitionsDynamic Partitions (continued)(continued)
Figure 2.2: Main memory use during dynamic partition allocation
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!
20
Where to put the jobsWhere 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
21
BestBest--FitFit and and FirstFirst--Fit Fit AllocationAllocation
• 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
22
BestBest--Fit Versus FirstFit Versus First--Fit AllocationFit 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
23
FirstFirst--Fit AllocationFit Allocation
Figure 2.3: An example of a first-fit free scheme
24
BestBest--FitFit
Figure 2.4: An example of a best-fit free scheme
?
25
A Note: The Memory ManagerA 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
26
Free Memory List
FirstFirst--Fit AlgorithmFit AlgorithmA new Job requires
a 200 size block
After Job is placed in memory the list of addresses where free space is available changes
.........The various addresseswhere we
can get free space.
Please note that 4075 + 105 = 4180 ( so the space (1045) up to 5225 must be filled with jobs)
Where can we put theJob?
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
28
BestBest--Fit AlgorithmFit AlgorithmA Job requires
200 spaces
Where will it be placed?
29
More Allocation SchemesMore 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
.
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 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
31
When Jobs finishWhen Jobs finish
• How do we reclaim space when jobs finish?
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 0 indicating free and 1 indicating busy—may be used
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
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 (although not to the free space below it... there is probably another process in the green area).
Join
35
– 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)
Case 1: Joining Two Free BlocksCase 1: Joining Two Free Blocks
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.
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— 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’.
38
Case 3: Case 3: DeallocatingDeallocating an Isolated Blockan Isolated Block
• Space to be deallocated is isolated from other freeareas1. System learns that the
memory block to be released is ‘tight’ between two other busy areas
2. Null entry in the busy list3. Must search the free
memory list for a null entry4. And point that list element
to the new free area
1
2
3
4
39
Memory Management:Memory Management: Early SystemsEarly Systems
• Types of memory allocation schemes: – Single-user systems– Fixed partitions– Dynamic partitions– Relocatable dynamic partitions
40
Problems so farProblems 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
41
RelocatableRelocatable Dynamic PartitionsDynamic 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
42
RelocatableRelocatable Dynamic PartitionsDynamic Partitions(continued)(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
43
RelocatableRelocatable Dynamic PartitionsDynamic Partitions(continued)(continued)
Figure 2.5: An assembly language program that performs a simple incremental operation
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 ‘
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?
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
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 the program’s relocation register is zero
48
RelocatableRelocatable Dynamic PartitionsDynamic Partitions(continued)(continued)
Figure 2.7: Three snapshots of memory before and after compaction
49
RelocatableRelocatable Dynamic PartitionsDynamic Partitions(continued)(continued)
Figure 2.8: Contents of relocation register and close-up ofJob 4 memory area (a) before relocation and (b) after relocation and compaction
50
RelocatableRelocatable Dynamic PartitionsDynamic Partitions(continued)(continued)
• Compacting and relocating optimizes the use ofmemory 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 elapsedGoal: Optimize processing time and memory use while
keeping overhead as low as possible
51
SummarySummary
• Four memory management techniques were used in early systems: single-user systems, fixed partitions, dynamic partitions, and relocatabledynamic 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
52
Summary (continued)Summary (continued)
• Compacting and relocating optimizes the use ofmemory 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
53
Objectives of this chapter wereObjectives 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
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