0% found this document useful (0 votes)
29 views86 pages

Unit 4 - OS

OPERATING SYSTEM NOTES UNIT - 4 AKTU (2023-24)

Uploaded by

waxaw90937
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)
29 views86 pages

Unit 4 - OS

OPERATING SYSTEM NOTES UNIT - 4 AKTU (2023-24)

Uploaded by

waxaw90937
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/ 86

RAJ KUMAR GOEL INSTITUTE OF TECHNOLOGY ,

Ghaziabad
Operating Systems
BCS-401

Unit: IV

Operating System

Mr. H.S.Tomer
B Tech :4th Sem
Assistant Professor
CSE(AI&ML)
1
Course Outcomes

Course outcome: After completion of this course students will be able to:

CO1 Understand the structure and functions of OS K1, K2

CO2 Understand the principles of concurrency and Deadlocks K2

CO3 Learn about Processes, Threads and Scheduling algorithms K1, K2

CO4 Learn various memory management scheme K2

CO5 Study I/O management and File systems. K2,K4


2
Course Outcome of Unit 4

After completion of this Unit students will be able to:-

• CO4: Learn various memory management scheme.

3
Memory Management
Memory Management:-
Memory is the important part of the computer that is used to store the data. Its
management is critical to the computer system because the amount of main memory
available in a computer system is very limited. At any time, many processes are
competing for it. Moreover, to increase performance, several processes are executed
simultaneously. For this, we must keep several processes in the main memory, so it is
even more important to manage them effectively.
 Memory management is a method in the operating system to manage operations
between main memory and disk during process execution.
 The main aim of memory management is to achieve efficient utilization of
memory. Memory management keeps track of the status of each memory location,
whether it is allocated or free.
 It allocates the memory dynamically to the programs at their request and frees it for
reuse when it is no longer needed.
4
Memory Management

Why Memory Management is Required?


• Allocate and de-allocate memory before and after process execution.
• To keep track of used memory space by processes.
• To minimize fragmentation issues.
• To proper utilization of main memory.
• To maintain data integrity while executing of process.

5
Memory Management
Logical address: A logical address, also known as a virtual address, is an address
generated by the CPU during program execution. It is the address seen by the
process and is relative to the program’s address space. The process accesses memory
using logical addresses, which are translated by the operating system into physical
addresses.
Physical address: A physical address is the actual address in main memory where
data is stored. It is a location in physical memory, as opposed to a virtual address.
Physical addresses are used by the memory management unit (MMU) to translate
logical addresses into physical addresses.

6
Memory Management

7
Memory Management

8
Memory Management
Address Binding:- The Address Binding refers to the mapping of computer
instructions and data to physical memory locations. Address binding helps allocate a
physical memory location to a logical pointer by associating a physical address to the
logical address or the virtual address.
It is a part of the computer memory management and is performed
by the operating system on the behalf of applications that need access to the memory.

Types of Address Binding in Operating System


There are mainly three types of an address binding in the OS. These are as follows:
• Compile Time Address Binding
• Load Time Address Binding
• Execution Time or Dynamic Address Binding

9
Memory Management

Compile Time Address Binding:


If the compiler is responsible of performing address binding then it is called as
compile time address binding. This type of address binding will be done before
loading the program into memory. The compiler required to interact with the
operating system memory manager to perform compile time address binding.
Load Time Address Binding: Loading the program into memory. Load time
address binding will be done by operating memory manager.
Execution Time or Dynamic Address Binding:
The address binding will be postponed even after loading the program into memory.
The program will keep on changing the locations in the memory till the time of
program execution. This type of Address binding will be done by the processor at the
time of program execution.

10
Memory Management

11
Memory Management
Execution Time Address
Compile Time Address Binding Load Time Address Binding
Binding
Compiler is responsible for the Loader is responsible for the Processor is responsible for the
compile time address binding. load time address binding. load time address binding.

It generates logical address It generates dynamic absolute


It generates physical address.
(virtual address). address.(Physical address)
Done before loading the program Done after loading the program Done at the time of program
into memory. into memory. execution.
Instructions are loaded in From memory instructions are
Code is compiled here.
memory. executed by CPU.
It is static address binding. It is static address binding. It is dynamic address binding.
Compiler interacts with operating
It is done by operating system It is done by processor at the
system memory manager to
memory manager itself. time of program execution.12
perform it.
Memory Management
Memory Management Schemes:- It is classified as follows:-

