MEMORY MANAGEMENT

mangledcobwebΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 5 μήνες)

76 εμφανίσεις

MEMORY MANAGEMENT

Review: What are the 2 main purposes of any o.s.? (Provide the user program a simple interface to
all the hardware and manage all the resources of the system.) Memory is one of the important
resources the o.s. must manage.

Review: Wh
at is the hierarchy of memory devices and the speed and cost of each? (cache:fast,exp;
RAM:medium,medium; disk:slow,cheap)

The part of the o.s.software that does this is called the "memory manager". Its jobs:

1. keep track of what parts are in use.

2. allocate memory to processes when needed.

3. deallocate when processes are done.

4. swapping, or paging, between main memory and disk, when disk is too small to hold all current
processes.

Note: Before multiprogramming, memory management was a s
imple job: When a user typed a
command, the o.s. copies the requested program from disk to RAM and executes it. After the
process finishes, the o.s. displays the prompt again. When another command is received, the new
program is loaded into memory, ove
rwriting the first one. The organization of memory was also
very simple. See p. 191. Early PCs were organized and ran this way and some palmtop computers
and embedded systems today.

1. FIXED PARTITIONS (of historical significance only) Divide memory i
nto n (not necessarily
equal) partitions at startup. Then either:

(1) When a job comes in determine the smallest partition the job would fit in. If that
partition is occupied, put the job in a queue. Disadvantage? It is possible that a larger
partition

goes unused while a queue of processes forms waiting for a particular smaller
partition. This can result in under use of the resource.

(2) Keep one queue of jobs needing memory and as partitions become available take
the job closest to the front of the
queue that will fit into that partition. Disadvantage? A
large partition of memory may be allocated to a very small process: under utilization again.

(3) Keep one queue of jobs and a partition becomes available, search the queue for
the largest waitin
g job that would fit into the partition. Disadvantage? This discriminates
against small jobs. (Starvation?) Often these are interactive processes which should receive
quick service.

(4) ) Keep one queue of jobs and a partition becomes available, searc
h the queue for
the largest waiting job that would fit into the partition but make sure one of the partitions is
small, or use a counter that keeps track of the number of times a job is passed over as too
small for the available partition. Have a policy t
hat after a certain count, that job is allocated
memory, wasting a large partition or not.


Review: What is the goal of multiprogramming? (To keep the CPU busy.)
Idealistically, if the average concurrent process computes 20% of the time, 5 of these
proc
esses should be kept in memory at a time. When would this not guarantee the CPU is
kept busy? (When all 5 processes block for I/O at the same time, which is possible.)

2. CPU UTILIZAITON FORMULA AND DEGREE OF MULTIPROGRAMMING.


If the probability that
a certain type process spends the fraction 'p' of it's execution time
waiting for I/O, when 'n' such processes are in memory the probability that all 'n' processes block for
I/O is p^n. This assumes all processes are independent. (Ex. probability of havi
ng 4 children all
girls.) This is not exactly the case in computers, since only one process can compute at a time (on a
single processor system). Therefore, even though the resulting curve is not an exact model,
CPU
utilization is given by 1
-
p^n.


Ex. I
f p = 20% and n = 5 of these processes are in memory, the probability that all 4
processes block is the product (1/5)*(1/5)*(1/5)*(1/5)*(1/5) = (1/5)^5 = 1/3125. 1
-

1/3125 =
3124/3125 = .99968. This is very close to 100% CPU utilization.

If only 2 suc
h processes were in memory, (1/5)^2 = 1/25. 1
-

1/25 = 24/25 = .96. This is only 96%
CPU utilization.


Ex.2 If p = 75%, how many processes would be needed to achieve 96% CPU utilization?



.96 = 1
-

p^n



p^n = .04



log
p
(p
n
) = log
p

(.04)




n

= log
(3/4)

(.04) = (log .04) / (log 3/4) = 11.189 ~ 12 processes


Ex 3 If p = 75%, how many processes would be needed to achieve 99% CPU utilization?



.99 = 1
-

p^n



p^n = .01



log
p
(p
n
) = log
p

(.01)




n = log
(3/4)

(.01) = (log .01) /
(log 3/4) = 16 processes

Note: in both interactive systems (where processes block waiting for keyboard input) and batch
systems (where processes block for disk reads) percent of time waiting for I/O is commonly 80% or
more! The above model is valid for m
aking predictions about CPU performance, even though the
model is only an approximation.

3. MULTIPROGRAMMING introduced TWO PROBLEMS with memory management:
Relocation and Protection. Review: what hardware solution exists to protect programs from being

overwritten by other programs? (base and limit registers) Recall our discussion of physical vs.
virtual memory. Remember: the compiler and linker do not know where in physical memory the
program will be stored and uses '0' as the starting address for
every program. The MMU (memory
management unit) converts a virtual address to a physical address using the base and limit registers.
This allows relocation "on the fly". Otherwise, all physical addresses would have to be calculated at
load time. This is

possible, but not efficient and does not solve the protection problem. When
addresses are calculated at load time, they are 'absolute addresses'. There is no way of stopping a
program from building an instruction that accesses any word in memory.

The
protection problem is solved with the use of base and limit registers. The MMU makes sure the
program does not access any memory below (less than) the address in the base register and the
program does not access any memory above where it should by using t
he limit register.
Note that
the base register would contain the starting address of the partition assigned to the process and the
limit register would contain the size of the partition.
This method is not used very much anymore.


4. SWAPPING

