0% found this document useful (0 votes)
47 views18 pages

COOS UNIT-IV (Part-2)

co&os memory management & virtual memory

Uploaded by

l.shirisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views18 pages

COOS UNIT-IV (Part-2)

co&os memory management & virtual memory

Uploaded by

l.shirisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CO&OS UNIT-IV (Part-2)

Memory Management:
 Swapping,  Demand Paging,
 Contiguous Memory Allocation  Page-Replacement Algorithms
 Paging, Structure of the Page Table  Allocation of Frames
 Segmentation  Thrashing Case Studies - UNIX,
 Virtual Memory Linux, Windows
Swapping:

Swapping is a process of swapping a process temporarily to a secondary memory from the main
memory which is fast than compared to secondary memory. But as RAM is of less size so the process
that is inactive is transferred to secondary memory. The main part of swapping is transferred time and
the total time is directly proportional to the amount of memory swapped.

Swap-in: A swap-in process in which a process moves from secondary storage / hard disk to main
memory (RAM).
Swap out: Swap out takes a process out of the main memory and places it in secondary memory.

Advantages of Swapping
1. It helps the CPU to manage multiple processes within a single main memory.
2. It helps to create and use virtual memory.
3. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore, processes do not
have to wait very long before they are executed.
4. It improves the main memory utilization.

Disadvantages of Swapping
1. If the computer system loses power, the user may lose all information related to the program in
case of substantial swapping activity.
2. If the swapping algorithm is not good, the composite method can increase the number of Page
Fault and decrease the overall processing performance.
CO&OS UNIT-IV Part-2 Page 1 of 18
Contiguous and Non-Contiguous Memory Allocation:

Memory is a huge collection of bytes, and memory allocation refers to allocating space to computer
applications.

There are two types of memory allocation:

1. Contiguous and
2. Non-contiguous memory allocation.

Contiguous memory allocation allows a single memory space to complete the tasks. On the other hand,
non-contiguous memory allocation assigns the method to distinct memory sections at numerous memory
locations.

In this article, you will learn about Contiguous and Non-contiguous memory allocation with their
advantages, disadvantages, and differences.

What is Contiguous Memory Allocation?

It is the type of memory allocation method. When a process requests the memory, a single contiguous
section of memory blocks is allotted depending on its requirements.

It is completed by partitioning the memory into fixed-sized partitions and assigning every partition to a
single process. However, it will limit the degree of multiprogramming to the number of fixed partitions
done in memory.

This allocation also leads to internal fragmentation. For example, suppose a fixed-sized memory block
assigned to a process is slightly bigger than its demand. In that case, the remaining memory space in the
block is referred to as internal fragmentation. When a process in a partition finishes, the partition becomes
accessible for another process to run.

The OS preserves a table showing which memory partitions are free and occupied by processes in the
variable partitioning scheme. Contiguous memory allocation speeds up process execution by decreasing
address translation overheads.

Advantages and Disadvantages of Contiguous Memory Allocation

There are various advantages and disadvantages of contiguous memory allocation. Some of the
advantages and disadvantages are as follows:

Advantages
1. It is simple to keep track of how many memory blocks are left, which determines how many more
processes can be granted memory space.
2. The read performance of contiguous memory allocation is good because the complete file may be
read from the disk in a single task.
3. The contiguous allocation is simple to set up and performs well.

CO&OS UNIT-IV Part-2 Page 2 of 18


Disadvantages

1. Fragmentation isn't a problem because every new file may be written to the end of the disk after
the previous one.
2. When generating a new file, it must know its eventual size to select the appropriate hole size.
3. When the disk is filled up, it would be necessary to compress or reuse the spare space in the holes.

What is Non-Contiguous Memory Allocation?


It allows a process to obtain multiple memory blocks in various locations in memory based on its
requirements. The non-contiguous memory allocation also reduces memory wastage caused
by internal and external fragmentation because it uses the memory holes created by internal and external
fragmentation.

The two methods for making a process's physical address space non-contiguous are paging and
segmentation. Non-contiguous memory allocation divides the process into blocks (pages or segments) that
are allocated to different areas of memory space based on memory availability.

