Download - Chapter07 New
-
8/3/2019 Chapter07 New
1/42
Chapter 7Memory Management
Operating Systems:Internals and Design Principles, 6/E
William Stallings
Dave Bremer Otago Polytechnic, N.Z.
2009, Prentice Hall
-
8/3/2019 Chapter07 New
2/42
R oadmap
Basic requirements of MemoryManagement
Memory PartitioningBasic blocks of memory management Paging
Segmentation
-
8/3/2019 Chapter07 New
3/42
T he need for memory
managementMemory is cheap today, and gettingcheaper
But applications are demanding more andmore memory, there is never enough!
Memory Management, involves swappingblocks of data from secondary storage.Memory I/O is slow compared to a CPU T he OS must cleverly time the swapping to
maximise the CPUs efficiency
-
8/3/2019 Chapter07 New
4/42
Memory Management
M emory needs to be allocated to ensure areasonable supply of ready processes toconsume available processor time
-
8/3/2019 Chapter07 New
5/42
Memory ManagementR
equirementsR elocationProtection
SharingLogical organisationPhysical organisation
-
8/3/2019 Chapter07 New
6/42
R equirements: R elocation
T he programmer does not know where theprogram will be placed in memory when it
is executed, it may be swapped to disk and return to mainmemory at a different location (relocated)
Memory references must be translated tothe actual physical memory address
-
8/3/2019 Chapter07 New
7/42
Memory ManagementT
ermsTerm DescriptionFrame Fixed - length block of main
memory.Page Fixed - length block of data in
secondary memory (e.g. on disk).Segment Variable-length block of data that
resides in secondary memory.
Table 7.1 Memory Management Terms
-
8/3/2019 Chapter07 New
8/42
A ddressing
-
8/3/2019 Chapter07 New
9/42
R equirements: Protection
Processes should not be able to referencememory locations in another processwithout permissionImpossible to check absolute addresses atcompile timeMust be checked at run time
-
8/3/2019 Chapter07 New
10/42
R equirements: Sharing
A llow several processes to access thesame portion of memory
Better to allow each process access to thesame copy of the program rather thanhave their own separate copy
-
8/3/2019 Chapter07 New
11/42
R equirements: Logical
OrganizationMemory is organized linearly (usually)Programs are written in modules
Modules can be written and compiledindependently
Different degrees of protection given tomodules (read -only, execute -only)Share modules among processesSegmentation helps here
-
8/3/2019 Chapter07 New
12/42
R equirements: Physical
OrganizationCannot leave the programmer with theresponsibility to manage memory
Memory available for a program plus itsdata may be insufficient Overlaying allows various modules to be
assigned the same region of memory but istime consuming to program
Programmer does not know how muchspace will be available
-
8/3/2019 Chapter07 New
13/42
-
8/3/2019 Chapter07 New
14/42
T ypes of Partitioning
Fixed PartitioningDynamic Partitioning
Simple PagingSimple SegmentationVirtual Memory Paging
Virtual Memory Segmentation
-
8/3/2019 Chapter07 New
15/42
Fixed Partitioning
Equal -size partitions (see fig 7.3a) A ny process whose size is less than
or equal to the partition size can beloaded into an available partition
T he operating system can swap aprocess out of a partition If none are in a ready or running
state
-
8/3/2019 Chapter07 New
16/42
Fixed Partitioning Problems
A program may not fit in a partition. T he programmer must design the program
with overlays
Main memory use is inefficient. A ny program, no matter how small, occupies
an entire partition.
T his is results in internal fragmentation.
-
8/3/2019 Chapter07 New
17/42
Solution Unequal Size
PartitionsLessens both problems but doesnt solve completely
In Fig 7.3b, Programs up to 16M can be
accommodated without overlay Smaller programs can be placed in
smaller partitions, reducing internalfragmentation
-
8/3/2019 Chapter07 New
18/42
Placement A lgorithm
Equal -size Placement is trivial (no options)
Unequal -size Can assign each process to the smallest
partition within which it will fit Queue for each partition Processes are assigned in such a way as to
minimize wasted memory within a partition
-
8/3/2019 Chapter07 New
19/42
Fixed Partitioning
-
8/3/2019 Chapter07 New
20/42
R emaining Problems with
Fixed PartitionsT he number of active processes is limitedby the system I.E limited by the pre -determined number of
partitionsA large number of very small process willnot use the space efficiently In either fixed or variable length partition
methods
-
8/3/2019 Chapter07 New
21/42
Dynamic Partitioning
Partitions are of variable length andnumber
Process is allocated exactly as muchmemory as required
-
8/3/2019 Chapter07 New
22/42
Dynamic Partitioning
ExampleE xternal FragmentationMemory external to allprocesses is fragmentedCan resolve usingcompaction OS moves processes so
that they are contiguous T ime consuming and
wastes CPU time
OS (8M)
P1(20M)
P2(14M)
P3(18M)
Empty(56M)
Empty (4M)
P4(8M)
Empty (6M)
P2(14M)
Empty (6M)
R efer to Figure 7.4
-
8/3/2019 Chapter07 New
23/42
Dynamic Partitioning
Operating system must decide which freeblock to allocate to a process
Best-fit algorithm Chooses the block that is closest in size to the
request Worst performer overall Since smallest block is found for process, the
smallest amount of fragmentation is left Memory compaction must be done more often
-
8/3/2019 Chapter07 New
24/42
Dynamic Partitioning
First- fit algorithm Scans memory form the beginning and
chooses the first available block that is largeenough
Fastest May have many process loaded in the front
end of memory that must be searched over when trying to find a free block
-
8/3/2019 Chapter07 New
25/42
Dynamic Partitioning
Next- fit Scans memory from the location of the last
placement More often allocate a block of memory at the
end of memory where the largest block isfound
T he largest block of memory is broken up intosmaller blocks Compaction is required to obtain a large block
at the end of memory
-
8/3/2019 Chapter07 New
26/42
A llocation
-
8/3/2019 Chapter07 New
27/42
Buddy System
Entire space available is treated as asingle block of 2 U
If a request of size s where 2U -1
< s
-
8/3/2019 Chapter07 New
28/42
Example of Buddy System
-
8/3/2019 Chapter07 New
29/42
T ree R epresentation of
Buddy System
-
8/3/2019 Chapter07 New
30/42
R elocation
When program loaded into memory theactual (absolute) memory locations aredeterminedA process may occupy different partitionswhich means different absolute memorylocations during execution Swapping Compaction
-
8/3/2019 Chapter07 New
31/42
A ddresses
Logical R eference to a memory location independent
of the current assignment of data to memory.R elative A ddress expressed as a location relative to
some known point.
Physical or A bsolute T he absolute address or actual location in
main memory.
-
8/3/2019 Chapter07 New
32/42
R elocation
-
8/3/2019 Chapter07 New
33/42
R egisters Used during
ExecutionBase register Starting address for the process
Bounds register Ending location of the processT hese values are set when the process isloaded or when the process is swapped in
-
8/3/2019 Chapter07 New
34/42
R egisters Used during
ExecutionT he value of the base register is added toa relative address to produce an absoluteaddressT he resulting address is compared withthe value in the bounds register If the address is not within bounds, aninterrupt is generated to the operatingsystem
-
8/3/2019 Chapter07 New
35/42
Paging
Partition memory into small equal fixed -size chunks and divide each process intothe same size chunksT he chunks of a process are called pagesT he chunks of memory are called frames
-
8/3/2019 Chapter07 New
36/42
Paging
Operating system maintains a page tablefor each process Contains the frame location for each page in
the process Memory address consist of a page number
and offset within the page
-
8/3/2019 Chapter07 New
37/42
Processes and Frames
A .0 A .1 A .2 A
.3B.0B.1B.2C.0C.1C.2C.3
D.0D.1D.2
D.3D.4
-
8/3/2019 Chapter07 New
38/42
Page Table
-
8/3/2019 Chapter07 New
39/42
Segmentation
A program can be subdivided intosegments Segments may vary in length T here is a maximum segment lengthA ddressing consist of two parts a segment number and an offset
Segmentation is similar to dynamicpartitioning
-
8/3/2019 Chapter07 New
40/42
-
8/3/2019 Chapter07 New
41/42
Paging
-
8/3/2019 Chapter07 New
42/42
Segmentation