0% found this document useful (0 votes)
106 views

Operating System MCA BPUT Unit-5

Memory management is the process by which an operating system manages and allocates primary memory. It tracks allocated and free memory locations. There are several memory management techniques including single contiguous allocation, partitioned allocation, paged memory management, and segmented memory management. Contiguous allocation allocates all available memory to a single process, while partitioned allocation divides memory into partitions for multiple processes. Paged and segmented memory management use hardware to map memory pages and segments. Memory management aims to optimize memory usage and protect processes from interfering with each other.

Uploaded by

danger boy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Operating System MCA BPUT Unit-5

Memory management is the process by which an operating system manages and allocates primary memory. It tracks allocated and free memory locations. There are several memory management techniques including single contiguous allocation, partitioned allocation, paged memory management, and segmented memory management. Contiguous allocation allocates all available memory to a single process, while partitioned allocation divides memory into partitions for multiple processes. Paged and segmented memory management use hardware to map memory pages and segments. Memory management aims to optimize memory usage and protect processes from interfering with each other.

Uploaded by

danger boy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Subject: Operating System

Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

What is Memory Management?


Memory Management is the process of controlling and
coordinating computer memory, assigning portions known as
blocks to various running programs to optimize the overall
performance of the system.

It is the most important function of an operating system that


manages primary memory. It helps processes to move back
and forward between the main memory and execution disk. It
helps OS to keep track of every memory location, irrespective
of whether it is allocated to some process or it remains free.

Why Use Memory Management?


• It allows you to check how much memory needs to be
allocated to processes that decide which processor should
get memory at what time.
• Tracks whenever inventory gets freed or unallocated.
According to it will update the status.
• It allocates the space to application routines.
• It also makes sure that these applications do not interfere
with each other.
• Helps protect different processes from each other
• It places the programs in memory so that memory is
utilized to its full extent.

Memory Management Techniques


Single Contiguous Allocation
It is the easiest memory management technique. In this
method, all types of computer's memory except a small portion
which is reserved for the OS is available for one application.
For example, MS-DOS operating system allocates memory in
this way. An embedded system also runs on a single
application.

1|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Partitioned Allocation
It divides primary memory into various memory partitions,
which is mostly contiguous areas of memory. Every partition
store all the information for a specific task or job. This method
consists of allotting a partition to a job when it starts &
unallocated when it ends.

Paged Memory Management


This method divides the computer's main memory into fixed-
size units known as page frames. This hardware memory
management unit maps pages into frames which should be
allocated on a page basis.

Segmented Memory Management


Segmented memory is the only memory management method
that does not provide the user's program with a linear and
contiguous address space.

Segments need hardware support in the form of a segment


table. It contains the physical address of the section in
memory, size, and other data like access protection bits and
status.

Single Continuous Allocation : On this type of


memory management scheme usually the operating
system (monitor) is resides in either upper part or lower part
of the main memory. A single job is assigned to all the part of
the portion of the memory although, it requires a small portion.
The job is controlled by the CPU until it is completed an error
occurs. The CPU is ideal in the system at time of I/O operations,
this type of memory allocation is used for small in expensive
completing system.

2|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Dis-advantages :
1) A single job is assigned to all the memory so
large amount of memory may be wasted.
2) CPU time is wasted.
3) In this scheme the resources are not utilized in efficient
manner at the same time there is not enough
hardware flexibility to allow effective allocation of memory.

Partitioned Allocation : To solve the problems of


CPU wasted time and wasted memory the partitioned allocation
scheme was introduced. In this memory is partitioned. This
scheme is used and systems with MFT (
Multi programming with Fixed number of Tasks) and MVT (
Multi programming with Variable number of Tasks). Multi
programming means maintaining the concurrent execution of
more than one program by single CPU.

In Multiprogramming partitioned scheme the CPU time


is allocated to each job or a Time-Slice priority basis. For a
time-slice ( say 100 ms) the CPU executes a program until one
of the following conditions occur.

1. Job list completed.


2. Error is detected.
3. I/O is required.
4. Jobs Time-Slice is over.

