Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

UNIT-III

Memory Management: Swapping, Contiguous Memory Allocation, Paging,


structure of the Page Table, Segmentation
Virtual Memory Management: Virtual Memory, Demand Paging, Page-
Replacement Algorithms, Thrashing

Main Memory:
 Main memory, commonly referred to as RAM (Random Access Memory), is the computer's
primary temporary storage for actively processed data.
 Unlike permanent storage like hard drives, RAM is volatile, losing its contents when the
computer powers down.
 It's organized into addressable cells, each holding data measured in bytes.

Memory Management in an Operating System:

 Memory management in OS is a technique of controlling and managing the functionality


of Random access memory (primary memory).
 It is used for achieving better concurrency, system performance, and memory utilization.
 Memory management moves processes from primary memory to secondary memory and vice
versa.
 It also keeps track of available memory, memory allocation, and unallocated.

Uses of Memory Management in OS


 Memory management keeps track of the status of each memory location, whether it is
allocated or free.
 Memory management enables computer systems to run programs that require more
main memory than the amount of free main memory available on the system. This is
achieved by moving data between primary and secondary memory.
 Memory management addresses the system’s primary memory by providing
abstractions such that the programs running on the system perceive a large memory is
allocated to them.
 It is the job of memory management to protect the memory allocated to all the
processes from being corrupted by other processes. If this is not done, the computer
may exhibit unexpected/faulty behavior.
 Memory management enables sharing of memory spaces among processes, with the
help of which, multiple programs can reside at the same memory location (although
only one at a time).

Functions of Memory Management in OS

Some of the major functions fulfilled by memory management are discussed below.

Memory Allocation
Memory management ensures that the needed memory space is allocated to the new process
whenever a process is created and requires memory. Memory Management also keeps track
of the system's allocated and free memory.

Memory Deallocation

Like memory allocation, whenever a process completes its execution, memory management
ensures that the space and the memory resources it holds are released. Any newly created
process can use the freed memory.

Memory Sharing

Memory sharing is also one of the main goals of memory management in OS. Some
processes might require the same memory simultaneously. Memory management ensures
that this is made possible without breaking any authorization rules.

Memory Protection
Memory Protection refers to preventing any unauthorized memory access to any process.
Memory management ensures memory protection by assigning correct permissions to each
process.

Logical Address
 It is a virtual address generated by the CPU while a program is running. It is referred to as a
virtual address because it does not exist physically.
 Using this address, the CPU access the actual address or physical address inside the
memory, and data is fetched from there.
 The set of all logical addresses generated by the CPU for a program is called the logical
address space.

Physical Address

 Physical Address is the actual address of the data inside the memory.

 The logical address is a virtual address and the program needs physical memory for its
execution.

 The user never deals with the Physical Address. The user program generates the logical
address and is mapped to the physical address by the Memory Management Unit (MMU).

 The set of all physical addresses corresponding to the logical addresses in the logical
address space is called the physical address space.
Comparison between Logical Address and Physical Address

Logical Address Physical Address


 It is a virtual address.  It is the actual location in the memory.

 It is visible to the user.  It is not visible to the user.

 It is generated by the CPU.  It is computed by the MMU.

 It is used by the user to access the


 It is not accessible directly by the user.
physical address inside the memory.

 The set of all logical addresses  The set of all physical addresses corresponding to the
generated by the CPU is called the logical addresses in the logical address space is called
logical address space. the physical address space.

Memory Management Unit (MMU) in OS

 MMU is a hardware device that does the run-time mapping from the virtual address
to the physical address. It is located within the Central Processing Unit.

Let us understand the concept of mapping with the help of a simple MMU scheme and that
is a base-register scheme.

 In the above diagram, the base register is termed the Relocation register.
 The relocation register is a special register in the CPU and is used for the mapping of
logical addresses used by a program to physical addresses of the system's main
memory.
SWAPPING:

 Swapping in the operating system is a memory management scheme that


temporarily swaps out an idle or blocked process from the main memory to
secondary memory.
 Which ensures proper memory utilization and memory availability for those
