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

Os Module 3

Uploaded by

shihankv807
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 views29 pages

Os Module 3

Uploaded by

shihankv807
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/ 29

1

MODULE 3
MEMORY MANAGEMENT

SYLLABUS:
Memory management - Different address bindings – compile, link and run time
bindings. - Difference between logical address and physical address - Contiguous
memory allocation – fixed partition and variable partition – Allocation Strategies
- first fit, best fit and worst fit - Define fragmentation – internal and external, and
suggest solutions - Paging and paging hardware - Segmentation, and the
advantages of segmentation over paging

Concept of virtual memory - Demand paging - Page-faults and how to handle page
faults. - Page replacement algorithms: FIFO, optimal, LRU, LRU Approximation,
Counting based (LFU and MFU) - Learn the concept of thrashing.

MEMORY MANAGEMENT
 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.
ADDRESS BINDINGS
Address binding is the process of mapping from one address space to another
address space. Logical address is address generated by CPU during execution
whereas Physical Address refers to location in memory unit(the one that is loaded
into memory).
Compile Time - If it is known at compile time where a program will reside in
physical memory, then absolute code can be generated by the compiler, containing
actual physical addresses. However if the load address changes at some later time,
then the program will have to be recompiled.
Load time – If the location at which a program will be loaded is not known at
compile time, then the compiler must generate relocatable code, which references
addresses relative to the start of the program. If the starting address changes, then
the program must be reloaded but not recompiled.
2

Execution Time - If a program can be moved around in memory during the course
of its execution, then binding must be delayed until execution time. This requires
special hardware.

 Program goes through several steps before being executed.


 Address represented in different ways
 Address in source program is symbolic (variable name).
 Compiler binds these symbolic address to relocatable address (offset).
 Linkage editor or loader binds the relocatable address to absolute address
(physical address).
 Binding of instruction and data to memory address can done at compile time,
load time or execution time.
LOGICAL ADDRESS AND PHYSICAL ADDRESS
 Address uniquely identifies a location in the memory.
 We have two types of addresses that are logical address and physical address.
 The logical address is a virtual address and can be viewed by the user.
 The user can’t view the physical address directly.
 The logical address is used like a reference, to access the physical address.
3

LOGICAL ADDRESS PHYSICAL ADDRESS

It is the virtual address generated by The physical address is a location in a


CPU memory unit.
Set of all logical addresses generated by Set of all physical addresses mapped to
CPU in reference to a program is the corresponding logical addresses is
referred as Logical Address Space. referred as Physical Address.
The user can view the logical address of The user can never view physical
a program. address of program
The user uses the logical address to The user can not directly access
access the physical address. physical address.
The Logical Address is generated by the Physical Address is Computed by
CPU MMU

 Logical and physical address are same in compile and load time address
binding scheme.
 Logical and physical address are different in execution time address binding
scheme.
4

CONTIGUOUS MEMORY ALLOCATION


 Assign a process in consecutive memory block (memory block having
consecutive address).
 When a process need to execute,
o It request memory
o Size of process compare with amount of contiguous main memory
available to execute the process.
o If sufficient memory found then process is allocated memory.
o Otherwise it added to queue until sufficient free contiguous memory is
available.

 This is implemented in os with the help of two registers.


 Base register
 Limit register
 Base register contain starting address of memory location.
 Limit register store the amount of byte consumed by the process.

Physical address = Base register address + logical address.

Contiguous memory allocation can be divided into:


1. Fixed (or static) partitioning
2. Variable (or dynamic) partitioning

FixedPartitioning:
 This is the oldest and simplest technique used to put more than one processes
in the main memory.
 In this partitioning, number of partitions (non-overlapping) in RAM are fixed
but size of each partition may or may not be same.
 As it is contiguous allocation, hence no spanning is allowed.
 Here partition are made before execution or during system configure.
5

 As illustrated in above figure, first process is only consuming 1MB out of