13
Memory Management
Contiguous memory allocation it is a memory management technique, whenever
there is a request by the user process for the memory then a single section of the one
after the other memory block is given to that process according to its requirement.
1.Bare Machine: -
Bare machine is logical hardware which is used to execute the program in the
processor without using the operating system
Initially, when the operating systems are not developed, the execution
of an instruction is done by directly on hardware without using any interfering
hardware. It accepts the instruction in only machine language, due to this those person
who has sufficient knowledge about Computer field are able to operate a computer. so
after the development of the operating system Bare machine is referred to as
inefficient.

14
Memory Management
2.Resident Monitor: The resident monitor works
like an operating system that controls the
instructions and performs all necessary functions.
It also works like job sequencer because it also
sequences the job and sends them to the
processor.
After scheduling the job Resident
monitors loads the programs one by one into the
main memory according to their sequences. One
most important factor about the resident monitor
is that when the program execution occurred
there is no gap between the program execution
and the processing is going to be faster.
15
Memory Management

The above two approaches of memory management are used when their is single user
and single CPU and one process exist in the main memory at a time.
To improve the performance of CPU the concept of Multiprogramming was
introduced in which two or more than two process exists in the main memory
simultaneously. In multiprogramming we use :-
1. Fixed partition Allocation
2. Variable partition Algorithm.

16
Memory Management

3. Fixed Partition Scheme:-Each process in this method of contiguous memory


allocation is given a fixed size continuous block in the main memory. This
means that the entire memory will be partitioned into continuous blocks of
fixed size, and each time a process enters the system, it will be given one of the
available blocks. Because each process receives a block of memory space that
is the same size, regardless of the size of the process. Static partitioning is
another name for this approach.

17
Memory Management

18
Memory Management
Advantages:-
• This strategy is easy to employ because each block is the same size. Now all that is left to
do is allocate processes to the fixed memory blocks that have been divided up.
• It is simple to keep track of how many memory blocks are still available, which
determines how many further processes can be allocated memory.
• This approach can be used in a system that requires multiprogramming since numerous
processes can be maintained in memory at once.
Disadvantages:-
• We won't be able to allocate space to a process whose size exceeds the block since the size
of the blocks is fixed.
• The amount of multiprogramming is determined by block size, and only as many processes
can run simultaneously in memory as there are available blocks.
•Internal Fragmentation
Suppose the size of the process is lesser than the size of the partition in that case some size
of the partition gets wasted and remains unused. This wastage inside the memory is
19
generally termed as Internal fragmentation
Memory Management
Fragmentation :- Fragmentation is an unwanted problem where the memory blocks cannot
be allocated to the processes due to their small size and the blocks remain unused. It can also
be understood as when the processes are loaded and removed from the memory they create
free space or hole in the memory and these small blocks cannot be allocated to new upcoming
processes and results in inefficient use of memory
Types of Fragmentation:-There are mainly two types of fragmentation in the operating
system. These are as follows:
1. Internal Fragmentation
2. External Fragmentation

20
Memory Management

1. Internal Fragmentation:- When a process is allocated to a memory block, and if the


process is smaller than the amount of memory requested, a free space is created in the given
memory block. Due to this, the free space of the memory block is unused, which cause
Internal fragmentation.

How to resolve Internal Fragmentation?


The problem of internal fragmentation may arise due to the fixed sizes of the
memory blocks. It may be solved by assigning space to the process via dynamic
partitioning. Dynamic partitioning allocates only the amount of space requested by the
process. As a result, there is no internal fragmentation.

21
Memory Management

Example: Suppose there is fixed


partitioning (i.e. the memory blocks are of
fixed sizes) is used for memory allocation
in RAM. These sizes are 2MB, 4MB, 4MB,
8MB. Some part of this RAM is occupied
by the Operating System (OS).
Now, suppose a process P1 of
size 3MB comes and it gets memory block
of size 4MB. So, the 1MB that is free in
this block is wasted and this space can’t be
utilized for allocating memory to some
other process. This is called internal
fragmentation.
22
Memory Management

2. External Fragmentation :-
External fragmentation happens when a dynamic memory allocation method allocates some
memory but leaves a small amount of memory unusable. The quantity of available memory is
substantially reduced if there is too much external fragmentation. There is enough memory
space to complete a request, but it is not contiguous. It's known as external fragmentation.

