Operating Systems COMP 4850/CISG 5550 Basic Memory Management Swapping

Download Report

Transcript Operating Systems COMP 4850/CISG 5550 Basic Memory Management Swapping

Operating Systems
COMP 4850/CISG 5550
Basic Memory Management
Swapping
Dr. James Money
Memory Management
• One of the most important resources to
manage is memory
• Here, we typically refer to managing RAM
in the memory hierarchy, however, we
involve several members
• The part of the OS that handles this is
called the memory manager
Memory Hierarchy
Basic Memory Management
• There are two classes for memory
management systems:
– Systems that move processes back and forth
from main memory to disk
– System that do not do this swapping
• We will first consider the second case,
which is a simpler implementation
Monoprogramming
• The simplest scheme is to run just one
program at a time
• The memory is shared between the OS
and the program in memory
• There are various ways this setup can be
arranged
Monoprogramming
• Three models:
– OS at bottom of memory, with user program
at top
– User program with OS in ROM at top of
memory
– OS at bottom and device drivers in ROM at
top with user program in between. The ROM
contains the BIOS (Basic Input Output
System)
Monoprogramming
Monoprogramming
• In this setup, one program runs at a time
• Typically, a command shell or similar program
•
•
•
issues a prompt and gets the program to run
Then the program is read from disk into
memory and executed
The program exists and the command shell
runs again, replacing the exited process
Typically only used on embedded systems
Multiprogramming with Fixed
Partitions
• Most modern OSes allow you to run
multiple programs simultaneously
• This allows the CPU to be utilized even
when one process is blocked for I/O
• The easiest approach is to divide memory
into n, possibly unequal, partitions of
memory
Multiprogramming with Fixed
Partitions
• This partitioning can be done when the OS
first starts up
• Each partition has an input queue
associated with it
• Then, when a job arrives it is put into an
input queue for the smallest partition to
hold it
• However, unused partition space is lost
Multiprogramming with Fixed
Partitions
• The problem with multiple input queues is
it can waste CPU time
• Small jobs may wait for the smaller
memory partition despite the fact the
large memory partition might be free
• An alternate approach is to use a single
input queue
Multiprogramming with Fixed
Partitions
• In a single input queue scheme, when a
partition becomes free, the job closest to
the front of the queue that fits is loaded
into the slot
• Another approach is to search the entire
queue for the best fit into the free
partition
• This discriminates against small jobs and
tends to be avoided
Multiprogramming with Fixed
Partitions
Multiprogramming with Fixed
Partitions
• One solution for the small jobs is to have
at least one small partition
• Another option is to not skip over a job
more than k times
• This system was used by IBM mainframes
in OS/360 called MFT or OS/MFT
• Most OSes do not use this scheme today
Modeling Multiprogramming
• When we use multiprogramming, we
increase CPU utilization
• In a crude analogy, if the average process
only uses 10% of the CPU, then 10
processes will keep the CPU busy all the
time
• A basic assumption here is not all of the
processes are waiting for I/O at the same
time
Modeling Multiprogramming
• An improved model is to use a
probabilistic setup
• Assume that a process spends the fraction
p of its time waiting for I/O to complete
• When n processes in memory at once, the
probability that all n processes are waiting
for I/O is
pn
Modeling Multiprogramming
• Thus, we see that CPU utilization is given
by the difference,
CPU Utilization = 1-pn
• The CPU utilization as a function of n is
called the degree of multiprogramming
Modeling Multiprogramming
f(n)
=
1-pn
n
Modeling Multiprogramming
• Note that if a process spends 80% of its
time waiting on I/O, then at least 10
processes are needed in memory to get
the waste below 10%
• Realize that 80% may be a conservative
estimate, given waiting for keyboard input
• This model assumes each process is
separate
Modeling Multiprogramming
• However, even with its simplicity, we can
make predictions about performance
• Suppose a system has 32MB of memory,
the OS requires 16MB and each program
needs 4MB
• With 80% wait time, we have utilization
1-0.84=0.5904
Modeling Multiprogramming
• If we add another 16MB, we go to 8 way
multiprogramming and we have utilization
of
1-0.88 = 0.8322784
• This is a 38% increase
• If we repeat with another 16MB though,
we get 93%, only a 10% improvement
• We can use this to decide how much RAM
to buy
Analysis of Multiprogramming
• We can use this model to analyze batch
systems
• Suppose the average wait is 80% and four
jobs are submitted at 10:00, 10:10, 10:15,
and 10:20 with CPU minutes needed of
4,3,2, and 2 each respectively
• For the first process, with only 20% used
CPU time, only 12 seconds of CPU time is
allocated every minute
Analysis of Multiprogramming
• The process will need
0.2 x = 4
x = 4/0.2 = 20 minutes
to finish.
• From 10:00-10:10, job 1 gets 1/5 of that
time of 2 minutes of work done
• At 10:10, job 2 arrives
Analysis of Multiprogramming
• Now with 2 jobs, the utilization increases
to 36%, but each process only gets
36%/2 = 18% of that
• Thus the processes go from 0.2 CPU
minutes per minute to 0.18
• At 10:15, we get job 3, we have 0.16 CPU
minutes per process
Analysis of Multiprogramming
Relocation and Protection
• Two problems need to be solved in
multiprogramming
– Relocation – how to get new addresses for
program
– Protection – how to prevent processes from
overwriting each other
Relocation and Protection
• When a program is linked, it is combined
together with libraries and user
procedures and must know the address
the program will be in memory
• Suppose the first instruction is a call to
absolute address 100 in the binary
program
Relocation and Protection
• If the program is loaded into partition 1 at
memory location 200K, then the program
will jump to location 100 in the OS most
likely
• However, what we want to happen is to
jump to address 200K+100
• This is called the relocation problem
Relocation and Protection
• One solution is to modify the instructions
as the program is read into memory
• The binary program has a list of addresses
to be relocated
• This solves the relocation problem, but
does not address protection
• A program could craft a new instruction
and jump to that location
Relocation and Protection
• For OS/360, IBM choose a protection
mechanism that divides the memory in
2KB blocks and uses a 4 bit protection key
for each block
• The program status word (PSW) contained
this 4 bit key and if it did not match, then
the program could not write to the
address
• Only the OS could modify the PSW value
Relocation and Protection
• An alternative to both relocation and protection
•
•
is to use two special hardware registers called
base and limit registers
When a program is loaded the base is set to the
start of the memory location which is added to
every memory reference and limit which
contains the end of its partition
In our example, the base would be 200K, so that
a reference to address 100 would be in effect
200K+100
Relocation and Protection
• When a memory reference is made, the
limit register is consulted to make sure
there is not a reference beyond the limit
register
• The comparison is quick test, so this does
not require extra circuits
Relocation and Protection
• The disadvantage is the need for the
addition which can be slow
• This can be eliminated with special
addition circuits
• CDC 6600 – first supercomputer used this
• Intel 8088(cheaper 8086) had base but no
limit registers
Swapping
• With current OSes the scenario is different
with regards to fixed partitions of memory
• We want to have dynamically sized
partitions
• In addition, we sometimes cannot fit the
entire program into memory at once
Swapping
• There are two approaches depending on
the type of hardware available
– Swapping – simplest, brings each process
into/out of memory as a whole unit from disk
– Virtual memory – allows portions of a process
to be brought in/out of memory from disk
• We first look at swapping
Swapping
Swapping
• The major difference with the partitioning
scheme now is the ability to have dynamic
partition sizes and numbers
• This improves memory utilization, but at
the cost of keeping track of this freedom
• This tracking can be done with bitmaps or
linked lists of free and used partitions
Swapping
• Swapping creates the problem of multiple holes
•
•
in memory
It is possible to move all the processes
downward in memory as far as possible, which is
called memory compaction
This is not done until a last resort since it wastes
a lot of CPU time – a 256MB machines which
moves 4 bytes in 40 nsecs takes 2.7 sec to
compact all of memory
Swapping
• One point of concern is how much
memory to allocate for a process when it
is created or swapped into RAM
• If they are fixed size, then there is no
problem
• However, many times the process data
segment can grow due to dynamic
memory heaps
Swapping
• If there is a hole in an adjacent block,
then it can be allowed to grow into it
• If there is not a hole, there we have a
problem
• We may have to swap out other process
when the current process needs more
room for the data segment
Swapping
• If we know the process will grow, then we
can give it extra memory when it starts or
swapped into memory
• However, when it is swapped to disk, we
do not save the unused portions
Swapping
• If there are two segments, such as a data
and stack segment, then we can have
them growing toward each other at
opposite ends of the partition
• There is typically a barrier that marks
where one segment cannot cross another
• If it requests to do so, the OS has to
allocate a bigger partition
Swapping