basic memory management
TRANSCRIPT
-
8/8/2019 Basic Memory Management
1/36
Memory Management
-
8/8/2019 Basic Memory Management
2/36
Readings
4.1 and 4.2 of the text book
-
8/8/2019 Basic Memory Management
3/36
Introduction
Our machines today have 10,000 times more memory than the IBM 7094
leadingedgemachineofthe1960 s
Cost of memory had dropped dramatically
Bill Gates (former chair of Microsoft) once said 640K should be enough
Our programs tend to expand to fill the memory available
Technology does not allow for each program to have infinitely large and fast
memory
Operating systems must manage memory
-
8/8/2019 Basic Memory Management
4/36
Whatifaprogramislargerthanphysical
memory?
Registers
On-chip Cache
Main MemoryMagnetic (Hard) Disk
Magnetic Tape
Memory Hierarchy
-
8/8/2019 Basic Memory Management
5/36
Memory Management
We will review different approaches:
r Memorymanagementintheabsenceofmultiprogramming
r Memory management using fixed partitions
r Memory management using dynamic partitions
r Virtual memory
-
8/8/2019 Basic Memory Management
6/36
Basic Memory Management
Three different ways to organize an operating system withone
user process
(Palm computers, iPhones) (MS-DOS)
BIOS
-
8/8/2019 Basic Memory Management
7/36
Basic Memory Management
One user program at a time
When a command is typed, the OS copies the requested program from
disk to memory and executes it
Newprogramstobeloadedoverridetheprogramcurrentlyinmemory
-
8/8/2019 Basic Memory Management
8/36
Multiprogramming with Fixed Partitions
Having multiple processes running at once means that when one
process is blocked waiting for I/O to finish another can use the CPU
Need to divide up memory among programs
Simpleapproach:Dividememoryupintonpartitions
r Partitions do not necessarily have to be of the same size
-
8/8/2019 Basic Memory Management
9/36
Multiprogramming with Fixed Partitions
Multiple Queues
r Jobisputintotheinputqueueforthesmallestpartitionlargeenoughto
hold it
Single Queuer Job that is closest to the front of the queue that fits in it could be loaded
into the empty partition
r Alternatively search the entire input queue whenever a partition becomes
free and pick the largest job that fits
-
8/8/2019 Basic Memory Management
10/36
Multiprogramming with Fixed Partitions
(a) Separate input queues for each partition
(b) Single input queue
-
8/8/2019 Basic Memory Management
11/36
Questions
Single Queue:
r Whatistheproblemwithtakingthefirstjobthatfitsintothepartition?
r What is the problem with taking the largest job that fits into the
partition?r What do you thinkinternal fragmentationis?
Multiple Queues:
r What is the disadvantage?
-
8/8/2019 Basic Memory Management
12/36
Multiprogramming with Fixed Partitions
Was used by OS/360 on large IBM mainframes for many years
Incoming jobs were queued until a suitable partition was available
Today no modern OS uses fixed partitions
-
8/8/2019 Basic Memory Management
13/36
Swapping
Dynamic partitions
r Allocate as much memory as needed by each process
One approach to managing memory is to useswapping.
Swappingconsists of:r Bringing in each process in its entirely
r Running it for a while
r Putting it back on the disk.
The number of partitions, location and size vary dynamically asprocesses come and go
-
8/8/2019 Basic Memory Management
14/36
Graphical Depiction of Swapping
-
8/8/2019 Basic Memory Management
15/36
Swapping - Example
Memory allocation changes as
r processes come into memory
r leave memory
Shadedregionsareunusedmemory
-
8/8/2019 Basic Memory Management
16/36
Questions
What would happen if you swapped a process memory space that is not
idle?
WhatwouldhappenifyouswappedoutaprocesswaitingforanI/O
operationthatputsdataintobuffers?
-
8/8/2019 Basic Memory Management
17/36
How Much Memory to Allocate
How about allocating a fixed size of memory when the processes is
created or swapped in?
r Simple, but a process s data segment may grow through dynamic
allocation of memory How about dynamic allocation of memory?
r Let s say there is a unused contiguous memory (hole) adjacent to the
process s memory
r That s great it can be used.
r What if there isn t?
-
8/8/2019 Basic Memory Management
18/36
How Much Memory to Allocate
How about dynamic allocation of memory?
r Let ssaythatthememoryadjacenttoaprocess sallocatedmemoryis
being used by another process?
r
Do you move the process that needs more memory?r Do we swap out other processes to make room to create a large enough
hole?
r Why not allocate a little additional memory to allow for growth?
Note: It can still run out
-
8/8/2019 Basic Memory Management
19/36
How Much Memory to Allocate?
(a) Allocating space for growing data segment
(b) Allocating space for growing stack & data segment
-
8/8/2019 Basic Memory Management
20/36
Question
Should unused memory allocated to a process be swapped to disk?
-
8/8/2019 Basic Memory Management
21/36
Keeping Track of Holes
When a process terminates:
r Compactmemory?
Move all processes above the hole down in memory.
Can be very slow: 256MB of memory, copy 4 bytes in 40ns compacting memory in 2.7 sec
Almost never used
Result: OS needs to keep track ofholes.
Problem to avoid: memoryfragmentation.
Two approaches to keeping track of holes:r Bit maps
r Linked lists
-
8/8/2019 Basic Memory Management
22/36
Memory Management with Bitmaps
Memory is divided up into allocation units
r Variesfromafewwordstoseveralkilobytes
A bit in a bitmap is associated with each allocation unit
-
8/8/2019 Basic Memory Management
23/36
Swapping Data Structure: Bit Maps
Partofmemorywith5processes,3holes
r tickmarksshowallocationunits
r shaded regions are free
Corresponding bit map
-
8/8/2019 Basic Memory Management
24/36
Properties of Bit-Map Swapping
Allocation unit is k bits
bitmapusesM/kbits,whereMistheamountofmemory;Couldbequite
large.
Searchingbit-mapforaholeisslow
-
8/8/2019 Basic Memory Management
25/36
Memory Management using Linked Lists
Maintain a linked list of allocated and free memory segments
A segment is a process or a hole between two processes
Each entry in the list specifies
r A hole (H) or process (P)
r The address at which it starts,
r The length
r Pointer to the next entry
-
8/8/2019 Basic Memory Management
26/36
Swapping Data Structure: Linked Lists
Keep a list of blocks (process=P, hole=H)
As time goes on, memory becomes more and more fragmented; Memory
utilizationdeclines
This is referred to asexternal fragmentation.
-
8/8/2019 Basic Memory Management
27/36
What Happens When a Process Terminates?
What happens when a process terminates (or is swapped out)?
There are four possibilities as seen on the next slide
-
8/8/2019 Basic Memory Management
28/36
What Happens When a Process Terminates?
Merge neighboring holes to create a bigger hole
-
8/8/2019 Basic Memory Management
29/36
What Happens When a Process Terminates?
Searching for the memory allocated is easy since most operating
systems have a pointer to the memory allocated for the process
-
8/8/2019 Basic Memory Management
30/36
Hole Selection Strategy
We have a list of holes of sizes
10, 20, 10, 50, 5
A new process is created that needs size 4.
Which hole to use?
Lots of algorithms
-
8/8/2019 Basic Memory Management
31/36
First/Best/Next/Worst/Quick Fit
First fit: For a process of sizes, find first hole larger thans
Best fit: For a process of sizes, use smallest hole that
has size(hole) >= s.
Worst fit: find the biggest hole that fits.
Quick Fit: maintain separate lists for common block sizes.
-
8/8/2019 Basic Memory Management
32/36
Problems
Relocation: The mechanism for fixing memory references in memoryr Cannot be sure where program will be loaded in memory: address locations of variables, code routines
cannot be absolute
Protection: One process should not be able to access another processes memory partition
Solutions:r Relocationduringloading
r Program Status Word (protection)
r Base and limit (protection and relocation)
-
8/8/2019 Basic Memory Management
33/36
Relocation
Alogical address(orvirtual address) is a reference to a memory
location independent of the current assignment of data
Arelative addressis a logical address
r Addressisexpressedasalocationrelativetosomeknownpoint
Aphysical addressis an actual location in main memory
-
8/8/2019 Basic Memory Management
34/36
Relocation using Base and Limit Registers
Thebase registeris loaded with the starting address in main memory of the
program
Thebounds registerthat indicates the ending location of the program
These values must be set when the program is loaded into memory or when the
processimageisswappedin
Mapping logical address to physical address:
r Value in the base register is added to the relative address to produce a physical address
r Resulting address is compared with the value in the bounds register
-
8/8/2019 Basic Memory Management
35/36
Where Found?
Older versions of Unix
Microsoft Windows 3.1 operating systems
-
8/8/2019 Basic Memory Management
36/36
Summary
Using fixed-size and variable-size partitions are inefficient in the use
of memory
Fixed-size results in internal fragmentation
Variablesizeresultsinexternalfragmentation