Timesharin
g systems and graphically oriented pc's often have more concurrent processes than main
memory can hold. Some processes must be kept on the hard disk temporarily.

Question: Is this a problem in purely batch systems? (No. Keeping the CPU busy is the prim
e
concern and just enough jobs can be admitted to the system and kept until completion. Then other
jobs can be admitted.)

Swapping refers to bringing a process in its entirety into memory, running it for a while, then
returning it back to the disk. When
the process is brought into memory again, it may not be at the
same location. All addresses in the process must be relocated, either by software when it is swapped
in, or by hardware during program execution. (Ex. if address 100 is to be accessed, the bas
e address
of the partition containing the process is automatically added to 100.) This method uses
variable
length partitions.

See p. 197. This provides better resource utilization, but requires a lot of work on
the part of the o.s. (overhead).

Question:

Does a particular process always require the same amount of space in memory? (No
-

the
data segments may grow. Therefore extra space for growth is often allocated. Variable length
partitions permit this. However, any extra space unused is not swapped

to disk with the process.
Extra space between processes allows one process to grow upward (perhaps for the heap of
dynamically allocated variables) as well as downward (perhaps for the stack of local variables and
return addresses). When the extra space

is filled and the process still needs more, it must be
swapped to disk until a larger partition is available.

5. KEEPING TRACK OF MEMORY


The size of an allocation unit must be made. This is a design decision.


Bitmaps
-


This is a table of bits with 1
bit representing each allocation unit of memory.
A '1' means that unit is occupied. '0' means it is free. The size of the bitmap depends on the
size of the allocation unit. Small allocation units provide good resource utilization but makes
the bitmap l
arge. The bitmap itself is taking up memory space! Large units may waste space
but have smaller bitmaps. The size of the bitmap is not the serious problem. The o.s. must
search for enough '0' bits to accomodate a process being brought into memory. Th
at is a
slow process because sequential '0' bits may be in 2 different allocation units.

Linked Lists
-

Each node has a field indicating whether the segment of memory contains a


process or not. If not, it is a 'hole'. The starting address of the alloca
tion




unit is stored in the node as well as the number of allocation units in the process or


hole. If this is a doubly linked list it is easy to look each way when a process exits,


to see if there is an adjacent hole with which to merge. see p.200

Algorithms for allocating memory

-

when linked list management is used.

1. First Fit
-

fast (as little searching as possible)
-

allocates first hole found that is large




enough.

2. Next Fit
-

almost exactly the same as First Fit except that it keeps t
rack of where it last




allocated space and starts from there for the next search instead of from the



beginning. Slightly better performance.

3. Best Fit
-

searches the entire list looking for a hole that is closest to the size needed by



the proc
ess. Slow. Also does not improve resource utilization b/c it tends




to leave many very small (and therefore useless) holes.

4. Worst Fit
-

the opposite of Best Fit. Chooses the largest available hole and breaks off




a hole that is large enough to b
e useful (i.e. hold another process). In practice



this has not been shown to work better than the others.

Question: Would breaking the list into 2 lists improve memory management? (It would




improve performance in that the hole list could be kept by

size, which




decreases the amount of searching when looking for a hole big enough for




a new or returning process. However, when deallocating memory, it is very



time
-
consuming to search for the place in the hole list and insert the new hole



node

there. How does 2 lists effect the above algorithms? Best Fit and Worst



Fit become equally fast. Next fit and First Fit don't make sense.


VIRTUAL MEMORY

Precursor: Overlays. This required the programmer to break up a program into modules that coul
d
be kept on disk and swapped into memory, usually one at a time. The operating system just loaded
module 0 first and when that finished, loaded the next module. This method was quickly replaced
by virtual memory (1961).

Primary concept: the entire sp
ace needed for the program code, data and stack MAY exceed the
amount of physical memory available. The O.S. keeps those parts of the program currently in use in
memory and the rest on disk. Why does this work in a multiprogramming system? (When a proce
ss
is waiting for something to be brought in from disk, the process is blocked: waiting for I/O and
cannot run. At that time the CPU can be given to another process.




1. Paging
-

a virtual memory system technique used in memory management.


Program
generated addresses are called VIRTUAL ADDRESSES and form the virtual
address space. A virtual address goes to the MMU that maps the virtual address onto the physical
memory address. The entire core image of a program must be kept on disk. Only part of
the
program can be in memory. The virtual address space is divided into PAGES. The physical address
space has corresponding units of space called PAGE FRAMES, which are always the same size as
the pages. See ex. p. 203
-
4.


In the actual hardware, a "pre
sent/absent bit" keeps track of which pages are in physical
memory. When a program tries to access an address whose page not in memory, a trap called a
PAGE FAULT occurs. The O.S. picks a little
-
used page frame and swaps it to disk making room to
fetch
the needed page.


An incoming virtual address is split into 2 parts: a few bits on the left for the page number
and the rest for the offset, i.e. where the actual address lies within the page.

Ex. An incoming 16 bit virtual address is split into:


a
4 bit page number (i.e. there must be 2^4 or 16 pages in the entire virtual address space)
and a 12 bit offset (i.e. the page has 2^12 or 4096 addressed bytes in the page.

virtual address: 20500 is 20 bytes from the start of the virtual page at 204
80


20500 in binary is 0101000000010100 using 16 bits


'0101' or 5 is the beginning of the virtual page on which this address is found



'000000010100' or 20 is the offset, or how far from the beginni
ng of the page









the actual address is found.

****On the page frame the address will have the same offset**********