chapter 2web.fsktm.um.edu.my/~mazliza/kuliah/os/02-early-memory-mgmt.pdf · job 1 (30k) remainder...

33
1 CHAPTER 2: EARLY MEMORY MANAGEMENT SYSTEMS WIA2004 (2) 1 INTRODUCTION Management of main memory is critical. The performance of the entire system depends on: How much memory is available, How it is optimized while jobs are being processed. This chapter introduces: The memory manager (RAM) Core memory (primary storage) WIA2004 (2) 2 2

Upload: others

Post on 22-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

1

CHAPTER 2: EARLY MEMORY MANAGEMENT SYSTEMS

WIA2004 (2) 1

INTRODUCTION

• Management of main memory is critical.

• The performance of the entire system depends on:

• How much memory is available,

• How it is optimized while jobs are being processed.

• This chapter introduces:

• The memory manager (RAM)

• Core memory (primary storage)

WIA2004 (2) 22

Page 2: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

2

INTRODUCTION

• 4 types of memory allocation schemes:

• Single-user systems,

• Fixed partitions,

• Dynamic partitions,

• Relocatable dynamic partitions.

• These early memory management schemes are seldom used by today’s OSs but are important to study because each introduces fundamental concepts that helped memory management evolve.

WIA2004 (2) 33

SINGLE-USER CONTIGUOUS SCHEME

• Commercially available in 1940s and 1950s.

• Each program was loaded in its entirety into memory and allocated as much contiguous memory space allocated as it needed.

• If the program was too large and didn’t fit the available memory space, it couldn’t be executed,

• Although early computers were physically large, they had very little memory.

• Computers have only a finite amount of memory.

WIA2004 (2) 4

Page 3: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

3

SINGLE-USER CONTIGUOUS SCHEME

• If a program doesn’t fit, then either the size of the main memory must be increased or the program must be modified.

• Make the program smaller – Use methods that allow program segments (partitions made to the program) to be overlaid.

• Transfer segments of a program from secondary storage into main memory for execution

• Two or more segments take turns occupying the same memory locations.

WIA2004 (2) 5

SINGLE USER CONTIGUOUS SCHEME

• How it works:

• Job/program is loaded (entirety), into memory and is allocated as much contiguous space in memory as it needs.

• If program fits

• Execute

• Else

• End

• If a program doesn’t fit, then either the size of main memory must be increased, or the program must be modified to fit.

WIA2004 (2) 6

Page 4: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

4

WIA2004 (2) 7

Operating System

Job 1 (30K)

Remainder of Main Memory is Unused

10K

40K

• It is a non-networked environment.

• All available memory is allocated to a job.

• Jobs are processed sequentially, one after the other.

• The Memory Manager:

1. Evaluate the incoming job to see if it fits the memory space.

• If it fits, load into memory. Otherwise, reject and evaluate the next process.

WIA2004 (2) 8

Page 5: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

5

2. Monitor the occupied memory space.

• When the resident job ends its execution and no longer needs to be in memory, indicate that the entire amount of main memory space is now available and return to Step 1 (evaluate the next incoming job).

WIA2004 (2) 9

• Major problem:

• It doesn’t support multiprogramming (multiple jobs or processes occupying memory at the same time)

WIA2004 (2) 10

Page 6: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

6

ALGORITHM

1. Store first memory location of program into base register (for memory protection).

2. Set program counter equal to address of first memory location (to keep track of memory space used by the program).

3. Read first instruction of program.

4. Increment program counter by number of bytes in instruction.

WIA2004 (2) 11

5. Has the last instruction been reached?

• If yes, then stop loading program.

• If no, then continue with Step 6.

6. Is program counter greater than memory size?

• If yes, then stop loading program.

• If no, then continue with Step 7.

7. Load instruction in memory.

WIA2004 (2) 12

Page 7: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

7

8. Read next instruction of program.

9. Go to Step 4.

WIA2004 (2) 13

DISADVANTAGES

• Does not support multiprogramming or networking.

• It can handle only one job at a time.

• This configuration was first used in research institutions but proved unacceptable for the business community.