processes which are ready to be executed.

 The area of the secondary memory where swapped-out processes are stored is
called swap space.

 In the case of high-priority processes, the process with low priority is swapped out of
the main memory and stored in swap space then the process with high priority is
swapped into the main memory to be executed first.

 The main goals of an operating system include Maximum utilization of the CPU.
This means that there should be a process execution every time, the CPU should
never stay idle and there should not be any Process starvation or blocking.

 Swapping in OS is done to get access to data present in secondary memory and


transfer it to the main memory so that it can be used by the application programs.It
can affect the performance of the system but it helps in running more than one
process by managing the memory. Therefore swapping in os is also known as
the memory compaction technique.

 There are two important concepts in the process of swapping which are as follows:
 Swap In
 Swap Out

Swap
In:

 The method of removing a process from secondary memory (Hard Drive) and
restoring it to the main memory (RAM) for execution is known as the Swap
In method.

Swap Out:

 It is a method of bringing out a process from the main memory(RAM) and sending it
to the secondary memory(hard drive) so that the processes with higher priority or
more memory consumption will be executed known as the Swap Out method.

Note:- Swap In and Swap Out method is done by Medium Term Scheduler(MTS).

Advantages of Swapping in OS
The advantages of the swapping method are listed as follows:

 Swapping in OS helps in achieving the goal of Maximum CPU Utilization.


 Swapping ensures proper memory availability for every process that needs to be
executed.
 Swapping helps avoid the problem of process starvation means a process should not
take much time for execution so that the next process should be executed.
 CPU can perform various tasks simultaneously with the help of swapping so that
processes do not have to wait much longer before execution.
 Swapping ensures proper RAM (main memory) utilization.
 Swapping creates a dedicated disk partition in the hard drive for swapped processes
which is called swap space.
 Swapping in OS is an economical process.
 Swapping method can be applied on priority-based process scheduling where a high
priority process is swapped in and a low priority process is swapped out which
improves the performance.

Disadvantages of Swapping in OS

There are some limited disadvantages of the swapping method which are listed as follows:

 If the system deals with power-cut during bulky swapping activity then the user
may lose all the information which is related to the program.
 If the swapping method uses an algorithm that is not up to the mark then the number
of page faults can be increased and therefore this decreases the complete
performance.
 There may be inefficiency in a case when there is some common resource used by the
processes that are participating in the swapping process.

Memory Management Techniques:


The memory management techniques can be classified into following main categories:

o Contiguous memory management schemes


o Non-Contiguous memory management schemes

Contiguous memory management schemes:


In a Contiguous memory management scheme, each program occupies a single contiguous
block of storage locations, i.e., a set of memory locations with consecutive addresses .
Single contiguous memory management schemes:

 The Single contiguous memory management scheme is the simplest memory


management scheme used in the earliest generation of computer systems.
 In this scheme, the main memory is divided into two contiguous areas or partitions.
The operating systems reside permanently in one partition, generally at the lower
memory, and the user process is loaded into the other partition.

Advantages of Single contiguous memory management schemes:

o Simple to implement.
o Easy to manage and design.
o In a Single contiguous memory management scheme, once a process is loaded, it is
given full processor's time, and no other processor will interrupt it.

Disadvantages of Single contiguous memory management schemes:

o Wastage of memory space due to unused memory as the process is unlikely to use all
the available memory space.
o The CPU remains idle, waiting for the disk to load the binary image into the main
memory.
o It cannot be executed if the program is too large to fit the entire available main
memory space.
o It does not support multiprogramming, i.e., it cannot handle multiple programs
simultaneously.

Multiple Partitioning:
o The single Contiguous memory management scheme is inefficient as it limits
computers to execute only one program at a time resulting in wastage in memory
space and CPU time.
o The problem of inefficient CPU use can be overcome using multiprogramming that
allows more than one program to run concurrently.
o To switch between two processes, the operating systems need to load both processes
into the main memory.
o The operating system needs to divide the available main memory into multiple parts
to load multiple processes into the main memory.
o Thus multiple processes can reside in the main memory simultaneously.

