0% found this document useful (0 votes)
31 views27 pages

Unit - V

Uploaded by

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

Unit - V

Uploaded by

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

Unit-V

Chapter – 5
Memory Management

1. What is the concept of paging?


 Paging refers to the transfer of memory pages from physical memory to disk and vice
versa.
 Virtual memory uses a technique called demand paging for its implementation.
 Logical address space of a process can be noncontiguous; process is allocated physical
memory whenever thelatter is available.

2. Explain static and dynamic memory partitioning method.


a) Static Memory Partitioning:
 Main memory is divided into multiple partitions of fixed size at the time of system
generation.
 A processmay be loaded into a partition of equal size or greater size. Partitions can be
of equal size or unequal size
 The size of each partition is fixed and can not be changed.
 Each partition is allowed to store only one process.

 Advantages:
 Simple to implement
 It requires minimal operating system software and processing overhead as
partitions are fixed at the time of system generation.
 Disadvantages:
 Memory wastage
 Inefficient use of memory due to internal fragmentation.
 Maximum number of active processes is fixed.

b) Dynamic Memory partitioning:


 When a process enters in main memory, it is allocated exact size that is required by
that process.
 So inthis method, partitions can vary in size depending on memory space required by a
process entering inmain memory.
 Operating system maintains a table indicating which parts of memory are available
andwhich are occupied.
 When new process arrives and it needs space, system searches for available
memoryspace in mainmemory.
 If it is available then memory is allocated to the process by creating a partition
inmemory.
 Like this depending on size of process and available memory, partitions take place in
mainmemory.
 For example:-Consider following table with process and memory space.
 Total memory size is 64M.from this 8M partition is occupied by operating system and
remaining can bepartitioned as per the size of the process
 Advantages:
 No internal fragmentation.
 More efficient use of main memory.
 Disadvantages:
 It suffers from external fragmentation.
 It needs compaction

Q.What is fragmentation?
 As processes are loaded and removed from memory, the free memory space is broken
into little pieces. It happens after sometimes that processes cannot be allocated to
memory blocks considering their small size and memory blocks remains unused.
 This problem is known as Fragmentation.
Fragmentation is of two types −
S.N. Fragmentation & Description

External fragmentation
1 Total memory space is enough to satisfy a request or to reside a process in it, but it is
not contiguous, so it cannot be used.

Internal fragmentation
2 Memory block assigned to process is bigger. Some portion of memory is left unused, as
it cannot be used by another process.

 The following diagram shows how fragmentation can cause waste of memory and a
compaction technique can be used to create more free memory out of fragmented
memory –

 External fragmentation can be reduced by compaction or shuffle memory contents to


place all free memory together in one large block.
 To make compaction feasible, relocation should be dynamic.
 The internal fragmentation can be reduced by effectively assigning the smallest
partition but large enough for the process.
3. Give difference between External fragmentation and Internal fragmentation
Q.What is Compaction
 Compaction is a method to overcome the external fragmentation problem.
 All free blocks are brought together as one large block of free space.
 Compaction requires dynamic relocation.
 Certainly, compaction has a cost and selection of an optimal compaction strategy is
difficult.
 One method for compaction is swapping out those processes that are to be moved
within the memory, and swapping them into different memory locations
Q. Explain different Placement Algorithm
 If the free memory is present within a partition then it is called "internal
fragmentation".
 Similarly if the free blocks are present outside the partition, then it is called "external
fragmentation".
 Solution to the "external fragmentation" is compaction.
 Solution to the "internal fragmentation" is the "placement" algorithm only.
 Because memory compaction is time-consuming, when it is time to load or swap a
process into main memory and if there is more than one free block of memory of
sufficient size, then the operating system must decide which free block to allocate by
using three different placement algorithms.
 This is Shown in the figure below:


a) Best-fit:- It chooses the block, that is closest in size to the given request from the
beginning to the ending free blocks.
 We must search the entire list, unless it is ordered by size.
 This strategy produces the smallest leftover hole.

b) First-fit:- It begins to scan memory from the beginning and chooses the first available
block which is large enough.
 Searching can start either at the beginning of the set of blocks or where the previous
first-fit search ended.
 We can stop searching as soon as we find a free block that is large enough.

c) Worst-fit:- It allocates the largest block.


 We must search the entire the entire list,unless it is sorted by size.
 This strategy produces the largest leftover hole,which may be more useful than the
smaller leftover hole from a best-fit approach.

4. List three main levels of data storage and explain cache storage.
{{**Note: - Any other relevant explanation shall be considered. **}}
Three levels of data storage:
1. Primary Storage
2. Secondary Storage
3. Tertiary Storage

 Cache Storage:
 A Cache (Pronounced as “cash”) is a small and very fast temporary storage memory.
 It is designed to speed up the transfer of data and instructions.
 It is located inside or close to the CPU chip.
 It is faster than RAM and the data/instructions that are most recently or most
