Unit 4 OS
Unit 4 OS
Memory Management
In a multiprogramming computer, the Operating System resides in a part of memory, and the rest is used by multiple
processes.
The task of subdividing the memory among different processes is called Memory Management.
Memory management is a method in the operating system to manage operations between main memory and disk during
process execution.
The main aim of memory management is to achieve efficient utilization of memory.
Why Memory Management is Required?
Allocate and de-allocate memory before and after process execution.
To keep track of used memory space by processes.
To minimize fragmentation issues.
To proper utilization of main memory.
To maintain data integrity while executing of process.
Logical and Physical Address Space
Logical Address Space:
An address generated by the CPU is known as a “Logical Address”.
It is also known as a Virtual address. Logical address space can be defined as the size of the process.
A logical address can be changed.
Physical Address Space:
An address seen by the memory unit (i.e the one loaded into the memory address register of the memory) is commonly
known as a “Physical Address”.
A Physical address is also known as a Real address.
The set of all physical addresses corresponding to these logical addresses is known as Physical address space. A physical
address is computed by MMU.
The run-time mapping from virtual to physical addresses is done by a hardware device Memory Management Unit (MMU).
The physical address always remains constant.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different types of loading :
Static Loading: Static Loading is basically loading the entire program into a fixed address. It requires more memory space.
Dynamic Loading:
The entire program and all data of a process must be in physical memory for the process to execute. So, the size of a process is
limited to the size of physical memory.
To gain proper memory utilization, dynamic loading is used.
In dynamic loading, a routine is not loaded until it is called.
All routines are residing on disk in a relocatable load format. One of the advantages of dynamic loading is that the
unused routine is never loaded.
This loading is useful when a large amount of code is needed to handle it efficiently.
Static and Dynamic Linking
To perform a linking task a linker is used. A linker is a program that takes one or more object files generated by a compiler and
combine them into a single executable file.
Static Linking:
In static linking, the linker combines all necessary program modules into a single executable program. So there is no runtime
dependency. Some operating systems support only static linking, in which system language libraries are treated like any other object
module.
Dynamic Linking:
The basic concept of dynamic linking is similar to dynamic loading. In dynamic linking, “Stub” is included for each appropriate
library routine reference.
A stub is a small piece of code. When the stub is executed, it checks whether the needed routine is already in memory or not. If not
available then the program loads the routine into memory.
Swapping
When a process is executed it must have resided in memory.
Swapping is a process of swapping a process temporarily into a secondary memory from the main memory, which is fast
compared to secondary memory.
A swapping allows more processes to be run and can be fit into memory at one time. The main part of swapping is transferred
time and the total time is directly proportional to the amount of memory swapped.
Swapping is also known as roll-out, or roll because if a higher priority process arrives and wants service, the memory
manager can swap out the lower priority process and then load and execute the higher priority process.
After finishing higher priority work, the lower priority process swapped back in memory and continued to the execution
process.
Example: Early computer systems and some embedded systems still use mono programming, but it's not common in modern desktop
or server environments.
Multi-Programming:
Examples: Virtually all modern desktop and server operating systems, including Windows, macOS, Linux, and others, support multi-
programming.
Multi-programming is the standard in today's computing environments because it enables better resource utilization and a more
responsive user experience.
Modelling multiprogramming
Modelling multiprogramming involves creating abstractions and models to understand and simulate how multiple programs
or processes run concurrently on a computer system.
This can be useful for various purposes, including performance analysis, system design, and optimization
It involves developing mathematical models to analyze and predict the behaviour of multiple programs running concurrently.
Queuing theory and scheduling algorithms are often used to model the performance of multiprogramming systems.
Relocation ensures that programs can be loaded and executed at different memory locations.
- Relocation is the process of adjusting the memory addresses of a program when it is loaded into memory.
- It ensures that a program can be placed anywhere in memory and still operate correctly.
- This is especially important in multiprogramming, where programs are loaded and executed independently.
- Relocation involves updating memory references, such as pointers and addresses, within the program to reflect its new location in
memory.
4. Protection:
- Protection mechanisms ensure that one program cannot interfere with or access the memory or resources of another program.
- Memory protection prevents processes from accessing each other's memory areas, which enhances security and stability.
- Protection mechanisms can involve hardware support, such as memory segmentation and paging, as well as operating system
controls.
Protection mechanisms prevent unauthorized access to memory and resources, enhancing system security and stability.
Memory management is a critical aspect of computer systems, ensuring that programs and data are efficiently allocated and
deallocated in memory. Two common data structures used in memory management are bitmaps and linked lists
1. Bitmaps:
A bitmap is a simple data structure that represents the allocation status of individual memory blocks (usually fixed-sized blocks) in a
memory region.
Each block in memory corresponds to a bit in the bitmap. Typically, a '0' in the bitmap indicates that the corresponding block is free,
while a '1' indicates that it is allocated or in use.
Allocation: When allocating memory, the bitmap is consulted to find a free block (a '0' bit), and this block is marked as allocated ('1')
to indicate it's in use.
Deallocation: When deallocating memory, the corresponding bit in the bitmap is set back to '0' to mark it as free.
Advantages:
o Quick and efficient for finding free memory blocks.
o Compact representation, especially when dealing with large memory regions.
o Simple to implement.
Disadvantages:
2. Linked Lists:
Linked lists are more versatile and can be used for dynamic memory allocation. Instead of representing individual blocks, linked
lists maintain a list of memory blocks (nodes) where each node contains information about the block's size and status (allocated or
free). These memory blocks can be of variable sizes.
Allocation: To allocate memory, the allocator traverses the linked list, looking for a free block that is large enough to satisfy the
allocation request. Once found, the block is marked as allocated, and a reference to it is returned.
Deallocation: When deallocating memory, the block is marked as free, and the allocator may perform coalescing, merging adjacent
free blocks to prevent fragmentation.
Advantages:
Disadvantages:
- Requires additional metadata (size information) for each block, increasing memory overhead.
Multiprogramming with fixed and variable partitions, relocation, and protection are essential concepts in operating system design and
memory management. These concepts help in efficiently utilizing a computer's memory resources while ensuring the security and
isolation of processes. Let's explore each of these concepts in more detail:
1. Multiprogramming:
Multiprogramming allows multiple programs to run concurrently, improving system throughput and responsiveness.
Multiprogramming is a technique in which multiple programs are loaded into memory simultaneously, and the CPU is
switched between them to keep it busy.
It improves the overall system throughput and response time by allowing multiple processes to run concurrently.
Variable Partitions: variable partitions allocate memory dynamically based on process requirements
Variable partitioning, also known as dynamic partitioning, allows for more flexible memory allocation.
Memory is divided into variable-sized partitions, and processes are allocated memory dynamically based on their size and
memory availability.
Variable partitioning is more memory-efficient but requires more complex memory management
Buddy System:
The buddy system allocates memory in powers of 2 (e.g., 2^0, 2^1, 2^2, etc.).
When a memory request is made, the system allocates a block of the nearest larger size and splits it into smaller blocks until
the requested size is reached.
Free memory blocks are merged (buddies) to form larger blocks when possible to prevent fragmentation.
This approach is efficient and can help minimize both external and internal fragmentation.
Slab Allocation:
Slab allocation is often used in the Linux kernel for managing memory for kernel data structures.
It divides memory into slabs, where each slab contains a specific type of data structure (e.g., file control blocks).
Slabs are allocated, deallocated, and managed as units, reducing memory fragmentation.
First Fit, Best Fit, and Worst Fit:
- First Fit allocates the first available block that is large enough.
- These algorithms have their advantages and disadvantages, and their performance depends on the specific use case.
The choice of memory allocation strategy depends on the specific requirements and constraints of the system, including the type of
applications running, available hardware, and the desired balance between memory utilization and management complexity. Modern
operating systems often use a combination of these strategies to optimize memory allocation.
Fragmentation
There are two types of fragmentation in OS which are given as Internal fragmentation and External fragmentation.
Internal Fragmentation:
Internal fragmentation happens when the memory is split into mounted-sized blocks.
Whenever a method is requested for the memory, the mounted-sized block is allotted to the method.
In the case where the memory allotted to the method is somewhat larger than the memory requested, then the difference
between allotted and requested memory is called internal fragmentation.
We fixed the sizes of the memory blocks, which has caused this issue. If we use dynamic partitioning to allot space to the
process, this issue can be solved.
The above diagram clearly shows the internal fragmentation because the difference between memory allocated and required space or
memory is called Internal fragmentation.
External Fragmentation:
External fragmentation happens when there’s a sufficient quantity of area within the memory to satisfy the memory request
of a method.
However, the process’s memory request cannot be fulfilled because the memory offered is in a non-contiguous manner.
Whether you apply a first-fit or best-fit memory allocation strategy it’ll cause external fragmentation.
In the above diagram, we can see that, there is enough space (55 KB) to run a process-07 (required 50 KB) but the memory (fragment)
is not contiguous. Here, we use compaction, paging, or segmentation to use the free space to run a process.
In internal fragmentation fixed-sized memory, blocks square In external fragmentation, variable-sized memory blocks
1. measure appointed to process. square measure appointed to the method.
Internal fragmentation happens when the method or process External fragmentation happens when the method or process
2. is smaller than the memory. is removed.
Internal fragmentation occurs when memory is divided External fragmentation occurs when memory is divided into
4. into fixed-sized partitions. variable size partitions based on the size of processes.
Internal fragmentation occurs with paging and fixed External fragmentation occurs with segmentation and dynamic
6. partitioning. partitioning.
8. It occurs in worst fit memory allocation method. It occurs in best fit and first fit memory allocation method.
Paging in OS is a dynamic and flexible technique that can load the processes in the memory partitions more optimally.
Paging is a memory management scheme that eliminates the need for a contiguous allocation of physical memory. This
scheme permits the physical address space of a process to be non-contiguous.
The basic idea behind paging is to divide the process into pages so we can store these pages in the memory at different holes
(partitions) and use these holes efficiently.
The paging in OS is used to support non-contiguous memory allocation.
Secondary memory will be divided into fixed-sized partitions of equal size, and each of these is called a page.
Similarly, the main memory is divided into equal fixed-size partitions; each called a frame.
The mapping between logical pages and physical pages is maintained in a data structure called the page table.
Types of Paging in OS
There are primarily two types of Paging in OS commonly used in operating systems:
Fixed-size paging
Variable-size paging
Demand Paging in OS
Demand paging is a memory management scheme where pages are brought into memory only when they are demanded by
the running process, reducing memory wastage and improving overall system performance.
When a page that is not present in memory is accessed, a page fault occurs, prompting the operating system to fetch the
required page from secondary storage into memory.
This approach allows for more efficient memory utilization as only the necessary pages are loaded, reducing initial memory
overhead and allowing for larger program sizes to be accommodated.
Whenever the process is created, paging in OS will be applied to the process, and a page table will be created. The base
address of the page table will be stored in the PCB.
Paging in OS is for every process, and every process will have its own page table.
Page Table of the processes will be stored in the main memory.
There is no external fragmentation in the paging in OS because the page size is the same as the frame size.
The internal fragmentation exists on the last page, and internal fragmentation in the paging in OS is considered, where P is
the page size.
Maintaining the page table is considered an overhead (burden) for the system.
Paging in OS Diagram
The below diagram explains the technique of mapping a logical address to a physical address in paging in OS (operating system):
In the above diagram of the translation of logical to a physical address in paging in the operating system as the CPU generates a
logical address containing the page number and offset.
Then, corresponding to the page number, we check the frame number in which it is stored from the page table.
The frame number is then added with the offset value to get the physical address, and then the memory is accessed with it.
The address translation in paging in OS is an address space that is the range of valid addresses available in a program or
process memory.
It is the memory space accessible to a program or process.
The memory can be physical or virtual and is used for storing data and executing instructions.
The two main types of address space are described below:
Benefits of Paging in OS
Paging in OS is a fundamental technique used in modern operating systems to efficiently manage memory resources. By
dividing memory into fixed-size pages and mapping virtual addresses to physical addresses, paging provides increased
memory utilization, simplified memory management, memory protection, and supports virtual memory systems.
Page replacement algorithms further enhance the efficiency of paging by determining which pages to swap in and out of
physical memory. As operating systems continue to evolve, paging remains a vital component in ensuring optimal system
performance and resource management.
Segmentation
A process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes
are called segments.
Segmentation gives the user’s view of the process which paging does not provide. Here the user’s view is mapped to physical
memory.
Virtual Memory Segmentation: Each process is divided into a number of segments, but the segmentation is not done all at
once. This segmentation may or may not take place at the run time of the program.
Simple Segmentation: Each process is divided into a number of segments, all of which are loaded into memory at run time,
though not necessarily contiguously.
There is no simple relationship between logical addresses and physical addresses in segmentation. A table stores the
information about all such segments and is called Segment Table.
It maps a two-dimensional Logical address into a one-dimensional Physical address. It’s each table entry has:
o Base Address: It contains the starting physical address where the segments reside in memory.
o Segment Limit: Also known as segment offset. It specifies the length of the segment.
Translation of Two-dimensional Logical Address to Dimensional Physical Address.
No Internal fragmentation.
Segment Table consumes less space in comparison to Page table in paging.
As a complete module is loaded all at once, segmentation improves CPU utilization.
The user’s perception of physical memory is quite similar to segmentation. Users can divide user programs into modules via
segmentation. These modules are nothing more than separate processes’ codes.
The user specifies the segment size, whereas, in paging, the hardware determines the page size.
Segmentation is a method that can be used to segregate data from security operations.
Flexibility: Segmentation provides a higher degree of flexibility than paging. Segments can be of variable size, and processes can
be designed to have multiple segments, allowing for more fine-grained memory allocation.
Sharing: Segmentation allows for sharing of memory segments between processes. This can be useful for inter-process
communication or for sharing code libraries.
Protection: Segmentation provides a level of protection between segments, preventing one process from accessing or modifying
another process’s memory segment. This can help increase the security and stability of the system.
As processes are loaded and removed from the memory, the free memory space is broken into little pieces, causing External
fragmentation.
Overhead is associated with keeping a segment table for each activity.
Due to the need for two memory accesses, one for the segment table and the other for main memory, access time to retrieve
the instruction increases.
Fragmentation: As mentioned, segmentation can lead to external fragmentation as memory becomes divided into smaller
segments. This can lead to wasted memory and decreased performance.
Overhead: Using a segment table can increase overhead and reduce performance. Each segment table entry requires
additional memory, and accessing the table to retrieve memory locations can increase the time needed for memory operations.
Complexity: Segmentation can be more complex to implement and manage than paging. In particular, managing multiple
segments per process can be challenging, and the potential for segmentation faults can increase as a result.
Segmented Paging
Pure segmentation is not very popular and not being used in many of the operating systems. However, Segmentation can be combined
with Paging to get the best features out of both the techniques.
In Segmented Paging, the main memory is divided into variable size segments which are further divided into fixed size pages.
4. Each Page table contains the various information about every page of the segment.
5. The Segment Table contains the information about every segment.
6. Each segment table entry points to a page table entry and every page table entry is mapped to one of the page within a
segment.
Translation of logical address to physical address
The CPU generates a logical address which is divided into two parts: Segment Number and Segment Offset.
The Segment Offset must be less than the segment limit.
Offset is further divided into Page number and Page Offset. To map the exact page number in the page table, the page number
is added into the page table base.
The actual frame number with the page offset is mapped to the main memory to get the desired word in the page of the
certain segment of the process.
When a process requests a page that is not present in physical memory, a page fault occurs. The operating system needs to
determine which page to evict from physical memory to make space for the requested page.
Various page replacement algorithms, such as FIFO (First-In-First-Out), LRU (Least Recently Used), and Optimal, are used
to select the victim page for eviction.
These algorithms aim to minimize the number of page faults and optimize the overall system performance.
2. Second Chance:
LRU replaces the page that has not been used for the longest time.
It requires maintaining a list of pages in order of their usage, with the most recently used page at the front.
LRU is effective but can be computationally expensive to implement because it needs to constantly update the usage order.
4. Optimal:
The Optimal algorithm is an idealized algorithm that makes replacement decisions based on future knowledge of page
accesses.
It replaces the page that will not be used for the longest time in the future.
While Optimal provides the lowest possible page fault rate, it is not practical because it requires predicting future page
accesses, which is typically not feasible.
LFU replaces the page that has been used the least frequently.
It maintains a counter for each page that is incremented each time the page is accessed.
LFU aims to keep frequently used pages in memory.
6. Clock:
WS-Clock is an extension of the Clock algorithm that considers the working set of pages used by a process.
It scans through pages in memory and checks their reference status, but it also takes into account the working set window.
If a page falls outside the working set window, it is a candidate for replacement.
The choice of a page replacement algorithm depends on factors such as the system's hardware, the memory management requirements
of the operating system, and the specific workload characteristics. No single algorithm is universally superior; each has its strengths
and weaknesses in different scenarios. Modern operating systems may even use a combination of these algorithms or adaptive
approaches to optimize page replacement.
Assuming a system that has no pages loaded in the memory and uses the FIFO Page replacement algorithm. Consider the following
reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Case-1: If the system has 3 frames, the given reference string the using FIFO page replacement algorithm yields a total of 9 page
faults. The diagram below illustrates the pattern of the page faults occurring in the example.
Case-2: If the system has 4 frames, the given reference string using the FIFO page replacement algorithm yields a total of 10 page
faults. The diagram below illustrates the pattern of the page faults occurring in the example.
It can be seen from the above example that on increasing the number of frames while using the FIFO page replacement algorithm, the
number of page faults increased from 9 to 10.
Note – It is not necessary that every string reference pattern cause Belady anomaly in FIFO but there is certain kind of string
references that worsen the FIFO performance on increasing the number of frames.
Page fault rate: Page fault rate is the number of page faults that occur during the execution of a process. Belady’s Anomaly
occurs when the page fault rate increases as the number of page frames allocated to a process increases.
Page replacement algorithm: Belady’s Anomaly is specific to some page replacement algorithms, including the First-In-
First-Out (FIFO) algorithm and the Second-Chance algorithm.
System workload: Belady’s Anomaly can occur when the system workload changes. Specifically, it can happen when the
number of page references in the workload increases.
Page frame allocation: Belady’s Anomaly can occur when the page frames allocated to a process are increased, but the total
number of page frames in the system remains constant. This is because increasing the number of page frames allocated to a
process reduces the number of page faults initially, but when the workload increases, the increased number of page frames
can cause the process to evict pages from its working set more frequently, leading to more page faults.
Impact on performance: Belady’s Anomaly can significantly impact system performance, as it can result in a higher
number of page faults and slower overall system performance. It can also make it challenging to choose an optimal number
of page frames for a process.
Advantages :
The MULTICS (Multiplexed Information and Computing Service) operating system was one of the early systems that combined
segmentation and paging to address the drawbacks of each technique:
1. Segmentation: MULTICS divided a program's address space into segments, as discussed above. Segments were used for code, data,
and other logical units.
2. Paging: Inside each segment, MULTICS used a paging mechanism. Paging divides each segment into fixed-sized pages. This
combination allowed for more granular control of memory allocation and improved protection.
By using both segmentation and paging, MULTICS gained the benefits of both techniques:
Page faults occur when a program tries to access a virtual page that is not currently in physical memory. When a page fault happens,
the operating system must handle it:
Locate the page on disk.
Find an available page frame in physical memory.
Load the page from disk into the free page frame.
Update the page table to reflect the new mapping.
Resume the interrupted process.
The concept of "Locality of Reference" is a fundamental principle in computer science and memory management. It refers to the
tendency of a program's memory access pattern to repeatedly access a relatively small portion of the available memory for a certain
period of time. Locality of Reference is crucial for optimizing memory management, cache usage, and overall system performance.
There are two primary types of locality of reference:
1. Temporal Locality:
Temporal locality, also known as "temporal reference," refers to the idea that if a program or process accesses a particular
memory location, it is likely to access the same location again in the near future.
Programs often use variables, data structures, or instructions repeatedly in a short time span. Caching mechanisms take
advantage of this by retaining recently accessed data in a faster, smaller memory (cache) to reduce the time it takes to access
that data again.
Examples of temporal locality include loop iterations, function calls, and the reuse of variables.
2. Spatial Locality:
Spatial locality, also known as "spatial reference," suggests that when a program accesses a specific memory location, it is
likely to access neighbouring memory locations in the same vicinity.
This locality principle aligns with the idea that data is often stored in contiguous memory locations, and programs tend to
access data that is stored nearby.
Caching mechanisms, such as cache lines, exploit spatial locality by loading not only the requested memory location but also
nearby data into cache because it is likely to be used soon.
Examples of spatial locality include array traversal, sequential file reading, and data structures with closely related elements.