How to remove external fragmentation?


This problem occurs when you allocate RAM to processes continuously. It is done in
paging and segmentation, where memory is allocated to processes non-contiguously. As a
result, if you remove this condition, external fragmentation may be decreased.
Compaction is another method for removing external fragmentation. External
fragmentation may be decreased when dynamic partitioning is used for memory allocation by
combining all free memory into a single large block. The larger memory block is used to
allocate space based on the requirements of the new processes. This method is also known as
defragmentation. 23
Memory Management
Example: Suppose in the above example, if three
new processes P2, P3, and P4 come of sizes 2MB,
3MB, and 6MB respectively. Now, these processes
get memory blocks of size 2MB, 4MB and 8MB
respectively allocated.
So, now if we closely analyze this situation then
process P3 (unused 1MB)and P4(unused 2MB) are
again causing internal fragmentation. So, a total of
4MB (1MB (due to process P1) + 1MB (due to
process P3) + 2MB (due to process P4)) is unused
due to internal fragmentation.
Now, suppose a new process of 4 MB comes.
Though we have a total space of 4MB still we
can’t allocate this memory to the process. This is
called external fragmentation.
24
Memory Management

S.NO Internal Fragmentation External Fragmentation


1. If the process is larger than the If the process is removed, then external
memory, then internal fragmentation fragmentation occurs.
occurs.
2. Fixed-sized memory blocks are Variable-sized memory blocks are designated
designated for internal fragmentation. for external fragmentation.

3. Internal fragmentation happens when External fragmentation happens when


memory is split into fixed-sized memory is split into variable size
distributions. distributions.
4. The best-fit block is the solution to Paging, compaction, and segmentation are
internal fragmentation. solutions to external fragmentation.
25
Memory Management
Partition Description Table:- In both fixed and dynamic memory allocation schemes,
the operating system must keep list of each memory location which are free and which
are busy as new jobs come into the system, the free partitions must be allocated it is
called Partition Description Table. A data structure used to keep track of their
status(free or allocated),partition size and partition base address.
To select a specific partition for a given process we can use :-
1. First Fit Allocation algorithm
2. Best Fit Allocation algorithm

26
Process Synchronization
1. First Fit allocation algorithm :- First-Fit Allocation is a memory allocation technique
used in operating systems to allocate memory to a process. In First-Fit, the operating system
searches through the list of free blocks of memory, starting from the beginning of the list,
until it finds a block that is large enough to accommodate the memory request from the
process. Once a suitable block is found, the operating system splits the block into two parts:
the portion that will be allocated to the process, and the remaining free block.
Advantages of First-Fit Allocation in Operating Systems:
•Simple and efficient search algorithm
•Minimizes memory fragmentation
•Fast allocation of memory
Disadvantages of First-Fit Allocation in Operating Systems:
•Poor performance in highly fragmented memory
•May lead to poor memory utilization
•May allocate larger blocks of memory than required.

27
Memory Management
2. Best-Fit allocation algorithm:- The operating system maintains a list of all free memory
blocks available in the system. When a process requests memory, the operating system
searches the list for the smallest free block of memory that is large enough to accommodate
the process. If a suitable block is found, the process is allocated memory from that block. If
no suitable block is found, the operating system can either wait until a suitable block
becomes available or request additional memory from the system.
Advantages of Best-Fit Allocation :
• Memory Efficient. The operating system allocates the job minimum possible space in the
memory, making memory management very efficient.
• Reduced memory fragmentation
• Minimizes external fragmentation
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.
• Increased computational overhead
• May lead to increased internal fragmentation 28
Memory Management

29
Memory Management
4. Dynamic Partitioning Allocation :-
Dynamic partitioning tries to overcome the
problems caused by fixed partitioning. In
this technique, the partition size is not
declared initially. It is declared at the time of
process loading.
The first partition is reserved for the
operating system. The remaining space is
divided into parts. The size of each partition
will be equal to the size of the process. The
partition size varies according to the need of
the process so that the internal
fragmentation can be avoided.

30
Memory Management

Advantages of Dynamic Partitioning over fixed partitioning :-

1. No Internal Fragmentation
2. No Limitation on the size of the process
3. Degree of multiprogramming is dynamic

Disadvantages of dynamic partitioning:-