The multiple partitioning schemes can be of two types:


o Fixed Partitioning
o Dynamic Partitioning

Fixed Partitioning

 The main memory is divided into several fixed-sized partitions in a fixed partition
memory management scheme or static partitioning.
 These partitions can be of the same size or different sizes. Each partition can hold a
single process.
 The number of partitions determines the degree of multiprogramming, i.e., the
maximum number of processes in memory.
 These partitions are made at the time of system generation and remain fixed after
that.

Advantages of Fixed partitioning memory management schemes:

o Simple to implement.
o Easy to manage and design.

Disadvantages of Fixed Partitioning memory management schemes:

o This scheme suffers from internal fragmentation.


o The number of partitions is specified at the time of system generation .
DYNAMIC PARTITIONING
 Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In this
technique, the partition size is not declared initially.
 It is declared at the time of process loading. The first partition is reserved for the operating
system. The remaining space is divided into parts.
 The size of each partition will be equal to the size of the process. The partition size varies
according to the need of the process so that the internal fragmentation can be avoided .

Advantages of Dynamic Partitioning over fixed partitioning


1. No Internal Fragmentation
Given the fact that the partitions in dynamic partitioning are created according to the need
of the process, It is clear that there will not be any internal fragmentation because there will
not be any unused remaining space in the partition.
2. No Limitation on the size of the process
In Fixed partitioning, the process with the size greater than the size of the largest partition
could not be executed due to the lack of sufficient contiguous memory. Here, In Dynamic
partitioning, the process size can't be restricted since the partition size is decided according
to the process size.
3. Degree of multiprogramming
is dynamic Due to the absence of internal fragmentation, there will not be any unused
space in the partition hence more processes can be loaded in the memory at the same time.
Disadvantages of dynamic partitioning
External Fragmentation:
Absence of internal fragmentation doesn't mean that there will not be external
fragmentation.
Let's consider three processes P1 (1 MB) and P2 (3 MB) and P3 (1 MB) are being loaded
in the respective partitions of the main memory.
After some time P1 and P3 got completed and their assigned space is freed. Now there are
two unused partitions (1 MB and 1 MB) available in the main memory but they cannot be
used to load a 2 MB process in the memory since they are not contiguously located. The
rule says that the process must be contiguously present in the main memory to get executed.
We need to change this rule to avoid external fragmentation.
Compaction:

 Compaction is a memory management technique in which the free space of a running


system is compacted, to reduce fragmentation problem and improve memory
allocation efficiency.
 Compaction is used by many modern operating systems, such as Windows, Linux,
and Mac OS X.
 As in the fig we have some used memory(black color) and some unused
memory(white color).
 The used memory is combined. All the empty spaces are combined together.
 This process is called compaction. This is done to prevent to solve the problem of
fragmentation, but it requires too much of CPU time.
By compacting memory, the operating system can reduce or eliminate fragmentation and
make it easier for programs to allocate and use memory.
The compaction process usually consists of two steps:
1. Copying all pages that are not in use to one large contiguous area.
2. Then, write the pages that are in use into the newly freed space.
Non-Contiguous memory management schemes:
 In a Non-Contiguous memory management scheme, the program is divided into
different blocks and loaded at different portions of the memory that need not
necessarily be adjacent to one another.
 This scheme can be classified depending upon the size of blocks and whether the
blocks reside in the main memory or not.
Paging
 Paging is the memory management technique in which secondary memory is divided
into fixed-size blocks called pages, and main memory is divided into fixed-size
blocks called frames.
 The Frame has the same size as that of a Page. The processes are initially in
secondary memory, from where the processes are shifted to main memory (RAM)
when there is a requirement.
 Each process is mainly divided into parts where the size of each part is the same as
the page size.
 One page of a process is mainly stored in one of the memory frames. Paging follows
no contiguous memory allocation.
 That means pages in the main memory can be stored at different locations in the
memory.

Advantages of paging:
 Pages reduce external fragmentation.
 Simple to implement.
 Memory efficient.
 Due to the equal size of frames, swapping becomes very easy.
 It is used for faster access of data.

You might also like