4MB in the main memory.
 i.e, Size of the partition > Size of the process
 so the remaining memory is unused. It can’t allocate for any other process
because each partition can hold at most one process.
 Hence it leads to Internal Fragmentation.
 From the figure,
 Sum of Internal Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)=
3+1+1+2 = 7MB
 Suppose process P5 of size 7MB comes. But this process cannot be
accommodated inspite of available free space because of contiguous
allocation (as spanning is not allowed). Hence, 7MB becomes part of External
Fragmentation.
Advantages
1. Easytoimplement:
Algorithms needed to implement Fixed Partitioning are easy to implement. It
simply requires putting a process into certain partition without focussing on the
emergence of Internal and External Fragmentation.
2. LittleOSoverhead:
Processing of Fixed Partitioning require lesser excess and indirect
computational power.
6

Disadvantages
1. InternalFragmentation:
Main memory use is inefficient. Any program, no matter how small, occupies
an entire partition. This can cause internal fragmentation.
2. ExternalFragmentation:
The total unused space (as stated above) of various partitions cannot be used to
load the processes even though there is space available but not in the contiguous
form (as spanning is not allowed).
3. Limitprocesssize:
Process of size greater than size of partition in Main Memory cannot be
accommodated. Partition size cannot be varied according to the size of
incoming process’s size. Hence, process size of 32MB in above stated example
is invalid.
4. LimitationonDegreeofMultiprogramming:
Partition in Main Memory are made before execution or during system
configure. Main Memory is divided into fixed number of partition.

VARIABLE PARTITION
 overcome the problems caused by fixed partitioning
 Os keep a table indicating which part of memory is available and which are
occupied.
 All memory available for process are considered as large block called hole.
 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.
7

Advantages of Variable Partitioning –


1. No Internal Fragmentation:
In variable Partitioning, space in main memory is allocated strictly according
to the need of process, hence there is no case of internal fragmentation. There
will be no unused space left in the partition.
2. No restriction on Degree of Multiprogramming:
More number of processes can be accommodated due to absence of internal
fragmentation. A process can be loaded until the memory is empty.
3. No Limitation on the size of the process:
In variable partitioning, the process size can’t be restricted since the partition
size is decided according to the process size.

Disadvantages of Variable Partitioning –


1. Difficult Implementation:
Implementing variable Partitioning is difficult as compared to Fixed
Partitioning as it involves allocation of memory during run-time rather than
during system configure.
2. External Fragmentation:
There will be external fragmentation inspite of absence of internal
fragmentation.
8

 For example, suppose in above example-


 process P1(2MB) and process P3(1MB) completed their execution.
 Hence two spaces are left i.e. 2MB and 1MB.
 Let’s suppose process P5 of size 3MB comes.
 The empty space in memory cannot be allocated as no spanning is
allowed in contiguous allocation.
 The rule says that process must be contiguously present in main memory
to get executed. Hence it results in External Fragmentation.

ALLOCATION STRATEGIES
 For both fixed and dynamic memory allocation schemes, the operating system
must keep list of each memory location noting which are free and which are
busy. Then as new jobs come into the system, the free partitions must be
allocated.
 First fit
 Best fit
 Worst fit
These are Contiguous memory allocation techniques.
FIRST FIT
 Allocate the first free partition or hole which is large enough that can
accommodate the process. It finishes after finding the first suitable free
partition.
9

Advantages
 It is fast in processing. As the processor allocates the nearest available
memory partition to the job, it is very fast in execution.

Disadvantages
 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.

BEST FIT
 Allocate the smallest free partition which meet the requirement of requesting
process.
 This algorithm first searches the entire list of free partitions and considers the
smallest hole that is adequate.
 It then tries to find a hole which is close to actual process size needed.

Advantages

 Memory utilization is much better than first fit as it searches the smallest free
partition first available.

Disadvantage

It is slower and may even tend to fill up memory with tiny useless holes.
WORST FIT
 In worst fit approach is to locate largest available free portion so that the
