0% found this document useful (0 votes)
15 views92 pages

4 Memory

The document discusses memory virtualization in operating systems, explaining how the OS creates an illusion of memory space for each process and the benefits of this approach, such as ease of programming and memory efficiency. It covers memory management techniques, including multiprogramming, address space abstraction, and the use of base and bounds registers for memory relocation. Additionally, it addresses issues related to memory virtualization, such as context switching and segmentation faults, highlighting the importance of effective memory management in OS design.

Uploaded by

Khalifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views92 pages

4 Memory

The document discusses memory virtualization in operating systems, explaining how the OS creates an illusion of memory space for each process and the benefits of this approach, such as ease of programming and memory efficiency. It covers memory management techniques, including multiprogramming, address space abstraction, and the use of base and bounds registers for memory relocation. Additionally, it addresses issues related to memory virtualization, such as context switching and segmentation faults, highlighting the importance of effective memory management in OS design.

Uploaded by

Khalifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Operating Systems

COMS(3010A)
Memory

Branden Ingram
[email protected]
Office Number : ???
Recap

• Interrupt Stack
• System Calls
• UpCalls
• UNIX
Memory Virtualization

• What is memory virtualization?


• OS virtualizes its physical memory.
• OS provides an illusion of memory space per each process
• It seems to be seen like each process uses the whole memory
What are the benefits of Memory
Virtualization

• Ease of use in programming


• Memory efficiency in terms of times and space
• The guarantee of isolation for processes as well as OS
• Protection from errant accesses of other processes
Memory Management in Early OS’s

• Load only one process in memory 0KB


• Poor utilization and efficiency
Operating System
(code, data, etc.)
64KB

Current
Program
(code, data, etc.)

max
Physical Memory
Multiprogramming and Time Sharing
0KB
Operating System
• Load multiple processes in memory (code, data, etc.)
• Execute one for a short while
64KB
• Switch processes between them in memory
Free
• Increase utilization and efficiency
128KB
Process A
• Cause an important protection issue
(code, data, etc.)
• Errant memory accesses from other processes
192KB
Process C
(code, data, etc.)
256KB

Free

320KB
Process B
(code, data, etc.)
448KB
Free
max
Physical Memory
Address Space

• OS creates an abstraction of physical memory 0KB


• This is called the Address Space
• The address space contains all the info Program Code
about a running process 1KB
• That is it consists of program code, heap, Heap
stack and etc 2KB

Free

15KB
Stack
16KB
Address Space
Address Space

• Code 0KB
• Where instructions live
• Heap Program Code
• Dynamically allocate memory 1KB
• “malloc” in C language Heap
• “new” in object-oriented language 2KB
• Stack
• Store return addresses or values
• Contain local variables arguments to Free
routines

15KB
Stack
16KB
Address Space
Address Space
0KB
Operating System
(code, data, etc.) 0KB

64KB Program Code


Free
128KB 15KB

Process A Heap
(code, data, etc.) 30KB

192KB
Process C
(code, data, etc.) Free
256KB

Free
60KB

320KB Stack
Process B
64KB
(code, data, etc.)
448KB Address Space
Free
max
Physical Memory
Well how do we map between Physical
Memory and the Address Space?

• Well the OS ofcourse


Well how do we map between Physical
Memory and the Address Space?

• Well the OS of course

• Every address in a running program is virtual


• This “Virtual Address” is the address in the Address Space
• OS translates the virtual address to physical address
Virtual Addresses

• The virtualized address in address space


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

printf("location of code : %p\n", (void *) main);


printf("location of heap : %p\n", (void *) malloc(1));
int x = 3;
printf("location of stack : %p\n", (void *) &x);

return x;
}

A simple program that prints out addresses


Virtual Addresses

• The output in 64-bit Linux machine 0x000000

location of code : 0x000000 Program Code


location of heap : 0xcf2010
location of stack : 0x7fff9ca45fcc 0xcf2010
Heap
0xd13000

Free

0x7fff9ca45fcc
Stack
0x7fff9ca45fcc
Address Space
Address Translation

• Hardware transforms a virtual address to a physical address


• The desired information is actually stored in a physical address

• The OS must get involved at key points to set up the hardware


• The OS must manage memory to judiciously intervene
Address Translation - Example

• C - Language code
void func()
int x;
...
x = x + 3; // this is the line of code we are interested in

