Unit 4 - OS
Unit 4 - OS
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:
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
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.
9
Memory Management
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.
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
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
21
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.
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
1. No Internal Fragmentation
2. No Limitation on the size of the process
3. Degree of multiprogramming is dynamic
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
33
Memory Management
34
Memory Management
35
Memory Management
36
Memory Management
Worst Fit Algorithm
37
Memory Management
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 :
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.
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
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.
Optimal 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
Ans 5
Ans 6
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.
=0.9*(100+20) + (1-0.9)*(2*100+20)
=0.9*120+0.1*220=108+22= 130 ns
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
On an abstract level there are two types of localities which are as follows:-
• Temporal Localities.
• Spatial Localities.
84
Memory Management
85
Faculty Video Links, Youtube & NPTEL Video Links and Online
Courses Details
• 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