Unit-4 OS Notes
Unit-4 OS Notes
Memory Management
Physical 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.
Calculation: -
Physical Address Space = Size of the Main Memory
If, physical address space = 64 KB = 26 KB = 26 * 210 Bytes = 216 bytes
Let us consider, word size = 8 Bytes = 23 Bytes
Hence, Physical address space (in words) = (216) / (23) = 213 Words(=N) (i.e. log2 N= log2 213=13)
Therefore, Physical Address = 13 bits
In General, If Physical Address Space = N Words
then, Physical Address = log2 N
Logical Address Space
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.
Calculation: -
Physical Address Space = Size of the process
If, logical address space = 128 MB = 27 * 210 bytes = 227 bytes
Let us consider, word size = 4 Bytes = 22 Bytes
Hence, logical address space (in words) = (227) /(22) = 225 Words(=L) (i.e. log2 L= log2 225=25)
Therefore, logical Address = 25 bits
In General, If logical Address Space = L Words
then, logical Address = log2 L
What is a Word?
The Word is the smallest unit of the memory. It is the collection of bytes. Every operating system defines
different word sizes after analyzing the n-bit address that is inputted to the decoder and the 2 ^ n memory
locations that are produced from the decoder.
Basic bare machine
Bare Machine is a logic hardware in the computer system which can execute the programs in the processor
without using the Operating System. we cannot execute any process inside the processor without the Operating
System. But, with the Bare Machine, it is possible.
But the only drawback was that the Bare Machine accepts the program and instructions in Machine
Language. Due to this, only the trained people who were qualified in the computer field and were able to
understand and instruct the computer in Machine language were able to operate on a computer.
Resident Monitor
The Resident Monitor is a code which runs on Bare Machine.
Its acts like an operating system which controls everything inside a processor and performs all the
functions.
The Resident Monitor is thus also known as the Job Sequencer because like the Operating system, it also
sequences the jobs and sends it to the processor for execution.
After the jobs are scheduled, the Resident Monitor loads the Programs one by one into the main memory
according to their sequence.
Parts of Resident Monitor: - The Resident Monitors are divided into 4 parts: -
1. Control Language Interpreter
2. Loader
3. Device Driver
4. Interrupt Processing
Multi-programming with fixed partitioning is a contiguous memory management technique in which the main
memory is divided into fixed sized partitions which can be of equal or unequal size. Whenever we have to
allocate a process memory then a free partition that is big enough to hold the process is found. Then the
memory is allocated to the process. If there is no free space available then the process waits in the queue to be
allocated memory. It is one of the oldest memory management techniques which is easy to implement.
In fixed partitioning,
1. The partitions cannot overlap.
2. A process must be contiguously present in a partition for the execution.
Advantages of fixed partitioning: -
The main memory is divided into partitions of equal or different sizes.
The operating system always resides in the first partition while the other partitions can be used to store
user processes.
The memory is assigned to the processes in contiguous way.
Disadvantages of fixed partitioning: -
1. Internal Fragmentation: - If the size of the process is lesser then the total size of the partition then some size
of the partition gets wasted and remain unused. This is wastage of the memory and called internal fragmentation.
As shown in the image below, 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 below, 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 load the process, process will not
be loaded.
3. Limitation on the size of the process: - If the process size is larger than the size of maximum sized partition
then that process cannot be loaded into the memory. Therefore, a limitation can be imposed on the process size
that is it cannot be larger than the size of the largest partition.
4. Degree of multiprogramming is less: - By Degree of multi programming, we simply mean the maximum
number of processes that can be loaded into the memory at the same time. In fixed partitioning, the degree of
multiprogramming is fixed and very less due to the fact that the size of the partition cannot be varied according
to the size of processes.
Example-1
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 below.
Example-2
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 noncontiguous 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.
Page Table in OS: - Page Table is a data structure used by the virtual memory system to store the mapping
between logical addresses and physical addresses.
Logical addresses are generated by the CPU for the pages of the processes therefore they are generally
used by the processes.
Physical addresses are the actual frame address of the memory. They are generally used by the hardware
or more specifically by RAM subsystems.
The image given below considers-
Physical Address Space = M words Physical Address = log 2 M = m bits
Logical Address Space = L words Logical Address = log 2 L = l bits
Page Size = P words page offset = log 2 P = p bits
Size of the page table
However, the part of the process which is being executed by the CPU must be present in the main memory
during that time period. The page table must also be present in the main memory all the time because it has the
entry for all the pages.
The size of the page table depends upon the number of entries in the table and the bytes stored in one entry.
Let's consider,
Logical Address, log 2 L=l = 24 bits
Logical Address space, L=2l => L=224 => L=224 bytes
Let's say, Page size= P= 4 KB = 22 * 210 Bytes = 212 bytes
Page offset, p= log 2 P= log 2 212 =12 bits
Number of bits in a page = Logical Address - Page Offset = (l-p) = (24 – 12) = 12 bits
Number of pages = 2(l-p) = 2(24-12) = 212 = 22 * 210 bytes = 4 KB
Let's say, Page table entry = 1 Byte
Therefore, the size of the page table = 4 KB X 1 Byte = 4 KB
The CPU always accesses the processes through their logical addresses. However, the main memory
recognizes physical address only.
Memory Management Unit converts the page number of the logical address to the frame number of the
physical address. The offset remains same in both the addresses.
To perform this task, Memory Management unit needs a special kind of mapping which is done by page
table. The page table stores all the Frame numbers corresponding to the page numbers of the page table.
The Segment number is mapped to the segment table. The limit of the respective segment is compared
with the offset. If the offset is less than the limit then the address is valid otherwise it throws an error as the
address is invalid. In the case of valid addresses, the base address of the segment is added to the offset to get the
physical address of the actual word in the main memory.
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compared to the page table in paging.
Disadvantages of Segmentation
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Difference between Paging and Segmentation
2 Paging divides program into fixed size pages. Segmentation divides program into variable
size segments.
8 Logical address is divided into page number and Logical address is divided into segment number
page offset and segment offset
9 Page table is used to maintain the page information. Segment Table maintains the segment
information
10 Page table entry has the frame number and some Segment table entry has the base address of the
flag bits to represent details about pages. segment and some protection bits for the
segments.
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.
Performance of demand paging: - The performance of demand paging depends on various factors-
1. Page Size 2. Page Replacement Algorithm 3. Page Table Size 4. Page Table Organization
5. Translation looks aside buffer (TLB)
A Translation look aside buffer can be defined as a memory cache which can be used to reduce the time
taken to access the page table again and again.
It is a memory cache which is closer to the CPU and the time taken by CPU to access TLB is lesser than
that taken to access main memory.
In other words, we can say that TLB is faster and smaller than the main memory but cheaper and bigger
than the register.
TLB follows the concept of locality of reference which means that it contains only the entries of those
many pages that are frequently accessed by the CPU.
In translation look aside buffers, there are tags and keys with the help of which, the mapping is done.
TLB hit is a condition where the desired entry is found in translation look aside buffer. If this happens
then the CPU simply access the actual location in the main memory.
However, if the entry is not found in TLB (TLB miss) then CPU has to access page table in the main
memory and then access the actual frame in the main memory.
Therefore, in the case of TLB hit, the effective access time will be lesser as compare to the case of TLB
miss.
If the probability of TLB hit is P% (TLB hit rate) then the probability of TLB miss (TLB miss rate) will
be (1-P) %.
Therefore, the effective access time can be defined as; EAT = p (t + m) + (1 - p) (t + k * m + m)
Where, p → TLB hit rate, t → time taken to access TLB, m → time taken to access main memory, k = 1, if the
single level paging has been implemented.
By the formula, we come to know that
1. Effective access time will be decreased if the TLB hit rate is increased.
2. Effective access time will be increased in the case of multilevel paging.
Example- Consider a paging hardware with a TLB. Assume that the entire page table and all the pages are in the
physical memory. It takes 10 milliseconds to search the TLB and 80 milliseconds to access the physical memory.
If the TLB hit ratio is 0.6, the effective memory access time (in milliseconds) is _________.
Ans- Given, TLB hit ratio(p)= 0.6
Therefore, TLB miss ratio = 0.4 (i.e. 1-.6=.4)
Time taken to access TLB (t) = 10 ms
Time taken to access main memory (m) = 80 ms and k=1because single level paging has been implemented. So,
Effective Access Time (EAT) = 0.6 (10 + 80) + 0.4 (10 + 1*80 + 80)
= 90 * 0.6 + 0.4 *170 = 54+68 =122 ms
6. Page fault rate 0? p ? 1
if p = 0, no page faults
if p = 1, every reference results in a page fault.
Inverted Page Table
Inverted Page Table is the global page table which is maintained by the Operating System for all the
processes. In inverted page table, the number of entries is equal to the number of frames in the main
memory. It can be used to overcome the drawbacks of page table.
There is always a space reserved for the page regardless of the fact that whether it is present in the main
memory or not. However, this is simply the wastage of the memory if the page is not present.
We can save this wastage by just inverting the page table. We can save the details only for the pages which are
present in the main memory. Frames are the indices and the information saved inside the block will be Process
ID and page number
What is Thrashing?
If the number of page faults is equal to the number of referred pages or the number of page faults are so
high so that the CPU remains busy in just reading the pages from the secondary memory then the
effective access time will be the time taken by the CPU to read one word from the secondary memory
and it will be so high. The concept is called thrashing.
If the page fault rate is PF %, the time taken in getting a page from the secondary memory and again
restarting is S (service time) and the memory access time is ma then-
The effective access time can be given as;
EAT = PF * S + (1 - PF) * (ma)
Where, PF= page fault rate, S= service time, ma= memory access time
Comparison between the Paging and Swapping
Basic Paging permits a process's memory address space to be Swapping allows multiple
non-contiguous. programs in the operating
system to run concurrently.
Flexibility Paging is more flexible as the only pages of a process Swapping is less flexible
are moved. because it moves the entire
process back and forth
between RAM and the back
store.
Main Functionality During paging, the pages are equal-size memory Swapping involves processes
chunks that travel between the primary and secondary switching between main
memory. memory and secondary
memory.
Multiprogramming Paging enables more processes to run in the main Compared to paging,
memory. Swapping enables fewer
programs to run in the main
memory.
Workloads Swapping is appropriate for heavy workloads. The paging is appropriate for
light to medium workloads.
Usage Paging allows virtual memory to be implemented. Swapping allows the CPU to
access processes more
quickly.
Processes There are many processes in the main memory during There are some processes in
swapping. the main memory while
paging.
Cache memory organization
The data or contents of the main memory that are used frequently by CPU are stored in the cache memory
so that the processor can easily access that data in a shorter time.
Whenever the CPU needs to access memory, it first checks the cache memory. If the data is not found in
cache memory, then the CPU moves into the main memory.
The cache is the fastest component in the memory hierarchy and approaches the speed of CPU components.
Cache memory is organized as distinct set of blocks where each set contains a small fixed number of blocks.
Cache memory is placed between the CPU and the main memory. The block diagram for a cache memory can be
represented as:
When the CPU needs to access memory, the cache is examined. If the word is found in the cache, it is
read from the fast memory.
If the word addressed by the CPU is not found in the cache, the main memory is accessed to read the
word.
A block of words one just accessed is then transferred from main memory to cache memory. The block
size may vary from one word (the one just accessed) to about 16 words adjacent to the one just accessed.
The performance of the cache memory is frequently measured in terms of a quantity called hit ratio.
When the CPU refers to memory and finds the word in cache, it is said to produce a hit.
If the word is not found in the cache, it is in main memory and it counts as a miss.
The ratio of the number of hits divided by the total CPU references to memory (hits plus misses) is the hit
ratio.
Levels of memory:
Level 1 It is a type of memory in which data is stored and accepted that are immediately stored in CPU. Most
commonly used register is accumulator, Program counter, address register etc.
Level 2 It is the fastest memory which has faster access time where data is temporarily stored for faster access.
Level 3 It is memory on which computer works currently. It is small in size and once power is off data no longer
stays in this memory.
Level 4 It is external memory which is not as fast as main memory but data stays permanently in this memory.
Cache Performance
When the processor needs to read or write a location in the main memory, it first checks for a corresponding
entry in the cache.
If the processor finds that the memory location is in the cache, a Cache Hit has occurred and data is
read from the cache.
If the processor does not find the memory location in the cache, a cache miss has occurred. For a
cache miss, the cache allocates a new entry and copies in data from the main memory, then the
request is fulfilled from the contents of the cache.
The performance of cache memory is frequently measured in terms of a quantity called Hit ratio.
Hit Ratio(H) = hit / (hit + miss) = no. of hits/total accesses
Miss Ratio = miss / (hit + miss) = no. of miss/total accesses = 1 - hit ratio(H)
Cache Mapping:
There are three different types of mapping used for the purpose of cache memory which are as follows:
Direct mapping,
Associative mapping
Set-Associative mapping
1. Direct Mapping -
In direct mapping, the cache consists of normal high-speed random-access memory.
Each location in the cache holds the data, at a specific address in the cache.
This address is given by the lower significant bits of the main memory address.
This enables the block to be selected directly from the lower significant bit of the memory address.
The remaining higher significant bits of the address are stored in the cache with the data to complete the
identification of the cached data.
The simplest technique, known as direct mapping, maps each block of main memory into only one
possible cache line. or In Direct mapping, assign each memory block to a specific line in the cache.
If a line is previously taken up by a memory block when a new block needs to be loaded, the old block
is trashed.
An address space is split into two parts index field and a tag field. The cache is used to store the tag
field whereas the rest is stored in the main memory.
Direct mapping`s performance is directly proportional to the Hit ratio.
i = j modulo m
where
i = cache line number
j = main memory block number
m = number of lines in the cache
As shown in the above figure, the address from processor is divided into two field a tag and an index.
The tag consists of the higher significant bits of the address and these bits are stored with the data in cache. The
index consists of the lower significant bit of the address. Whenever the memory is referenced, the following
sequence of events occurs
1. The index is first used to access a word in the cache.
2. The tag stored in the accessed word is read.
3. This tag is then compared with the tag in the address.
4. If two tags are same this indicates cache hit and required data is read from the cache word.
5. If the two tags are not same, this indicates a cache miss. Then the reference is made to the main memory
to find it.
For a memory read operation, the word is then transferred into the cache. It is possible to pass the information to
the cache and the process simultaneously.
Direct mapped cache with a multi-word block
The index part in the address is used to access the cache and the stored tag is compared with required tag
address.
For a read operation, if the tags are same, the word within the block is selected for transfer to the processor. If
tags are not same, the block containing the required word is first transferred to the cache. In direct mapping, the
corresponding blocks with the same index in the main memory will map into the same block in the cache, and
hence only blocks with different indices can be in the cache at the same time.
2. Associative Mapping
In this type of mapping, associative memory is used to store the content and addresses of the memory
word. Any block can go into any line of the cache. This means that the word id bits are used to identify
which word in the block is needed, but the tag becomes all of the remaining bits. This enables the
placement of any word at any place in the cache memory.
It is considered to be the fastest and most flexible mapping form. In associative mapping, the index bits
are zero.
An associative memory can be considered as a memory unit whose stored data can be identified for
access by the content of the data itself rather than by an address or memory location.
Associative memory is often referred to as Content Addressable Memory (CAM).
When a write operation is performed on associative memory, no address or memory location is given to
the word.
The memory itself is capable of finding an empty unused location to store the word.
On the other hand, when the word is to be read from an associative memory, the content of the word, or
part of the word, is specified. The words which match the specified content are located by the memory
and are marked for reading.
The following diagram shows the block representation of an Associative memory.
From the block diagram, we can say that an associative memory consists of a memory array and logic for
'm' words with 'n' bits per word.
The functional registers like the argument register A and key register K each have n bits, one for each bit
of a word. The match register M consists of m bits, one for each memory word.
The words which are kept in the memory are compared in parallel with the content of the argument
register.
The key register (K) provides a mask for choosing a particular field or key in the argument word.
If the key register contains a binary value of all 1's, then the entire argument is compared with each
memory word. Otherwise, only those bits in the argument that have 1's in their corresponding position of
the key register are compared.
Thus, the key provides a mask for identifying a piece of information which specifies how the reference to
memory is made.
The cells present inside the memory array are marked by the letter C with two subscripts. The first
subscript gives the word number and the second specifies the bit position in the word. For instance, the
cell Cij is the cell for bit j in word i.
A bit Aj in the argument register is compared with all the bits in column j of the array provided that Kj =
1. This process is done for all columns j = 1, 2, 3......, n.
If a match occurs between all the unmasked bits of the argument and the bits in word i, the corresponding
bit Mi in the match register is set to 1. If one or more unmasked bits of the argument and the word do not
match, Mi is cleared to 0.
An example of four way set associative cache having four blocks in each set is shown in the following figure
m = v * k
i= j mod v
where
i = cache set number
j = main memory block number
v = number of sets
m = number of lines in the cache number of sets
k = number of lines in each set
Fully associative mapping
In fully associative type of cache memory, each location in cache stores both memory address as well as data.
1. Definition Cache Memory is the high speed of computer Virtual Memory is a logical unit
memory that reduces the access time of files or of computer memory that
documents from the main memory. increases the capacity of main
memory by storing or executing
programs of larger size than the
main memory in the computer
system.
2. Memory Unit Cache Memory is defined as a memory unit in a Virtual Memory is not defined as
computer system. a memory unit.
3. Size Its size is very small as compared to Virtual Its size is very large as compared
Memory. to the Cache Memory.
5. Operation Generally, it stores frequently used data in the The virtual memory keeps those
cache memory to reduce the access time of data or programs that may not
files. completely be placed in the main
memory.
6. Management Cache Memory is controlled by the hardware of Whereas the virtual memory is
a system. control by the Operating System
(OS).
The OPTIMAL Page Replacement Algorithms works on a certain principle. The principle is:
Replace the Page which is not used in the Longest Dimension of time in future.