• Load a value from memory


• Increment it by three
• Store the value back into memory
Address Translation - Example

• Assembly

128 : movl 0x0(%ebx), %eax ; load 0+ebx into eax


132 : addl $0x03, %eax ; add 3 to eax register
135 : movl %eax, 0x0(%ebx) ; store eax back to mem

• Presume that the address of ‘x’ has been place in ebx register
• Load the value at that address into eax register
• Add 3 to eax register
• Store the value in eax back into memory
Address Translation - Example

• Fetch instruction at address 128


0KB
128 movl 0x0(%ebx),%eax • Execute this instruction (load from address 61KB)
132 Addl 0x03,%eax • Fetch instruction at address 132
135 movl %eax,0x0(%ebx) • Execute this instruction (no memory reference)
Program Code • Fetch the instruction at address 135
• Execute this instruction (store to address 61 KB)
15KB
16KB Heap
30KB

Free

60KB
61KB 3000 Stack
64KB
Physical Memory
What happens if the OS needs to relocate a
process?

• The OS wants to place the process somewhere else in physical


memory, not at address 0
• The address space start at address 0
Memory Relocation
0KB
Operating System
(code, data, etc.) 0KB

64KB Program Code


Free
128KB 15KB

Process A Heap
(code, data, etc.) 30KB

192KB
Process C
(code, data, etc.) Free
256KB

Free
60KB

320KB Stack
Process B
64KB
(code, data, etc.)
448KB Address Space
Free
max
Physical Memory
Memory Relocation
0KB
Operating System
0KB (code, data, etc.)
64KB
Program Code
Free

Relocated Process
15KB 128KB
Heap Process A
30KB (code, data, etc.)
192KB
Process C
(code, data, etc.)
Free 256KB

Free
60KB
Stack 320KB
Process B
64KB (code, data, etc.)
Address Space 448KB
Free
max
Physical Memory
Base and Bounds Registers
0KB
Operating System 128KB
0KB (code, data, etc.)
base register
64KB
Program Code
Free

Relocated Process
15KB 128KB
Heap Process A
30KB (code, data, etc.)
192KB
Process C
(code, data, etc.)
Free 256KB

Free
60KB
Stack 320KB
Process B
64KB (code, data, etc.)
Address Space 448KB
64KB Free
max
bounds register
Physical Memory
Hardware base Relocation

• When a program starts running, the OS decides where in physical memory a


process should be loaded

• Set the base register value

Physical address = virtual address + base

• Every virtual address must not be greater than bound and negative

0 <= virtual address < bounds


Address Translation

128 : movl 0x0(%ebx), %eax


0KB
128 movl 0x0(%ebx),%eax
• Fetch instruction at address 128 132 Addl 0x03,%eax
135 movl %eax,0x0(%ebx)
Program Code
131200 = 128 + 128KB(base)
15KB
Heap
30KB
• Execute this instruction
• Load from address 61KB

Free
189KB = 61KB + 128KB(base)

60KB
61KB 3000 Stack
64KB
Address Space
Two ways of Bounds Register
0KB
Operating System 128KB
0KB (code, data, etc.)
base register
64KB
Program Code
Free
15KB 128KB

Relocated Process
Heap Program Code
192KB
30KB Heap
bound register
Free

Free Stack
192KB
Process C
256KB (code, data, etc.)
60KB
Free
Stack 320KB
Process B
64KB (code, data, etc.)
Address Space 448KB
64KB Free
max
bounds register
Physical Memory
What are the Issues an OS must handle for
Memory Virtualizing?

• The OS must take action to implement base-and-bounds approach

• Three critical junctures:


• When a process starts running:
• Finding space for address space in physical memory

• When a process is terminated:


• Reclaiming the memory for use

• When context switch occurs:


• Saving and storing the base-and-bounds pair
OS Issues: When a Process Starts Running
0KB
Operating System
• The OS must find a room for a new address space (code, data, etc.)
• free list : A list of the range of the physical 64KB
memory which are not in use Free
The OS lookup the free list 128KB
Free list Program Code

Process A
Heap
Free
64KB
Stack
192KB
Process C
256KB (code, data, etc.)
256KB
Free
320KB
Process B
(code, data, etc.)
448KB
448KB Free
max
Physical Memory
OS Issues: When a Process Is Terminated

