OS UNIT 3 PART 1
OS UNIT 3 PART 1
Swapping has been subdivided into two concepts: swap-in and swap-out.
Swap-out is a technique for moving a process from RAM to the hard disc.
Swap-in is a method of transferring a program from a hard disc to main
memory, or RAM.
Advantages
If there is low main memory so some processes may has to wait for much long
but by using swapping process do not have to wait long for execution on CPU.
It utilize the main memory.
Using only single main memory, multiple process can be run by CPU using
swap partition.
The concept of virtual memory start from here and it utilize it in better way.
This concept can be useful in priority based scheduling to optimize the
swapping process.
Disadvantages
If there is low main memory resource and user is executing too many processes
and suddenly the power of system goes off there might be a scenario where
data get erase of the processes which are took parts in swapping.
Chances of number of page faults occur.
Low processing performance.
Memory Allocation
Memory allocation refers to the process of assigning memory to different processes
or programs running on a computer system. There are two types of memory
allocation techniques that operating systems use: contiguous and non-contiguous
memory allocation. In contiguous memory allocation, memory is assigned to a
process in a contiguous block. In non-contiguous memory allocation, memory is
assigned to a process in non-adjacent blocks.
Contiguous Memory Allocation
Contiguous memory allocation is a technique where the operating system allocates a
contiguous block of memory to a process. This memory is allocated in a single,
continuous chunk, making it easy for the operating system to manage and for the
process to access the memory. Contiguous memory allocation is suitable for
systems with limited memory sizes and where fast access to memory is important.
Contiguous memory allocation can be done in two ways
Fixed Partitioning − In fixed partitioning, the memory is divided into fixed-
size partitions, and each partition is assigned to a process. This technique is
easy to implement but can result in wasted memory if a process does not fit
perfectly into a partition.
Dynamic Partitioning − In dynamic partitioning, the memory is divided into
variable size partitions, and each partition is assigned to a process. This
technique is more efficient as it allows the allocation of only the required
memory to the process, but it requires more overhead to keep track of the
available memory.
Advantages of Contiguous Memory Allocation
Simplicity − Contiguous memory allocation is a relatively simple and
straightforward technique for memory management. It requires less overhead
and is easy to implement.
Efficiency − Contiguous memory allocation is an efficient technique for
memory management. Once a process is allocated contiguous memory, it can
access the entire memory block without any interruption.
Low fragmentation − Since the memory is allocated in contiguous blocks,
there is a lower risk of memory fragmentation. This can result in better
memory utilization, as there is less memory wastage.
Disadvantages of Contiguous Memory Allocation
Limited flexibility − Contiguous memory allocation is not very flexible as it
requires memory to be allocated in a contiguous block. This can limit the
amount of memory that can be allocated to a process.
Memory wastage − If a process requires a memory size that is smaller than
the contiguous block allocated to it, there may be unused memory, resulting in
memory wastage.
Difficulty in managing larger memory sizes − As the size of memory
increases, managing contiguous memory allocation becomes more difficult.
This is because finding a contiguous block of memory that is large enough to
allocate to a process becomes challenging.
External Fragmentation − Over time, external fragmentation may occur as a
result of memory allocation and deallocation, which may result in non −
contiguous blocks of free memory scattered throughout the system.
Overall, contiguous memory allocation is a useful technique for memory
management in certain circumstances, but it may not be the best solution in all
situations, particularly when working with larger amounts of memory or if flexibility is
a priority.
Non-contiguous Memory Allocation
Non-contiguous memory allocation, on the other hand, is a technique where the
operating system allocates memory to a process in non-contiguous blocks. The
blocks of memory allocated to the process need not be contiguous, and the
operating system keeps track of the various blocks allocated to the process. Non-
contiguous memory allocation is suitable for larger memory sizes and where efficient
use of memory is important.
Non-contiguous memory allocation can be done in two ways
Paging − In paging, the memory is divided into fixed-size pages, and each
page is assigned to a process. This technique is more efficient as it allows the
allocation of only the required memory to the process.
Segmentation − In segmentation, the memory is divided into variable-sized
segments, and each segment is assigned to a process. This technique is
more flexible than paging but requires more overhead to keep track of the
allocated segments.
Non-contiguous memory allocation is a memory management technique that divides
memory into non-contiguous blocks, allowing processes to be allocated memory that
is not necessarily contiguous. Here are some of the advantages and disadvantages
of non-contiguous memory allocation −
Advantages of Non-Contiguous Memory Allocation
Reduced External Fragmentation − One of the main advantages of non-
contiguous memory allocation is that it can reduce external fragmentation, as
memory can be allocated in small, non-contiguous blocks.
Increased Memory Utilization − Non-contiguous memory allocation allows
for more efficient use of memory, as small gaps in memory can be filled with
processes that need less memory.
Flexibility − This technique allows for more flexibility in allocating and
deallocating memory, as processes can be allocated memory that is not
necessarily contiguous.
Memory Sharing − Non-contiguous memory allocation makes it easier to
share memory between multiple processes, as memory can be allocated in
non-contiguous blocks that can be shared between multiple processes.
Disadvantages of Non-Contiguous Memory Allocation
Internal Fragmentation − One of the main disadvantages of non-contiguous
memory allocation is that it can lead to internal fragmentation, as memory can
be allocated in small, non-contiguous blocks that are not fully utilized.
Increased Overhead − This technique requires more overhead than
contiguous memory allocation, as the operating system needs to maintain
data structures to track memory allocation.
Slower Access − Access to memory can be slower than contiguous memory
allocation, as memory can be allocated in non-contiguous blocks that may
require additional steps to access.
Fragmentation
The process of dividing a computer file, such as a data file or an executable program
file, into fragments that are stored in different parts of a computer’s storage medium,
such as its hard disc or RAM, is known as fragmentation in computing. When a file is
fragmented, it is stored on the storage medium in non-contiguous blocks, which
means that the blocks are not stored next to each other.
Cause of Fragmentation
This can happen when a file is too large to fit into a single contiguous block of free
space on the storage medium, or when the blocks of free space on the medium are
insufficient to hold the file. Because the system must search for and retrieve
individual fragments from different locations in order to open the file, fragmentation
can cause problems when reading or accessing the file.
Effect of Fragmentation
This can reduce system performance and make it more difficult to access the file. It
is generally best to defragment your hard disc on a regular basis to avoid
fragmentation, which is a process that rearranges the blocks of data on the disc so
that files are stored in contiguous blocks and can be accessed more quickly.
Types of Fragmentation
There are two main types of fragmentation:
Internal fragmentation: Internal fragmentation occurs when there is unused
space within a memory block. For example, if a system allocates a 64KB block of
memory to store a file that is only 40KB in size, that block will contain 24KB of
internal fragmentation. When the system employs a fixed-size block allocation
method, such as a memory allocator with a fixed block size, this can occur.
Fragmentation can also occur at various levels within a system. File fragmentation,
for example, can occur at the file system level, in which a file is divided into multiple
non-contiguous blocks and stored on a storage medium. Memory fragmentation can
occur at the memory management level, where the system allocates and
deallocated memory blocks dynamically. Network fragmentation occurs when a
packet of data is divided into smaller fragments for transmission over a network.
Compaction is a technique to collect all the free memory present in the form of
fragments into one large chunk of free memory, which can be used to run other
processes.
It does that by moving all the processes towards one end of the memory and all the
available free space towards the other end of the memory so that it becomes
contiguous.
It is not always easy to do compaction. Compaction can be done only when the
relocation is dynamic and done at execution time. Compaction cannot be done when
relocation is static and is performed at load time or assembly time.
Before Compaction
Before compaction, the main memory has some free space between occupied
spaces. This condition is known as external fragmentation. Due to less free space
between occupied spaces, large processes cannot be loaded into them.
Main Memory
Occupied space
Free space
Occupied space
Occupied space
Free space
After Compaction
After compaction, all the occupied space has been moved up and the free space at
the bottom. This makes the space contiguous and removes external fragmentation.
Processes with large memory requirements can be now loaded into the main
memory.
Main Memory
Occupied space
Occupied space
Occupied space
Free space
Free space
Purpose of Compaction in Operating System
While allocating memory to process, the operating system often faces a problem
when there’s a sufficient amount of free space within the memory to satisfy the
memory demand of a process. However the process’s memory request can’t be
fulfilled because the free memory available is in a non-contiguous manner, this
problem is referred to as external fragmentation. To solve such kinds of problems
compaction technique is used.