140 likes | 732 Views
Dynamic Storage Allocation. Algorithms for Allocation First-Fit Best-Fit Worst-Fit Algorithm for Delete Fifty-Percent Rule. Dynamic Storage-Allocation Problem. First-fit : Allocate the first hole that is big enough.
E N D
Dynamic Storage Allocation • Algorithms for Allocation • First-Fit • Best-Fit • Worst-Fit • Algorithm for Delete • Fifty-Percent Rule Operating System Concepts
Dynamic Storage-Allocation Problem • 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. How to satisfy a request of size n from a list of free holes. 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
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
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
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
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 • [Initialize] Q AVAIL • [End of list?] P Next(Q); If P = NULL return(NULL). • [Check size.] if Size(P) N goto 4 else Q P; goto 2. • [Reserve block.] K Size(P) – N. if K = 0 Next(Q) Next(P) else Size(P) K; Return(P+K). Operating System Concepts
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
Free • Assume AVAIL is sorted by memory location (if P NULL, Next(P) > P). • return block of size N beginning at location P0. • [Initialize.] Q AVAIL. • [Advance P.] P Next(Q). if P = NULL or P > P0 goto 3 else Q P and repeat. • [Check upper bound.] if P0 + N = P and P NULL N N + Size(P); Next(P0) Next(P) else Next(P0) P. • [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
Improvements to Free • Use doubly linked list and tags to free in constant time. Operating System Concepts
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. • 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 + B A B C A C B C B B B B Operating System Concepts