Unit 4 Main Memory (First Part)
Unit 4 Main Memory (First Part)
MANAGEMENT
CHAPTER IV
CONTENT
Memory Management Background, Swapping, Contiguous
Memory Allocation Schemes, Paging, Segmentation,
Virtual Memory Management: Background, Demand
paging scheme, Process Creation, Page Replacement
Policies, Allocation of Frames, Thrashing
BACKGROUND
• Computer solves the problem
• CPU executes the instruction.
• Instructions are stored into Memory.
• Memory Hierarchy – Size ? Speed ? Access ?
BACKGROUND
• Programs are stored into secondary memory
• CPU cannot access Secondary memory directly.
• Programs are loaded into main memory and then CPU fetched it directly.
• Number of processes loaded into main memory is Degree of
multiprogramming.
BACKGROUND- ADDRESS BINDING
• Address binding of instructions and data to memory addresses can happen at
three different stages.
Compile time: If memory location known a priori, absolute code can be
generated; must recompile code if starting location changes.
Load time: Compiler must generate re-locatable code if memory location is not
known at compile time. In this case, Final binding is delayed.
Execution time: Binding delayed until run time if the process can be moved
during its execution from one memory segment to another.
Program
8
LINKING
Static Linking:
• All objects module are copied in one executable file. All library function file
are also copied in one executable code.
• Load time larger, If bug then need to recompile everything.
Dynamic Linking
• Linking postponed until execution time.
• Small piece of code, stub, used to locate the appropriate memory-resident
library routine.
• Stub replaces itself with the address of the routine, and executes the routine.
• Dynamic linking is particularly useful for libraries.
9
LOADING
• Load programs (2ry memory) to main memory.
• Static Loading: Load all programs into main memory.
Dynamic Loading
• Routine is not loaded until it is called
• Better memory-space utilization; unused routine is never loaded.
• Useful when large amounts of code are needed to handle infrequently
occurring cases.
• No special support from the operating system is required implemented
through program design.
10
SWAPPING
• A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution.
OS
Memory
FIXED PARTITION
• Number of partitions are
fixed.
OS OS
• Size of each partition may or
may not be same.
4 MB 4 MB • Spanning is not allowed.
8 MB 4 MB • Internal Fragmentation.
8 MB 4 MB • There is limit in process size.
4 MB 4 MB
• Limitation on degree of
multiprogramming.
• External Fragmentation.
VARIABLE PARTITION
• Number of partitions are not fixed.
OS
• Spanning is not allowed.
• No restriction on degree of Multiprogramming
• No Internal Fragmentation.
• No limitation on process size.
• External Fragmentation.
• Solution to External Fragmentation :
Compaction
ALGORITHMS: FIRST FIT, BEST FIT, WORST FIT
• First-fit: Allocate the first hole that is big enough. Searching can
start at the beginning of the set of holes or where previous first fit
search ended.
• 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.
FIRST FIT
• Allocate the first hole that is big enough.
Searching can start at the beginning of
the set of holes or where previous first fit
search ended.
• If process P1 comes and its size is 15.
• Simple, Fast and Convenient
BEST FIT
• Allocate the smallest hole
that is big enough; must
search entire list, unless
ordered by size. Produces
the smallest leftover hole.
• If process P1 comes and its
size is 15.
• Small internal
fragmentation but this can
be disadvantage.
WORST FIT
• Allocate the largest hole;
must also search entire list.
Produces the largest
leftover hole.
• If process P1 comes and its
size is 15.
• Large Internal
Fragmentation but this can
be advantage
NUMERICAL BASED ON ALGORITHM
• Given memory partition of 500 KB, 200 KB, 300 KB and 600 KB (in order),
how would each of the first fit, best fit and worst fit algorithms place processes
of 212 KB, 417 KB, 112 KB and 426 KB (in order)
500 KB
200 KB
300 KB
600 KB
• Given memory partition of 500 KB, 200 KB, 300 KB
FIRST FIT and 600 KB (in order), how would first fit algorithm
place processes of 212 KB, 417 KB, 112 KB and 426
KB (in order)
600 KB P2
P4 is in waiting
• Given memory partition of 500 KB, 200 KB, 300
BEST FIT KB and 600 KB (in order), how would best fit
algorithm place processes of 212 KB, 417 KB, 112
KB and 426 KB (in order)
200 KB P3
Internal Fragmentation = (500-417)+(200-
300 KB P1 112)+(300-212)+(600-426)= 433
600 KB P4
Is there a external fragmentation = No
• Given memory partition of 500 KB, 200 KB, 300 KB
WORST FIT and 600 KB (in order), how would worst fit algorithm
place processes of 212 KB, 417 KB, 112 KB and 426
KB (in order)
500 KB P2
Internal Fragmentation = (500-417)+(300-112)+(600-
200 KB 212)= 659
300 KB P3
Is there a external fragmentation = YES
600 KB P1
P4 is in wait.
FRAGMENTATION (SUMMARIZED…)
10 10 12
where pi is an index into the outer page table, and p2 is the displacement within the page of the outer page table.
• The virtual page number is hashed into a page table. This page table
contains a chain of elements hashing to the same location.
Each element consists of three fields (i) virtual Page No , (ii) Value of the
mapped page frame (iii) a pointer to the next element in the linked list
Operating Systems 6KS01 42
INVERTED PAGE TABLE
• Entry consists of the virtual address of the page stored in that real
memory location, with information about the process that owns that
page.
• Decreases memory needed to store each page table, but increases
time needed to search the table when a page reference occurs.
• Use hash table to limit the search to one — or at most a few —
page-table entries.
4
1
3 2
4