CNC Unit 5
CNC Unit 5
Memory Management
Ch- 5 Main Memory :- The main memory is central to the operation of a Modern
Computer. Main Memory is a large array of words or bytes, ranging in size
from hundreds of thousands to billions. Main memory is a repository of
Memory Management rapldily avallable information shared by the CPU and /o devices. Main
memory is the place where programs and information are kept when the
processoris effectively utilizing them. Main memory is associated with the
processor, so moving instructions and information into and out of the
processor is extremely fast. Main memory is also known as RAM (Random
Access Memory). This memory is volatile. RAM oses its data when a power
interruption occurs.
" Memory Management in Operating System (o$) - Memory Management Techniques:
" Memory is the important part of the computer that is used to store The memory management techniques can be classified into following
the data. Its management is critical to the computer system because main categories:
the amount of main memory available in a computer system is very "Contiguous memory management schemes
limited. At any time, many processes are competing for it. Moreover,
executed
to increase performance, several processes are " Non-Contiguous memory management schemes
simultaneously. For this, we must keep several processes in the main
Mao Manigeant Schene
memory, so it is even more important to manage them effectively.
Memory Management in OS
User Programs
Uer Intertac
Operating System
System Calls
Menory Network Powe Pertl
Mengmen Manegmy
Hardware Claselflcatlon of memory management scheme
D y n a m i cP a r
Th
management schemnes:
" Advantages of Single contiguous memory partition
Contiguous memory management schemes: " Simple to implement. much c
In aContiguous memory management scheme, each program occupies " Easy to manage and design.
alloca
a single contiguous block of storage locations, i.e., a set of memory scheme, once a process is i sin
locations with consecutive addresses. " In a Single contiguous memory management other processor will
loaded, it is given full processor's time, and no
vac
" Single contiguous memory management schemes: interrupt it. Adv
The Single contiguous memory management scheme is the simplest
memory management scheme used in the earliest generation of computer " Disadvantages of Single contiguous memory management schemes:
systems. In this scheme, the main memory is divided into two contiguous
areas or partitions. The operating systems reside permanently in one " Wastage of memory space due to unused memory as the process is
partition, generally at the lower memory, and the user process is loaded into unlikely to use all the available memory space.
the other partition. " The CPUremains idle, waiting for the disk to load the binary image into the
main memory.
" It can not be executed if the program is too large to fit the entire available
main memory space.
" It does not support multiprogramming, i.e., It cannot handle multiple
programs simultaneously.
Mutiple Partitioning: " Fixed Partitioning :
The single Contiguous memory management scheme is ineficient as it The main memory is divided into several fixed-sized partitions in a fixed
limits computers to execute only one program at a time resulting in wastage partition memory management scheme or static partitioning. These
in memory space and CPU time. The problem of inefficient CPU use can be can be of the same size or different sizes. Each partition can holdpartitions
a single
process. The number of partitions determines the degree of
overcome using multiprogramming that allows more than one program to run multiprogramming, i.e., the maximum number of processes in memory. These
concurrently. To switch between two processes, the operating systems need partitions are made at the time of system generation and remain fixed after
to load both processes into the main memory. The operating system needs to that.
divide the available main memory into multiple parts to load multiple
processes into the main memory. Thus multiple processes can reside in the " Advantages of Fixed Partitioning memory management schemes:
main memory simutaneously. " Simple to implement.
" Easy to manage and design.
The multiple partittoning schemes can be of two types:
o Fixed Partitioning
o Dynamic Partitioning "Disadvantages of Fixed Partitioning memory management schemes:
" This scheme suffers from internal fragmentation.
" The number of partitions is specified at the time of system
generation.
" Dynamic Parttioning
The dynamic partitioning was designed to overcome the problems of a fixed Non-Contiguous menory management schemes
partitioning scheme. In a dynamic partitloning scheme, each process occuples only as
much memory as they require when loaded for processing. Requested processes are
allocated memory until the entire physical memory is exhausted or the remaining space
" Non-contiguous allocation, also known as dynamic or linked allocation, is a
memory allocation technique used in operating systems to allocate memory to
is insufficient to hold the requesting process. In ths scheme the partitions used are of processes that do not require a contiguous block of memory. In this technigue.
variable slre, and the number of partitions is not defined at the system generation time. each process is allocated aseries of non-contiguous blocks of memory that can
" Advantages of Dynamic Partitoning memory management schemes: be located anywhere in the physical memory.
"Simple to implement. "Non-contiguous allocation involves the use of pointers to link the non
"Easy to manage and design. contiguous memory blocks allocated to a process. These pointers are used by
the operating system to keep track of the memory biocks allocated to the
process and to locate them during the execution of the process.
" DisYdvantages of Dynamic Partitioning memory management schemes:
" This scheme also suffers from internal fragmentation.
" The number of partitions is specified at the time of system segmentation.
"Fundamental approaches :
There are two fundamental approaches to implementing noncontiguous memory allocation: Advantages to non-contiguous allocation.
" Paging "1. It reduces internal fragmentation since memory blocks can be allocated as
" Segmentation needed, regardless of their physical location.
" in paging each process consists of fixed-size components called pages. The size of a page is
2. It allows processes to be alocated memory in a more flexible and efficient
defined by the hardware of a computer, and demarcation of pages is implicit in it. The memory manner since the operating system can allocate memory to a process
can accommodate an integral nurmbber of pages. It is partitioned into memory areas that have wherever free memory is available.
the same size as a page, and each of these memory areas is considered separately for
allocation to a page. This way, any free memory area is exactly the same size as a page,
external fragmentation does not arise in the system. Internal fragmentation can arise because
SO Disadvantages to non-contiguous allocation.
the last page of a process is allocated a page-síze memory area even if it is smaller than a page 1. It can lead to external fragmentation, where the available memory is broken
in size.
" In segmentation, a programmer identifies components called segments in a process. A
into small, non-contiguous blocks, making it difficult to allocate large blocks of
segment is a logical entity in a program, e.g., a set of functions, data structures, or objects. memory to a process.
Segmentation facilitates sharing of code,data, and program modules processes. However, 2. The use of pointers to link memory blocks can introduce additional
Segments have different sizes, so the kernel has to use memory reuse techniques such as first
fit(It refersto a memory allocation algorithm where the system searches for the first avallable overhead, leading to slower memory allocation and de-allocation times.
memory partition ) or best-fit allocation (It refers to the smallest free block of memory to
accommiodate aprócess's memory request ). Consequently, external fragmentation can
" Dynamic Partitioning
"Fxed Partttioning partitioning was designed to overcome the problems of a
fixed
" The dynamic occupies
"The main memory is divided into several fixed-sized partitions, inpartitions
a fixed partitioning scheme. In a dynamic partitioning scheme, each processRequested
partition memory management scheme or static partitioning. These only as much memory as they require when loaded for
processing.
can be of the same size or different sizes. Each partition can hold a single is exhausted
processes are allocated memory until the entire physical memory process. In this
process. The number of partitions determines the degree of multiprogramming, or the remaining space is insufficient to hold the requesting
iLe., the maximum number of processes in memory. These partitions are made scheme the partitions used are of variable size, and the number of partitions is
at the time of system generation and remain fxed after that. not defined at the system generation time.
" Advantages of Fixed Partitioning memory management schemes:
"Advantages of Dynamic Partitioning memory management schemes:
"Simple to implement. " Simple to implement.
" Easy to manage and design. " Easy to manage and design.
"Disadvantages of Fed Partitioning memory management schemes: " Disadvantages of Dynamic Partitioning memory management schemes:
"This scheme suffers from internal fragmentation. " This scheme also suffers from internal fragmentation.
"The number of partitions is specified at the time of system generation. " The number of partitions is specified at the time of system segmentation.
" Free Space Management Techniques : - " Free space management is a crucial function of operating systems, as it ensures
Introduction: that storage devices are utilized efficiently and effectively.
Free space management is a critical aspect of operating systems as it involves The system keeps tracks of the free disk blocks for allocating space to files when
managing the available storage space on the hard disk or other secondary storage they are created. Also, to reuse the space released from deleting the files, free
devices. The operating system uses various techniques to manage free space and space management becomes crucial. The system maintains a free space list
optimize the use of storage devices. Here are some of the commonly used free which keeps track of the disk blocks that are not allocated to some file or
space management techniques: directory.
" Linked Allocation: In this technique, each file is represented bya linked list of The free space list can be implemented mainly as:
disk blocks. When a file is created, the operating system finds enough free space " Bitmap or Bit vector -
on the disk and links the blocks of the file to form a chain. This method is simple "A Bitmap or Bit
Vector is series or collection of bits where each bit corresponds
to implement but can lead to fragmentation and wastage of space. to a disk block. The bit can take two values: 0and 1: 0indicates that the block is
ContiguousAllocation: In this technique, each file is stored as a contiguous block allocated and 1 indicates a free block. The given instance of disk blocks on the
of disk space. When a file is created, the operating system finds a contiguous disk in Figure 1 (where green blocks are allocated) can be represented by a
block of free space and assigns it to the file. This method is efficient as it bitmap of 16 bits as: 0000111000000110.
minimizes fragmentation but suffers from the problem of external fragmentation.
Aduntg Simple to understand Finding the first free block is efficient, It requies Here are some advantages and disadvantages of free space management technlques In operstngsystems:
Scanning the words ( group of8bits) in a bitmap for a non-zero word. (A 0-valued word " Advantages:
hes al bits o, The first fee block is then found by scanning for the first I bit in the non 1, Efflclent use of storage space: Free space management techniques help to optimize the use of storage space on
oo rd the hard disk or other secondary storage devices.
require less
Ld e- In this approech, the free disk blocks are linked together ie. a free block 2. Easy to Implement: Some techniques, such as linked allocation, are simple to implement and
contains apointer to the next free block. The block mumber of the very first disk block is overhead in terms of processing and memory resources.
disk fragmentation and
stored at a separate location on disk and is also cached in memory. 3. Faster access to files: Techniques such as contiguous allocation can help to reduce
Improve access time to files.
" Dlsadvantages:
space, which can decrease
1, Fragmentation: Techniques such as linked allocatlon can lead to fragmentatlon of disk
the efficiency of storage devices.
odctd 2 Overhead: Some indexed allocation, require additional overhead in terms of memory and
BocudsBode,
In Figure-2, the free space list head points to Block 5 which
points to Block 6, the next free block and so on. The last free
techniqyedexas blocks.
processing resources to maintain
block would contain a null pointer indicating the end of free list. scalability: Some techniques, such as FAT, have limited scalability in terms of the number of files that can
3.beLimited
stored on the disk.
Adrawback of this method is the /O required for free space list
Blochock11 uddi traversal. 4. Risk of data loss: In some cas such as with contiguous allocation, if a file becomes corrupted or damaged, it
may be difficult to recover the
Overall, the cholce of free space management
5.system technique depends on the specific requirements of the operating
and the storage devices being used.. While some techniques offeradvantages in terms of eficdency and
onsidered.
speed, they may also have limitations and drawbacks t need
Virtual Memory " Virtual memory can be implemented with the help of:
" What is Virtual Memory? wbilo s t . " 1. Paging
Memory is a where large in form of pages
spred
procecESeves
tin and tions of
of processes are loaded intÍ the main memory. This
ony the lao pages ges or portions
is provided for user programs when a very small physical
tec que is useful as a in " 2. Segmentation
memory is there. Thus Virtual menory is a techniqueethat allows the execution of processes that are not
the physicalmemory completely.
Virtual Memory mainly gives the illusion of more physical memory than there really is " Need of Virtual Memory
with the help of Demand Paging.
Following are the reasons due to which there is a need for Virtual Memory:
In real scenarios, most processes never need all their pages at once, for the following reasons:
"Eror handing code is not needed unless that specific error occurs, some of which are quite "In case, if acomputer running the Windows operating system needs more
rare. memory or RAM than the memory installed in the system then it uses a small
" Arrays are often over-sized for worst-case scenarios, and only asmall fraction of the arrays portion of the hard drive for this purpose.
are actualy used in practice.
Certain features of certain programs are rarely used. " Suppose there is a situation when your computer does not have space in the
physical memory, then it writes things that it needs to remember into the
" Inan Operating system, the memory is usually stored in the form of units that are known as hard disk in aswap file and that as virtual memory.
pages. Basically, these are
a atomic units used to store large programs.
Disadvantages
" Given
bel
. Virtua
the Virtual memory
" Execution of the Program in
Benefits of having Virtual Memory few pieces of the program are brought
" With the help of the Operating system
. T h
space available is huge
" 1. large programs can be written, as the virtual into the main memory:
compared to physical memory.
brought in the main mernory is known as
" 2. Less lðO required leads to faster and easy swapping of processes. "A portion of the process that is
"3. More physical memory available, as programs are stored on virtual Resident Set.
memory, so they occupy very less space on actual physical
memory.
" Whenever an address is needed that
is not in the main memory, then it
"4. Therefore, the Logical address space can be much more larger than that placed in the blocked state by the
generates an interrupt. The process is
that contains the logical address
of physical address space. Operating system. Those pieces of the process
"5. Virtual menory allows address spaces to be shared by several processes. are brought into the main memory.
"6. During the process creation, virtual memory allows: copy-on-wrte and
Memory-mapped fles
Paging
" The basic idea behind demand paging is that when a process is swapped in, its pages are not Advantages of Virtual Memory
swapped in all at once. Rather they are swapped in only when the process needs them(On Given below are the advantages of using Virtual Memory:
demand).initially, oniy those pages are loaded which will be required by the process
immediately. " Virtual Memory allows you to run more applications at a time.
" The pages that are not moved into the memory, are marked as invalid in the page table. For
an ivalid entry, the rest of the v
table isd l o A n e çase of pages that are loaded in the " With the help of virtual memory, you can easily fit many large programs into
ali
memoryY, they are marked 2s with the information about where to find the smaller programs.
swapped out page.
Page Replacement "With the help of Virtualmemory, a multiprogramming environment can be
easily implemented.
" As studied inDermand Paging, only certain pages of a process are loaded initially into the
memory. This alows us to get more number of processes into memory at the same time. but
what happens when a process requests for more pages and no free memory is available to
"As more processes should be maintained in the main memory which leads to
the effective utilization of the CPU.
bring them in. Foliowing steps can be taken to deal with this problem :
1. Putthe process in the wait queue, until any other process finishes its execution thereby " Data should be read from disk at the time when required.
freeing frames. " Common data can be shared easily between memory.
" 2. Or, remove some other process completely from the memory to free frames.
3. O, find some, pages that are notbeing used right now, move them tothe disk to get free " With the help of irtual memory, speed isgained when only a particular
frames. This technique is called Page replacement and is mostcommonlyused. We have segment of the program is required for the execution of the program.
some great algorithins to carry on page replacement efficiently. " The process may even become larger than all of the physical memory.
Segmentation :
Disadvantages of Virtual Memory " The segmentation technology is used in operating systems and the prOcess is divided
into many, pieces called segments and these of variable size. In this.
"Given below are the drawbacks of using Virtual Memory: Segmentation uses a variable partitioning m e onetsegment
e
is equal to one
" Virtual memory reduces the stability of the system. complete memory block.
"A process is divided into Segments. The chunks that a program is divided into which are
" The performance of Virtual memory is not as good as that of RAM. not necessarily all of the exact sizes are called segments. Segmentation gives the user's
"Ifa system isusing virtual memory then applications may run slower. view of the process which paging does not provide. Here the user's view is mapped to
physical memory.
" Virtual memory negatively affects the overall performance of a system.
" Virtual memory occupies the storage space, which might be otherwise Types of Segmentation in Operating System
used for long term data storage. " Virtual Memory Segmentation: Each process is divided into a number of segments, but
"This memory takes more time in order to switch between applications. the segmentation
at the is notprogram.
time of the done allat onice. This segmentation may or may not
" Simple Segmentation: Each process is divided into a number of segments, all of which
into memory at rün time, though not necessarily contiguously.
"There is no simple relationship between logical addresses and physical addresses in
segmentation. Àtable stores the information about all such segments and is called
Segment Table.
What is Segment Table?
" It mapsatwo-dimensional Logical address into aone-dimensional Advantages of Segmentation
Physical address. It's each table entry has: "1. No internal fragmentation
"Base Address: It contains the starting physical address where the 2. Average Segment Size is larger than the actual page size.
segmets reside in memory.
" 3. Less overhead
Segment imit:Also known as segment offset. It specifies the length " 4. It is easier torelocate segments than entire address space.
of the segment.
"5. The segment table is of lesser size as compared to the page table in paging.
Disadvantages of Segmentation
"1. It can have external fragmentation.
" 2. it is difficult to allocate contiguous memory to variable sized partition.
"3. Costly memory management algorithms.
Paging:
" Paging is a memory management scheme that
eliminates the need for a contlguous Dpertirg
allocation of physical
fromthe Seconda t o h e process of retrieving processes in the form of pages System
into the main memory is
purpose of paging is to separate
used to split the main each procedure i known as paging. Theframes
into pages. Additionally,
basic
will be
memory. This scheme
process to be non-contiguous. permits the physical address space of a
" In Operating Systems, Paging is a storage
the secondary storage into the main
mechanism used to retrieve processes from
behind the dhiSto memory in
divide each process in thethe form of pages. The main idea
will also be in the form of frames. form of pages. The main memory
" One page of the process is to be stored in
pages can be stored at one of the frames of the memory. The
the
to find the contiguous frames different locations of the memory but the prioritý is always Mapping
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
be equal. Considering the defines different frame sizes. The sizes of each frame must
size needs to be as same asfact that the pages are
frame size. process to mapped to the frames in
be non - contiguous. Paging, page Main Memory
Collecton of Frmea)
" Example
" Let us consider the main memory size 16 Kb and Frame size is 1 KB
therefore the 16 KO
Frtne - t
main memory willbe divided into the collection of 16 frames of 1 KB each. PresessPi
" There are 4 processes in the system that is P1, P2, P3 and P4 of4 KB each. Each
process is divided into pages of 1 KB each so that one page can be stored in one
frame.
" Initialy, all the frames are empty therefore pages of the processes will get stored
in the contiguous way.
Presess P)
" Frames, pages and the mapping between the two is shown in the image below.
" Let us consider that, P2 and P4 are moved to waiting state after some P
8frames become empty and therefore other pages can be loaded in that time. Now,
place. The process P5 of size 8 KB (8 pages) is waiting inside the ready queue.empty
" 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
Therefore, we can load the pages of process PS in the place of P2 and P4. places. (Colteptlen d reme)
Paglng
"Memory Management Unit
Page Table:
"The purpose of Memory Management Unit (MMU) is to comvert the logical address into
the physical address. The logical address is the address generated by the CPU for every "A Page Table is adata structure used by the operating system to keep track of
page while the physical address is the actual address of the frame where each page will
be stored. the mapping between virtual addresses used by a process and the
corresponding physical addresses in the system's memory.
"When apage is to be accessed by the CPU by using the logical address, the operating information
system needs to obtain the physical address to access that page physically. "APage Table Entry (PTE) is an entry in the Page Table that stores the
The logical address has two parts. about a particular page of memory. Each PTE contains information such as
"1. Page Number
physical address of the page in memory, whether the page is present in
permissions.
memory or not, whether it is writable or not, and other access
"2. Offset of the
" The size and format of a PTE can vary depending on the architectureenough
Page numbertp): Number of bits required to represent the pages in Logical system and the operating system used. In general, a PTE Contains
Address Space or Page number information to allow the operating system to manage memory efficiently and
Page oftsettd}: Number of bits required to represent a particular word in a page or page size of
Logical Address Space or word number of a page or page offset. protect the system from malicious or accidental access to memory.
" Memory management unit of OS needs to convert the page number to the frame
number.
nformation Stored ln ge Table Entry
"Frome Number It gves the frameknoOn number whlch theP you are loolklng for ls present. The number of bits egured deoends o the
as Inddress
t
number of frames. Fame bit is also
" Numberfblts for frame=Sbe ofphyskcal memory/ Frome She
Optlonat felnatire
" Present/Absent Bit: Present or absent bit says whother a partcular pege you are looking or ls present or abent In case t s nt present, th¡t ts called
Page Fault. It Is set to 0 If the correspondingpage is not n memory. Used to control pege faults by the operating ysten to upport rtual memory.
Sometimes thls blt is also known as a i blt
PeseyAbsentProtection ReleeCoching| Dirly
" Protectign
write, etc).
Bitt The protectlon blt says what kind of protection you want on that pee Sa, these bits are for the protecton of the pae frame (red,
PAGE TABLE ENTRY
" Roterençod
accessed. Blt: Referenced blt will say whether this pege has been refrred to in the lest dodt oyce or not. It ls set to 1by hadware when the page is
" run
Cchng Enble/Dlbled:
according Sometimes
to thelnput glven we nesd
by the user. freshcase,
In that date,theLetinformatlon
ys say the will
usgr come
ls typlg
lnto some inormaton
the main memorytrom the keyboard
Therefors and yourconans
nalh memory programtheshoukd
latest
Inormaon wihich ls typad by the user. Now If you try to put thet page ln the cache, that cache willshowche od lnformation $o whenever freshness s
the coeso oth onomany els o memo.The information present lin the cosest level to the PU and thenmtpn
the CPU should be able to see It asfrst as possble. That s the roaOn ormatkon
weWnt tod N ccct
c n So ths bit enables or dsables caching of the page.
" Modd BR: Modifedbit ys whether the page has been modiñed or not, Modined moans sometimes yos might try to wrtte somethin onto the
PRSe- pore ls modifed, thenwhenever you should eplace that pate with some other pare, then the modiñed informaton should be kept on the
hard
when disk or t has
swapped out.toSometimes
be wrtenthls
backmodified
or t hasbittols bealsosavd
calledback.
the ItDlrty
bsetbit.to 1by hardware on the write- ccess to a pege which ls used to avold writhng
: Brai
Paging :
" Paging is a
grat
allocaYinn memory manaoem:
access a piece of memory
"Page Fault: A page fault will happen if a program tries to specifies the operating Page Faut Handling marked as invalid.
memory (main memory). The fault page that has been
that does not exist in physical
into virtual memory management
and then relocate it from when youaccess a while translating
system totrace all data memory, such as a hard disk. " A Page Fault happens would notice that the invalid bit is set
secondary memory to its primary The paging hardware operating system trap.
table, which willcause an
What is Page Faut in Operating System? the address across the
page the needed page into
A page fault will happen ifa program tries
to by the OS's failure to load
" Page faults dominate more like an error. exist in physical mermory (main memory). The The trap is caused primarily
access a piece of memory that does nottrace all data into virtual memory management memory.
fault specfies the operating system to
its primary memory, such asa hard
and then relocate t from secondary memory to
disk.
not loaded into memory. The page fault
"A page fault trap occurs if therequested page isnotify the operating system to retrieve
is used to
primarily causes an exception,which cotinue operation. Once all of the data has been
to
the "pages" from virtual memory program
placed into pBysical memory, the resumes normaloperation. The Page fault
the user is unaware of it.
process occurs in the background, and thus
and the program counter is often saved
"1. The computer's hardware track to the kernel about the current state of instruction.
on the stack. The CPU registers hold information
the general registers and other volatile
" 2. An assembly program is started, which savesdestroying it.
data to prevent the Operating system from