Memory Management Damian Gordon Memory Management HARD DISK

  • Slides: 90
Download presentation
Memory Management Damian Gordon

Memory Management Damian Gordon

Memory Management HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2

Memory Management HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2

CPU

CPU

CPU Cache 1

CPU Cache 1

Cache

Cache

Cache CPU

Cache CPU

Cache CPU Cache 2

Cache CPU Cache 2

Main Memory (RAM)

Main Memory (RAM)

Secondary Memory (Hard Disk)

Secondary Memory (Hard Disk)

Memory Management HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2

Memory Management HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2

Memory Management Approximate number of clock cycles to access the various elements of the

Memory Management Approximate number of clock cycles to access the various elements of the memory hierarchy. HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2 101

Memory Management Approximate number of clock cycles to access the various elements of the

Memory Management Approximate number of clock cycles to access the various elements of the memory hierarchy. HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2 101 103

Memory Management Approximate number of clock cycles to access the various elements of the

Memory Management Approximate number of clock cycles to access the various elements of the memory hierarchy. HARD DISK (SECONDARY MEMORY) CACHE 1 (MAIN MEMORY) 2 101 103 107

Memory Management � Let’s consider an operating system model with a single user, and

Memory Management � Let’s consider an operating system model with a single user, and how they use memory.

Single-User System MAIN MEMORY

Single-User System MAIN MEMORY

Single-User System MAIN MEMORY 200 K available

Single-User System MAIN MEMORY 200 K available

Single-User System � If I create a program: MAIN MEMORY 200 K available

Single-User System � If I create a program: MAIN MEMORY 200 K available

Single-User System � If I create a program: PROGRAM 1 MAIN MEMORY 200 K

Single-User System � If I create a program: PROGRAM 1 MAIN MEMORY 200 K available

Single-User System � If I create a program: PROGRAM 1 � to be processed,

Single-User System � If I create a program: PROGRAM 1 � to be processed, it has to be written entirely into Main Memory, in contiguous space MAIN MEMORY 200 K available

Single-User System PROGRAM 1 MAIN MEMORY 200 K available

Single-User System PROGRAM 1 MAIN MEMORY 200 K available

Single-User System � If the program is bigger: MAIN MEMORY PROGRAM 1 200 K

Single-User System � If the program is bigger: MAIN MEMORY PROGRAM 1 200 K available

Single-User System � If it doesn’t fit in the memory, it’s can’t be processed.

Single-User System � If it doesn’t fit in the memory, it’s can’t be processed. MAIN MEMORY 200 K available

Memory Management � This is the limitation of all computers, if a program is

Memory Management � This is the limitation of all computers, if a program is too big, we have to do one of two things: 1. Get more memory 2. Make the program smaller