frequently used by CPU are stored in cache.
 As CPU has to fetch instruction from main memory speed of CPU depending on
fetching speed from main memory.
 CPU contains register which has fastest access but they are limited in number as well
as costly. Cache is cheaper so we can access cache.
 Cache memory is a very high speed memory that is placed between the CPU and main
memory, to operate at the speed of the CPU.
 It is used to reduce the average time to access data from the main memory.
 The cache is a smaller and faster memory which stores copies of the data from
frequently used main memory locations.
 Most CPUs have different independent caches, including instruction and data.

5. Describe activities of I/O system and secondary storage management


a) I/O System:
 Input / Output device management provides an environment for the better interaction
between system and the I / O devices such as printers, scanners, tape drives etc.
 To interact with I/O devices in an effective manner, the operating system uses some
special programs known as device driver.
 The device drivers take the data that operating system has defined as a file and then
translate them into streams of bits or a series of laser.
 A device driver is a specific type of computer software that is developed to allow
interaction with hardware devices.
 Typically this continues an interface for communicating with the I/O device, through
the specific computer bus or communication subsystem that the hardware is connected
with.
 The device driver is a specialized hardware dependent computer program that enables
another program, typically an operating system to interact transparently with a
hardware device, and usually provides the required interrupt handling necessary for
the time dependent hardware interfacing.
 Activities:
 Providing interfaces to other system components.
 Managing devices
 Transferring data
 Detecting I/O completion

b) Secondary storage Management:


 The computer system provides secondary storage to back up main memory.
 Secondary storage is required because main memory is too small to accommodate
all data and programs, and the data that it holds is lost when power is lost.
 Most modern computer systems use disks as the principal on-line storage medium
for programs and data.
 Most of the programs including compilers, assemblers, word processors, editors,
and formatters are stored on a disk until loaded into memory. Secondary storage
consists of tapes drives, disk drives, and other media.
 Activities:
 Free space management
 Storage allocation
 Storage de-allocation
 Disk scheduling.
 To read a piece of data that is stored at the end of the file, one has to read all of the
data that comes before it-you cannot jump directly to the desired data.
 This is similar to the way cassette tape players work.
 If one wants to listen to the last song on a cassette tape, he has to either fast-forward
over all of the songs that come before it or listen to them. There is no way to jump
directly to a specific song.

6. Explain memory management in detail.


a) Main-Memory Management:
 Primary-Memory or Main-Memory is a large array of words or bytes.
 Each word or byte has its ownaddress.
 Main-memory provides storage that can be access directly by the CPU.
 That is to say for aprogram to be executed, it must in the main memory.
 The major activities of an operating in regard tomemory-management are:
 Keep track of which part of memory are currently being used and by whom.
 Decide which process are loaded into memory when memory space becomes
available
 Allocate and De-allocate memory space as needed.

b) Secondary-Storage Management:
 Systems have several levels of storage, including primary storage, secondary storage
and cachestorage.
 Instructions and data must be placed in primary storage or cache to be referenced by a
runningprogram.
 Because main memory is too small to accommodate all data and programs, and its data
arelost when power is lost, the computer system must provide secondary storage to
back up mainmemory.
 Secondary storage consists of tapes, Disks, and other media designed to hold
information thatwill eventually be accessed in primary storage (primary, secondary,
cache) is ordinarily divided intobytes or words consisting of a fixed number of bytes.
 Each location in storage has an address; the setof all addresses available to a program
is called an address space.
 The three major activities of anoperating system in regard to secondary storage
management are:
 Managing the free space available on the secondary-storage device
 Allocation of storage space when new files have to be written.
 Scheduling the requests for memory access.

Q. Describe free space management with their different techniques


 Since disk space is limited, we need to reuse the space from deleted files for new files,
if possible.
 To keep track of free disk space, the system maintains a free-space list.
 The free-space list records all free disk blocks – those not allocated to some file or
directory.
 To create a file, we search the free-space list for the required amount of space, and
allocate that space to the new file.
 This space is then removed from the free-space list.
 When a file is deleted, its disk space is added to the free-space list.

7. Explain Bit map free-space management technique.


1. Bit Vector
 The free-space list is implemented as a bit map or bit vector.
 Each block is represented by 1 bit. If the block is free, the bit is 1; if the block is
allocated, the bit is 0.
 For example, consider a disk where block
 2,3,4,5,8,9,10,11,12,13,17,18,25,26 and 27 are free, and the rest of the block are