1. External Fragmentation
2. Complex Memory Allocation

31
Memory Management
To select a specific partition for a given process we can use :-
1. First Fit Allocation algorithm
2. Best Fit Allocation algorithm
3. Worst Fit Allocation algorithm

3. Worst Fit Allocation algorithm :- In this allocation technique, the process traverses
The whole memory and always search for the largest hole/partition, and 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.
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 32
Memory Management

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.

33
Memory Management

34
Memory Management

35
Memory Management

36
Memory Management
Worst Fit Algorithm

37
Memory Management

Non-Contiguous Allocation :- in this allocation method a process can acquire


several memory blocks at different locations in the memory according to its need. The
available free space is distributed here and there, unlike contiguous memory allocation,
where all the free space is allocated in one place.
Advantages to Non-Contiguous Allocation.
• It reduces internal fragmentation since memory blocks can be allocated as needed,
regardless of their physical location.
• It allows processes to be allocated memory in a more flexible and efficient manner since
the operating system can allocate memory to a process wherever free memory is available.
Disadvantages to Non-Contiguous Allocation.
• It can lead to external fragmentation, where the available memory is broken into small,
non-contiguous blocks, making it difficult to allocate large blocks of memory to a process.
• The use of pointers to link memory blocks can introduce additional overhead, leading to
slower memory allocation and deallocation.
38
Memory Management
Paging :- It is a memory management scheme that eliminates the need for contiguous
allocation of physical memory. The process of retrieving processes in the form of
pages from the secondary storage into the main memory is known as paging. This
scheme permits the physical address space of a process to be non – contiguous.

Physical Memory is divided into fixed size blocks called frames


Logical memory is divided into fixed sized blocks called Pages.
The size of a page is same as that of frame.

In paging the pages of a process is placed or mapped into the available frames of
memory. Whenever this process is to be executed.

39
Memory Management
Logical Address is divided into: -. n
Page No: - Specify which page is considered. Page No. Offset
Offset: - Specify the location of an instruction within a Particular Page
n-d d
Physical Address is divided into: - m
Frame No: - Specify the frame to be considered. Frame No. Offset
Offset: - Specify the location of an instruction within a particular frame
m-d d
Let the size of Logical Address space =2n
Physical Address space =2m
Size of a Page =2d
No. of Frames= Size of Physical memory No. of Pages= Size of Logical memory
Size of each page Size of each page

40
Memory Management

41
Memory Management

42
Memory Management

43
Memory Management

44
Memory Management
Hardware Support: - Each O.S has its own method for storing page table. Most of them
allocated a page table for each process and a point to page table is stored in PCB.
Ways of Hardware Implementation of the page table: -
1. Page table is implemented as a set of dedicated registers. These registers are built
with high speed logic to make page translation efficient. But this is used when the
page table is reasonably small.
2. Page table is placed in the main memory. The Physical address of the page table is
held in a register [Page Table Base Register (PTBR)]. In this the page number of the
logical address is added to this register to find the page table entry in Physical
memory.
Advantage: - Switching among process is easy.
Disadvantage: - Every memory reference generated by the CPU register have two
trips to Memory.
45
Memory Management
3. Use of a special, small, fast look up hardware cache called Translation Look – aside
Buffer (TLB). This TLB is an associative memory (search is by content, rather than
address).
TLB has:-Two parts tag and value.
Contain only a free of the page table entry.
TLB Working: - when a Logical address is generated by the CPU then its page
number is presented in TLB. If page number is found in TLB then it is called TLB hit
[Frame no. is immediately available to access Physical Memory]. If the page is not
found it is called TLB Miss [then memory reference to page table is made] and the
same will be updated to TLB for next quick reference.

46
Memory Management

47
Memory Management
Advantages :

 Eliminates external fragmentation


 Pages are mapped appropriately anyway
 Allows demand paging and pre-paging
 More efficient swapping
 No need for considerations about fragmentation
Disadvantages :
 Longer memory access times (page table lookup)
 Can be improved using TLB
 Memory requirements (one entry per VM page)
 Internal fragmentation

