280 likes | 364 Views
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. translation.
E N D
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 translation linking loading execution Source module1 Object module1 Source module2 Object module2 Load module (in secondary memory) Load module (in main memory) . . . Source module3 Object module3
Logical-to-physical address binding notations: • binding( relocation) • static binding • dynamic binding • (dynamically) relocatable Fun1(){ … fun2(); … } … call m2,fun2 … … Call Ladd1 … Ladd1: //fun2 … … Call Fadd1 … Fadd1: //fun2 … Fun2(){ … } //fun2 …
Static binding • programming-time binding • compile-time binding • link-time binding • load-time binding 0 1000 Function f …… …… Store 120 …… Branch 0 Function f …… …… Store 1120 …… Branch 1000 0 …… …… Store 20 …… Branch f int i; …… i= …; …… f(); 100 1100 20 i 120 1120 i i
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) Address_map Physicaladdress Logical address Main memory Processor Data transfers(R/W)
Relocation register Logical address Physicaladdress + Main memory Processor Data transfers(R/W) Physical_address=logical_address+ RR 0 1000 Function f …… …… Store 120 …… Branch 0 Function f …… …… Store 120 …… Branch 0 0 …… …… Store 20 …… Branch f int i; …… i= …; …… f(); 100 1100 20 i 120 1120 i i 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
OS OS processes processes queues 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 B A C B A C B A’ C B C’ B A C B A C B’ C B’
Linked list implementation free size occupied size occupied size occupied size free size A B C D E
… 00011111 11100000 • Bitmap implementation 1-KB block is represented by one bit( A, B, C, D, E: 3KB, 2KB, 5KB, 1KB, 5KB) Releasing block: B [i]=b [i] & ‘11011111’ Allocating block: B [i]=b [i] | ‘11000000’ Searching block: 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: H . 4 . 3 . 2 . 1 . 0 0 4 6 8 12 14
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_Fit selecting the hole with the closest fit. • Worst_fit using the largest currently available hole for any given request.
Equilibrium state: Prob(release)==Prob(request) Prob(n++)==Prob(n--) • Measures of memory utilization B A C B A C a b B A C B A C 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)
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:
7.4 Managing insufficient memory • Memory compaction p2 2 2 p1 3 p1 3 p1 3 p2 p2 3 2 2 p1 4 p3 5 p2 2 11 5 11 20 p3 5 5 5 p3 p3 ~ ~ 9 ~ ~ ~ ~ ~ ~ ~ ~ 9 ~ ~ ~ ~ 9 ~ ~
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. 0 A A A A B C B C C D E D 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.3 monitor 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.8 Pb(s){ do{ R=0; SWAP(R,S); while(!R); } Vb(s){ s=1; }
4.13 f(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);
p4 p2 p4 p3 p2 p4 p1 p3 p1 p2 p3 p1 125 15 15 35 50 20 15 55 15 70 55 50 Wall-clock countdown Hardware timers 500 20 • 4.19 Timer queue TQ Hardware timers 520 70 Timer queue TQ Hardware timers 590 55 Timer queue TQ