In the first two cases the job can be removed from the
memory. In the last two, the job is only temporarily
suspended, it is to be resumed. In each case the CPU is then
assign to the job with next highest priority.

Advantages :

1) Multiprogramming eliminates the CPU wasted time.


2) With partitions the memory wastage is reduced.

3|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Disadvantages :

1) The major problem with a partitioned allocation once is


fragmentation.
2) Fragmentation is the development of unused 20k regions
called fragments in the memory.

Difference between Contiguous and


Noncontiguous Memory Allocation
1. Contiguous Memory Allocation :
Contiguous memory allocation is basically a method in which a
single contiguous section/part of memory is allocated to a
process or file needing it. Because of this all the available
memory space resides at the same place together, which
means that the freely/unused available memory partitions are
not distributed in a random fashion here and there across the
whole memory space.

The main memory is a combination of two main portions- one


for the operating system and other for the user program. We
can implement/achieve contiguous memory allocation by
dividing the memory partitions into fixed size partitions.

4|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

2. Non-Contiguous Memory Allocation :


Non-Contiguous memory allocation is basically a method on the
contrary to contiguous allocation method, allocates the
memory space present in different locations to the process as
per it’s requirements. As all the available memory space is in a
distributed pattern so the freely available memory space is also
scattered here and there.
This technique of memory allocation helps to reduce the
wastage of memory, which eventually gives rise to Internal and
external fragmentation.

Difference between Contiguous and Non-contiguous


Memory Allocation :
S.NO. Contiguous Memory Non-Contiguous
Allocation Memory Allocation
1. Contiguous memory Non-Contiguous memory
allocation allocates allocation allocates
consecutive blocks of separate blocks of
memory to a file/process. memory to a file/process.
2. Faster in Execution. Slower in Execution.
3. It is easier for the OS to It is difficult for the OS to
control. control.
4. Overhead is minimum as More Overheads are there
not much address as there are more address
translations are there translations.

5|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

while executing a
process.
5. Internal fragmentation External fragmentation
occurs in Contiguous occurs in Non-Contiguous
memory allocation memory allocation
method. method.
6. It includes single partition It includes paging and
allocation and multi- segmentation.
partition allocation.
7. Wastage of memory is No memory wastage is
there. there.
8. In contiguous memory In non-contiguous
allocation, swapped-in memory allocation,
processes are arranged in swapped-in processes can
the originally allocated be arranged in any place
space. in the memory.

Contiguous memory allocation


techniques
First-Fit Memory Allocation:
This method keeps the free/busy list of jobs organized by
memory location, low-ordered to high-ordered memory. In
this method, first job claims the first available memory with
space more than or equal to it’s size. The operating system
doesn’t search for appropriate partition but just allocate the
job to the nearest memory partition available with sufficient
size.

6|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

As illustrated above, the system assigns J1 the nearest


partition in the memory. As a result, there is no partition with
sufficient space is available for J3 and it is placed in the
waiting list.
Advantages of First-Fit Memory Allocation:
It is fast in processing. As the processor allocates the nearest
available memory partition to the job, it is very fast in
execution.
Disadvantages of First-Fit Memory Allocation :
It wastes a lot of memory. The processor ignores if the size of
partition allocated to the job is very large as compared to the
size of job or not. It just allocates the memory. As a result, a
lot of memory is wasted and many jobs may not get space in
the memory, and would have to wait for another job to
complete.

Best-Fit Memory Allocation:


This method keeps the free/busy list in order by size –
smallest to largest. In this method, the operating system first
searches the whole of the memory according to the size of the
7|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

given job and allocates it to the closest-fitting free partition in


the memory, making it able to use memory efficiently. Here
the jobs are in the order from smalest job to largest job.

As illustrated in above figure, the operating system first


search throughout the memory and allocates the job to the
minimum possible memory partition, making the memory
allocation efficient.
Advantages of Best-Fit Allocation :
Memory Efficient. The operating system allocates the job
minimum possible space in the memory, making memory
management very efficient. To save memory from getting
wasted, it is the best method.
Disadvantages of Best-Fit Allocation :
It is a Slow Process. Checking the whole memory for each job
makes the working of the operating system very slow. It takes
a lot of time to complete the work.

