OS Lecture-11 (Memory Management)
OS Lecture-11 (Memory Management)
Memory Management
Background
Memory is central to the operation of a modern computer system.
• Program must be brought (from disk) into memory and placed within a
process for it to be run.
• Protection of memory:
– Protection of memory is necessary to ensure correct operation.
– Protect the operating system from access by user processes.
– Protect user processes from one another.
– This protection must be provided by the hardware & can be implemented in
several ways.
Base and Limit Registers
• One method to implement protection: use Base and Limit Registers
─ We can provide this protection by using two registers (base & limit)
• The base register >> holds the physical address of the first byte in
the legal range.
– Procedure (2): restrict the ability to load base & limit registers only
to OS.
Hardware Address Protection (cont.)
• Procedure (1): The CPU hardware compare every address generated in
user mode with the registers.
• If (CPU generated address ≥ base) & (CPU generated address < base + limit) …
₋ Then …..the CPU generated address is legal and allowed to access the
memory
₋ Else…… the CPU generated address is illegal and NOT allowed to access the
memory…..(causing a trap (/error) to OS).
Hardware Address Protection (cont.)
• Procedure (2): restrict the ability to load base & limit registers ONLY to OS.
• This scheme allows the operating system to change the value of the
registers but prevents user programs from changing the registers’
contents.
Address Binding
Multistep Processing of a User Program
Address Binding
• Address binding (or relocation): The process of associating program
instructions and data to physical memory addresses.
─ The linkage editor or loader will in turn bind the relocatable addresses to
absolute addresses (physical address) (such as 74014).
– Load time. When it is not known at compile time where the process will reside
in memory, then the compiler translates symbolic addresses to relative
(relocatable) addresses. The loader translates these to absolute addresses
(Static).
– Execution time. If the process can be moved during its execution from one
memory segment to another, then binding must be delayed until run time. The
absolute addresses are generated by special hardware (e.g. MMU)
• Most general-purpose OSs use this method (Dynamic).
Secondary storage
routine()
Memory {
-----;
main() -----;
{ -----;
---; -----;
---; }
---;
---;
}
Dynamic Loading Advantages
• Better memory utilization.
• Stub is executed.
• It replaces itself with the address of the routine and executes the
routine.
• Next time when that code segment is reached the library routine is
executed directly
Dynamic Linking - Example
Secondary storage
printf()
Memory {
-----;
main() -----;
{ -----;
---; -----;
---; }
---;
---;
}
Swapping
• Swapping is a mechanism in which a process can be swapped temporarily
out of main memory to a backing store, and then brought back into
memory for continued execution.
• Backing store is a usually a hard disk drive or any other secondary storage
which fast in access and large.
Swapping (cont.)
• How swapping performed???
– The system maintains a ready queue consisting of all processes whose
memory images are on the backing store or in memory and are ready
to run.
– The dispatcher checks to see whether the next process in the queue is
in memory.
Memory Management
Schemes (/Strategies)
Contiguous
Paging Segmentation
Allocation
Multiple-partition Conventional
method Variable size Paging Hierarchical Hashed Inverted
(Fixed-partition method Paging Paging Paging
allocation) (basic method)
Memory Management Strategy #1:
Contiguous Memory Allocation
Contiguous Memory Allocation
• The memory is usually divided into two partitions:
– One for the resident OS
– One for the user processes.
• Basic methodology:
– Each process is contained in a single contiguous section of memory.
• A job arrives, put it into the input queue for the smallest partition
large enough to hold it
– Any space in a partition not used by a job is lost
Example: Multiprogramming With Fixed Partitions
800K
Partition 4
700K
Partition 3
Multiple input queues
400K
Partition 2
200K
Partition 1
100K
OS
0
Single Input Queue
800K
• Disadvantage of multiple
Partition 4
input queues 700K
– Small jobs may wait,
while a queue with
larger memory is Partition 3
empty
400K
• Solution: single input
queue Partition 2
200K
Partition 1
100K
OS
0
Variable-partition Allocation
• Problem with multiple-partition allocation: Degree of multiprogramming
limited by number of partitions.
0 0 0 0 0
OS OS OS OS OS
400 400 400 400 400
P1 P1 P1 P5
1000 900
1000 1000 1000 1000
P4 P4 P4
P2 1700 1700 1700
2000 2000 2000 2000 2000
P3 P3 P3 P3 P3
2300 2300 2300 2300 2300
– 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: Best-fit:
P1>>> 100, 500, 200, 300, 600 P1>>> 100, 500, 200, 300, 600
P2>>> 100, 288, 200, 300, 600 P2>>> 100, 500, 200, 88, 600
P3>>> 100, 288, 200, 300, 183 P3>>> 100, 83, 200, 88, 600
100, 116, 200, 300, 183 final set P4>>> 100, 83, 88, 88, 600
P4 (426K) must wait of holes 100, 83, 88, 88, 174
Worst-fit:
P1>>> 100, 500, 200, 300, 600
P2>>> 100, 500, 200, 300, 388
P3>>> 100, 83, 200, 300, 388
100, 83, 200, 300, 276
P4 (426K) must wait
>>> In this example, Best-fit turns out to be the best because there is no wait processes.
Fragmentation
• Fragmentation is a problem appears in memory allocation schemes
due to unusable memory space.
• Fragmentation types:
─ External fragmentation: memory space to satisfy a request is
available, but is not contiguous…(i.e. storage is fragmented into a
large number of small holes).
• Both the first-fit and best-fit strategies for memory allocation suffer from
external fragmentation.
used free
Partition 2
un-used Partition 2 used
used
Partition 3 Partition 3 used
un-used
used free
Partition 4
un-used Partition 4 used
Internal External
Fragmentation Fragmentation
External Fragmentation Solution
• Compaction: Memory contents shuffled to place all free memory
together in one large block
– Dynamic relocation (run-time binding) needed
– Compaction is expensive (high overhead)
1900K 1900K
200K P3
2100K 2100K 2100K
original allocation moved 600K moved 400K moved 200K
Internal vs. External Fragmentation
Key Internal Fragmentation External Fragmentation
When there is a difference between When there are small and non-
required memory space vs. allotted contiguous memory blocks which
1 Definition memory space, problem is termed cannot be assigned to any process, the
as Internal Fragmentation. problem is termed as External
Fragmentation.
Best Fit Block Search is the solution Compaction is the solution for external
4 Solution for internal fragmentation. fragmentation.