• Not cost effective to spend almost $200,000 for an equipment that could be used by only one person at a time.

WIA2004 (2) 14

Page 8: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

8

IN CONCLUSION..

• The amount of work performed by the Memory Manager is minimal.

• Only two hardware items are needed:

• A register,

• An accumulator.

• Once the program is entirely loaded into memory, it remains there until execution is complete, either through normal termination or by intervention of the OS.

WIA2004 (2) 15

FIXED PARTITIONS

• Introduced protection of the job’s memory space.

• Once a partition was assigned to a job, no other job could be allowed to enter its boundaries, either accidentally or inten�onally ─ not a problem in single-user contiguous allocation schemes.

• More flexible than the Single-User scheme because it allows several programs to be in memory at the same time.

• Still requires the entire program be stored contiguously and in memory from the beginning to the end of its execution.

WIA2004 (2) 16

Page 9: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

9

FIXED PARTITIONS

• The first attempt to support multiprogramming used fixed (static) partitions within main memory.

• One partition for each job.

• The size of each partition was designated when the system was powered on.

• Each partition could only be reconfigured when the system was shut down.

• Once the system was in operation, the partition sizes remained static.

WIA2004 (2) 17

FIXED PARTITIONS

WIA2004 (2) 18

Page 10: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

10

FIXED PARTITIONS

• Memory manager allocates memory space to jobs.

WIA2004 (2) 19

FIXED PARTITIONS

• In order to allocate memory spaces to jobs, the Memory Manager must keep a table which shows:• Each partition size,

• Its address,

• Its access restrictions,

• Its current status (free or busy).

• As each job terminates, the status of its memory partition is changed from busy to free so an incoming job can be assigned to that partition.

WIA2004 (2) 20

Page 11: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

11

FIXED PARTITIONS

• Disadvantages

• The scheme works well if all the jobs run on the system are of the same size, or if the sizes are known ahead of time and don’t vary between reconfiguration.

• If the partition sizes are too small:

• Larger jobs are rejected if they’re too big to fit into the largest partition.

• Large jobs wait if the large partitions are busy.

• Large jobs may have a longer turnaround time as they wait for free partitions of sufficient size or may never run. WIA2004 (2) 21

FIXED PARTITIONS

• Disadvantages

• If the partition sizes are too big, memory is wasted.

• If a job does not occupy the entire partition, the unused memory in the partition will remain idle and cannot be allocated to another job.

• Partial use of fixed partitions and the coinciding creation of unused spaces within the partition is called internal fragmentation and is a major drawback of this scheme.

WIA2004 (2) 22

Page 12: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

12

DYNAMIC PARTITIONS

• Available memory is still kept in contiguous blocks but jobs are given only as much memory as they request when they are loaded for processing.

• It fully utilizes memory when the first jobs are loaded.

• As new jobs enter the system that are not the same size as those that just vacated memory, they are fit into the available spaces on a priority basis (FCFS).

WIA2004 (2) 23

DYNAMIC PARTITIONS

• The subsequent allocation of memory creates fragments of free memory between blocks of allocated memory ̶ external fragmentation.

• Lets memory go to waste.

WIA2004 (2) 24

Page 13: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

13

DYNAMIC PARTITIONS

WIA2004 (2) 25

BEST-FIT VS. FIRST-FIT ALLOCATION

• For both schemes, the OS must keep lists of each memory location, noting which are free and which are busy.

• As new jobs come into the system, the free partitions must be allocated fairly.

• First-fit memory allocation: First partition fitting the requirements.

• Best-fit memory allocation: Smallest partition fitting the requirements. Least wasted space.

WIA2004 (2) 26

Page 14: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

14

BEST-FIT VERSUS FIRST-FIT ALLOCATION

• 2 ways to organize the lists the free and used partitions, either:

• Best-fit ̶ keeps the free/busy lists in order by size, smallest to largest.

• The first-fit ̶ keeps the free/busy list organized by memory locations (low-order memory to high-order memory).

WIA2004 (2) 27

FIRST-FIT ALLOCATION

WIA2004 (2) 28

Page 15: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

15

BEST-FIT ALLOCATION

