Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Dynamic Storage Allocation

 Algorithms for Allocation


 First-Fit
 Best-Fit
 Worst-Fit
 Algorithm for Delete
 Fifty-Percent Rule

Operating System Concepts 9.1 Silberschatz, Galvin and Gagne 2002


Dynamic Storage-Allocation Problem

How to satisfy a request of size n from a list of free holes.

 First-fit: Allocate the first hole that is big enough.


 Best-fit: Allocate the smallest hole that is big enough;
must search entire list, unless ordered by size.
Produces the smallest leftover hole.
 Worst-fit: Allocate the largest hole; must also search
entire list. Produces the largest leftover hole.

First-fit and best-fit better than worst-fit in terms of


speed and storage utilization.
50-percent rule: Given N allocated blocks another 1/2N
will be lost due to fragmentation  1/3 of memory lost.

Operating System Concepts 9.2 Silberschatz, Galvin and Gagne 2002


Fragmentation

 External Fragmentation – total memory space exists to satisfy


a request, but it is not contiguous.
 Internal Fragmentation – allocated memory may be slightly
larger than requested memory; this size difference is memory
internal to a partition, but not being used.
 Reduce external fragmentation by compaction
 Shuffle memory contents to place all free memory together in one
large block.
 Compaction is possible only if relocation is dynamic, and is done at
execution time.
 I/O problem
 Latch job in memory while it is involved in I/O.
 Do I/O only into OS buffers.
 50-percent rule – Given N allocated blocks another 1/2N will be
lost due to fragmentation  1/3 of memory lost.

Operating System Concepts 9.3 Silberschatz, Galvin and Gagne 2002


Best Fit vs. First Fit

 Memory sizes 1300 and 1200


 Requests: 1000, 1100, 250
 Request First-Fit Best-Fit
 1300, 1200 1300, 1200
 1000 300, 1200 1300, 200
 1100 300, 100 200, 200
 250 50, 100 stuck

Operating System Concepts 9.4 Silberschatz, Galvin and Gagne 2002


Best Fit vs. First Fit

 Memory sizes 1300 and 1200


 Requests: 1100, 1050, 250
 Request First-Fit Best-Fit
 1300, 1200 1300, 1200
 1100 200, 1200 1300, 100
 1050 200, 150 250, 200
 250 stuck 0, 200

Operating System Concepts 9.5 Silberschatz, Galvin and Gagne 2002


First-Fit Method

 Request a block of size N. Returns first available block


of size N. If no block available, then returns NULL
 AVAIL points to first available block

1. [Initialize] Q  AVAIL
2. [End of list?] P  Next(Q); If P = NULL return(NULL).
3. [Check size.] if Size(P)  N goto 4 else Q  P; goto 2.
4. [Reserve block.] K  Size(P) – N.
if K = 0 Next(Q)  Next(P) else Size(P)  K;
Return(P+K).

Operating System Concepts 9.6 Silberschatz, Galvin and Gagne 2002


First-Fit Method

 Improvements
 In step 4, if K < c (small constant) return block of size N +
K.

 Roving pointer. Start next search where previous search


ended. This prevents accumulation of small blocks in the
beginning of AVAIL.

Operating System Concepts 9.7 Silberschatz, Galvin and Gagne 2002


Free

 Assume AVAIL is sorted by memory location (if P 


NULL, Next(P) > P).
 return block of size N beginning at location P0.

1. [Initialize.] Q  AVAIL.
2. [Advance P.] P  Next(Q).
if P = NULL or P > P0 goto 3 else Q  P and repeat.
3. [Check upper bound.]
if P0 + N = P and P  NULL
N  N + Size(P); Next(P0)  Next(P)
else Next(P0)  P.
4. [Check lower bound.]
if Q + Size(Q) = P0
Size(Q)  Size(Q) + N; Link(Q)  Link(P0)
else Link(Q)  Po; Size(P0)  N.

Operating System Concepts 9.8 Silberschatz, Galvin and Gagne 2002


Improvements to Free

 Use doubly linked list and tags to free in constant time.

Operating System Concepts 9.9 Silberschatz, Galvin and Gagne 2002


Fifty Percent Rule

 If allocation and deletion are used continually in such a way that


the system tends to an equilibrium condition, where there are N
reserved blocks in the system, on the average, each equally
likely to be the next one deleted, and where the probability that
allocating a new block does not change the number of blocks is
p, then the average number of available blocks tends to
approximately 1/2pN.

A B C C B A B B B C B B

 N = A + B + C (number of reserved blocks)


 M = ½(2A + B + ),   {0,1,2} (number of available blocks)
 Average change in M when a block is freed is (C-A)/N and when
a block is allocated is (1-p)
 C - A - N + Np = 0  2M = N + A - C + 

Operating System Concepts 9.10 Silberschatz, Galvin and Gagne 2002

You might also like