OS - Unit 4
OS - Unit 4
OS - Unit 4
Unit 4
Memory Management:
1. Both of these concepts are for single CPU. Both of these concepts are for single CPU.
Multi-programming increases CPU utilization In multi-tasking also increases CPU utilization, it also
4.
by organising jobs . increases responsiveness.
The idea is to reduce the CPU idle time for as The idea is to further extend the CPU Utilization
5. long as possible. concept by increasing responsiveness Time Sharing.
Memory Management Techniques
Contiguous memory Management
• Contiguous memory management schemes: In this
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: It
is the most effortless memory management
technique. In this strategy, a wide range of computer
memory aside from a little part which is held for the
working framework is accessible for one application.
In other words it is also known as fixed sized partition
of the system that separates memory into fixed-size
segments (might possibly be of a similar size). In this
whole partition is permitted to a procedure and if
there is some wastage inside the segment is
apportioned to a procedure and if there is some
wastage inside the segment, at that point it is called
an internal fragmentation.
Multiple partitioning schemes
• Multiple Partitioning: 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. The problem of
inefficient CPU use can be overcome using
multiprogramming that allows more than one program to
run concurrently. To switch between two processes, the
operating systems need to load both processes into the
main memory. The operating system needs to divide the
available main memory into multiple parts to load
multiple processes into the main memory. Thus multiple
processes can reside in the main memory
simultaneously.
• The multiple partitioning schemes can be of two types:
• Fixed Partitioning
• Dynamic Partitioning
Static or Fix 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.
• 1. Internal Fragmentation: If the size of the process is
lesser then the total size of the partition then some size of
the partition get wasted and remain unused. This is
wastage of the memory and called internal fragmentation.
• As shown in the image, the 4 MB partition is used to load
only 3 MB process and the remaining 1 MB got wasted.
• 2. External Fragmentation: The total unused space of
various partitions cannot be used to load the processes
even though there is space available but not in the
contiguous form.
• As shown in the image, the remaining 1 MB space of each
partition cannot be used as a unit to store a 4 MB process.
Despite of the fact that the sufficient space is available to
Difference between Internal 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.
1. In internal fragmentation fixed-sized memory, blocks square In external fragmentation, variable-sized memory blocks square
measure appointed to process. measure appointed to the method.
2. Internal fragmentation happens when the method or process External fragmentation happens when the method or process is
is smaller than the memory. removed.
3. The solution of internal fragmentation is the best-fit block. The solution to external fragmentation is compaction and paging.
4. Internal fragmentation occurs when memory is divided into External fragmentation occurs when memory is divided into variable
fixed-sized partitions. size partitions based on the size of processes.
The difference between memory allocated and required space The unused spaces formed between non-contiguous memory
5. or memory is called Internal fragmentation. fragments are too small to serve a new process, which is called
External fragmentation.
6. Internal fragmentation occurs with paging and fixed External fragmentation occurs with segmentation and dynamic
partitioning. partitioning.
It occurs on the allocation of a process to a partition greater It occurs on the allocation of a process to a partition greater which is
7. than the process’s requirement. The leftover space causes exactly the same memory space as it is required.
degradation system performance.
8. It occurs in worst fit memory allocation method. It occurs in best fit and first fit memory allocation method.
Advantages and Disadvantages of Fixed Partitioning
• Advantages
• Easy to implement: Algorithms needed to implement Fixed Partitioning are easy to implement. It simply
requires putting a process into a certain partition without focusing on the emergence of Internal and External
Fragmentation.
• Little OS overhead: Processing of Fixed Partitioning requires lesser excess and indirect computational power.
• Disadvantages
• Internal Fragmentation: Main memory use is inefficient. Any program, no matter how small, occupies an
entire partition. This can cause internal fragmentation.
• External Fragmentation: The total unused space (as stated above) of various partitions cannot be used to
load the processes even though there is space available but not in the contiguous form (as spanning is not
allowed).
• Limit process size: Process of size greater than the size of the partition in Main Memory cannot be
accommodated. The partition size cannot be varied according to the size of the incoming process size. Hence,
the process size of 32MB in the above-stated example is invalid.
• Limitation on Degree of Multiprogramming: Partitions in Main Memory are made before execution or during
system configure. Main Memory is divided into a fixed number of partitions. Suppose if there are n 1
partitions in RAM and n2 are the number of processes, then n2 <= n1 condition must be fulfilled. Number of
processes greater than the number of partitions in RAM is invalid in Fixed Partitioning.
Dynamic or Variable Partitioning
• Answer C
GATE Question 2
• Consider five memory partitions of size 100 KB, 500 KB, 200 KB, 450 KB and 600 KB in same order. If
sequence of requests for blocks of size 212 KB, 417 KB, 112 KB and 426 KB in same order come, then which
of the following algorithm makes the efficient use of memory?
A. Best fit algorithm
B. First fit algorithm
C. Next fit algorithm
D. Both next fit and best fit results in same
1)First Fit-->212 in 500-->288 remaining, 417 in 450-->33 remaining, 112 in 288(of 500)-->176 remaining,
426 in 600-->174 remaining, so total remaining-->100+176+200+33+174--->683 remaining
3)Next fit--> so we will start with first fit and then next time, next partition we will put in block starting
from previously placed partition, 212 in 500-->288 remaining, 417 in 450-->33 remaining, 112 in 600--
>488 remaining, 426 in 488-->62 remaining, still remaining space is-->100+288+200+33+62--->683
remaining
Non-Contiguous allocation
• In contiguous allocation, space in memory should be
allocated to the whole process. If not, then that space
remains unallocated.
• In Non-Contiguous allocation, the process can be divided
into different parts and hence filling the space in the main
memory. In this example, process P can be divided into two
parts of equal size – 2KB. Hence one part of process P can
be allocated to the first 2KB space of main memory and the
other part of the process can be allocated to the second
2KB space of main memory.
• In order to avoid this time-consuming process, we divide
our process in secondary memory in advance before
reaching the main memory for its execution. Every process
is divided into various parts of equal size called Pages. We
also divide our main memory into different parts of equal
size called Frames.
• Size of page in process = Size of frame in memory
Memory Protection in Operating Systems
In the diagram, when the
scheduler selects a process for
the execution process, the
dispatcher, on the other hand, is
responsible for loading the
relocation and limit registers with
the correct values as part of the
context switch as every address
generated by the CPU is checked
against these 2 registers, and we
may protect the operating
system, programs, and the data
of the users from being altered by
this running process.
Memory Protection in Operating Systems
• In Memory protection, we have to protect the operating system from user processes and which
can be done by using a relocation register with a limit register.
• Relocation register has the value of the smallest physical address.
• Limit register has the range of the logical addresses.
• These two registers have some conditions like each logical address must be less than the limit
register.
• The memory management unit is used to translate the logical address with the value in the
relocation register dynamically after which the translated (or mapped) address is then sent to
memory.
• Need of Memory protection :
• Memory protection prevents a process from accessing unallocated memory in OS as it stops
the software from seizing control of an excessive amount of memory and may cause damage
that will impact other software which is currently being used or may create a loss of saved
data.
• Resources of memory protection also help in detecting malicious or harmful applications, that
may after damaged the processes of the operating system.
Methods of memory protection
• There are various methods for protecting a process from accessing memory that has not been allocated and
some of the commonly used methods are given below:
• Memory Protection using Keys: The concept of using memory protection with keys can be found in
most modern computers with the purpose of paged memory organization and for the dynamic
distribution between the parallel running programs. The keys are based on the use of special codes as
we can verify the compliance between using arrays of memory cells and the number of running
programs. This key method gives the users a process to impose page-based protections without any
modification in the page tables.
• Memory Protection using Rings: In CS, the domains related to ordered protection are called Protection
Rings. This method helps in improving fault tolerance and provides security. These rings are arranged in
a hierarchy from most privileged to least privileged. In the single-level sharing OS, each and every
segment has a protection ring for the process of reading, writing, and executing operations of the
process. If there is a use of a higher ring number by the process then the ring number for the segment
creates a fault. But we do have some methods for calling the procedures safely that can run in a lower
ring number and then return to the number of the higher ring.
• Capability-based addressing: It is a method of protecting the memory that cannot be seen in modern
commercial computers. Here, the pointers (objects consisting of a memory address) are restored by the
capabilities objects that can only be created with the protected instructions and may only execute by a
kernel, or by another process that is authorized to execute and therefore it gives an advantage of
controlling the unauthorized processes in creating additional separate address spaces in memory.
Methods of memory protection
• Memory Protection using masks: The masks are used in the protection of memory during the
organization of paging. In this method, before the implementation, the page numbers are
indicated to each program and are reserved for the placement of its directives. Here the
allocated pages for the program are now given the control of the operating system in the form
of mask code (an n-bit binary code) which is formed for every working program that is
determined by the bit number of OD pages.
• Memory Protection using Segmentation: It is a method of dividing the system memory into
different segments. The data structures of x86 architecture of OS like local descriptor table and
global descriptor table are used in the protection of memory.
• Memory Protection using Simulated segmentation: With this technique, we can monitor the
program for interpreting the machine code instructions of system architectures. Through this,
the simulator can help in protecting the memory by using a segmentation using the scheme
and validating the target address of every instruction in real-time.
• Memory Protection using Dynamic tainting: Dynamic tainting is a technique that consists of
marking and tracking certain data in a program at runtime as it protects the process from illegal
memory accesses. In tainting technique, we taint a program to mark two kinds of data i.e.,
memory in the data space and the pointers.
Paging
• Paging is a memory management scheme that
eliminates the need for contiguous allocation
of physical memory. This scheme permits the
physical address space of a process to be non
– contiguous.
• Logical Address or Virtual Address
(represented in bits): An address generated by
the CPU
• Logical Address Space or Virtual Address
Space( represented in words or bytes): The
set of all logical addresses generated by a
program
• Physical Address (represented in bits): An
address actually available on memory unit
• Physical Address Space (represented in words
or bytes): The set of all physical addresses
corresponding to the logical addresses
Paging Example
• Let us consider the main memory size 16
Kb and Frame size is 1 KB therefore the
main memory will be divided into the
collection of 16 frames of 1 KB each.
• There are 4 processes in the system that
is P1, P2, P3 and P4 of 4 KB each. Each
process is divided into pages of 1 KB
each so that one page can be stored in
one frame.
• Initially, all the frames are empty
therefore pages of the processes will get
stored in the contiguous way.
• Frames, pages and the mapping
between the two is shown in the image.
Paging Example
• Let us consider that, P2 and P4 are
moved to waiting state after some
time. Now, 8 frames become empty
and therefore other pages can be
loaded in that empty place.
• The process P5 of size 8 KB (8 pages)
is waiting inside the ready queue.
• Given the fact that, we have 8 non
contiguous frames available in the
memory and paging provides the
flexibility of storing the process at
the different places.
• Therefore, we can load the pages of
process P5 in the place of P2 and P4.
Physical and Logical Address Space
• Physical address space in a system can be defined as the size of the main memory. It is really important to
compare the process size with the physical address space. The process size must be less than the physical
address space.
If, physical address space = 64 KB = 26 KB = 26 X 210 Bytes = 216 bytes and word size = 8 Bytes = 23 Bytes
Then, Physical address space (in words) = (216) / (23) = 213 Words
Therefore, Physical Address = 13 bits
In General, If, Physical Address Space = N Words then, Physical Address = log2 N
• Logical address space can be defined as the size of the process. The size of the process should be less
enough so that it can reside in the main memory.
if, Logical Address Space = 128 MB = (27 X 220) Bytes = 227 Bytes and Word size = 4 Bytes = 22 Bytes
Then, Logical Address Space (in words) = (227) / (22) = 225 Words
Therefore Logical Address = 25 Bits
In general, If, logical address space = L words, Then, Logical Address = Log2L bits
• Page Table is a data structure used by
Page Table
the virtual memory system to store
the mapping between logical
addresses and physical addresses.
Address Logical Address Space is set of all logical Physical Address is set of all physical
Space addresses generated by CPU in reference addresses mapped to the corresponding
to a program. logical addresses.
User can view the logical address of a User can never view physical address of
Visibility program. program.
The user can use the logical address to The user can indirectly access physical
Access access the physical address. address but not directly.
Editable Logical address can be change. Physical address will not change.
The logical address does not exist physical address is a location in the
physically in the memory memory that can be accessed physically
Logical address is generated by CPU in physical address is a location that exists in
perspective of a program the memory unit.
Example of Mapping
• For Main Memory- Important Formulas Paging
• Physical Address Space = Size of main memory
• Size of main memory = Total number of frames x Page size
• Frame size = Page size
• If number of frames in main memory = 2X, then number of bits in frame number = X bits
• If Page size = 2X Bytes, then number of bits in page offset = X bits
• If size of main memory = 2X Bytes, then number of bits in physical address = X bits
• For Process-
• Virtual Address Space = Size of process
• Number of pages the process is divided = Process size / Page size
• If process size = 2X bytes, then number of bits in virtual address space = X bits
• For Page Table-
• Size of page table = Number of entries in page table x Page table entry size
• Number of entries in pages table = Number of pages the process is divided
• Page table entry size = Number of bits in frame number + Number of bits used for optional fields if any
• NOTE-
• In general, if the given address consists of ‘n’ bits, then using ‘n’ bits, 2 n locations are possible.
• Then, size of memory = 2n x Size of one location.
• If the memory is byte-addressable, then size of one location = 1 byte.
• Thus, size of memory = 2n bytes.
• If the memory is word-addressable where 1 word = m bytes, then size of one location = m bytes.
• Thus, size of memory = 2n x m bytes.
Practice Numerical Paging
• Calculate the size of memory if its address consists of 22 bits and the memory is 2-byte addressable.
We have Number of locations possible with 22 bits = 222 locations
It is given that the size of one location = 2 bytes
Thus, Size of memory= 222 x 2 bytes= 223 bytes= 8 MB
• Calculate the number of bits required in the address for memory having size of 16 GB. Assume the memory is 4-byte
addressable.
Let ‘n’ number of bits are required. Then, Size of memory = 2n x 4 bytes.
Since, the given memory has size of 16 GB, so we have 2 n x 4 bytes = 16 GB
2n x 4 = 16G 2n x 22 = 234 2n = 232 n = 32 bits
• Consider a system with byte-addressable memory, 32 bit logical addresses, 4 kilobyte page size and page table entries of 4
bytes each. The size of the page table in the system in megabytes is _____.
Given Number of bits in logical address = 32 bits, Page size = 4KB, Page table entry size = 4 bytes
Process Size-
Number of bits in logical address = 32 bits Thus, Process size = 232 Bn= 4 GB
Number of Entries in Page Table-
Number of pages the process is divided = Process size / Page size = 4 GB / 4 KB = 2 20 pages
Thus, Number of entries in page table = 220 entries,
Page Table Size-
Page table size = Number of entries in page table x Page table entry size = 2 20 x 4 bytes = 4 MB
Practice Numerical Paging
• Consider a machine with 64 MB physical memory and a 32 bit virtual address space. If the page size is 4 KB, what is the
approximate size of the page table?
Given Size of main memory = 64 MB, Number of bits in virtual address space = 32 bits, Page size = 4 KB
We will consider that the memory is byte addressable.
Number of Bits in Physical Address-
Size of main memory = 64 MB = 226 B
Thus, Number of bits in physical address = 26 bits
Number of Frames in Main Memory = Size of main memory / Frame size = 64 MB / 4 KB = 226 B / 212 B = 214
Thus, Number of bits in frame number = 14 bits
Number of Bits in Page Offset- We have, Page size = 4 KB = 212 B
Thus, Number of bits in page offset = 12 bits, So, Physical address is----
Process Size- Number of bits in virtual address space = 32 bits
Thus, Process size = 232 B = 4 GB
Number of Entries in Page Table-
Number of pages the process is divided = Process size / Page size = 4 GB / 4 KB = 2 20 pages
Thus, Number of entries in page table = 220 entries
Page Table Size-
Page table size = Number of entries in page table x Page table entry size
= Number of entries in page table x Number of bits in frame number
= 220 x 14 bits
= 220 x 16 bits (Approximating 14 bits ≈ 16 bits)
= 220 x 2 bytes = 2 MB
Segmentation
• A process is divided into Segments. The chunks that a
program is divided into which are not necessarily all of the
same sizes are called segments. Segmentation gives user’s
view of the process which paging does not give. Here the
user’s view is mapped to physical memory.
• There are types of segmentation:
Virtual memory segmentation –Each process is divided
into a number of segments, not all of which are resident at
any one point in time.
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.
Segment Table – It maps two-dimensional Logical address
into one-dimensional Physical address. It’s each table
entry has:
Base Address: It contains the starting physical address
where the segments reside in memory.
Limit: It specifies the length of the segment.
Translation of Two dimensional Logical Address to one dimensional
Physical Address using segmentation