WIA2004 (2) 29

BEST-FIT ALGORITHM

• Memory manager keeps two lists:

• One for free memory blocks,

• One for busy memory blocks.

• A simple loop compares the size of each job to the size of each memory block until a block is found that is large enough to fit the job.

• The job is loaded into that memory block and the Memory Manager exits the loop to fetch the next job from the entry queue.

WIA2004 (2) 30

Page 16: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

16

BEST-FIT ALOGRITHM

• If the entire list is searched in vain, then the job is placed into a waiting queue.

• The Memory Manager fetches the next job and repeats the process.

WIA2004 (2) 31

FIRST-FIT ALLOCATION

WIA2004 (2) 32

Page 17: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

17

BEST-FIT ALGORITHM

• The goal is to find the smallest memory block into which the job will fit.

• The entire table must be searched before the allocation can be made because the memory blocks are physically stored in sequence according to their location in memory.

WIA2004 (2) 33

BEST-FIT ALGORITHM

WIA2004 (2) 34

Page 18: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

18

HYPOTHETICAL ALLOCATION SCHEMES

• Next-fit

• Starts searching from last allocated block, for the next available block when a new job arrives.

• Worst-fit

• Allocates largest free available block to a new job.

• Opposite of best-fit.

• Good way to explore theory of memory allocation.

• Not best choice for an actual system.

WIA2004 (2) 35

DEALLOCATION

• Eventually, the memory space must be released or deallocated.

• Deallocation might create external fragmentation.

WIA2004 (2) 36

Page 19: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

19

DEALLOCATION – FIXED PARTITION SYSTEM

• When the job is completed, the Memory Manager resets the status of the memory block where the job was stored to “free”.

• Any code may be used, e.g., binary values with 0 indicating free and 1 indicating busy.

WIA2004 (2) 37

DEALLOCATION – DYNAMIC PARTITION SYSTEM

• Algorithm is more complex because it tries to combine free areas of memory whenever possible.

• 3 possible scenarios:

• Case 1: When the block to deallocate is adjacent to another free block.

• Case 2: When the block to deallocate is between two free blocks.

• Case 3: When the block to deallocate is isolated from other free blocks.

WIA2004 (2) 38

Page 20: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

20

CASE 1: JOINING TWO FREE BLOCKS

WIA2004 (2) 39

CASE 1: JOINING TWO FREE BLOCKS

WIA2004 (2) 40

Page 21: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

21

CASE 2: JOINING THREE FREE BLOCKS

WIA2004 (2) 41

CASE 2: JOINING THREE FREE BLOCKS

WIA2004 (2) 42

Page 22: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

22

CASE 3: DEALLOCATING AN ISOLATED BLOCK

• The space to be deallocated is isolated from all other free areas.

• Using the deallocation algorithm, the system learns that the memory block to be released is not adjacent to any free blocks of memory.

• It is between two other busy areas.

• The system must search the table for a null entry.

• When the null entry is found:

• The beginning memory location of the terminating job is entered in the beginning address column.

WIA2004 (2) 43

CASE 3: DEALLOCATING AN ISOLATED BLOCK

• The job size is entered under the memory block size column.

• The status is changed from a null entry to free to indicate that a new block of memory is available.

WIA2004 (2) 44

Page 23: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

23

CASE 3: DEALLOCATING AN ISOLATED BLOCK

WIA2004 (2) 45

CASE 3: DEALLOCATING AN ISOLATED BLOCK

WIA2004 (2) 46

Page 24: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

24

CASE 3: DEALLOCATING AN ISOLATED BLOCK

WIA2004 (2) 47

CASE 3: DEALLOCATING AN ISOLATED BLOCK

WIA2004 (2) 48

Page 25: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

25

RELOCATABLE DYNAMIC PARTITIONS

• Both the fixed and dynamic memory allocation schemes cause unacceptable fragmentation that had to be resolved before the number of jobs waiting to be accepted became unwieldy.

• There was a growing need to use all the slivers of memory often left over.

• The solution: relocatable dynamic partitions.

WIA2004 (2) 49

RELOCATABLE DYNAMIC PARTITIONS