Non-contiguous memory allocation can decrease memory wastage, but it also raises address translation
overheads. As the process portions are stored in separate locations in memory, the memory execution is
slowed because time is consumed in address translation.

Advantages and Disadvantages of Non-Contiguous Memory Allocation

There are various advantages and disadvantages of non-contiguous memory allocation. Some of the
advantages and disadvantages are as follows:

Advantages

1. It has the advantage of reducing memory waste, but it increases overhead because of the address
translation.
2. It slows down the memory execution because time is consumed in address translation.

Disadvantages

1. The downside of this memory allocation is that the access is slow because you must reach the other
nodes using pointers and traverse them.

CO&OS UNIT-IV Part-2 Page 3 of 18


Comparison between the Contiguous and Non-Contiguous Memory Allocation
Here, you will learn the head-to-head comparison between the Contiguous and Non-Contiguous Memory
Allocation. Some of the head-to-head comparisons between Contiguous and Non-Contiguous Memory
Allocation are as follows:

Contiguous Memory Allocation Non-Contiguous Memory Allocation


It allocates only a single memory contiguous It separates the process into numerous blocks,
block to the process. each of which is assigned to a different
memory address space.

It is very faster in execution in comparison to It is slower in execution in comparison to


non-contiguous memory allocation. contiguous memory allocation.

There is no overhead of address translation There is an overhead of address translation


during execution because the process is stored during process execution because the process
in contiguous memory space in contiguous blocks are scattered across the memory space.
memory allocation.

In most cases, the operating system keeps a In the non-contiguous memory allocation, each
table that lists all available and occupied process must keep a table that primarily
partitions in the contiguous memory allocation. contains each block's base addresses acquired
by the memory.

The Operating System can better control The Non-Contiguous Memory Allocation is
contiguous memory allocation difficult for the Operating System to manage.

Contiguous memory allocation contains two It contains Paging and Segmentation.


memory allocations: single partition and multi-
partition.

The memory space is partitioned into fixed- It is broken into several blocks, which are then
sized partitions in the contiguous memory placed in different areas of the memory based
allocation, and each partition is only assigned to on available memory space.
one process.

Wastage of memory No wastage of memory

Swapped-in processes are placed in the initially Swapped-in processes in non-contiguous


allotted space in the contiguous memory memory allocation can be organized in any
allocation. location in memory.

Both internal and exterior fragmentation occurs. The non-Contiguous memory allocation
method causes external fragmentation.

CO&OS UNIT-IV Part-2 Page 4 of 18


Paging:
Paging: Paging is the procedure of memory allocation where different non-contiguous blocks of
memory are assigned a fixed size. The size is generally of 4KB. The paging is always performed
between active pages.

In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary
storage into the main memory in the form of pages.

The main idea behind the paging is to divide each process in the form of pages. The main memory will
also be divided in the form of frames.

One page of the process is to be stored in one of the frames of the memory. The pages can be stored at the
different locations of the memory but the priority is always to find the contiguous frames or holes.

Pages of the process are brought into the main memory only when they are required otherwise they reside
in the secondary storage.

Different operating system defines different frame sizes. The sizes of each frame must be equal.
Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same as
frame size.

CO&OS UNIT-IV Part-2 Page 5 of 18


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 below.

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.

CO&OS UNIT-IV Part-2 Page 6 of 18


Page Table

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


Logical Address Space = L words
Page Size = P words

Physical Address = log 2 M = m bits


Logical Address = log 2 L = l bits
page offset = log 2 P = p bits

CO&OS UNIT-IV Part-2 Page 7 of 18


The CPU always accesses the processes through their logical addresses. However, the main memory
recognizes physical address only.

In this situation, a unit named as Memory Management Unit comes into the picture. It 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.

In other words, the page table maps the page number to its actual location (frame number) in the

memory. In the image given below shows, how the required word of the frame is accessed with the help

of offset.

CO&OS UNIT-IV Part-2 Page 8 of 18


Segmentation:

In Operating Systems, Segmentation is a memory management technique in which the memory is divided
into the variable size parts. Each part is known as a segment which can be allocated to a process.