• The OS must put the memory back on the free list

0KB
Free list 0KB
Free list
Operating System Operating System
(code, data, etc.) (code, data, etc.)
64KB 64KB
Free 64KB 64KB
128KB
Process C
Free
(code, data, etc.)
256KB
Free
320KB 256KB 320KB 448KB
Process B Process B
(code, data, etc.) (code, data, etc.)
448KB 448KB
Free Free
max max
448KB
Physical Memory Physical Memory
OS Issues: When Context Switch Occurs

Process B PCB
• The OS must save and restore the base-and-bounds pair.

• In process structure or process control block(PCB) base : 320KB
bounds : 448KB

0KB
Operating System 0KB
(code, data, etc.) Operating System
64KB (code, data, etc.)
Free 64KB base register
128KB Free 128KB
Process C
Context Switch 128KB
(code, data, etc.) Process C
bounds register
256KB Currently Running
Free
base register 256KB 256KB
320KB 320KB Free
Process B 320KB
Currently Running bounds register Process B
448KB (code, data, etc.)
448KB
448KB
Free
max Free
max
Physical Memory
Physical Memory
What if there is not enough space together
to fit a program?

• How do we start a new program in this case


Inefficiency of the Base and Bound Approach

• Big chunk of “free” space


• “free” space takes up physical memory. 0KB
• Hard to run when an address space does not fit into
Program Code
physical memory
15KB
Heap
30KB

Free

60KB
Stack
64KB
Address Space
Segmentation

• Segment is just a contiguous portion of the address


space of a particular length 0KB
• Logically-different segment: code, stack, heap
Program Code

• Each segment can be placed in different part of 15KB


physical memory Heap
• Base and bounds exist per each segment 30KB

Free

60KB
Stack
64KB
Address Space
Segmentation
0KB
Operating System
• Segment is just a contiguous portion of the address (code, data, etc.)
space of a particular length
64KB
• Logically-different segment: code, stack, heap
Free
128KB
• Each segment can be placed in different part of Stack
physical memory 192KB
• Base and bounds exist per each segment Free
256KB
Segment Register Code
260KB
Segment Base Size Heap
Code 256KB 4KB 320KB
Heap 260KB 60KB
Stack 128KB 64KB
Free

max
Physical Memory
Address Translation with Segmentation

Physical address = offset + base

• The offset of virtual address 100 is 100.


• The code segment starts at virtual address 0 in
address space.
64KB
Segment Base Size
Code 128KB 15KB
Free

0KB 128KB 100 + 128KB


100 instruction
Program Code Code
15KB 143KB
Heap Heap
30KB 158KB
Free
60KB Free
Stack
64KB 260KB
Address Space Physical Memory
Address Translation with Segmentation

Virtual address + base is not the correct physical address

• The offset of virtual address 16000 is 640.


• The code segment starts at virtual address 0 in
address space.
64KB
Segment Base Size
Heap 143KB 15KB
Free

0KB 128KB
Program Code Code
15KB 143KB
16000 data
640 + 143KB
Heap Heap
30KB 158KB
Free
60KB Free
Stack
64KB 260KB
Address Space Physical Memory
What happens if an address is incorrectly
referenced?

• Segmentation Fault occurs


Segmentation Faults

• If an illegal address such as 37KB which is beyond the end of heap is referenced,
the OS occurs segmentation fault
• The hardware detects that address is out of bounds

0KB
Program Code
15KB
Heap
30KB
Free
60KB
Stack
64KB
Address Space
Referring to a Segment

• Explicit approach
• Chop up the address space into segments based on the top few bits of virtual
address
13 12 11 10 9 8 7 6 5 4 3 2 1 0

Segment Offset
Referring to a Segment

• Explicit approach
• Chop up the address space into segments based on the top few bits of virtual
address
13 12 11 10 9 8 7 6 5 4 3 2 1 0

Segment Offset

• Example: virtual address 4200 (01000001101000)

Segment bits
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Code 00
Heap 01 0 1 0 0 0 0 0 1 1 0 1 0 0 0
Stack 10
- 11 Segment Offset
Referring to a Segment
1 // get top 2 bits of 14-bit VA
2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
3 // now get offset
4 Offset = VirtualAddress & OFFSET_MASK
5 if (Offset >= Bounds[Segment])
6 RaiseException(PROTECTION_FAULT)
7 else
8 PhysAddr = Base[Segment] + Offset
9 Register = AccessMemory(PhysAddr)