portion left will be big enough to be useful.
 It is the reverse of best fit.

Advantage

 Reduces the rate of production of small gaps.

Disadvantage

 If a process requiring larger memory arrives at a later stage then it cannot be


accommodated as the largest hole is already split and occupied.
10

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
 Internal fragmentation
 External fragmentation
Internal fragmentation
 Memory block assigned to process is bigger. Some portion of memory is left
unused, as it cannot be used by another process.

External fragmentation
 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.
11

 In above diagram, we can see that, there is enough space (55 KB) to run a
process-07 (required 50 KB) but the memory (fragment) is not contiguous.
 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.
PAGING AND PAGING HARDWARE
 In Operating Systems, Paging is a storage mechanism used to retrieve
processes from the secondary storage into the main memory in the form of
pages.
 The main idea behind the paging is to divide each process in the form of
pages. The main memory will also be divided in the form of frames.
 One page of the process is to be stored in one of the frames of the memory.
The pages can be stored at the different locations of the memory but the
priority is always to find the contiguous frames or holes.
 Pages of the process are brought into the main memory only when they are
required otherwise they reside in the secondary storage.
 Different operating system defines different frame sizes.
 The sizes of each frame must be equal.
 Considering the fact that the pages are mapped to the frames in Paging, page
size needs to be as same as frame size.
12

Example

 Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore
the main memory will be divided into the collection of 16 frames of 1 KB
each.
 There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each.
Each process is divided into pages of 1 KB each so that one page can be stored
in one frame.
 Initially, all the frames are empty therefore pages of the processes will get
stored in the contiguous way.
13

 Let us consider that, P2 and P4 are moved to waiting state after some time.
Now, 8 frames become empty and therefore other pages can be loaded in that
empty place. The process P5 of size 8 KB (8 pages) is waiting inside the ready
queue.
 Given the fact that, we have 8 non contiguous frames available in the memory
and paging provides the flexibility of storing the process at the different
places. Therefore, we can load the pages of process P5 in the place of P2 and
P4.
14

 The purpose of Memory Management Unit (MMU) is to convert the logical


address into the physical address.
 The logical address is the address generated by the CPU for every page while
the physical address is the actual address of the frame where each page will
be stored.
 When a page is to be accessed by the CPU by using the logical address, the
operating system needs to obtain the physical address to access that page
physically.
 The logical address has two parts.
1) Page Number
2) Offset
 Memory management unit of OS needs to convert the page number to the
frame number.

Address Translation

Page address is called logical address and represented by page number and
the offset.

Logical Address = Page number + page offset

Frame address is called physical address and represented by a frame number


and the offset
Physical Address = Frame number + page offset
15

PAGING HARDWARE
 A data structure called page map table is used to keep track of the relation
between a page of a process to a frame in physical memory.

 When the system allocates a frame to any page, it translates this logical
address into a physical address and create entry into the page table to be used
throughout execution of the program.
 When a process is to be executed, its corresponding pages are loaded into any
available memory frames.
 Suppose you have a program of 8Kb but your memory can accommodate
only 5Kb at a given point in time, then the paging concept will come into
picture.
 When a computer runs out of RAM, the operating system (OS) will move idle
or unwanted pages of memory to secondary memory to free up RAM for other
processes and brings them back when needed by the program.
 This process continues during the whole execution of the program where the
OS keeps removing idle pages from the main memory and write them onto
the secondary memory and bring them back when required by the program.
16

Advantages

 Paging reduces external fragmentation, but still suffer from internal


fragmentation.
 Paging is simple to implement and assumed as an efficient memory
management technique.
 Due to equal size of the pages and frames, swapping becomes very easy.
Disadvantages
 Page table requires extra memory space, so may not be good for a system
having small RAM.
 Size of Page table can be very big and therefore it wastes main memory.
 CPU will take more time to read a single word from the main memory

To access a particular page from the physical memory always refer page map table.
 Page accessing speed depends upon page map table accessing speed.
 There are some page they are referred very frequently and as will required
