0% found this document useful (0 votes)
13 views

Chapter 3 Memory Management

Uploaded by

Zeph
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)
13 views

Chapter 3 Memory Management

Uploaded by

Zeph
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/ 10

Chapter 3: Central Memory Management

Introduction

Originally, central memory was a costly and limited resource that had to be carefully managed.
Although its size has significantly increased over time, the challenge of memory management remains
crucial due to the growing needs of users. In an operating system, the memory manager is responsible
for:

• Identifying free and occupied areas of memory,


• Allocating memory to processes (without waste and with security),
• Reclaiming memory when a process terminates,
• Addressing the limitation of physical memory to enable programs to continue their execution
even if they require more memory space than is currently available.

1. Logical Addressing/Physical Addressing


1.1. Program Processing

A program written in a programming language goes through several stages before being executed:
compilation, linking, loading, and then execution.

• During compilation, each object (variable, file, etc.) is assigned a logical address. This stage
generates an object file.
• Linking establishes the connection between the program and the various relevant files (header
files, files containing other parts of the program, language libraries). This stage generates an
executable file.
• Loading the program: the executable file and language libraries are loaded into central memory.

1.2. Address Binding

In a given system, two types of memory addresses are distinguished:

• The logical address is the address manipulated by the CPU and is visible to the process and the
programmer.
• The physical address is used to reference the physical memory, the one in the hardware.

Address binding refers to the correspondence between a logical address and a physical address. It can be
done during the linking phase, but in most cases, it is performed at the time of execution. Some systems
use a hardware component inside the processor to convert addresses, called MMU: Memory
Management Unit.

Two types of addressing are distinguished:


• Absolute addressing occurs when the logical address requires no processing to obtain the
physical address, and the two addresses are equivalent. However, the program must always be loaded at
the same addresses, or it will not be able to execute. Such a program is called non-relocatable.
• Relative addressing is an addressing scheme that is relative to the first address assigned to the
program. All addresses must be converted or translated starting from this address. This allows changing a
memory area from one execution to another. Such a program is called relocatable.

1/10
2. Memory Allocation Strategies

In a multiprogramming environment, multiple processes reside in memory at the same time. The
challenge then becomes allocating the available memory to these processes. Main memory is typically
divided into two major parts: one for the operating system (OS) and the other for user processes.

There are essentially two modes of central memory management: contiguous allocation mode, where
each program is placed in a single contiguous zone, and non-contiguous allocation mode, where a
program can be distributed across multiple non-adjacent zones.

2.1. Contiguous Memory Allocation

2.1.1. Fixed-Size Partitions (Static Partitioning)

One of the simplest schemes is to subdivide memory into fixed-size partitions (not necessarily of equal
size). Each partition can hold exactly one process. When a process terminates, the partition becomes
free and can be allocated for another process. However, this technique, now considered outdated, has
two major drawbacks:

• Saturation: A process can quickly occupy the entire allocated partition, leading to the
termination of its execution due to insufficient space. The program must then be moved to a larger
partition.

• Internal fragmentation occurs when allocated memory cannot be utilized. For example, if a
1KB process runs on a 3KB partition, there are 2KB of free space that remain unusable.

2.1.2. Variable-Size Partitions (Dynamic Partitioning)

The use of variable-size partitions allows for a better fit of partitions to the sizes of processes by
allocating memory based on demands and availability. The memory allocated to a process becomes
free again after the process is terminated. It's worth noting that the problem of internal fragmentation
no longer arises since each process is allocated exactly the amount of memory it needs.

The following figure provides an overview of the initial allocation of central memory (assumed to be
100 KB in size with 20 KB reserved for the operating system) to requesting processes, specifying the
sizes of the spaces they want to allocate.

2/10
After a certain period, memory takes on the appearance of an alternating pattern of holes (free areas) and
partitions (occupied) of various sizes. The formation of holes is the result of process termination and the
release of the space they occupied during their execution.

External fragmentation of memory, or fragmentation, is a situation in which a request cannot be satisfied


due to insufficient space, even though the sum of the sizes of free areas is greater than (or equal to) that
request. To reduce the effect of external fragmentation, it is possible to group free areas using two
different techniques: concatenation or compaction.