• SEG_MASK = 0x3000(11000000000000)
• SEG_SHIFT = 12
• OFFSET_MASK = 0xFFF (00111111111111)
• VA = 01100011100011
• Segment = 01000000000000 >> 12 = 01
• Offset = 00100011100011
Referring to a Stack Segment

• Stack grows backward


• Extra hardware support is need
• The hardware checks which way the segment grows
• 1: positive direction
• 0: negative direction
64KB
Free Segment Register(with Negative-Growth Support)
128KB Segment Base Size Grows Positive?
Stack Code 256KB 4KB 1
192KB
Heap 260KB 60KB 1
Free Stack 192KB 64KB 0
256KB
Code
260KB
Heap
320KB
Free
max
Physical Memory
Support for Sharing

• Segment can be shared between address space


• Code sharing is still in use in systems today
• by extra hardware support

• Extra hardware support is need for form of Protection bits


• A few more bits per segment to indicate permissions of read, write and execute

Segment Register(with Negative-Growth Support)


Segment Base Size Grows Positive? Protection
Code 256KB 4KB 1 Read-Execute
Heap 260KB 60KB 1 Read-Write
Stack 192KB 64KB 0 Read-Write
Fine-Grained and Coarse-Grained

• Coarse-Grained means segmentation in a small number


• e.g., code, heap, stack

• Fine-Grained segmentation allows more flexibility for address space in some


early system
• To support many segments, Hardware support with a segment table is
required
OS support: Fragmentation

• External Fragmentation: little holes of free space in physical memory that make
difficulty to allocate new segments
• There is 24KB free, but not in one contiguous segment
• The OS cannot satisfy the 20KB request
OS support: Fragmentation

• External Fragmentation: little holes of free space in physical memory that make
difficulty to allocate new segments
• There is 24KB free, but not in one contiguous segment
• The OS cannot satisfy the 20KB request

• Compaction: rearranging the existing segments in physical memory


• Compaction is costly
• Stop running process
• Copy data to somewhere
• Change segment register value
Memory Compaction
0KB 0KB
Operating System Operating System
(code, data, etc.) (code, data, etc.)
64KB 64KB
Free Allocated
128KB 192KB
Allocated
192KB
Free
256KB
Allocated
260KB
Allocated Free
320KB

Free

max max
Physical Memory Physical Memory
Free-Space Management

• Splitting
• Coalescing
• Keeping Track of Memory Region Sizes
• Strategies for Managing Memory
Splitting

• Finding a free chunk of memory that can satisfy the request and splitting it
into two
Splitting

• Finding a free chunk of memory that can satisfy the request and splitting it
into two

• For Example : When request for memory allocation is smaller than the
size of free chunks.
Splitting

• For Example : When request for memory allocation is smaller than the size of
free chunks.
• Two 10-byte free segments with a 1-byte request

30-byte heap Free Allocated Free


0 10 20 30

Addr:0 Addr:20
Free list: head Len:10 Len:10 NULL

Before Splitting
Splitting

• For Example : When request for memory allocation is smaller than the size of
free chunks.
• Two 10-byte free segments with a 1-byte request

30-byte heap Free Allocated Free


0 10 20 21 30

Addr:0 Addr:21
Free list: head Len:10 Len:9 NULL

After Splitting 10-byte free segment


Coalescing

• Merge returning a free chunk with existing chunks into a large single free
chunk if addresses of them are nearby
Coalescing

• Merge returning a free chunk with existing chunks into a large single free
chunk if addresses of them are nearby

• For Example : When request for memory that is bigger than free chunk
size, the list will not find such a free chunk
Coalescing

• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request

Addr:0 Addr:20 Addr:40 NULL


Free list: head Len:10 Len:10 Len:10

Before Coalescing

Addr:0
Free list: head Len:30 NULL

After Coalescing
Coalescing

• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request

Addr:0 Addr:20 Addr:40 NULL


Free list: head Len:10 Len:10 Len:10

Before Coalescing

Addr:0
Free list: head Len:30 NULL

After Coalescing
Coalescing

• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request

Addr:0 Addr:20 Addr:40 NULL


Free list: head Len:10 Len:10 Len:10