another hardware use called translation look aside buffer.
 A Translation look aside buffer can be defined as a memory cache which can
be used to reduce the time taken to access the page table again and again.
 It is a memory cache which is closer to the CPU and the time taken by CPU
to access TLB is lesser then that taken to access main memory.
17

 TLB hit is a condition where the desired entry is found in translation look
aside buffer. If this happens then the CPU simply access the actual location in
the main memory.
 If the entry is not found in TLB (TLB miss) then CPU has to access page table
in the main memory and then access the actual frame in the main memory.
 Therefore, in the case of TLB hit, the effective access time will be lesser as
compare to the case of TLB miss.
Advantages
 TLB reduces the effective access time.
 Only one memory access is required when TLB hit occurs.
Disadvantages
 TLB can hold the data of only one process at a time.
 When context switches occur frequently, the performance of TLB degrades
due to low hit ratio.
 As it is a special hardware, it involves additional cost.

SEGMENTATION
 Segmentation is a memory management technique which supports user's
view of memory. This technique of division of a computer's primary memory
into sections called segments.
 In a computer system using segmentation, a logical address space can be
viewed as multiple segments
 The size of the segment may grow or shrink that is it is of variable length.
 During execution, each segment has a name and a length.
 The address specifies both the segment name and the displacement within the
segment.
 The user, therefore, specifies each address by two quantities; segment name
and an offset.
 Normally it is implemented as segments are numbered and are referred to by
a segment number, in place of a segment name. Thus a logical address consists
of two tuples:

< segment – number, offset >


18

 Segment number(s) – It is the total number of bits required to


represent the segment.
 Segment Offset(d) – It specifies the number of bits required to
represent the size of the segment.

 The details about each segment are stored in a table called as segment table.
 Segment Table – It maps two-dimensional Logical address into one-
dimensional Physical address. It’s each table entry has:
 Base Address: It contains the starting physical address where
the segments reside in memory.
 Limit: It specifies the length of the segment.

 The logical address consists of two parts: a segment number (s) and an offset
(d) into that segment.
 The segment number used as an index into the segment table.
 The offset d of the logical address must be between 0 and the segment limit.
 If offset is beyond the end of the segment, we trap the Operating System.
 If offset is in the limit, then it is combined with the segment base to produce
the address in physical memory, hence the segment table is an array of base
limit and register pairs.
19

Example

Advantages of Segmentation –
 No Internal fragmentation.
 Segment Table consumes less space in comparison to Page table in paging.

Disadvantage of Segmentation

 At the time of swapping, processes are loaded and removed from the main
memory, then the free memory space is broken into small pieces, cause of this
occurs External fragmentation.

ADVANTAGES OF SEGMENTATION OVER PAGING

 Speed. Reloading segment registers to change address spaces is much faster


than switching page tables.
 Segment descriptor tables consume less memory than page tables.
 In segmentation, Procedure and data be distinguished and protected
separately. on the other hand, procedure data can not be distinguished and
protected separately .
20

 In segmentation, changing in data or procedure requires compiling only


affected segment not entire program but in paging, change in data or
procedure requires compiling entire program.

 In segmentation, sharing of different procedures are available but in paging,


sharing of different procedures are not available.

Paging v/s segmentation

PAGING SEGMENTATION

A page is of fixed block size. A segment is of variable size.


Paging may lead to internal Segmentation may lead to external
fragmentation fragmentation.

The user specified address is divided by The user specifies each address by two
CPU into a page number and offset quantities a segment number and the
offset (Segment limit).
The hardware decides the page size. The segment size is specified by the
user.
Paging involves a page table that Segmentation involves the segment
contains base address of each page. table that contains segment number and
offset (segment length).

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

 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.
 It allow the execution of process that are not completely in memory.

Advantages of Virtual Memory

1. The degree of Multiprogramming will be increased.


2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.

Disadvantages of Virtual Memory

