memory management functions partitioning - allocation

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

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

73 εμφανίσεις

csci2410 Systems Programming
Memory Management I (1)
memory management functions
•efficiently allocate & releasememory as processes
start & finish
•contiguous block for whole process?
called memory partitioning
•fit chunks of process wherever they will fit?
called memory paging
•translateaddresses fast
•map logical addresses in program to physical
addresses
•several translations for each instruction
•protect each process memory areas from interference
by another process
csci2410 Systems Programming
Memory Management I (2)
partitioning -allocation
•loads the whole process into a single contiguous memory
area
•partitioning may use
•fixed partitions
memory divided intoN regions with fixed boundaries
not necessarily all the same size
or
•variable partitions
memory isinitially single free area
when process P
i
loaded, allocate free |P
i| blocks
from contiguous region R
i
•partitioning is poor because it is
•inflexible
•susceptible to fragmentation
csci2410 Systems Programming
Memory Management I (3)
partitioning –allocation algorithms
how do you decide which partition to load a process into?
•best fit
choose smallest free partition in which process will fit
because that wastes least memory for each process
•worst fit
choose largest free partition in which process will fit
because
•wastes maximum space per allocation
•assuming ‘waste’ may be large enough to be usable
•first fit
choose first free partition in which process will fit
because
•should smooth out large / small fragments
•migrates free space towards end of memory
csci2410 Systems Programming
Memory Management I (4)
partitioning -address translation
•to execute a program statement in process P of the form
a = myfunction(b.c)
the system must know the physicaladdressin memory of
variablesa, b& cand of the start ofmyfunction
•but we want to be able to allocate P to any partition
so these physical addresses will vary from run to run
•to make programs relocatableprogram cannot contain any
actual addresses so
•compiler generates a list of all the names in the program
•linker converts each name to a logical address
logical address = offset from process origin
•when program is loaded, loader stores the process origin
in a base register
•in each instruction, each logical address is translated to
its current physical address
csci2410 Systems Programming
Memory Management I (5)
partitioning -memory protection
•no process should be able to write into or read from the
memory allocated to another process
•consider
char array[4] = {`a’,`b’,`c’,`d’};
printf(“arrayis %s\n”, array);
or
char array[5] = “abcd”;
if (s = 0)
{ printf(“arrayis %s\n”, array);} what may happen when these are executed? why?
•loader also stores end address of partition in a limit register
•if (physical address < base register) ||
(physical address > base register + partition size)
a memory violation(segmentation error) occurs
csci2410 Systems Programming
Memory Management I (6)
partition N
Process A
offset (logical address)
0x001208
X
base register
0x400000
limit register
0x500000
physical address
0x401208
physical = base register + logical address
csci2410 Systems Programming
Memory Management I (7)
partitioning -fragmentation
•fragmentationoccurs when there are areas scattered through
memory which are too small to be usable
•fixed partitioning with N partitions
•table of N entries with each entry specifying start
address & size of region and free / allocated flag
•too inflexible to be useful
•small processes leave wasted memory in partitions -
internal fragmentation
•variable partitioning
•requires dynamic linked list with each entry specifying
start addresses (R.start) & size (R..size)
•as processes finish and new ones start, wasted space
between partitions –external fragmentation
•so list must be regularly compactedto collect unused
space into larger chunks
any process can be relocated but how often should you
do this?
csci2410 Systems Programming
Memory Management I (8)
segmentation
•each process consists of
•code
•data
•stack (state)
•these don’t have to be allocated together –can be loaded
into separate segments
•reduces maximum size of offset
•allows smaller partitions to be used
•allows code to be shared between processes
•each segment will need a separate base registerand limit
registerfor each segment
actual code address = CS + offset
actual data address = DS + offset
actual stack address = SS + offset
csci2410 Systems Programming
Memory Management I (9)
paging & page frames
•contiguous allocation of partitioning is impractical
but provides fast and easy address translation & memory
protection
•need a mechanism which preserves
•the fast address translation & memory protection
•without overhead involved in correcting fragmentation
•paging divides RAM into small fixed-size page frames
•with a page frame size of K bytes, if ProcessArequires nK
total memory, it is allocated to n page frames which
•may be scattered through memory
•leave a wasted fragment only in last page
e.g.
page frame size = 2K (2
11)
RAM = 2M (2
21)
total no. of page frames = 2
21
/ 211
= 210
ProcessAneeds = 512K (2
9
* 210)
= 219 / 211
= 28
pages
page frames needed = 256
csci2410 Systems Programming
Memory Management I (10)
logical pages
•each process is divided into logical pagessuch that each
page fits in a single page frame
•if size of page frame = 2K (2
11) bytes
maximum offset in a frame = 2
11
needing 11 bits to specify
•size of logical address space= maximum offset in process
= 2 address size
if operating system uses 16bit addressing ,
maximum offset = 2
16
(64K) = logical address space
•divide 16 bit address into 2 parts
•bottom 11 bits = offset in page
•top 5 bits =logicalpage number
•theoretical maximum number of pages =
logical address space / pagesize
e.g. 2
16
/ 211
= 25
= 32
csci2410 Systems Programming
Memory Management I (11)
address translation -paging
•with partitioning
•single base register is loaded with partition start
•added to logical address to give physical address
•with pages scattered over multiple page frames, need
equivalent of multiple base registers to translate offsets in
each page
•with appropriate page size, a transparent mapping is trivial
A page0 A page1 Bpage0 A page2 C page0 Bpage1......
32K 34K 36K 38K 40K 42K
csci2410 Systems Programming
Memory Management I (12)
0 0 0 0 1 1 0 0 0 1 1 0 1 0 1 1
15 11 10 0 bit
page number offset in page
address translation -paging
•Logical Logical
Address Page Offset in Page
30 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0*2K + 3
2052 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1*2K + 4
6153 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 3*2K + 9
•if logical page 1 is allocated to page frame 13 (0 1 1 0 1)
logical address (1*2K + 4 )
0 0 0 0 10 0 0 0 0 0 0 0 1 0 0
translates to
physical address (13*2K+ 4)
0 1 1 0 10 0 0 0 0 0 0 0 1 0 0
K 20 22 24 26 28 30 32 34 36 38 40 42 44 46
Frame 10 11 12 13 14 15 16 17 18 19 20 21 22 23
A0 A1 A2