Before Coalescing
Coalescing

• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request

Addr:0 Addr:20 Addr:40 NULL


Free list: head Len:10 Len:10 Len:10

Before Coalescing

Addr:0 Addr:20 Addr:40 NULL


Free list: head Len:10 Len:10 Len:10

After Coalescing
Coalescing

• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request

Addr:0 Addr:10 Addr:40 NULL


Free list: head Len:10 Len:10
Len:10

Before Coalescing

Addr:0 Addr:40 NULL


Free list: head Len:20 Len:10

After Coalescing
How do we do this reorganisation in an
efficient way
• By tracking the size of allocated regions
• By embedding the free list
Tracking the size of allocated regions

• The interface to free(void *ptr) does not take a size parameter.

• How does the library know the size of memory region that will be back
into free list?
Tracking the size of allocated regions

• The interface to free(void *ptr) does not take a size parameter.

• How does the library know the size of memory region that will be back
into free list?
• Most allocators store extra information in a header block

ptr = malloc(20);

The header used by malloc library


ptr
The 20 bytes returned by caller

An Allocated Region Plus Header


The header

• The header minimally contains the size of the allocated memory region
• The header may also contain
Additional pointers to speed up deallocation
A magic number for integrity checking

ptr = malloc(20);

hptr
Size = 20
typedef struct __header_t {
Magic = 12345 int size;
ptr
The 20 bytes int magic;
returned by } header_t;
caller
An Allocated Region Plus Header Simple Header
The header

• The size for required free region is the size of the header plus the size of the
space
allocated to the user.
• If a user request N bytes, the library searches for a free chunk of size N
plus the size of the header
The header

• The size for required free region is the size of the header plus the size of the
space
allocated to the user.
• If a user request N bytes, the library searches for a free chunk of size N
plus the size of the header

• Simple pointer arithmetic to find the header pointer

void free(void *ptr) {


header_t *hptr = (void *)ptr – sizeof(header_t);
}
Embedding a Free List

• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space

• But we have a little problem here


Embedding a Free List

• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space

• But we have a little problem here


• The library that is required by malloc to actually allocate memory cant
use malloc itself.
Embedding a Free List

• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space

• But we have a little problem here


• The library that is required by malloc to actually allocate memory cant
use malloc itself.

• Therefore we embed the free list directly in the heap


Embedding a Free List

• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space

• But we have a little problem here


• The library that is required by malloc to actually allocate memory cant
use malloc itself.

• Therefore we embed the free list directly in the heap

typedef struct __node_t {


int size;
struct __node_t *next; Description of a node of the
} nodet_t; free list
Embedding a Free List

typedef struct __header_t {


int size; Simple Header for an allocated
int magic; block
} header_t;

typedef struct __node_t {


int size; Description of a node of the
struct __node_t *next; free list
} nodet_t;
Embedding a Free List

hptr
Size = 20
Magic = 12345 Allocated Block
ptr

hptr
Size = 20
Next = 0 Free Block
Embedding a Free List : Allocation

• If a chunk of memory is requested, the library will first find a chunk


that is large enough to accommodate the request

• This is done by iterating through the free list and checking the size
Embedding a Free List : Allocation

• If a chunk of memory is requested, the library will first find a chunk


that is large enough to accommodate the request

• This is done by iterating through the free list and checking the size

• The library will:


• Split the large free chunk into two.
• One for the request and the other being the remaining free chunk
• Shrink the size of free chunk in the list
Embedding a Free List : Allocation

• Example a request for 100 bytes by ptr = malloc(100)


Embedding a Free List : Allocation

• Example a request for 100 bytes by ptr = malloc(100)


• Allocating 108 bytes out of the existing one free chunk.
• shrinking the one free chunk to 3980(4088 minus 108)
Embedding a Free List : Allocation

• Example a request for 100 bytes by ptr = malloc(100)


• Allocating 108 bytes out of the existing one free chunk.
• shrinking the one free chunk to 3980(4088 minus 108)

hptr
Size = 4088
Next = 0 A 4KB Heap
With 1 Free Chunk
Embedding a Free List : Allocation

• Example a request for 100 bytes by ptr = malloc(100)


• Allocating 108 bytes out of the existing one free chunk.
• shrinking the one free chunk to 3980(4088 minus 108)