1) Concatenation of Holes

This technique involves consolidating contiguous holes into a single space as soon as they are formed.
The implementation involves running an algorithm to merge the free space with its neighboring holes.

3/10
2) Compaction of Holes

This technique involves consolidating holes (freed and free spaces) into a single zone by running a
garbage collection algorithm. The role of the garbage collector is to move still-active processes to new
locations.

2.1.3. Allocation Algorithms

In the case of contiguous allocation (with fixed or variable-size partitions), one question to consider is:
how to choose a space to allocate to a process from a list of free zones? Several allocation (placement)
algorithms exist, among which we can mention:

• The First-fit algorithm (The first found): choose the first free partition or the first hole (in address
order) of sufficient size.
• The Best-fit algorithm (The best choice): choose the smallest free partition of sufficient size.
• The Worst-fit algorithm (The worst choice): choose the largest free partition to keep small free
areas.

It should be noted that the last two algorithms require traversing the entire list of free spaces.

4/10
2.2. Non-contiguous Memory Allocation

A solution to the problems of external fragmentation and saturation is to allow the logical address
space of a process to no longer be contiguous, thus enabling the allocation of physical memory to a
process wherever it is available, at scattered addresses in memory. This is the allocation mode applied
by current systems. Address correspondence is carried out during execution.

Memory can be allocated in fixed or variable-sized zones. When all zones have the same size, they are
called pages, and the systems are referred to as paged systems. When their size can vary, they are
called segments, and the systems are segmented. Both modes can also be combined, resulting in
segments composed of pages.

3. Swapping Technique

When physical memory is not large enough to accommodate all processes, it is necessary to move
some of these processes to the disk and then bring them back to main memory for execution. This
movement of processes between main memory and the disk is called swapping.

This technique became widespread during the era of time-sharing when multiple processes needed to be
executed per user. When a process completes its time quantum and there are more processes than
memory can hold, it is entirely moved to the hard disk by a process called the Swapper. It is the
scheduler, in collaboration with the Swapper, that is responsible for bringing it back once its execution
time approaches. All of this remains transparent to the user. Once brought back, the process will be
placed in a new address translated and calculated by the MMU.

This technique assumes that a portion of the hard disk complements main memory; it is called the Swap
Zone, and it is used only to store swapped-out processes from main memory.

4. Virtual Memory
4.1. Presentation

In the early days of computing, when a program was too large for the available physical memory
space, it had to be divided into separately loadable modules (overlays). This overlay-based
segmentation was carried out by the programmer and represented a complex task.

5/10
This technique gave way to automatic management of overlay branches, making it transparent to the
programmer. This method, called Virtual Memory, also allowed for the definition of an infinite
central memory from the programmer's perspective, enabling the execution of programs of any size
on architectures with small central memories.

Virtual memory is a technique that involves the separation of logical addressing and physical
addressing. It is based on the fact that only small parts of programs need to be in memory for
immediate execution. Therefore, logical addressing can be more extensive than the actual physical
space, and swap mechanisms (swapping) are used to transfer parts of programs between main
memory and auxiliary memory.

As a result, the number of addressable words no longer depends on the physically available memory
words. It only depends on the number of bits in the address.

To implement a virtual memory system, three techniques are proposed: paging, segmentation, and
segmentation/paging. In this course, we study paging.

4.2. Paging

4.2.1. Presentation

The paging technique involves dividing the virtual address space supported by auxiliary memory into
pages of the same size. The main memory, in turn, is also divided into pages of the same size as those
in the virtual space.

The principle of paging (or paged memory) is to have only certain pages of the program, the useful
pages, in main memory. The other pages reside in auxiliary memory and must be brought into main
memory when referenced. This auxiliary memory must be able to contain the entire program to be
executed.

4.2.2. Address Translation Mechanism

Let's take the example of an address space of 64 KB, which is 216. When the program references a
memory address, it generates a corresponding 16-bit virtual address ranging from 0 to 65535. In a 16-
bit address, let's allocate 4 bits for the page number and 12 bits to select a word inside the page, called
the offset. Therefore, the virtual memory in our example consists of 16 pages, each with a size of 4
KB.