48
Memory Management
Segmentation: - It is a technique for the noncontiguous storage allocation. In this the logical
memory is divided into independent address space of valuable size called segment.
• The general division can be: Main program, Set of subroutines. Functions and set of data
structure (stack, quay etc).
• Each segment has a name and length which is loaded into Physical memory as it is for
Simplicity the segments are referred by a segment number, rather than segment name.
• Logical Address has Segment Name, Offset.
Hardware Support in Segmentation: -
To map a logical address to physical address a segment table is used. The segment table
has two fields.
Segment Base (S): - It contains the starting physical address where the segment resides in
memory.
Segment Size(d): - It specifies the size of segment

49
Memory Management
• The segments of a program can be placed anywhere in the main memory.
• For each process in each execution, there is one segment address table.
• Segment-table base register (STBR) points to the segment table’s location in memory
• Segment-table length register (STLR) indicates number of segments used by a program

Address Mapping: -
• The Logical address consists of two parts S and D
• If offset is not between 0 to segment size – O.S. generates error message.
• If offset value is legal then it is added to the segment base to produce the address in
the physical memory of the desired byte.

50
Memory Management

51
Memory Management
S.NO Paging Segmentation
In paging, the program is divided into fixed size In segmentation, the program is divided into
1
pages. variable size sections.
2 For the paging operating system is accountable. For segmentation compiler is accountable.

3 It is faster in comparison to segmentation. Segmentation is slow.


Segmentation could result in external
4 Paging could result in internal fragmentation.
fragmentation.
While segmentation also comprises the segment
Paging comprises a page table that encloses the
5 table which encloses segment number and segment
base address of every page.
offset.
In paging, the processor needs the page number, In segmentation, the processor uses segment
6
and offset to calculate the absolute address. number, and offset to calculate the full address.
The size of the page needs always be equal to the
7 There is no constraint on the size of segments.
size of frames.
8 Paging results in a less efficient system. Segmentation results in a more efficient system.
52
Memory Management
Advantages :
 No internal fragmentation
 Average segment size is larger than the actual page size.
 It is easier to relocate segments than entire address space.
 The segment table is of lesser size as compared to the page table in paging.
 Less overhead.
Disadvantages :
 It can have external fragmentation.
 Costly memory management algorithms.
 It is difficult to allocate contiguous memory to variable sized partition.
 Segments of unequal size not suited well for swapping.

53
Memory Management
Segmentation with Paging: - This approach divided the virtual memory into segments
of different size but each segment is divided into fixed size pages.
•Segment able and page table both are used for mapping the logical address to
corresponding Physical address.
•Each Virtual address is divided into 3 fields.
S = Segment No.
P = Page No. S P D
D = Offset within page

54
Memory Management

CPU generates a logical address consisting of three parts-


 Segment Number
 Page Number
 Page Offset

55
Memory Management

56
Memory Management
Address Mapping: -
1. The System has a pail of register
STLR [Limit Register]
STBR [Base Register]
2. The value of segment number (S) is checked with the value of Limit Register.
If < Error
If > (S + value of Limit Register) to form the index of the segment table.
3. An entry of segment table is chosen if
Size > P Error [segment size violation]
Size < P Check Access -- No -- Error
Yes -- Base (b) of chosen Entry is added with page no (P) to
result the index of Page Table.
4. Select the Frame no. of Page table.
5. Offset (d) is added in frame no. to access actual Physical address.
57
Memory Management

58
Memory Management

59
Memory Management
Virtual Memory: - It is a technique that allows the execution of a processes that may not be
completely in memory.
To execute any process or instruction it is required to place the entire logical
address space in physical memory. It has also been observed that in many cases, the entire
program is not needed for instance
• Programs often have code to handle unusual ever conditions.
• Arrays, lists and tables are often allocated more memory than they actually need.
• Contain options and features of a program may be used rarely.
Even in cases where the entire program is needed, it may not all be needed at the
same time, thus gives many benefits like:-
1. It feels programmers from the concerns of memory – storage limitations.
2. As each program could take less Physical memory, more programs could be run at the same
time with a corresponding increase in CPU utilization and throughput, nut with no increase
in response time or turnaround time.
3. Less I/O could be needed to load or swap each user program into memory so each user
60
program could run faster.
Memory Management
Virtual Memory is implemented by
•Demand Paging
•Demand Segmentation
•Demand Paged Segmentation
Demand Paging: - It is a combination of paging and swapping. In which a page is brought into
the memory for its execution only when it is demanded, otherwise it is remained in backing
storage (dish).
For swapping a page a lazy swapper is used, it never swaps a page into memory unless
that page will be needed.
As we are viewing a process as a sequence of pages, rather than as one large contiguous
address space, thus we use page, rather than swapper, in connection with demand paging.