Worst-Fit Memory Allocation :


In this allocation technique, the process traverses the whole
memory and always search for the largest hole/partition, and
8|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

then the process is placed in that hole/partition. It is a slow


process because it has to traverse the entire memory to
search the largest hole.
Here is an example to understand Worst Fit-Allocation –

Here Process P1=30K is allocated with the Worst Fit-Allocation


technique, so it traverses the entire memory and selects
memory size 400K which is the largest, and hence there is an
internal fragmentation of 370K which is very large and so
many other processes can also utilize this leftover space.
Advantages of Worst-Fit Allocation :
Since this process chooses the largest hole/partition, therefore
there will be large internal fragmentation. Now, this internal
fragmentation will be quite big so that other small processes
can also be placed in that leftover partition.
Disadvantages of Worst-Fit Allocation :
It is a slow process because it traverses all the partitions in
the memory and then selects the largest partition among all
the partitions, which is a time-consuming process.

9|Page
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

What is Paging?
Paging is a storage mechanism that allows OS to retrieve
processes from the secondary storage into the main memory in
the form of pages. In the Paging method, the main memory is
divided into small fixed-size blocks of physical memory, which
is called frames. The size of a frame should be kept the same
as that of a page to have maximum utilization of the main
memory and to avoid external fragmentation. Paging is used
for faster access to data, and it is a logical concept.

Example
For example, if the main memory size is 16 KB and Frame size
is 1 KB. Here, the main memory will be divided into the
collection of 16 frames of 1 KB each.

There are 4 separate processes in the system that is A1, A2,


A3, and A4 of 4 KB each. Here, all the processes are divided
into pages of 1 KB each so that operating system can store one
page in one frame.

At the beginning of the process, all the frames remain empty


so that all the pages of the processes will get stored in a
contiguous way.

10 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

In this example you can see that A2 and A4 are moved to the
waiting state after some time. Therefore, eight frames become
empty, and so other pages can be loaded in that empty blocks.
The process A5 of size 8 pages (8 KB) are waiting in the ready
queue.

11 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

In this example, you can see that there are eight non-contiguous
frames which is available in the memory, and paging offers the
flexibility of storing the process at the different places. This allows
us to load the pages of process A5 instead of A2 and A4.

What is Paging Protection?


The paging process should be protected by using the concept of
insertion of an additional bit called Valid/Invalid bit. Paging
Memory protection in paging is achieved by associating protection
bits with each page. These bits are associated with each page
table entry and specify protection on the corresponding page.

Advantages of Paging
Here, are advantages of using Paging method:

• Easy to use memory management algorithm


• No need for external Fragmentation
• Swapping is easy between equal-sized pages and page
frames.

Disadvantages of Paging
Here, are drawback/ cons of Paging:

• May cause Internal fragmentation


• Complex memory management algorithm
• Page tables consume additional memory.
• Multi-level paging may lead to memory reference overhead.

Paging as a memory management scheme


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

12 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

• 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
Example:
• If Logical Address = 31 bit, then Logical Address Space
= 231 words = 2 G words (1 G = 230)
• If Logical Address Space = 128 M words = 2 *
7

220 words, then Logical Address = log 2 227 = 27 bits


• If Physical Address = 22 bit, then Physical Address
Space = 222 words = 4 M words (1 M = 220)
• If Physical Address Space = 16 M words = 2 *
4

220 words, then Physical Address = log 2 224 = 24 bits


The mapping from virtual to physical address is done by the
memory management unit (MMU) which is a hardware device
and this mapping is known as paging technique.
• The Physical Address Space is conceptually divided into
a number of fixed-size blocks, called frames.
• The Logical address Space is also splitted into fixed-
size blocks, called pages.
• Page Size = Frame Size
Let us consider an example:
• Physical Address = 12 bits, then Physical Address
Space = 4 K words
• Logical Address = 13 bits, then Logical Address Space
= 8 K words
• Page size = frame size = 1 K words (assumption)