allocated. The free space bit map would be
 001111001111110001100000011100000 …
 The main advantage of this approach is its relatively simplicity and efficiency in
finding the first free block, or n consecutive free blocks on the disk.
 The given instance of disk blocks on the disk in Figure 1 (where green blocks are
allocated) can be represented by a bitmap of 16 bits as: 0000111000000110.

2. Linked List :
 In this approach, the free disk blocks are linked together i.e. a free block contains a
pointer to the next free block.
 The block number of the very first disk block is stored at a separate location on disk
and is also cached in memory.
 In Figure-2, the free space list head points to Block 5 which points to Block 6, the
next free block and so on.
 The last free block would contain a null pointer indicating the end of free list.
 A drawback of this method is the I/O required for free space list traversal.

8. Differentiate between contiguous and linked memory allocation method


9. What is Paging
Non-Contiguous Memory Allocation-
 Non-contiguous memory allocation is a memory allocation technique.
 It allows to store parts of a single process in a non-contiguous fashion.
 Thus, different parts of the same process can be stored at different places in the main
memory.

Techniques-
There are two popular techniques used for non-contiguous memory allocation-

1. Paging
2. Segmentation
Paging-
 Paging is a fixed size partitioning scheme.
 In paging, secondary memory and main memory are divided into equal fixed size
partitions.
 The partitions of secondary memory are called as pages.
 The partitions of main memory are called as frames.
 Each process is divided into parts where size of each part is same as page size.
 The size of the last part may be less than the page size.
 The pages of process are stored in the frames of main memory depending upon their
availability.
Example-
Consider a process is divided into 4 pages P0, P1, P2 and P3.
 Depending upon the availability, these pages may be stored in the main memory
frames in a non-contiguous fashion as shown-

10.Explain 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.
 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
o Large virtual memory.
o More efficient use of memory.
o There is no limit on degree of multiprogramming.

 Disadvantages
o 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.
11.Explain concept of Virtual Memory with Diagram
 Virtual memory is the separation of user logical memory from physicalmemory.
 This separation allows an extremely large virtual memory to beprovided for
programmers when only a smaller physical memory is available.
 Virtual memory makes the task of programming much easier, because theprogrammer
no longer needs to worry about the amount of physical memoryavailable for execution
of program.
 It is the process of increasing the apparentsize of a computer's RAM by using a section
of the hard disk storage as anextension of RAM.
 As computers have RAM of capacity 64 or 128 MB to be usedby the CPU resources
which is not sufficient to run all applications that are usedby most users in their
expected way and all at once.

 Example:
 Consider, an e-mail program, a web browser and a word processor is loadedinto RAM
simultaneously; the 64 MB space is not enough to store all theseprograms.
 Without a virtual memory, a message “You cannot load any moreapplications. Please
close an application to load a new one.” would bedisplayed.
 By using a virtual memory, a computer can look for empty areas ofRAM which is not
being used currently and copies them on to the hard diskdevice.
 Thus RAM is freed to load new applications.
 Actually it is doneautomatically, the user do not even know that it is happening, and
the userfeels like RAM has unlimited space even though the RAM capacity is 32 MB.
 Itis a process of increasing computer’s RAM by using a section of the hard diskstorage
as an extension of RAM.

12.Differentiate between paging and segmentation

OR
13.Explain swapping in operating system with diagram and example.
 Swapping:A process must be in the main memory so that it can execute.
 Swapping is a memory/process management technique used by the operating system
to increase the utilization of the processor.
 A process in execution may go into blocked state due to expiry of time
quantum,occurance of interrupt,etc. when a process is in blocked state and next
process is waiting for execution then operating system performs swapping.
 Swapping is a process of moving blocked process from the main memory to the
backing store and new process from backing store to main memory.
 Swapping forms a queue of temporarily suspended process and the execution
continues with the newly arrived process.
 In the above diagram, two processes P1 and P2 are shown.
 A process P1 is in main memory and in blocked state.
 Process P2 is in backing store waiting for its turn to execute.
 As P1 is blocked, operating system swap out this process by moving it from main
memory to backing store and swap in process P2 by loading it from backing store to
main memory.
 This process of swap out and swap in is called as swapping of processes.
 Example:Consider three processes P1,P2 and P3 are in memory.
 A Round-Robin CPU scheduling algorithm is in use.
 A process P1 starts its execution.
 When a time quantum expires memory manager swaps out the process P1 and swaps
in process P2.
 If P1 requires more time than the time quantum then it is added to blocked queue and
waits for its turn for execution.
 Once a time quantum of P2 expires ,manager swaps out it and swaps in P3.
 when time quantum of P3 expires, manager swaps out it and swaps in P1.
 This cycle of swap out and swap in continues till all the processes completes their