• The Memory Manager relocates programs to gather together all of the empty blocks and compact them to make one block of memory large enough to accommodate some or all of the jobs waiting.

• The compaction of memory (defragmentation) is performed by the OS to reclaim fragmented sections of the memory space.

• Every program in memory must be relocated so they’re contiguous.

WIA2004 (2) 50

Page 26: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

26

RELOCATABLE DYNAMIC PARTITIONS

• The compaction of memory

• Every address and reference to an address, within each program must be adjusted to account for the program’s new location in memory.

• All other values within the program must be left alone.

• OS must distinguish between addresses and data values.

WIA2004 (2) 51

RELOCATABLE DYNAMIC PARTITIONS

WIA2004 (2) 52

Page 27: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

27

RELOCATABLE DYNAMIC PARTITIONS

WIA2004 (2) 53

RELOCATABLE DYNAMIC PARTITIONS

WIA2004 (2) 54

Page 28: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

28

RELOCATABLE DYNAMIC PARTITIONS

• Issues:

• What goes on behind the scenes when relocation and compaction take place?

• What keeps track of how far each job has moved from its original storage area?

• What lists have to be updated?

WIA2004 (2) 55

RELOCATABLE DYNAMIC PARTITIONS

• After relocation and compaction, both the free and the busy lists are updated.

• Free list is changed to show the partition for thenew block of free memory.

• Busy list is changed to show the new locations for all of the jobs already in process that were relocated.

WIA2004 (2) 56

Page 29: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

29

RELOCATABLE DYNAMIC PARTITIONS

• Special-purpose registers are used for relocation.

• Bounds register

• Used to store the highest (or lowest) location in memory accessible by each program.

• This ensures that, during execution, a program won’t try to access memory locations that doesn’t belong to it.

WIA2004 (2) 57

RELOCATABLE DYNAMIC PARTITIONS

• Relocation register

• Contains the value that must be added to each address referenced in the program so that the system will be able to access the correct memory addresses after relocation.

• If the program isn’t relocated, the value, “zero” is stored in the program’s relocation register.

WIA2004 (2) 58

Page 30: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

30

RELOCATABLE DYNAMIC PARTITIONS

WIA2004 (2) 59

RELOCATABLE DYNAMIC PARTITIONS

• By compacting and relocating, the Memory Manager optimizes use of memory, and thus, improves throughput.

• A drawback is that more overhead is incurred than with the two previous memory allocation schemes.

• The crucial factor is the timing of the compaction.

• When and how often should it be done.

WIA2004 (2) 60

Page 31: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

31

RELOCATABLE DYNAMIC PARTITIONS

• 3 options for the timing of compaction:

1. Do it when a certain percentage of memory becomes busy, e.g., 75%

2. Compact memory only when there are jobs waiting to get in.

3. Do it after a prescribed amount of time has elapsed.

WIA2004 (2) 61

RELOCATABLE DYNAMIC PARTITIONS

• The best choice for any system is decided by the OS designer who, based on the job mix and other factors, tries to optimize both processing time and memory use while keeping overhead as low as possible.

WIA2004 (2) 62

Page 32: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

32

SUMMARY

• 4 memory management techniques:

• Single-user systems,

• Fixed partitions,

• Dynamic partitions,

• Relocatable dynamic partitions.

WIA2004 (2) 63

SUMMARY

• Common requirements of memory management techniques:

• Entire program loaded into memory,

• Contiguous storage,

• Memory residency until job completed.

• Each places severe restrictions on job size.

WIA2004 (2) 64

Page 33: CHAPTER 2web.fsktm.um.edu.my/~mazliza/kuliah/OS/02-early-memory-mgmt.pdf · Job 1 (30K) Remainder of Main Memory is Unused 10K 40K •It is a non-networked environment. •All available

33

SUMMARY

• Sufficient for first three generations of computers which processed jobs in batch mode.

• Turnaround time was measured in hours.

• New modern memory management trends in late 1960s and early 1970s

• Common characteristics of memory schemes:

• Programs are not stored in contiguous memory,

• Not all segments reside in memory during job execution.

WIA2004 (2) 65