The details about each segment are stored in a table called a segment table. Segment table is stored in one
(or many) of the segments.

Segment table contains mainly two information about segment:

1. Base: It is the base address of the segment


2. Limit: It is the length of the segment.

Why Segmentation is required?

Till now, we were using Paging as our main memory management technique. Paging is more close to the
Operating system rather than the User. It divides all the processes into the form of pages regardless of the
fact that a process can have some relative parts of functions which need to be loaded in the same page.

Operating system doesn't care about the User's view of the process. It may divide the same function into
different pages and those pages may or may not be loaded at the same time into the memory. It decreases
the efficiency of the system.

It is better to have segmentation which divides the process into the segments. Each segment contains the
same type of functions such as the main function can be included in one segment and the library functions
can be included in the other segment.

Translation of Logical address into physical address by segment table

CPU generates a logical address which contains two parts:

1. Segment Number
2. Offset

CO&OS UNIT-IV Part-2 Page 9 of 18


For Example:

Suppose a 16 bit address is used with 4 bits for the segment number and 12 bits for the segment offset so
the maximum segment size is 4096 and the maximum number of segments that can be refereed is 16.

When a program is loaded into memory, the segmentation system tries to locate space that is large enough
to hold the first segment of the process; space information is obtained from the free list maintained by
memory manager. Then it tries to locate space for other segments. Once adequate space is located for all
the segments, it loads them into their respective areas.

The operating system also generates a segment map table for each program.

With the help of segment map tables and hardware assistance, the operating system can easily translate a
logical address into physical address on execution of a program.

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.

The above figure shows how address translation is done in case of segmentation.

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.

CO&OS UNIT-IV Part-2 Page 10 of 18


Paging VS Segmentation

S. No. Paging Segmentation

1 Non-Contiguous memory allocation Non-contiguous memory allocation

2 Paging divides program into fixed size Segmentation divides program into variable
pages. size segments.

3 OS is responsible Compiler is responsible.

4 Paging is faster than segmentation Segmentation is slower than paging

5 Paging is closer to Operating System Segmentation is closer to User

6 It suffers from internal fragmentation It suffers from external fragmentation

7 There is no external fragmentation There is no external fragmentation

8 Logical address is divided into page Logical address is divided into segment
number and page offset number and segment offset

9 Page table is used to maintain the page Segment Table maintains the segment
information. information

10 Page table entry has the frame number Segment table entry has the base address of
and some flag bits to represent details the segment and some protection bits for the
about pages. segments.

Virtual Memory:

Virtual Memory is a storage scheme that provides user an illusion of having a very big main memory. This
is done by treating a part of secondary memory as the main memory.

In this scheme, User can load the bigger size processes than the available main memory by having the
illusion that the memory is available to load the process.

Instead of loading one big process in the main memory, the Operating System loads the different parts of
more than one process in the main memory.

By doing this, the degree of multiprogramming will be increased and therefore, the CPU utilization will
also be increased.

How Virtual Memory Works?

In modern word, virtual memory has become quite common these days. In this scheme, whenever some
pages needs to be loaded in the main memory for the execution and the memory is not available for those
many pages, then in that case, instead of stopping the pages from entering in the main memory, the OS
search for the RAM area that are least used in the recent times or that are not referenced and copy that into
the secondary memory to make the space for the new pages in the main memory.

CO&OS UNIT-IV Part-2 Page 11 of 18


Since all this procedure happens automatically, therefore it makes the computer feel like it is having the
unlimited RAM.

The CPU contains a register which contains the base address of page table that is 5 in the case of P1 and 7
in the case of P2. This page table base address will be added to the page number of the Logical address
when it comes to accessing the actual corresponding entry.

Advantages of Virtual Memory


1. The degree of Multiprogramming will be increased.
2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.

Disadvantages of Virtual Memory

1. The system becomes slower since swapping takes time.


2. It takes more time in switching between applications.
3. The user will have the lesser hard disk space for its use.

Demand Paging
Demand Paging is a popular method of virtual memory management. In demand paging, the pages of a
process which are least used, get stored in the secondary memory.

