Basic Memory Management
Memory management is a crucial
function of the operating system. Its primary purpose is to manage the
computer's primary memory, where data and programs are stored either
temporarily or permanently for processing by the CPU. Basic memory management
in operating systems typically involves the following tasks:
- Keeping Track of Used and Unused Memory: The
operating system keeps track of which parts of memory are currently being
used and by whom, and which parts are not in use.
- Allocating and Deallocating Memory Blocks:
When a process requests memory, the operating system must allocate memory
to that process if possible. After a process finishes and no longer needs
its allocated memory, the operating system must deallocate that memory so
it can be used by other processes.
- Memory Protection: The operating system must
ensure that a process can't interfere with the memory of another process.
This is achieved by setting up protections in hardware that prevent a
process from accessing memory that it doesn't own.
- Physical and Logical Address Space: Each
process operates in its own logical address space, which it perceives as a
contiguous sequence of addresses, i.e., a large array. In reality, these
logical addresses are mapped to physical addresses in the computer's
memory. The operating system manages this mapping.
The operating system might use
various strategies to manage memory, such as partitioning (dividing memory into
sections), paging (dividing memory into fixed-size pages), and segmentation
(dividing memory into variable-size segments based on logical divisions within
a program).
These are just the basics; more
advanced memory management schemes might involve the use of virtual memory,
where memory can be "extended" by using disk space. Memory management
can be a complex task, especially in modern operating systems where multiple
processes might be running concurrently, each of which requires access to
memory.
Basic Memory Management
Partitioning
Partitioning is a memory
management technique used by operating systems to divide the primary memory or
main memory into distinct, non-overlapping sections or partitions.
There are two main types of
partitioning:
- Fixed Partitioning: In this approach, the
memory is divided into a number of fixed-size partitions. Each partition
may contain exactly one process. When a partition is free, a process is
selected from the input queue and is loaded into the free partition. When
the process terminates, the partition becomes available for another
process. There are two types of fixed partitioning:
- Equal-size partitions: All partitions are
of the same size. This approach is simple but may not make efficient use
of memory if processes are much smaller than the partition size.
- Unequal-size partitions: Partitions are of
different sizes. This is more flexible and can reduce wasted memory
(internal fragmentation), but it may be more complex to manage.
- Dynamic Partitioning: In this approach, the
partitions are of variable length and number. When a process arrives and
needs memory, the exact amount of memory required by the process is
allocated. While dynamic partitioning improves memory utilization and
reduces internal fragmentation, it can lead to external fragmentation as
free memory can become divided into non-contiguous blocks.
In both fixed and dynamic
partitioning, the operating system must manage the partitions, keep track of
which partitions are in use and which are free, and assign processes to free
partitions.
While simple and effective in
certain situations, partitioning can lead to problems like internal
fragmentation (unused memory within a partition) or external fragmentation
(unused memory between partitions). Modern operating systems often use more
sophisticated memory management techniques such as paging and segmentation.
Partitioning Fixed and Variable
In the context of memory
management in operating systems, partitioning is a scheme to divide main memory
into sections where separate jobs or processes are loaded. There are two types
of partitioning: Fixed Partitioning and Variable (or Dynamic) Partitioning.
- Fixed Partitioning: In Fixed Partitioning,
the main memory is divided into fixed-size partitions at system
initialization, and this division remains constant during the system
operation. There are two variations:
- Equal-size Partitions: Here, memory is
divided into equal-sized chunks. This is straightforward but can lead to
significant internal fragmentation (unused memory within a partition) if
the loaded processes are significantly smaller than the partition size.
- Unequal-size Partitions: Here, memory is
divided into partitions of different sizes. This is often done to
accommodate processes of different sizes more efficiently, hence reducing
internal fragmentation.
In both cases, each partition can
only hold one process at a time.
- Variable (Dynamic) Partitioning: In Variable
Partitioning, the memory is not divided into partitions at system
initialization. Instead, each time a process arrives and needs to be
loaded into memory, it is allocated exactly as much memory as it requires.
Over time, as processes are loaded and removed, memory is continually divided
and coalesced, creating partitions of variable size and number.
The advantage of Variable
Partitioning is that it reduces internal fragmentation because partitions are
made to fit the process sizes exactly. However, it can lead to external
fragmentation, where free memory becomes divided into non-contiguous chunks.
Both Fixed and Variable
Partitioning have their pros and cons, and the choice depends on the specific
requirements and constraints of the system. Today, many systems use more
advanced techniques like paging and segmentation to manage memory, which can
handle the memory in a more flexible and efficient way.
Free Space management
Techniques
Free space management is crucial
in an operating system to keep track of all the unallocated space in the
memory. Without it, the operating system may not be able to allocate memory
efficiently, leading to poor performance or even system failures. Here are some
common techniques used in free-space management:
- Bitmaps: Memory is divided into allocation
units, and each is represented by a bit in the bitmap. If the bit is set
(1), the allocation unit is occupied. If it is cleared (0), the unit is
free. Bitmaps provide an easy way to find contiguous free space but can be
inefficient if the allocation unit is small.
- Linked List (Free List): This method uses a
linked list to manage free memory. Each node in the list points to a free
block of memory. The list can be traversed to find a suitable block when
allocation is required. This method is simple and efficient for large
allocation units but finding a suitable block may require traversing the
list.
- Buddy System: This is a dynamic memory
allocation system that partitions memory into halves until it reaches a
partition size adequate to satisfy a memory request. When a partition is
freed, the buddy system merges adjacent blocks back into larger ones if
possible. This method is efficient and reduces external fragmentation but
can suffer from internal fragmentation.
- Boundary Tags: This method involves storing
the size of free blocks in the header and footer of each block, allowing
efficient coalescing of adjacent free blocks. The free blocks can be
organized in a binary tree, linked list, or similar data structure for
efficient searching.
- Indexed Allocation: In this method, an index
block is associated with a file. The index block contains pointers to
every block that the file uses. It provides fast direct access but
requires a priori knowledge of how many blocks a file will need.
Each of these techniques has its
own strengths and weaknesses. The best choice of free space management
technique often depends on the specific system and its requirements.