0% found this document useful (0 votes)
9 views24 pages

Partitioned Memory Allocation

The document discusses various memory allocation policies including First Fit, Best Fit, Worst Fit, and Buddy Allocation, detailing their rationale, implementation, advantages, and disadvantages. It explains how each policy allocates memory blocks and addresses issues such as fragmentation and allocation speed. Additionally, it covers concepts of multiprogramming and swapping, highlighting the need for effective scheduling in memory management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Partitioned Memory Allocation

The document discusses various memory allocation policies including First Fit, Best Fit, Worst Fit, and Buddy Allocation, detailing their rationale, implementation, advantages, and disadvantages. It explains how each policy allocates memory blocks and addresses issues such as fragmentation and allocation speed. Additionally, it covers concepts of multiprogramming and swapping, highlighting the need for effective scheduling in memory management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Partitioned Memory Allocation

Allocation Policies

Best Fit
First Fit
Worst Fit
Buddy Allocation
First Fit Allocation
1K bytes
To allocate n bytes, use the
first available free block
such that the block size is
larger than n. 2K bytes 2K bytes

To allocate 400 bytes,


we use the 1st free block 500 bytes 500 bytes
available
Rationale & Implementation

Simplicity of implementation
Requires:
Free block list sorted by address
Allocation requires a search for a suitable partition
Deallocation requires a check to see if the freed partition
could be merged with adjacent free partitions (if any)
Example
Allocate 1K Deallocate last Deallocate 3rd

6K 6K 6K 6K

1K 1K 1K
4K
3K 3K

4K 4K 4K
11K
1K 1K
4K
3K 3K
First Fit Allocation

Advantages Disadvantages
Simple External fragmentation
Tends to produce larger
free blocks toward
the end of the address
space
Best Fit Allocation
1K bytes 1K bytes
To allocate n bytes, use the
smallest available free
block such that the block
size is larger than n. 2K bytes 2K bytes

To allocate 400 bytes,


we use the 3rd free block 500 bytes
available (smallest)
Rationale & Implementation

To avoid fragmenting big free blocks


To minimize the size of external fragments produced
Requires:
Free block list sorted by size
Allocation requires search for a suitable partition
Deallocation requires search + merge with adjacent free
partitions, if any
Example
Allocate 1K Deallocate last Deallocate 3rd

6K 6K 6K 6K

4K 4K 4K
8K
4K 4K 4K

1K 1K 1K 1K
3K 3K 3K 3K
Best Fit Allocation

Advantages Disadvantages
Works well when most External fragmentation
allocations are of small Slow allocation
size Slow deallocation
Relatively simple Tends to produce many useless
tiny fragments (not really
great)
Worst Fit Allocation
1K bytes 1K bytes
To allocate n bytes, use
the largest available
free block such that the
block size is larger than 2K bytes
n.

To allocate 400 bytes,


we use the 2nd free block 500 bytes
available (largest)
Rationale & Implementation

To avoid having too many tiny fragments


Requires:
Free block list sorted by size
Allocation is fast (get the largest partition)
Deallocation requires merge with adjacent free partitions, if
any, and then adjusting the free block list
Example
Allocate 2K Allocate 1K Deallocate 4th

6K 6K 6K 6K

2K 2K 2K
4K
2K 2K

5K 5K 5K
7K

1K 1K 1K
3K
2K 2K 2K
Worst Fit Allocation

Advantages Disadvantages
Works best if External fragmentation
allocations are of Tends to break large free
medium sizes blocks such that large
partitions cannot be
allocated
Buddy Allocation

To allocate a partition of n bytes: 1K byte


1K byte
Divide available space into two blocks
(called buddies). 2K bytes
Recursively divide the first block in the
same manner. 4K bytes
Continue until we have the smallest block
its size is > n
eg. to allocate a 599 bytes out of 16K
bytes space, see right 8K bytes
Further Buddy Allocations

To allocate n bytes, pick the smallest block available that is


>n
If size > 2n split block into buddies until you have the
smallest block available that is > n
Memory allocator needs to maintain only lists of sizes 1, 2,
4, 8, 16, … 2n bytes
For a memory space of size n, we have log n lists to search
Very fast allocation
Example
Allocate 599 bytes Allocate 1180 bytes Allocate 2000 bytes
1K byte 1K byte 1K byte
1K byte 1K byte 1K byte

2K bytes 2K bytes 2K bytes


4K bytes 4K bytes 2K bytes
2K bytes

8K bytes 8K bytes 8K bytes


Deallocation
Deallocate 1st block
When a block is freed: 1K byte 2K bytes
1K byte
Merge any two adjacent
buddies of the same size 2K bytes 2K bytes
Recursively continue the 2K bytes 2K bytes
merge until the largest 2K bytes 2K bytes
block possible is
reconstructed

8K bytes 8K bytes
Example (cont’d)
Deallocate 2nd block Deallocate 3rd block
2K bytes
4K bytes
2K bytes
2K bytes 2K bytes
2K bytes 2K bytes
16K bytes

8K bytes 8K bytes
Buddy Allocation (cont’d)

Advantages Disadvantages
Fast allocation Internal fragmentation
Fast deallocation External fragmentation
Very simple data
structures (log n + 1
lists of available
blocks, at max)
Multiprogramming

Multiprogramming: Ability to run more than one program


simultaneously
Early systems: degree of multiprogramming had to be large to
maximize the return on hardware investment
Multiprogramming requires all programs to be resident in
memory simultaneously
Can be done by partitioned memory allocation, but
Size of memory limits the number of programs that can run
simultaneously
Swapping

Allows more programs simultaneously than the memory can


accommodate
Use a partition on disk to “extend” the main memory
Processes are added until memory is full
If more programs need to be loaded, pick one program and save its
partition on disk (the process is swapped out, cannot run)
Later, bring the process back from disk (process is swapped in) and
swap out another process
Done on an all-or-nothing basis
The entire process is either in memory or on swap space (cannot be
between both),
Memory & Scheduling

Start Ready Running


Process
Process loaded
in main I/O
creation, I/O Done
memory requested
resources done
allocated Zombie
I/O Wait
Done
Process
Swapped waiting Resources
out Process for I/O deallocated
on disk
Memory & Scheduling (cont’d)

Need two levels of scheduling:


Upper level decides on swapping:
When
Who
For how long
Lower level decides on who actually runs on the CPU (what we have
covered so far)
Upper level is invoked if there are processes swapped out, and
whenever we need to load more programs than can fit in
main memory

You might also like