A page is copied to the main memory when its demand is made or page fault occurs. There are various
page replacement algorithms which are used to determine the pages which will be replaced. We will
discuss each one of them later in detail.

CO&OS UNIT-IV Part-2 Page 12 of 18


Page Replacement:

In Virtual Memory Management, Page Replacement Algorithms play an important role. The main
objective of all the Page replacement policies is to decrease the maximum number of page faults.

Page Fault – It is basically a memory error, and it occurs when the current programs attempt to access the
memory page for mapping into virtual address space, but it is unable to load into the physical memory
then this is referred to as Page fault.

Basic Page Replacement Algorithm

Page Replacement technique uses the following approach. If there is no free frame, then we will find the
one that is not currently being used and then free it. A-frame can be freed by writing its content to swap
space and then change the page table in order to indicate that the page is no longer in the memory.

1. First of all, find the location of the desired page on the disk.
2. Find a free Frame: a) If there is a free frame, then use it. b) If there is no free frame then make use
of the page-replacement algorithm in order to select the victim frame. c) Then after that write the
victim frame to the disk and then make the changes in the page table and frame table accordingly.
3. After that read the desired page into the newly freed frame and then change the page and frame
tables.
4. Restart the process.

Figure: Page Replacement

Page Replacement Algorithms:

The page replacement algorithm decides which memory page is to be replaced. The process of
replacement is sometimes called swap out or write to disk. Page replacement is done when the requested
page is not found in the main memory (page fault).

CO&OS UNIT-IV Part-2 Page 13 of 18


There are two main aspects of virtual memory, Frame allocation and Page Replacement. It is very
important to have the optimal frame allocation and page replacement algorithm. Frame allocation is all
about how many frames are to be allocated to the process while the page replacement is all about
determining the page number which needs to be replaced in order to make space for the requested page.

What If the algorithm is not optimal?

1. If the number of frames which are allocated to a process is not sufficient or accurate then there can be a
problem of thrashing. Due to the lack of frames, most of the pages will be residing in the main memory
and therefore more page faults will occur.

2. If the page replacement algorithm is not optimal then there will also be the problem of thrashing. If the
number of pages that are replaced by the requested pages will be referred in the near future then there will
be more number of swap-in and swap-out and therefore the OS has to perform more replacements then
usual which cause performance deficiency. Therefore, the task of an optimal page replacement algorithm
is to choose the page which can limit the thrashing.

Types of Page Replacement Algorithms:


There are various page replacement algorithms. Each algorithm has a different method by which the pages
can be replaced.

1. Optimal Page Replacement algorithm → this algorithms replaces the page which will not be
referred for so long in future. Although it cannot be practically implementable but it can be used as
a benchmark. Other algorithms are compared to this in terms of optimality.
2. LEAST RECENT USED (LRU) page replacement algorithm → this algorithm replaces the
page which has not been referred for a long time. This algorithm is just opposite to the optimal
page replacement algorithm. In this, we look at the past instead of staring at future.
3. FIFO → in this algorithm, a queue is maintained. The page which is assigned the frame first will
be replaced first. In other words, the page which resides at the rare end of the queue will be
replaced on the every page fault.

Q. Consider a main memory with five page frames and the following sequence of page references: 3,
8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3. which one of the following is true with respect to page replacement
policies First-In-First-out (FIFO) and Least Recently Used (LRU)?

A. Both incur the same number of page faults C. LRU incurs 2 more page faults than FIFO
B. FIFO incurs 2 more page faults than LRU D. FIFO incurs 1 more page faults than LRU

CO&OS UNIT-IV Part-2 Page 14 of 18


Solution:

Number of frames = 5

FIFO
According to FIFO, the page which first comes in the memory will first goes out.

Number of Page Faults = 9


Number of hits = 6

LRU

According to LRU, the page which has not been requested for a long time will get replaced with the new
one.

Number of Page Faults = 9


Number of Hits = 6

The Number of page faults in both the cases is equal therefore the Answer is option (A).

Numerical on Optimal, LRU and FIFO

CO&OS UNIT-IV Part-2 Page 15 of 18