61
Memory Management
Working: - When a process is to be swapped in, the page guesses which pages will be used
before the process is swapped out again. Instead of swapping in a whole process, the pages
brings only those necessary pages into memory
• Thus avoid reading into memory pages not to be used.
• Decreasing the swap time and
• Amount of physical memory needed.
To distinguish between the pages that all in main memory and in dish a valid – invalid but is
in the table.
If bit is Valid: - the associated page is valid and in memory
Invalid: - the associated page is invalid or on disk.
While process execution and accesses pages that all memory resident, execution process is
normal. But if the process tries to access a page that was not brought into memory it is called
page fault and the page need to be brought from disk and produce is

62
Memory Management

1. Check an internal table [PCB] to determine whether the reference was a valid or an invalid
memory access.
2. If invalid reference = Process terminates
Valid reference = Need to bring page from disk.
3. Find a few frames [Page Replacement Alga].
4. Schedule disk operation to read the desired page into newly allocated frame.
5. When disk read is completed, modify internal table, [PCB] and Page table.
6. Restart the instruction that was interrupted by the trap.

63
Memory Management

64
Memory Management
Page Replacement: - If CPU is looking for a page and that page is not found in main memory it
is called page fault, then this page need to be brought to memory for the execution of the process
for this we may need to replace a page from main memory to backup storage this is called Page
Replacement.
Page Replacement is done by swapping if there are free frames in the memory then swap
in the required page into the frame which is free. If there is no free frame then 1st finds the frame
which is not currently in use. The content of this frame is swapped out from memory to backup
storage. Then bring the required page in the frame which is now free.
Steps of Page fault service routine for Page Replacement: -
1. Find the location of the desire page on the disk.
2. Find a free frame
If there is a free frame, use it.
If there is no free frame, use a page replacement algorithm to select a victim frame.
3. Write the victim frame to the disk, change the page and frame tables accordingly.
4. Read the desired page into the newly free frame, change the page and frame tables.
5. Restart the uses process. 65
Memory Management
Page Replacement Algorithms: - There are various algorithm for page Replacement.
• Algorithm with lowest page fault rate is best.
• Algorithm is evaluated by running the particular algorithm on a string of memory reference and
computing page fault.
• The string of memory reference is called Reference String.
The Various Algorithms are: -
1. FIFO (First In First Out): -
The oldest page present is the memory is selected for replacement.

2. ORA (Optimal Replacement Algorithm):-


Page that will not be used for longest processed of time is replaced.
Future Knowledge of Reference string is required.

3. LRU (Least Recently Used): -


The page that has used for longest period of time is replaced.
66
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
First-In-First-Out (FIFO) Algorithm

Optimal Algorithm

Least Recently Used (LRU) Algorithm

67
Memory Management
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames . Find the
number of page faults.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frame. Find number of page fault.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4
page frames. Find number of page faults.
Example-4: Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
for a memory with three frames and calculate number of page faults by using FIFO (First
In First Out) Page replacement algorithms.
Example-5: Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 4, 0
for a memory with three frames and calculate number of page faults by using OPTIMAL
Page replacement algorithms.

68
Memory Management

Ans 1

69
Memory Management

Ans 2

70
Memory Management

Ans 3

71
Memory Management

Ans 4

Number of Page Hits = 8


Number of Page Faults = 12
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Hit Percentage = 8 *100 / 20 = 40%
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60% 72
Memory Management

Ans 5

Number of Page Hits = 8


Number of Page Faults = 12
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Hit Percentage = 8 *100 / 20 = 40%
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%
73
Memory Management

Ans 6

Number of Page Hits = 7


Number of Page Faults = 13
The Ratio of Page Hit to the Page Fault = 7 : 12 - - - > 0.5833 : 1
The Page Hit Percentage = 7 * 100 / 20 = 35%
74
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 35 = 65%
Memory Management

75
Memory Management

76
Memory Management

77
Memory Management

• Local replacement – each process selects from only its own set of allocated
frames
– More consistent per-process performance
– But possibly underutilized memory

• Global replacement – process selects a replacement frame from the set of all
frames; one process can take a frame from another
– But then process execution time can vary greatly
– But greater throughput so more common