execution.

14.Explain page fault


Page Fault in OS-
 A page fault occurs when a page referenced by the CPU is not found in the main
memory.
 The required page has to be brought from the secondary memory into the main
memory.
 A page has to be replaced if all the frames of main memory are already occupied.

15.Explain page replacement


Page Replacement
Page replacement is a process of swapping out an existing page from the frame of a main
memory and replacing it with the required page.
Page replacement is required when-
 All the frames of main memory are already occupied.
 Thus, a page has to be replaced to create a room for the required page.

16.Explain different page replacement algorithms


Page Replacement Algorithms-
Page replacement algorithms help to decide which page must be swapped out from the main
memory to create a room for the incoming page.
Various page replacement algorithms are-

1. FIFO Page Replacement Algorithm


2. LIFO Page Replacement Algorithm
3. LRU Page Replacement Algorithm
4. Optimal Page Replacement Algorithm
5. Random Page Replacement Algorithm

A good page replacement algorithm is one that minimizes the number of page faults.

FIFO Page Replacement Algorithm-


 As the name suggests, this algorithm works on the principle of “First in First out“.
 It replaces the oldest page that has been present in the main memory for the longest
time.
 It is implemented by keeping track of all the pages in a queue.

LIFO Page Replacement Algorithm-


 As the name suggests, this algorithm works on the principle of “Last in First out“.
 It replaces the newest page that arrived at last in the main memory.
 It is implemented by keeping track of all the pages in a stack.

NOTE
Only frame is used for page replacement during entire procedure after all the frames get
occupied.

LRU Page Replacement Algorithm-


 As the name suggests, this algorithm works on the principle of “Least Recently
Used“.
 It replaces the page that has not been referred by the CPU for the longest time.

Optimal Page Replacement Algorithm-


 This algorithm replaces the page that will not be referred by the CPU in future for the
longest time.
 It is practically impossible to implement this algorithm.
 This is because the pages that will not be used in future for the longest time can not be
predicted.
 However, it is the best known algorithm and gives the least number of page faults.
 Hence, it is used as a performance measure criterion for other algorithms.

Random Page Replacement Algorithm-


 As the name suggests, this algorithm randomly replaces any page.
 So, this algorithm may behave like any other algorithm like FIFO, LIFO, LRU,
Optimal etc.

PRACTICE PROBLEMS BASED ON PAGE REPLACEMENT ALGORITHMS-


Problem-01:
 A system uses 3 page frames for storing process pages in main memory.
 It uses the First in First out (FIFO) page replacement policy.
 Assume that all the page frames are initially empty.
 What is the total number of page faults that will occur while processing the page
reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
 Also calculate the hit ratio and miss ratio.

Solution-
Total number of references = 10
From here,
Total number of page faults occurred = 6

Calculating Hit ratio-


Total number of page hits
= Total number of references – Total number of page misses or page faults
= 10 – 6
=4

Thus, Hit ratio


= Total number of page hits / Total number of references
= 4 / 10
= 0.4 or 40%

Calculating Miss ratio-


Total number of page misses or page faults = 6
Thus, Miss ratio
= Total number of page misses / Total number of references
= 6 / 10
= 0.6 or 60%

Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4
= 0.6 or 60%

Problem-02:
 A system uses 3 page frames for storing process pages in main memory.
 It uses the Least Recently Used (LRU) page replacement policy.
 Assume that all the page frames are initially empty.
 What is the total number of page faults that will occur while processing the page
reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.

Solution-
Total number of references = 10

From here,
Total number of page faults occurred = 6

In the similar manner as above-


 Hit ratio = 0.4 or 40%
 Miss ratio = 0.6 or 60%

Problem-03:
 A system uses 3 page frames for storing process pages in main memory.
 It uses the Optimal page replacement policy. Assume that all the page frames are
initially empty.
 What is the total number of page faults that will occur while processing the page
reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Also calculate the hit ratio and miss ratio.
Solution-
Total number of references = 10

From here,
Total number of page faults occurred = 5

In the similar manner as above-


 Hit ratio = 0.5 or 50%
 Miss ratio = 0.5 or 50%

Q. Explain FIFO (First in First out) page replacement algorithm for reference string
7012030423103.
 First-In-First-Out (FIFO) Algorithm: A FIFO replacement associates with each page
thetime when that page was bought into memory.
 When the page must be replaced, the oldestpage is chosen.
 It maintains a FIFO queue to hold all pages in memory.
 We replace the page at the head of the queue.
 When a page is brought into the memory, we insert it at thetail of the queue.
 Reference string: Consider three frames are available.
Page fault 11
Page hit 2

You might also like