Chapter 7 Physical Memory • Preparing a program for execution • Memory partitioning schemes • Allocation strategies for variable partitions • Managing insufficient memory
Jan 08, 2016
Chapter 7 Physical Memory
• Preparing a program for execution
• Memory partitioning schemes
• Allocation strategies for variable partitions
• Managing insufficient memory
7.1 preparing a program for execution
• Program transformations compilation/assembly linking loading
Source module1
Object module1
Source module2
Object module2
Source module3
Object module3
. . .
Load module (in secondary
memory)
Load module (in main memory)
translation linking loading execution
• Logical-to-physical address binding
notations: binding( relocation) static binding dynamic binding (dynamically) relocatable
Fun1(){ … fun2(); …}
Fun2(){ …}
… call m2,fun2 …
//fun2 …
… Call Ladd1 …Ladd1: //fun2 …
… Call Fadd1 …Fadd1: //fun2 …
• Static binding programming-time binding compile-time binding link-time binding load-time binding
int i;
……
i= …;
……
f();
……
……
Store 20
……
Branch f
i20
0Function f
……
……
Store 120
……
Branch 0
i
100
0
120
Function f
……
……
Store 1120
……
Branch 1000
i
1100
1000
1120
• Dynamic binding
the binding occurs at runtime, i.e., immediately preceding each memory reference. This delays the binding of the program to the machine until the latest possible moment.
Physical_address=address_map(logical_address)
Processor Main memory
Address_map
Logical
address
Physicaladdress
Data transfers(R/W)
Processor Main memory
Relocation register
Logical
addressPhysicaladdress
Data transfers(R/W)
+
Physical_address=logical_address+ RR
int i;
……
i= …;
……
f();
……
……
Store 20
……
Branch f
i20
0Function f
……
……
Store 120
……
Branch 0
i
100
0
120
Function f
……
……
Store 120
……
Branch 0
i
1100
1000
1120
Address map
7.2 Memory partitioning schemes
• Fixed partitions
properties: the sizes of partitions are determined statically the sizes of partitions cannot be changed at runtime partitions have different sizes to accommodate
the different programs
Schemes to schedule for the partitions: using a separate queue for each partition using a common queue
processes
OS
queues
processes
OS
queues
• Variable partitions
properties: the sizes of partitions are determined at
runtime the sizes of partitions equal the exact amount of
space requested memory consist of variable size blocks,
alternating between occupied blocks and free blocks
A B C D E
memory management: requests to allocate free memory areas
AB C
A’B C
AB
B C’
AB C
C
AB C
C
B’ B’
• Linked list implementation
freesize A
occupied
size B
occupied
size Cfreesize E
occupied
size D
• Bitmap implementation
00011111 11100000 …
1-KB block is represented by one bit( A, B, C, D, E: 3KB, 2KB, 5KB, 1KB, 5KB)
Releasing block:
Allocating block:
Searching block:
B [i]=b [i] & ‘11011111’
B [i]=b [i] | ‘11000000’
B [0]=b [0] & ‘10000000’
B [0]=b [0] & ‘01000000’
……
• The buddy system assumption:
fixed number of possible hole sizes, and each of which is a power of 2 buddies:
any hole of size 2i can be divided into two holes of size 2i-1, these two holes are called buddies implementation:
0 4 6 8 12 14
H
01234 .....
allocation( a request for n unit)find the smallest hole size such that n <=2i;
if list I is not empty
remove a hole form the list&allocate;
else
find a larger hole list( not empty) than list I;
remove and divide into two holes(l&r) of half of size;
place r on the next-lower list;
if hole l is the smallest one for the request
allocate;
else
divide repeatedly;
release( for the block of size 2i)If the buddy of the block is occupied
the new hole is added to the list i;
Else
remove the buddy from the list I;
coalesce the two holes into a hole of size 2i+1;
repeat until the largest possible hole is created;
7.3 Allocation strategies for variable partitions
• First-Fit:finding the first hole large enough to accommodate the given request.
• Next_Fit( rotating-first-fit)starting each search at the point where the previous search stopped.
• Best_Fitselecting the hole with the closest fit.
• Worst_fitusing the largest currently available hole for any given request.
• Measures of memory utilization
Equilibrium state:
Prob(release)==Prob(request)
Prob(n++)==Prob(n--)
AB C AB
AB C AB C
C
a b
c d
m=a+b+c+d (7.1)
n= (7.2)
N=d+b (7.3)
Prob(n++)=Prob(release)*a/m (7.4)
Prob(n--)=Prob(release)*d/m+Prob(request)*(1-p) (7.5)
A=d+(1-p)m (7.6)
22 cbd
)1( pmd
ma
m=d+(1-p)m+b+c+d
=(1-p)m+2b+2d
=(1-p)m+2n
Conclusion1:n=0.5pm
The fraction of memory occupied by holes:
f =?
Assumption:
average hole size: h
average occupied block size: b
k=
M=nh+mb
=0.5kmb+mb
=mb(0.5k+1)
Conclusion2:
bh
2)15.0(5.0
kk
kmbkmh
Mnh
f
7.4 Managing insufficient memory
• Memory compaction
~~ ~~
2
3
4
2
5
5
9 ~~ ~~
3
2
5
20
~~ ~~
3
2
11
5
9 ~~ ~~
3
2
11
5
9
p1
p2
p3
p1
p2
p3
p3
p1
p2
p2
p1
p3
• Swapping
creating new space by selecting one of the resident processes and temporarily evicting it to secondary storage.
properties: can always evict as many resident
processes as is necessary; affects only one or a small number of
processes; requires accesses to secondary
memory.
• Overlays
different portions of the program replace( overlay) each other in memory as execution proceeds.
A
B C
D E
0A
B C
A
D
A
C
E
FAQs
• 3.1 m.A() x=11,y=9;m.A() x=12,y=10;m.B() x=11,y=10;m.B() x=10,y=10;m.B() x=10,y=10;m.B() x=10,y=10;m.A() x=10,y=8;m.A() x=10,y=8;
• 3.3monitor stack{
char array[MAX];
UINT bottom=MAX-1,top=MAX-1;
condition c;
void push(char x){
if(bottom-top<MAX)
array[top--]=x;
if(bottom-top==1)
c.signal;
}
void pop(char &x){
if(bottom==top)
c.wait;
x=array[++top];
}
}
• 4.8Pb(s){
do{
R=0;
SWAP(R,S);
while(!R);
}
Vb(s){
s=1;
}
• 4.13f(x){
P(mutex);if(x){
condcnt_c1++;if(urgentcnt)
V(urgent);else
V(mutex);P(condsem_c1);condcnt_c1--;
}x++;////////////////if(condcnt_c2){
urgentcnt++;V(condsem_c2);P(urgent);urgentcnt--;
}////////////////x=0;if(urgentcnt)
V(urgent);else
V(mutex);
• 4.19
500 20Hardware timers
Wall-clock countdown
Timer queue TQ p1 20 p2 125 p3 50 p4 15
520 70Hardware timers
Timer queue TQ p1 70 p2 55 p3 50 p4 15
590 55Hardware timers
Timer queue TQ p1 55 p2 15 p3 35 p4 15