A 4KB Heap with 1 Free Chunk A 4KB Heap after 1 Allocation


hptr
Size = 4088 Size = 100
Next = 0 Magic = 12345 Allocated
ptr
Block
Allocated 100 bytes
hptr
Size = 3980
Next = 0 Free
Block
Free 3980 bytes
A 4KB Heap with 3 Allocated
Size = 100
Chunks
Magic = 12345

Allocated 100 bytes

Size = 100
Magic = 12345
sptr

Allocated 100 bytes 100 allocated bytes


(but about to be freed)
Size = 100
Magic = 12345

Allocated 100 bytes


hptr
Size = 3764
Next = 0

Free 3764 bytes


Embedding a Free List : Free

• Example a request to free sptr : free(sptr)


Embedding a Free List : Free

• Example a request to free sptr : free(sptr)


• The 100 byte chunk is back
into the free list.

• The new Free chunk is added to the front of the list


A 4KB Heap with 2 Allocated
Size = 100
Chunks and 2 Free chunks
Magic = 12345

Allocated 100 bytes


hptr
Size = 100
Next = 16708
sptr

Free 100 bytes 100 freed bytes

Size = 100
Magic = 12345

Allocated 100 bytes

Size = 3764
Next = 0

Free 3764 bytes


Embedding a Free List : Free

• Example Remaining chunks are freed


A 4KB Heap with 0 Allocated
Size = 100
Chunks and 4 Free chunks
Next = 13000

Free 100 bytes

Size = 100
Next = 16708
sptr

Free 100 bytes

hptr Size = 100


Next = 3200

Free 100 bytes

Size = 3764
Next = 0

Free 3764 bytes


A 4KB Heap with 0 Allocated
Size = 100
Chunks and 4 Free chunks
Next = 13000
Fragmentation Occurs
Coalescing needed Free 100 bytes

Size = 100
Next = 16708
sptr

Free 100 bytes

hptr Size = 100


Next = 3200

Free 100 bytes

Size = 3764
Next = 0

Free 3764 bytes


Managing Free Space : Basic Strategies

• Best Fit:
• Finding free chunks that are big or bigger than the request
• Returning the one of smallest chunks in the group of candidates
Managing Free Space : Basic Strategies

• Best Fit:
• Finding free chunks that are equal or bigger than the request
• Returning the one of smallest in the chunks in the group of candidates

• Worst Fit:
• Finding the largest free chunks and allocating the amount of the request
• Keeping the remaining chunk on the free list
Managing Free Space : Basic Strategies

• First Fit:
• Finding the first chunk that is big enough for the request
• Returning the requested amount and the remaining rest of the chunk
Managing Free Space : Basic Strategies

• First Fit:
• Finding the first chunk that is big enough for the request
• Returning the requested amount and the remaining rest of the chunk

• Next Fit:
• Finding the first chunk that is big enough for the request.
• Searching at where one was looking at instead of the begging of the list
Basic Strategies - Examples

• Allocation Request Size 15

Addr:0 Addr:20 Addr:60 NULL


Free list: head Len:10 Len:30 Len:20
Basic Strategies - Examples

• Allocation Request Size 15

Addr:0 Addr:20 Addr:60 NULL


Free list: head Len:10 Len:30 Len:20

• Result of Best-fit

Addr:0 Addr:20 Addr:75 NULL


Free list: head Len:10 Len:30 Len:5
Basic Strategies - Examples

• Allocation Request Size 15

Addr:0 Addr:20 Addr:60 NULL


Free list: head Len:10 Len:30 Len:20

• Result of Best-fit

Addr:0 Addr:20 Addr:75 NULL


Free list: head Len:10 Len:30 Len:5

• Result of Worst-fit

Addr:0 Addr:35 Addr:60 NULL


Free list: head Len:10 Len:15 Len:20
Managing Free Space : Buddy Allocation

• Binary Buddy Allocation


Managing Free Space : Buddy Allocation

• Binary Buddy Allocation

• The allocator divides free space by two until a block that is big enough
to accommodate the request is found
Managing Free Space : Buddy Allocation

• Binary Buddy Allocation

• The allocator divides free space by two until a block that is big enough
to accommodate the request is found

64 KB

32 KB 32 KB

16 KB 16 KB

8 KB 8 KB

64KB free space for 7KB request

You might also like