13 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Address generated by CPU is divided into


• Page number(p): Number of bits required to represent
the pages in Logical Address Space or Page number
• Page offset(d): Number of bits required to represent
particular word in a page or page size of Logical Address
Space or word number of a page or page offset.
Physical Address is divided into
• Frame number(f): Number of bits required to represent
the frame of Physical Address Space or Frame number.
• Frame offset(d): Number of bits required to represent
particular word in a frame or frame size of Physical Address
Space or word number of a frame or frame offset.

The hardware implementation of page table can be done by using


dedicated registers. But the usage of register for the page table is
satisfactory only if page table is small. If page table contain large
number of entries then we can use TLB(translation Look-aside
buffer), a special, small, fast look up hardware cache.
• The TLB is associative, high speed memory.
• Each entry in TLB consists of two parts: a tag and a value.

14 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

• When this memory is used, then an item is compared with


all tags simultaneously. If the item is found, then
corresponding value is returned.

Main memory access time = m


If page table are kept in main memory,
Effective access time = m(for page table) + m(for
particular page in page table)

15 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

What is 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 segment which
can be allocated to a process.

The details about each segment are stored in a table called as


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 closer to Operating system
rather than the User. It divides all the process into the form of
pages regardless of the fact that a process can have some
relative parts of functions which needs 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 same type of functions
such as main function can be included in one segment and the
library functions can be included in the other segment,

16 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Translation of Logical address into physical


address by segment table
CPU generates a logical address which contains two parts:

1. Segment Number
2. Offset

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 address, the base address of the segment


is added to the offset to get the physical address of 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

17 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

4. It is easier to relocate segments than entire address


space.
5. The segment table is of lesser size as compare to the
page table in paging.

Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable
sized partition.
3. Costly memory management algorithms.

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.

1. Pages are smaller than segments.


2. Each Segment has a page table which means every
program has multiple page tables.
3. The logical address is represented as Segment Number
(base address), Page number and page offset.

Segment Number → It points to the appropriate Segment


Number.

Page Number → It Points to the exact page within the


segment

Page Offset → Used as an offset within the page frame

Each Page table contains the various information about every


page of the segment. The Segment Table contains the

18 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

information about every segment. 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.

19 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Advantages of Segmented Paging


1. It reduces memory usage.
2. Page table size is limited by the segment size.
3. Segment table has only one entry corresponding to one
actual segment.
4. External Fragmentation is not there.
5. It simplifies memory allocation.

Disadvantages of Segmented Paging


1. Internal Fragmentation will be there.
20 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

2. The complexity level will be much higher as compare to


paging.
3. Page Tables need to be contiguously stored in the
memory.

Sharing and Protection in a Segmented System


One of the advantages of segmentation over paging is that it
is logical rather than a physic concept. Generally segments
are not limited to a particular size as it is done with page
size. Rather, segments are allowed to be as large (with
reasonable limit) as they require to be.

A segment for an array is as large as the array. A segment


corresponding to a dynamic data structure such as stack,
queue, binary tree etc. may increase and decrease in size as
the data structure itself increases and decreases.

Sharing in a segmented system is straightforward when


compared to sharing in a paging system. For sharing a
paging system there must be an entry for each page in page
map table. To share a stack in a paging system which is
spread into 5 pages there will be 5 separate entries for each
of the pages.

It is a very difficult task to share dynamic data structure who


size keeps on changing during program execution. In a
paging system, when a stack grows into a new page,
information about this new page has to be adjusted at
execution time. In a segmentation system once the segment
is declared as shared, then the size of data structure may
increase or decrease without changing the logical fact that
they reside on a shared segment.

The sharing in a segmented system occurs only at the


segment level. Two users share a segment by simply having
entries in their segment table that will be pointing to the
same segment in primary storage:

21 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Protection of one segment from another segment is done


through protection bit. The memory mapping hardware
checks the protection bit associated with each segment table
entry to prevent illegal access to memory such as attempts
to write into a read-only segment.By placing an array in its
own segment, the memory management hardware will
automatically check that array indexes are legal and do not
stay outside the array boundaries. Thus, many common
program errors will be detected by the hardware before they
can cause serious damage.