Algorithm for Single-User System Store first location of program in the base register (for

Algorithm for Single-User System Store first location of program in the base register (for memory protection); Set Program Counter to the first memory location; Read first instruction of program; WHILE (last instruction reached OR Program Counter is greater than Memory Size) DO Increment the Program Counter; IF (last instruction reached) THEN Stop Loading Program; END IF; IF (Program Counter is greater than Memory Size) THEN Stop Loading Program; END IF; Load instruction into memory; Read next instruction of program; END WHILE;

Memory Management � To allow more than one program to run at the same

Memory Management � To allow more than one program to run at the same time, the memory is subdivided into FIXED PARTITIONS.

Fixed Partitions 250 K MAIN MEMORY

Fixed Partitions 250 K MAIN MEMORY

Fixed Partitions 250 K PARTITION 1 100 K PARTITION 2 25 K PARTITION 3

Fixed Partitions 250 K PARTITION 1 100 K PARTITION 2 25 K PARTITION 3 25 K PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � This leaves us with less memory, but at least more than

Fixed Partitions � This leaves us with less memory, but at least more than one user (and their user process) can be logged into the system at the same time. � If we want to adjust the size of the partitions we need to shut down the system, go into the boot login, and then restart.

Algorithm for Fixed Partitions WHILE (there are still programs in the queue) DO Determine

Algorithm for Fixed Partitions WHILE (there are still programs in the queue) DO Determine the program’s requested memory size; IF program size > Size of Largest Partition THEN Reject the program; PRINT “program Rejected: Not Enough Memory”; Exit; /* this iteration and get next program */ END IF; My. Counter : = 1; WHILE (My. Counter <= Number of Partitions in Memory) DO IF (program Size > Memory_Partition[Counter]. Size THEN My. Counter : = My. Counter + 1; ELSE IF (Memory_Partition[Counter]. Status == “FREE”; THEN Load program into Memory_Partition[Counter]; Memory_Partition[Counter]. Status : = “BUSY”; Exit; /* this iteration and get next program */ ELSE My. Counter : = My. Counter + 1; END IF; END WHILE; No partition available; put program in waiting queue; END WHILE;

Fixed Partitions � To make this work, the Memory manager needs to keep a

Fixed Partitions � To make this work, the Memory manager needs to keep a Partition Table to remember the status of all the partitions.

Fixed Partitions � To make this work, the Memory manager needs to keep a

Fixed Partitions � To make this work, the Memory manager needs to keep a Partition Table to remember the status of all the partitions. Partition Number 1 Partition Size 100 K Memory Address 200 K Access Program 1 Partition Status BUSY 2 25 K 300 K Program 4 BUSY 3 25 K 325 K 4 50 K 350 K 5 50 K 400 K FREE Program 2 BUSY FREE

Fixed Partitions � Which looks like this in memory:

Fixed Partitions � Which looks like this in memory:

Fixed Partitions � Which looks like this in memory: 250 K PARTITION 1 100

Fixed Partitions � Which looks like this in memory: 250 K PARTITION 1 100 K PARTITION 2 25 K PARTITION 3 25 K PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in:

Fixed Partitions � Let’s add some programs in:

Fixed Partitions � Let’s add some programs in: 250 K PARTITION 1 100 K

Fixed Partitions � Let’s add some programs in: 250 K PARTITION 1 100 K PARTITION 2 25 K PARTITION 3 25 K PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 250 K PARTITION 1

Fixed Partitions � Let’s add some programs in: PROGRAM 1 250 K PARTITION 1 100 K PARTITION 2 25 K PARTITION 3 25 K PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 250 K PARTITION 1

Fixed Partitions � Let’s add some programs in: PROGRAM 1 250 K PARTITION 1 100 K PARTITION PROGRAM 22 25 K PARTITION 3 25 K PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 250 K PARTITION 1

Fixed Partitions � Let’s add some programs in: PROGRAM 1 250 K PARTITION 1 100 K PARTITION PROGRAM 22 25 K PARTITION 3 25 K PROGRAM 3 PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K PARTITION PROGRAM 22 25 K PARTITION 3 25 K INTERNAL FRAGMENTATION 250 K PROGRAM 3 PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K PARTITION PROGRAM 22 25 K PARTITION 3 25 K INTERNAL FRAGMENTATION 250 K PROGRAM 3 INTERNAL FRAGMENTATION PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K PARTITION PROGRAM 22 25 K PARTITION 3 25 K INTERNAL FRAGMENTATION EMPTY PARTITION 250 K PROGRAM 3 INTERNAL FRAGMENTATION PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K

Fixed Partitions � Let’s add some programs in: PROGRAM 1 PARTITION 1 100 K PARTITION PROGRAM 22 25 K PARTITION 3 25 K INTERNAL FRAGMENTATION EMPTY PARTITION 250 K PROGRAM 3 INTERNAL FRAGMENTATION EMPTY PARTITION 4 50 K PARTITION 5 50 K

Fixed Partitions � Selecting the correct set of partition sizes is a tricky business,

Fixed Partitions � Selecting the correct set of partition sizes is a tricky business, too small and larger programs will be waiting forever to run, too big and there is a lot of wasted space.

Dynamic Partitions � An alternative is DYNAMIC PARTITIONS, where a program is given the

Dynamic Partitions � An alternative is DYNAMIC PARTITIONS, where a program is given the space it requests, if there is space available for it. � This takes care of lots of problems.

Dynamic Partitions � Let’s add some programs in: 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 3

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 3 250 K MAIN MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K MAIN 3 PROGRAM MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 4 250 K MAIN 3 PROGRAM MEMORY

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 250 K MAIN 3 PROGRAM MEMORY PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 250 K MAIN 3 PROGRAM MEMORY PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 250 K WAIT MAIN 3 PROGRAM MEMORY PROGRAM 4

Dynamic Partitions � It isn’t perfect, because the next program will go into the

Dynamic Partitions � It isn’t perfect, because the next program will go into the slot that is freed up by a completed program.

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 250 K WAIT MAIN 3 PROGRAM MEMORY PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 250 K WAIT MAIN 3 PROGRAM MEMORY PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 250 K WAIT PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 250 K PROGRAM 4

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5

Dynamic Partitions � Let’s add some programs in: PROGRAM 1 PROGRAM 2 PROGRAM 5 EXTERNAL FRAGMENTATION 250 K PROGRAM 4

Dynamic Partitions � So however we partition, we end up with fragmentation, one way

Dynamic Partitions � So however we partition, we end up with fragmentation, one way or the other.

Partitions PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions � So how do we decide where to slot in a new program?

Partitions � So how do we decide where to slot in a new program? PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions � So how do we decide where to slot in a new program?

Partitions � So how do we decide where to slot in a new program? PROGRAM 6 PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions � Here free is the first slot PROGRAM 1 PROGRAM 2 PROGRAM 6

Partitions � Here free is the first slot PROGRAM 1 PROGRAM 2 PROGRAM 6 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions � Here free is the first slot PROGRAM 1 PROGRAM 2 PROGRAM 6

Partitions � Here free is the first slot PROGRAM 1 PROGRAM 2 PROGRAM 6 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions � But here’s a better fit PROGRAM 1 PROGRAM 2 PROGRAM 6 PROGRAM

Partitions � But here’s a better fit PROGRAM 1 PROGRAM 2 PROGRAM 6 PROGRAM 3 PROGRAM 4 PROGRAM 5 250 K

Partitions � But here’s a better fit PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM

Partitions � But here’s a better fit PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 6 PROGRAM 4 PROGRAM 5 250 K

Partitions � So we can either add the new block to the first available

Partitions � So we can either add the new block to the first available slot (FIRST-FIT ALGORITHM) or we can add the new block to the most suitable available slot (BEST-FIT ALGORITHM).

Partitions � If the Memory Manager wants a FIRST-FIT ALGORITHM then it stores a

Partitions � If the Memory Manager wants a FIRST-FIT ALGORITHM then it stores a table in order of memory locations. � If the Memory Manager wants a BEST-FIT ALGORITHM then it stores a table in order of size of memory locations. Starts Size Status 200 50 BUSY 250 50 FREE 300 15 BUSY 315 40 FREE 355 25 BUSY 315 40 FREE 250 50 FREE Starts Size Status 300 15 BUSY 355 25 BUSY 315 40 BUSY

Deallocating space � After a program is completed, there are three cases for deallocating

Deallocating space � After a program is completed, there are three cases for deallocating space in memory:

Deallocating space � 1. There are programs either side of the freed space: PROGRAM

Deallocating space � 1. There are programs either side of the freed space: PROGRAM 5 PROGRAM 3 PROGRAM 6 PROGRAM 4 PROGRAM 8

Deallocating space � 2. There are is a program on one side, and it’s

Deallocating space � 2. There are is a program on one side, and it’s free on the other side of the freed space: PROGRAM 5 PROGRAM 3 PROGRAM 6 PROGRAM 8

Deallocating space � 3. There are no programs on either side of the freed

Deallocating space � 3. There are no programs on either side of the freed space: PROGRAM 5 PROGRAM 6 PROGRAM 8

Deallocating space � Let’s look at the implications of each of these cases in

Deallocating space � Let’s look at the implications of each of these cases in terms of what the Memory Manager has to do to remember the FREE and BUSY memory spaces.

Deallocating space � 1. There are programs either side of the freed space: PROGRAM

Deallocating space � 1. There are programs either side of the freed space: PROGRAM 5 PROGRAM 3 PROGRAM 6 PROGRAM 4 PROGRAM 8

Deallocating space � 1. There are programs either side of the freed space: 200

Deallocating space � 1. There are programs either side of the freed space: 200 -250 PROGRAM 5 200 -250 250 -300 PROGRAM 3 250 -300 300 -315 315 -340 340 -355 PROGRAM 6 PROGRAM 4 300 -315 315 -340 340 -355 355 -380 PROGRAM 8 355 -380

Deallocating space � 1. There are programs either side of the freed space: Starts

Deallocating space � 1. There are programs either side of the freed space: Starts Size Status Starts 200 50 BUSY 200 50 Status 200 -250 BUSY 250 -300 250 50 BUSY 250 -300 300 -315 315 -340 340 -355 300 15 BUSY 300 15 315 25 BUSY 315 25 355 -380 340 15 FREE 340 15 355 25 BUSY 355 25 FREE 300 -315 BUSY 315 -340 FREE 340 -355 BUSY 355 -380 200 -250 Size

Deallocating space � 2. There are is a program on one side, and it’s

Deallocating space � 2. There are is a program on one side, and it’s free on the other side of the freed space: PROGRAM 5 PROGRAM 3 PROGRAM 6 PROGRAM 8

Deallocating space � 2. There are is a program on one side, and it’s

Deallocating space � 2. There are is a program on one side, and it’s free on the other side of the freed space: 200 -250 PROGRAM 5 200 -250 250 -300 PROGRAM 3 250 -300 300 -315 PROGRAM 6 300 -355 315 -355 355 -380 PROGRAM 8 355 -380

Deallocating space � 2. There are is a program on one side, and it’s

Deallocating space � 2. There are is a program on one side, and it’s free on the other side of the freed space: Starts Size Status Starts 200 50 BUSY 200 50 Status 200 -250 BUSY 250 -300 250 50 BUSY 300 -315 300 15 BUSY 300 15 FREE 315 40 FREE 355 25 BUSY 200 -250 315 -355 355 -380 Size 250 -300 300 -355 355 -380

Deallocating space � 2. There are is a program on one side, and it’s

Deallocating space � 2. There are is a program on one side, and it’s free on the other side of the freed space: Starts Size Status 200 50 BUSY 250 50 BUSY 300 15 FREE 315 40 FREE 355 25 BUSY

Deallocating space � 2. There are is a program on one side, and it’s

Deallocating space � 2. There are is a program on one side, and it’s free on the other side of the freed space: Starts Size Status 200 50 BUSY 250 50 BUSY 300 15 FREE 315 40 FREE 355 25 BUSY Starts Size Status 200 50 BUSY 250 50 BUSY 300 55 FREE 355 25 BUSY

Deallocating space � 3. There are no programs on either side of the freed

Deallocating space � 3. There are no programs on either side of the freed space: PROGRAM 5 PROGRAM 6 PROGRAM 8

Deallocating space � 3. There are no programs on either side of the freed

Deallocating space � 3. There are no programs on either side of the freed space: 200 -250 PROGRAM 5 200 -250 250 -300 300 -315 250 -355 PROGRAM 6 315 -355 355 -380 PROGRAM 8 355 -380

Deallocating space � 3. There are no programs on either side of the freed

Deallocating space � 3. There are no programs on either side of the freed space: Starts Size Status Starts 200 50 BUSY 200 50 Status 200 -250 BUSY 250 -300 250 50 FREE 300 -315 300 15 BUSY 300 15 FREE 315 40 FREE 355 25 BUSY 200 -250 315 -355 355 -380 Size 250 -355 355 -380

Deallocating space � 3. There are no programs on either side of the freed

Deallocating space � 3. There are no programs on either side of the freed space: Starts Size Status 200 50 BUSY 250 50 FREE 300 15 BUSY 300 15 FREE 315 40 FREE 355 25 BUSY

Deallocating space � 3. There are no programs on either side of the freed

Deallocating space � 3. There are no programs on either side of the freed space: Starts Size Status 200 50 BUSY 250 50 FREE Starts 250 50 FREE 300 15 BUSY 200 50 BUSY 300 15 FREE 315 40 FREE 250 105 FREE 315 40 FREE 355 25 BUSY Size Status