Q. Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2. the number of frames in the memory is 3.
Find out the number of page faults respective to:

1. Optimal Page Replacement Algorithm


2. FIFO Page Replacement Algorithm
3. LRU Page Replacement Algorithm

Optimal Page Replacement Algorithm

Number of Page Faults in Optimal Page Replacement Algorithm = 5

LRU Page Replacement Algorithm

Number of Page Faults in LRU = 6

FIFO Page Replacement Algorithm

Number of Page Faults in FIFO = 6

CO&OS UNIT-IV Part-2 Page 16 of 18


Allocation of frames:
An important aspect of operating systems, virtual memory is implemented using demand paging.
Demand paging necessitates the development of a page-replacement algorithm and a frame allocation
algorithm. Frame allocation algorithms are used if you have multiple processes; it helps decide how
many frames to allocate to each process.

There are various constraints to the strategies for the allocation of frames:
 You cannot allocate more than the total number of available frames.
 At least a minimum number of frames should be allocated to each process. This constraint is supported
by two reasons. The first reason is, as less number of frames are allocated, there is an increase in the
page fault ratio, decreasing the performance of the execution of the process. Secondly, there should be
enough frames to hold all the different pages that any single instruction can reference.
Frame allocation algorithms –
The two algorithms commonly used to allocate frames to a process are:
1. Equal allocation: In a system with x frames and y processes, each process gets equal number of
frames, i.e. x/y. For instance, if the system has 48 frames and 9 processes, each process will get 5
frames. The three frames which are not allocated to any process can be used as a free-frame buffer
pool.
 Disadvantage: In systems with processes of varying sizes, it does not make much sense to give
each process equal frames. Allocation of a large number of frames to a small process will
eventually lead to the wastage of a large number of allocated unused frames.
2. Proportional allocation: Frames are allocated to each process according to the process size.
For a process pi of size si, the number of allocated frames is ai = (si/S)*m, where S is the sum of the
sizes of all the processes and m is the number of frames in the system. For instance, in a system with
62 frames, if there is a process of 10KB and another process of 127KB, then the first process will be
allocated (10/137)*62 = 4 frames and the other process will get (127/137)*62 = 57 frames.

 Advantage: All the processes share the available frames according to their needs, rather than
equally.

Global vs. Local Allocation –


The number of frames allocated to a process can also dynamically change depending on whether you
have used global replacement or local replacement for replacing pages in case of a page fault.
1. Local replacement: When a process needs a page which is not in the memory, it can bring in the
new page and allocate it a frame from its own set of allocated frames only.
 Advantage: The page in memory for a particular process and the page fault ratio is affected by
the paging behavior of only that process.
 Disadvantage: A low priority process may hinder a high priority process by not making its
frames available to the high priority process.
2. Global replacement: When a process needs a page which is not in the memory, it can bring in the
new page and allocate it a frame from the set of all frames, even if that frame is currently allocated to
some other process; that is, one process can take a frame from another.
 Advantage: Does not hinder the performance of processes and hence results in greater system
throughput.
 Disadvantage: The page fault ratio of a process cannot be solely controlled by the process itself.
The pages in memory for a process depend on the paging behavior of other processes as well.

CO&OS UNIT-IV Part-2 Page 17 of 18


Thrashing:

In case, if the page fault and swapping happens very frequently at a higher rate, then the operating system
has to spend more time swapping these pages. This state in the operating system is termed thrashing.
Because of thrashing the CPU utilization is going to be reduced.

Let's understand by an example, if any process does not have the number of frames that it needs to support
pages in active use then it will quickly page fault. And at this point, the process must replace some pages.
As all the pages of the process are actively in use, it must replace a page that will be needed again right
away. Consequently, the process will quickly fault again, and again, and again, replacing pages that it
must bring back in immediately. This high paging activity by a process is called thrashing.

During thrashing, the CPU spends less time on some actual productive work spend more time swapping.

Figure: Thrashing

By:
Dr. M. Ramu
Associate Professor
Dept. of IT
TKREC-R9
Hyderabad

CO&OS UNIT-IV Part-2 Page 18 of 18

You might also like