Paging VS Segmentation
Sr Paging Segmentation
No.
1 Non-Contiguous memory Non-contiguous memory
allocation allocation
2 Paging divides program Segmentation divides program
into fixed size pages. into variable size segments.
3 OS is responsible Compiler is responsible.
4 Paging is faster than Segmentation is slower than
segmentation paging
5 Paging is closer to Segmentation is closer to User
Operating System
6 It suffers from internal It suffers from external
fragmentation fragmentation
7 There is no external There is no external
fragmentation fragmentation
8 Logical address is divided Logical address is divided into
into page number and segment number and segment
page offset offset
9 Page table is used to Segment Table maintains the
maintain the page segment information
information.
10 Page table entry has the Segment table entry has the
frame number and some base address of the segment
flag bits to represent and some protection bits for
details about pages. the segments.

22 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Virtual Memory
A computer can address more memory than the amount physically
installed on the system. This extra memory is actually called virtual
memory and it is a section of a hard disk that's set up to emulate the
computer's RAM.
The main visible advantage of this scheme is that programs can be larger
than physical memory. Virtual memory serves two purposes. First, it
allows us to extend the use of physical memory by using disk. Second, it
allows us to have memory protection, because each virtual address is
translated to a physical address.
Following are the situations, when entire program is not required to be
loaded fully in main memory.
• User written error handling routines are used only when an error
occurred in the data or computation.
• Certain options and features of a program may be used rarely.
• Many tables are assigned a fixed amount of address space even
though only a small amount of the table is actually used.
• The ability to execute a program that is only partially in memory
would counter many benefits.
• Less number of I/O would be needed to load or swap each user
program into memory.
• A program would no longer be constrained by the amount of
physical memory that is available.
• Each user program could take less physical memory, more
programs could be run the same time, with a corresponding
increase in CPU utilization and throughput.
Modern microprocessors intended for general-purpose use, a memory
management unit, or MMU, is built into the hardware. The MMU's job is
to translate virtual addresses into physical addresses. A basic example
is given below −

23 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Virtual memory is commonly implemented by demand paging. It can also


be implemented in a segmentation system. Demand segmentation can
also be used to provide virtual memory.

Demand Paging
A demand paging system is quite similar to a paging system with
swapping where processes reside in secondary memory and pages are
loaded only on demand, not in advance. When a context switch occurs,
the operating system does not copy any of the old program’s pages out
to the disk or any of the new program’s pages into the main memory
Instead, it just begins executing the new program after loading the first
page and fetches that program’s pages as they are referenced.

24 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

While executing a program, if the program references a page which is not


available in the main memory because it was swapped out a little ago,
the processor treats this invalid memory reference as a page fault and
transfers control from the program to the operating system to demand the
page back into the memory.
Advantages
Following are the advantages of Demand Paging −

• Large virtual memory.


• More efficient use of memory.
• There is no limit on degree of multiprogramming.
Disadvantages
• Number of tables and the amount of processor overhead for
handling page interrupts are greater than in the case of the simple
paged management techniques.
25 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Swapping:
Swapping a process out means removing all of its pages from memory,
or marking them so that they will be removed by the normal page
replacement process. Suspending a process ensures that it is not
runnable while it is swapped out. At some later time, the system swaps
back the process from the secondary storage to main memory. When a
process is busy swapping pages in and out then this situation is called
thrashing.

26 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Thrashing :

At any given time, only few pages of any process are in main memory
and therefore more processes can be maintained in memory.
Furthermore time is saved because unused pages are not swapped in
and out of memory. However, the OS must be clever about how it
manages this scheme. In the steady state practically, all of main
memory will be occupied with process’s pages, so that the processor
and OS has direct access to as many processes as possible. Thus
when the OS brings one page in, it must throw another out. If it throws
out a page just before it is used, then it will just have to get that page
again almost immediately. Too much of this leads to a condition called
Thrashing. The system spends most of its time swapping pages rather
than executing instructions. So a good page replacement algorithm is
required.