1. The system becomes slower since swapping takes time.


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

 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.
22

DEMAND PAGING

 The process of loading the page into memory on demand (whenever page fault
occurs) is known as 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

 Large virtual memory.


 More efficient use of memory.
 There is no limit on degree of multiprogramming.
23

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.
PAGE FAULT
 A page fault occurs when a program attempts to access data or code that is in
its address space, but is not currently located in the system RAM.
HANDLE PAGE FAULT

Steps to handle page fault


1) OS looks PCB or internal table
2) Is there is any invalid reference ,it abort the process
If it is not present in memory it load the page from secondary memory and
store in main memory
3) Find the empty frame or free frame from physical memory
4) Schedule a disk operation to read the desired page into the newly allocated
frame
5) Modify the internal table kept with the process and page table to indicate the
page is now in memory
6) Restart the instruction that was interrupt by the trap. Process can access the
page.
24

 Suppose we want to execute the instruction load M


 It map to page map table in which the page contain M is invalid(i), it issue a
trap.
 Trap: highest priority non maskable interrupt, means it can’t be mask /ignored.
 Problem can be solved :page is in backing store(disk)
 Bring the missing page into memory.
 Page is now in memory: update it’s table entry
 Restart the instructionto execute it properly.
PAGE REPLACEMENT ALGORITHMS
 In case of page fault, Operating System might 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.
Different page replacement algorithms are:
 FIFO (First In First Out)
 LRU (Least Recently Used)
 Optimal
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.
25

Example: Consider page reference string 1, 3, 0, 3, 5, 6 ,3with 3 page frames.

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.
Optimal
 In this algorithm, pages are replaced which would not be used for the longest
duration of time in the future.
Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4
page frame.
26

LRU –

 In this algorithm page will be replaced which is least recently used.


Example: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2,3 with
4 page frames.

LRU APPROXIMATION
 LRU Page replacement need sufficient hardware support.
 Some system may not provide hardware support and other page replacement
algorithm must be used.
 Many system provide some help in the form of reference bit
 Reference bit for a page is set by hardware whenever that page is referenced.
 Initially all bits are cleared to 0s.
 With each page referred is set to 1 by hardware.
SECOND CHANCE ALGORITHM
 Actually a FIFO replacement algorithm, with small modification that cause
it to approximate LRU
 When a page is selected according to FIFO order, we check it reference bit.
 If it is set 1,we clear it and look for another page.
 Also called clock algorithm.
27

 In the worst case when all bit are set the pointer cycles through all the pages
giving each page a second chance and clearing its bit.

COUNTING BASED PAGE REPLACEMENT ALGORITHM


 In this method, it keep a counter of the number of reference that have been
made to each page.
LEAST FREQUENTLY USED (LFU)
Page with smallest count is replaced.
MOST FREQUENTLY USED (MFU)
Page with highest count is replaced.

THRASHING
 A process that is spending more time paging than executing is said to be
thrashing.
 In other words it means, that the process doesn't have enough frames to hold
all the pages for its execution, so it is swapping pages in and out very
frequently to keep executing.
 Sometimes, the pages which will be required in the near future have to be
swapped out.
 If a process doesn’t have enough pages, page fault rate is very high, it leads
to
 Low cpu utilization
 OS think that is need to increase the degree of
multiprogramming.
 Another process added to the system.
28

 There is a point when cpu utilization decreases, when the degree of


multiprogramming increasing.

 To limit the effect of thrashing by using Local replacement algorithm.


 If the process start thrashing it cannot steal frame from another process
and cause the latter to thrash as well.
Prevent thrashing
 Provide aprocess with many frame as it need.
 The working of set model
 It start by looking at how many frame a process is actually using. This
define the locality model.
 Locality model: It states that a process execute it move from locality
to locality.
 Locality: It is a set of pages that are actively used together.
Disadvantages
 Increase degree of multiprogramming
 System throughput decreases
 Page fault rate increases
 Effective access time increases.
29

You might also like