Now, let's assume that the physical memory is 32 KB, which is 215. Physical addresses can range from
0 to 32767. Since the offset is always 12 bits, there are 3 bits left for the physical page numbers.
Hence, the physical memory contains 8 pages, each with a size of 4 KB.

Now, the task is to calculate a physical address given a virtual address. To perform such a calculation,
we need to know the mapping status of virtual pages to physical pages.

6/10
This is achieved using a 16-word table called the Page Table. Assuming that the virtual page in
question is already in main memory, constructing a physical memory address from a virtual address is
done as follows:

• Extract the virtual page number.


• Look up the Page Table to find the corresponding physical page number.
• Form the physical address by combining the physical page number and the offset value.

4.2.3. Page Table

The page table has multiple entries (in our example, 16) corresponding to the virtual space that can be
addressed. Each entry in the table corresponds to a descriptor containing several fields.

The most important field is the memory cell number field (i.e., the physical page number) since the
purpose of mapping is to find this value.

Presence bit P:

• If this bit is set to 1, the entry is valid (page present in main memory) and can be used.
• If this bit is set to 0: the virtual page to which this entry belongs is not in main memory.
Accessing an entry in the page table with this bit set to 0 causes a page fault.

Protection bits: indicate the types of access allowed for this page. In the simplest case, a single bit is
used for protection (set to 0: read/write access, set to 1: read-only access).

7/10
Modification bit M: keeps track of the page's usage. The hardware automatically sets the modification
bit when a page is written. This bit is important when the system decides to evict a memory cell. If it
hasn't been modified, it can be simply discarded since the copy on disk is still valid.

Reference bit R: set each time a page is accessed in read or read/write mode. For some systems, this bit
helps choose which page to evict following a page fault.

4.2.4. Process Creation

During the creation of a process, the previous data structures are involved in the following way:

• Ensure that the number of available blocks (in auxiliary memory) for this process is sufficient.
Let N be the number of blocks allocated to the process.
• Create a page table in the system's memory space, and all entries in this table are initialized
with P=0, indicating that the pages are not yet resident in the main memory.
• Bring the page containing the first instruction of the process into the main memory, thereby
modifying its entry in the page table to mark P=1. Loading other pages necessary for the
execution of the process will occur during the handling of page faults.

4.2.5. On-demand paging or page fault

When referencing a page that is not present in the main memory, it is referred to as a page fault. In this
case, the operating system (OS) must read the requested page from auxiliary storage, place it in the main
memory, update its location in the page table, and finally resume the instruction that caused the page
fault.

The algorithm to be implemented during a page fault handling is the following exception handling, which
may involve the replacement procedure.

• If no block is free in the main memory to insert the page


Then
Select a page to evict
If this page has been modified
Then
Rewrite it in auxiliary storage
End if
End if
• Determine the number of the missing page.
• Bring it into the main memory and update the page table.
• Return to the program that caused the page fault.

The page replacement algorithms determine which page should be removed from the main memory to
make room for the requested page. They differ in the information they consider, which is related to the
past usage of pages (loading date, date of last reference, etc.).

8/10
4.2.6. Page Replacement Algorithms

To obtain the optimal algorithm, it would be necessary to know the future references to virtual pages.
However, this is impossible, so algorithms are based on past references or randomness.

Here are some possible replacement strategies:

• FIFO (First In First Out): Replace the page that was loaded into memory the earliest, which
involves dating each virtual page when loaded.

• LRU (Least Recently Used): Replace the least recently used page by the process. This requires
dating all references, which can be expensive. In practice, the paging system often uses only the
two bits R and M to choose the victim page.

• LFU (Least Frequently Used): Replace the least frequently used page. This requires updating a
counter of the number of references to a page, which is not always practical.

• RAND (Random choice): Randomly choose the page to replace, which, in theory, would not
require managing specific information.

• OPT (Optimal Algorithm): This strategy relies on the future knowledge of references to virtual
pages. If there are pages that will not be referenced, replace one of those pages; otherwise,
replace the page that will be referenced the latest. This is a nearly impossible strategy to
implement

9/10
10/10

You might also like