In the given diagram, initial degree of multi programming upto some


extent of point(lamda), the CPU utilization is very high and the system
resources are utilized 100%. But if we further increase the degree of
multi programming the CPU utilization will drastically fall down and the
system will spent more time only in the page replacement and the time
taken to complete the execution of the process will increase. This
situation in the system is called as thrashing.

27 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Causes of Thrashing :
1. High degree of multiprogramming : If the number of
processes keeps on increasing in the memory than number of
frames allocated to each process will be decreased. So, less
number of frames will be available to each process. Due to
this, page fault will occur more frequently and more CPU time
will be wasted in just swapping in and out of pages and the
utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of process = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes
are increased,frames per process are decreased. Hence CPU
time will be consumed in just swapping pages.
2. Lacks of Frames:If a process has less number of frames then
less pages of that process will be able to reside in memory and
hence more frequent swapping in and out will be required. This
may lead to thrashing. Hence sufficient amount of frames must
be allocated to each process in order to prevent thrashing.

Recovery of Thrashing :
• Do not allow the system to go into thrashing by instructing the
long term scheduler not to bring the processes into memory
after the threshold.
• If the system is already in thrashing then instruct the mid term
schedular to suspend some of the processes so that we can
recover the system from thrashing.

Page Replacement Algorithms


In an operating system that uses paging for memory management, a
page replacement algorithm is needed to decide which page needs to
be replaced when new page comes in.
Page Fault – A page fault happens when a running program accesses
a memory page that is mapped into the virtual address space, but not
loaded in physical memory.
Since actual physical memory is much smaller than virtual memory,
page faults happen. In case of page fault, Operating System might
28 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

have to replace one of the existing pages with the newly needed page.
Different page replacement algorithms suggest different ways to decide
which page to replace. The target for all algorithms is to reduce the
number of page faults.
Page Replacement Algorithms :

• First In First Out (FIFO) –


This is the simplest page replacement algorithm. In this
algorithm, the operating system keeps track of all pages in the
memory in a queue, the oldest page is in the front of the
queue. When a page needs to be replaced page in the front of
the queue is selected for removal.

• Example-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3


page frames. Find number of page faults.

Initially all slots are empty, so when 1, 3, 0 came they are allocated to the
empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page
slot i.e 1. —>1 Page Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot
i.e 3 —>1 Page Fault.
Finally when 3 come it is not avilable so it replaces 0 1 page fault

29 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

Belady’s anomaly – Belady’s anomaly proves that it is possible to have more


page faults when increasing the number of page frames while using the First in
First Out (FIFO) page replacement algorithm. For example, if we consider
reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we get 9 total page
faults, but if we increase slots to 4, we get 10 page faults.

• Optimal Page replacement –


In this algorithm, pages are replaced which would not be used for the
longest duration of time in the future.

Example-2:Consider the page references 7, 0, 1, 2, 0, 3, 0, 4,


2, 3, 0, 3, 2, with 4 page frame. Find number of page fault.

Initially all slots are empty, so when 7 0 1 2 are allocated to the


empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used
for the longest duration of time in the future.—>1 Page fault.
0 is already there so —> 0 Page fault..
4 will takes place of 1 —> 1 Page Fault.

Now for the further page reference string —> 0 Page


fault because they are already available in the memory.
Optimal page replacement is perfect, but not possible in
practice as the operating system cannot know future requests.
The use of Optimal Page replacement is to set up a
benchmark so that other replacement algorithms can be
analyzed against it.

30 | P a g e
Contact: 7008443534, 9090042626
Subject: Operating System
Created By: Asst. Prof. SK ABDUL ISRAR College: ABA, BLS

• Least Recently Used –


In this algorithm page will be replaced which is least recently
used.
Example-3Consider the page reference string 7, 0, 1, 2, 0, 3,
0, 4, 2, 3, 0, 3, 2 with 4 page frames. Find number of page
faults.

Initially all slots are empty, so when 7 0 1 2 are allocated to the


empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least
recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page
fault because they are already available in the memory.

31 | P a g e
Contact: 7008443534, 9090042626

You might also like