78
Memory Management
The performance of cache memory is frequently measured in terms of a quantity called
hit ratio. When the CPU refers to memory and finds the word in the 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.
Hit ratio = Number of hits
Number of hits + Number of miss.
Let t is cache-access time(TLB)
H is the hit ratio
T is the main memory access time.
Then, the
Average Access Time = H*(T + t) + (1-H) * (2*T + t)
The hit ratio h always lies in the closed interval of 0 and 1.
79
Memory Management

Topic objective: To compute the effective memory access time of cache memory.

TLB Hit ratio=0.9

RAM access time T=100 ns and TLB access time t=20 ns

Effective access time without TLB= 2T= 200 ns

Effective access time with TLB

=H*(T + t) + (1-H) * (2*T + t)

=0.9*(100+20) + (1-0.9)*(2*100+20)

=0.9*120+0.1*220=108+22= 130 ns

Reduction in effective access time=(200-130)*100/200=35%


80
Memory Management

THRASHING: - It is a situation in which lot of computer energy (time) is wasted in


moving pages from primary to secondary storage or vice-versa and accomplishes very little
useful results.
•A process is in thrashing if is spend more time in paging instead of their execution.

Cause of Thrashing: - As O.S monitor CPU utilization if it goes too low. O.S increases
the degree of multiprogramming by introducing a new process into the system.
As global page replacement is used with no regard to page belongs to any process. So if a
process requires more frames then it starts faulting and taking pages away from other
processes. These processes needs these pages and in and out and so on. So CPU utilization
decreases. [Device Ques up for paging ready Que Empty]

81
Memory Management
CPU scheduler sees (decreasing CPU
utilization) increase the degree of
multiprogramming as a result new process twist
to get started by taking frames from running
process causing page faults and so on. So now
the situation comes where thrashing will take
place and CPU utilization is dropped and page
fault rate is increased tremendously.
•At this point to increase CPU utilization and
stop thrashing degree of multiprogramming
should be reduced.
•The effects of thrashing can be limited by
using a local/priority Replacement Algo i.e.
when one process starts thrashing it cannot
steal frames from another process and cause the
latter to also thrash. 82
Memory Management
Measures take to Avoid Thrashing: - To avoid thrashing a strategy called Page-Fault
Frequency (PFF) is used.
• If page fault rate is too high = process needs more frames.
• If page fault rate is too low = process has too many frames.

•Using page – fault frequency upper bounds and lower bounds on the desired page fault rate
is established.
If actual page fault rate exceeds upper limit = allocate that process to another frame.
If actual page fault rate falls below lower limit = reference a frame from that process.

Thus, we can directly measure and control the page fault rate to present thrashing.

83
Memory Management

Locality of Reference: - The Virtual Address generated by a program in execution are


nonrandom but behave in a somewhat predictable manner, due to looking, sequential &
blocked formatted control structure inherent in the grouping of instruction. This property is
refused as Locality of Reference.
 Locality of Reference describe the fact that over on internal of virtual time, the virtual
addresses generated by a typical program tends to be restarted to small set of its name
space.
 Locality of reference refers to the tendency of the computer program to access the same
set of memory locations for a particular time period. The property of Locality of Reference
is mainly shown by loops and subroutine calls in a program.

On an abstract level there are two types of localities which are as follows:-
• Temporal Localities.
• Spatial Localities.
84
Memory Management

• Temporal Localities:-This type of optimization includes bringing in the frequently


accessed memory references to a nearby memory location for a short duration of time
so that the future accesses are much faster.

• Spatial Localities:-This type of optimization assumes that if a memory location has


been accessed it is highly likely that a nearby/consecutive memory location will be
accessed as well and hence we bring in the nearby memory references too in a nearby
memory location for faster access.

85
Faculty Video Links, Youtube & NPTEL Video Links and Online
Courses Details

Youtube/other Video Links

• https://www.youtube.com/watch?v=aKmuGwHj3Cw
• https://www.youtube.com/playlist?list=PLmXKhU9FNesSFvj6gASuWmQd23Ul5omtD
• https://www.youtube.com/watch?v=Fizc0nXRm2g
• https://www.youtube.com/watch?v=RgYU5r9A5TU
• https://nptel.ac.in/courses/106108101

10 June 2024 Nisha ACSE0403A OS Unit 5 86

You might also like