0% found this document useful (0 votes)
15 views

unit 2 OS

Director

Uploaded by

navika Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

unit 2 OS

Director

Uploaded by

navika Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit -2

Memory Management
What is 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 rapidly available information shared by the CPU and I/O devices. Main
memory is the place where programs and information are kept when the processor is
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 loses its data
when a power interruption occurs.

Main Memory

What is Memory Management?


In a multiprogramming computer, the Operating System resides in a part of memory, and the
rest is used by multiple processes. The task of subdividing the memory among different
processes is called Memory Management. 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.
Why Memory Management is Required?
 Allocate and deallocate 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 the process.

Address Binding :
The Association of program instruction and data to the actual physical memory locations is
called the Address Binding. Let’s consider the following example given below for better
understanding.
Consider a program P1 has the set of instruction such that I1, I2, I3, I4, and program counter
value is 10, 20, 30, 40 respectively.
Program P1
I1 --> 10
I2 --> 20
I3 --> 30
I4 --> 40

Program Counter = 10, 20, 30, 40

Types of Address Binding :


Address Binding divided into three types as follows.
1. Compile-time Address Binding
2. Load time Address Binding
3. Execution time Address Binding

Compile-time Address Binding :


 If the compiler is responsible for performing address binding then it is called
compile-time address binding.
 It will be done before loading the program into memory.
 The compiler requires interacts with an OS memory manager to perform compile-
time address binding.

Load time Address Binding :


 It will be done after loading the program into memory.
 This type of address binding will be done by the OS memory manager i.e loader.

Execution time or dynamic Address Binding :


 It will be postponed even after loading the program into memory.
 The program will be kept on changing the locations in memory until the time of
program execution.
 The dynamic type of address binding done by the processor at the time of program
execution.

Note :
The majority of the Operating System practically implement dynamic loading, dynamic
linking, dynamic address binding. For example – Windows, Linux, Unix all popular OS.
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. Memory management meant to satisfy some
requirements that we should keep in mind.
These Requirements of memory management are:
1. Relocation – The available memory is generally shared among a number of
processes in a multiprogramming system, so it is not possible to know in advance
which other programs will be resident in main memory at the time of execution of
this program. Swapping the active processes in and out of the main memory
enables the operating system to have a larger pool of ready-to-execute process.

When a program gets swapped out to a disk memory, then it is not always possible
that when it is swapped back into main memory then it occupies the previous
memory location, since the location may still be occupied by another process. We
may need to relocate the process to a different area of memory. Thus there is a
possibility that program may be moved in main memory due to swapping.
The figure depicts a process image. The process image is occupying a continuous
region of main memory. The operating system will need to know many things
including the location of process control information, the execution stack, and the
code entry. Within a program, there are memory references in various instructions and
these are called logical addresses.

After loading of the program into main memory, the processor and the operating
system must be able to translate logical addresses into physical addresses. Branch
instructions contain the address of the next instruction to be executed. Data reference
instructions contain the address of byte or word of data referenced.

concept of Logical Address Space and Physical Address Space


Logical and Physical Address Space
 Logical Address Space: An address generated by the CPU is known as a
“Logical Address”. It is also known as a Virtual address. Logical address space
can be defined as the size of the process. A logical address can be changed.
 Physical Address Space: An address seen by the memory unit (i.e the one
loaded into the memory address register of the memory) is commonly known as a
“Physical Address”. A Physical address is also known as a Real address. The set
of all physical addresses corresponding to these logical addresses is known as
Physical address space. A physical address is computed by MMU. The run-time
mapping from virtual to physical addresses is done by a hardware device Memory
Management Unit(MMU). The physical address always remains constant.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different types of
loading :
 Static Loading: Static Loading is basically loading the entire program into a fixed
address. It requires more memory space.
 Dynamic Loading: The entire program and all data of a process must be in
physical memory for the process to execute. So, the size of a process is limited to
the size of physical memory. To gain proper memory utilization, dynamic loading
is used. In dynamic loading, a routine is not loaded until it is called. All routines
are residing on disk in a relocatable load format. One of the advantages of
dynamic loading is that the unused routine is never loaded. This loading is useful
when a large amount of code is needed to handle it efficiently.

Static and Dynamic Linking


To perform a linking task a linker is used. A linker is a program that takes one or more object
files generated by a compiler and combines them into a single executable file.
 Static Linking: In static linking, the linker combines all necessary program
modules into a single executable program. So there is no runtime dependency.
Some operating systems support only static linking, in which system language
libraries are treated like any other object module.
 Dynamic Linking: The basic concept of dynamic linking is similar to dynamic
loading. In dynamic linking, “Stub” is included for each appropriate library
routine reference. A stub is a small piece of code. When the stub is executed, it
checks whether the needed routine is already in memory or not. If not available
then the program loads the routine into memory.

Logical vs Physical Address


An address generated by the CPU is commonly referred to as a logical address. The address
seen by the memory unit is known as the physical address. The logical address can be
mapped to a physical address by hardware with the help of a base register; this is known as
dynamic relocation of memory references.
Sharing – A protection mechanism must have to allow several processes to access the same
portion of main memory. Allowing each process access to the same copy of the program
rather than have their own separate copy has an advantage.

For example, multiple processes may use the same system file and it is natural to load one
copy of the file in main memory and let it be shared by those processes. It is the task of
Memory management to allow controlled access to the shared areas of memory without
compromising the protection. Mechanisms are used to support relocation supported sharing
capabilities.
Protection – There is always a danger when we have multiple programs at the same time
as one program may write to the address space of another program. So every process must be
protected against unwanted interference when other process tries to write in a process
whether accidental or incidental. Between relocation and protection requirement a trade-off
occurs as the satisfaction of relocation requirement increases the difficulty of satisfying the
protection requirement.

Prediction of the location of a program in main memory is not possible, that’s why it is
impossible to check the absolute address at compile time to assure protection. Most of the
programming language allows the dynamic calculation of address at run time. The memory
protection requirement must be satisfied by the processor rather than the operating system
because the operating system can hardly control a process when it occupies the processor.
Thus it is possible to check the validity of memory references.
Contiguous Memory Allocation
The main memory should accommodate both the operating system and the different client
processes. Therefore, the allocation of memory becomes an important task in the operating
system. The memory is usually divided into two partitions: one for the resident operating
system and one for the user processes. We normally need several user processes to reside in
memory simultaneously. Therefore, we need to consider how to allocate available memory to
the processes that are in the input queue waiting to be brought into memory. In adjacent
memory allotment, each process is contained in a single contiguous segment of memory.

Contiguous Memory Allocation

Memory Allocation
To gain proper memory utilization, memory allocation must be allocated efficiently. One of
the simplest methods for allocating memory is to divide memory into several fixed-sized
partitions and each partition contains exactly one process. Thus, the degree of
multiprogramming is obtained by the number of partitions.
 Multiple partition allocation: In this method, a process is selected from the input
queue and loaded into the free partition. When the process terminates, the partition
becomes available for other processes.
 Fixed partition allocation: In this method, the operating system maintains a table
that indicates which parts of memory are available and which are occupied by
processes. Initially, all memory is available for user processes and is considered
one large block of available memory. This available memory is known as a
“Hole”. When the process arrives and needs memory, we search for a hole that is
large enough to store this process. If the requirement is fulfilled then we allocate
memory to process, otherwise keeping the rest available to satisfy future requests.
While allocating a memory sometimes dynamic storage allocation problems
occur, which concerns how to satisfy a request of size n from a list of free holes.
There are some solutions to this problem:

First Fit
In the First Fit, the first available free hole fulfil the requirement of the process allocated.

First Fit
Here, in this diagram, a 40 KB memory block is the first available free hole that can store
process A (size of 25 KB), because the first two blocks did not have sufficient memory space.
Best Fit
In the Best Fit, allocate the smallest hole that is big enough to process requirements. For this,
we search the entire list, unless the list is ordered by size.
Best Fit
Here in this example, first, we traverse the complete list and find the last hole 25KB is the
best suitable hole for Process A(size 25KB). In this method, memory utilization is maximum
as compared to other memory allocation techniques.
Worst Fit
In the Worst Fit, allocate the largest available hole to process. This method produces the
largest leftover hole.

Worst Fit
Here in this example, Process A (Size 25 KB) is allocated to the largest available memory
block which is 60KB. Inefficient memory utilization is a major issue in the worst fit.
Fragmentation
Fragmentation is defined as when the process is loaded and removed after execution from
memory, it creates a small free hole. These holes can not be assigned to new processes
because holes are not combined or do not fulfill the memory requirement of the process. To
achieve a degree of multiprogramming, we must reduce the waste of memory or
fragmentation problems. In the operating systems two types of fragmentation:
1. Internal fragmentation: Internal fragmentation occurs when memory blocks are
allocated to the process more than their requested size. Due to this some unused
space is left over and creating an internal fragmentation problem.Example:
Suppose there is a fixed partitioning used for memory allocation and the different
sizes of blocks 3MB, 6MB, and 7MB space in memory. Now a new process p4 of
size 2MB comes and demands a block of memory. It gets a memory block of 3MB
but 1MB block of memory is a waste, and it can not be allocated to other
processes too. This is called internal fragmentation.
2. External fragmentation: In External Fragmentation, we have a free memory block,
but we can not assign it to a process because blocks are not contiguous. Example:
Suppose (consider the above example) three processes p1, p2, and p3 come with
sizes 2MB, 4MB, and 7MB respectively. Now they get memory blocks of size
3MB, 6MB, and 7MB allocated respectively. After allocating the process p1
process and the p2 process left 1MB and 2MB. Suppose a new process p4 comes
and demands a 3MB block of memory, which is available, but we can not assign it
because free memory space is not contiguous. This is called external
fragmentation.

Both the first-fit and best-fit systems for memory allocation are affected by external
fragmentation. To overcome the external fragmentation problem Compaction is used. In the
compaction technique, all free memory space combines and makes one large block. So, this
space can be used by other processes effectively.
Another possible solution to the external fragmentation is to allow the logical address space
of the processes to be noncontiguous, thus permitting a process to be allocated physical
memory wherever the latter is available.
Paging
Paging is a memory management scheme that eliminates the need for a contiguous allocation
of physical memory. This scheme permits the physical address space of a process to be non-
contiguous.
 Logical Address or Virtual Address (represented in bits): An address generated by
the CPU.
 Logical Address Space or Virtual Address Space (represented in words or bytes):
The set of all logical addresses generated by a program.
 Physical Address (represented in bits): An address actually available on a memory
unit.
 Physical Address Space (represented in words or bytes): The set of all physical
addresses corresponding to the logical addresses.

Example:
 If Logical Address = 31 bits, then Logical Address Space = 231 words = 2 G
words (1 G = 230)
 If Logical Address Space = 128 M words = 27 * 220 words, then Logical Address
= log2 227 = 27 bits
 If Physical Address = 22 bits, then Physical Address Space = 222 words = 4 M
words (1 M = 220)
 If Physical Address Space = 16 M words = 24 * 220 words, then Physical Address
= log2 224 = 24 bits

The mapping from virtual to physical address is done by the memory management unit
(MMU) which is a hardware device and this mapping is known as the paging technique.
 The Physical Address Space is conceptually divided into several fixed-size blocks,
called frames.
 The Logical Address Space is also split into fixed-size blocks, called pages.
 Page Size = Frame Size

Let us consider an example:


 Physical Address = 12 bits, then Physical Address Space = 4 K words
 Logical Address = 13 bits, then Logical Address Space = 8 K words
 Page size = frame size = 1 K words (assumption)

Paging
The address generated by the CPU is divided into:
 Page Number(p): Number of bits required to represent the pages in Logical
Address Space or Page number
 Page Offset(d): 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.

Physical Address is divided into:


 Frame Number(f): Number of bits required to represent the frame of
Physical Address Space or Frame number frame
 Frame Offset(d): Number of bits required to represent a particular word in a
frame or frame size of Physical Address Space or word number of a frame or
frame offset.

The hardware implementation of the page table can be done by using dedicated registers. But
the usage of the register for the page table is satisfactory only if the page table is small. If the
page table contains a large number of entries then we can use TLB(translation Look-aside
buffer), a special, small, fast look-up hardware cache.
 The TLB is an associative, high-speed memory.
 Each entry in TLB consists of two parts: a tag and a value.
 When this memory is used, then an item is compared with all tags simultaneously.
If the item is found, then the corresponding value is returned.

Page Map Table


Main memory access time = m
If page table are kept in main memory,
Effective access time = m(for page table)
+ m(for particular page in page table)

TLB Hit and Miss

Segmentation
A process is divided into Segments. The chunks that a program is divided into which are not
necessarily all of the exact sizes are called segments. Segmentation gives the user’s view of
the process which paging does not provide. Here the user’s view is mapped to physical
memory.
Types of Segmentation in Operating System
 Virtual Memory Segmentation: Each process is divided into a number of
segments, but the segmentation is not done all at once. This segmentation may or
may not take place at the run time of the program.
 Simple Segmentation: Each process is divided into a number of segments, all of
which are loaded into memory at run time, though not necessarily contiguously.

There is no simple relationship between logical addresses and physical addresses in


segmentation. A table stores the information about all such segments and is called a Segment
Table.
What is Segment Table?
It maps a two-dimensional Logical address into a one-dimensional Physical address. It’s each
table entry has:
 Base Address: It contains the starting physical address where the segments reside
in memory.
 Segment Limit: Also known as segment offset. It specifies the length of the
segment.
Segmentation
Translation of Two-dimensional Logical Address to Dimensional Physical Address.
Translation
The address generated by the CPU is divided into:
 Segment number (s): Number of bits required to represent the segment.
 Segment offset (d): Number of bits required to represent the size of the segment.

Advantages of Segmentation in Operating System


 No Internal fragmentation.
 Segment Table consumes less space in comparison to Page table in paging.
 As a complete module is loaded all at once, segmentation improves CPU
utilization.
 The user’s perception of physical memory is quite similar to segmentation. Users
can divide user programs into modules via segmentation. These modules are
nothing more than separate processes’ codes.
 The user specifies the segment size, whereas, in paging, the hardware determines
the page size.
 Segmentation is a method that can be used to segregate data from security
operations.
 Flexibility: Segmentation provides a higher degree of flexibility than paging.
Segments can be of variable size, and processes can be designed to have multiple
segments, allowing for more fine-grained memory allocation.
 Sharing: Segmentation allows for sharing of memory segments between
processes. This can be useful for inter-process communication or for sharing code
libraries.
 Protection: Segmentation provides a level of protection between segments,
preventing one process from accessing or modifying another process’s memory
segment. This can help increase the security and stability of the system.
Disadvantages of Segmentation in Operating System
 As processes are loaded and removed from the memory, the free memory space is
broken into little pieces, causing External fragmentation.
 Overhead is associated with keeping a segment table for each activity.
 Due to the need for two memory accesses, one for the segment table and the other
for main memory, access time to retrieve the instruction increases.
 Fragmentation: As mentioned, segmentation can lead to external fragmentation
as memory becomes divided into smaller segments. This can lead to wasted
memory and decreased performance.
 Overhead: Using a segment table can increase overhead and reduce performance.
Each segment table entry requires additional memory, and accessing the table to
retrieve memory locations can increase the time needed for memory operations.
 Complexity: Segmentation can be more complex to implement and manage than
paging. In particular, managing multiple segments per process can be challenging,
and the potential for segmentation faults can increase as a result.

Virtual Memory
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed
as though it were part of the main memory. The addresses a program may use to reference
memory are distinguished from the addresses the memory system uses to identify physical
storage sites and program-generated addresses are translated automatically to the
corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer system and
the amount of secondary memory available not by the actual number of main storage
locations.
It is a technique that is implemented using both hardware and software. It maps memory
addresses used by a program, called virtual addresses, into physical addresses in computer
memory.
1. All memory references within a process are logical addresses that are dynamically
translated into physical addresses at run time. This means that a process can be
swapped in and out of the main memory such that it occupies different places in
the main memory at different times during the course of execution.
2. A process may be broken into a number of pieces and these pieces need not be
continuously located in the main memory during execution. The combination of
dynamic run-time address translation and the use of a page or segment table
permits this.

If these characteristics are present then, it is not necessary that all the pages or segments are
present in the main memory during execution. This means that the required pages need to be
loaded into memory whenever required. Virtual memory is implemented using Demand
Paging or Demand Segmentation.
Demand Paging
The process of loading the page into memory on demand (whenever a page fault occurs) is
known as demand paging. The process includes the following steps are as follows:
Demand Paging
1. If the CPU tries to refer to a page that is currently not available in the main
memory, it generates an interrupt indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to
proceed the OS must bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address
space. The page replacement algorithms are used for the decision-making of
replacing the page in physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will
place the process back into the ready state.

Hence whenever a page fault occurs these steps are followed by the operating system and the
required page is brought into memory.
Advantages of Virtual Memory
 More processes may be maintained in the main memory: Because we are
going to load only some of the pages of any particular process, there is room for
more processes. This leads to more efficient utilization of the processor because it
is more likely that at least one of the more numerous processes will be in the ready
state at any particular time.
 A process may be larger than all of the main memory: One of the most
fundamental restrictions in programming is lifted. A process larger than the main
memory can be executed because of demand paging. The OS itself loads pages of
a process in the main memory as required.
 It allows greater multiprogramming levels by using less of the available (primary)
memory for each process.
 It has twice the capacity for addresses as main memory.
 It makes it possible to run more applications at once.
 Users are spared from having to add memory modules when RAM space runs out,
and applications are liberated from shared memory management.
 When only a portion of a program is required for execution, speed has increased.
 Memory isolation has increased security.
 It makes it possible for several larger applications to run at once.
 Memory allocation is comparatively cheap.
 It doesn’t require outside fragmentation.
 It is efficient to manage logical partition workloads using the CPU.
 Automatic data movement is possible.

Disadvantages of Virtual Memory


 It can slow down the system performance, as data needs to be constantly
transferred between the physical memory and the hard disk.
 It can increase the risk of data loss or corruption, as data can be lost if the hard
disk fails or if there is a power outage while data is being transferred to or from
the hard disk.
 It can increase the complexity of the memory management system, as the
operating system needs to manage both physical and virtual memory.

Page Fault Service Time: The time taken to service the page fault is called page fault
service time. The page fault service time includes the time taken to perform all the above six
steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
In an operating system that uses paging for memory management, a page replacement
algorithm is needed to decide which page needs to be replaced when a new page comes in.
Page Fault: A page fault happens when a running program accesses a memory page that is
mapped into the virtual address space but not loaded in physical memory. Since actual
physical memory is much smaller than virtual memory, page faults happen. In case of a 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.
Page Replacement Algorithms:
1. First In First Out (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.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames.Find the
number of page faults.

Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3
Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not
available in memory so it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is
also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault.
Finally, when 3 come it is not available so it replaces 0 1 page fault.
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.
For example, if we consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we
get 9 total page faults, but if we increase slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would not be
used for the longest duration of time in the future.
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.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7 because it is
not used for the longest duration of time in the future.—>1 Page fault. 0 is already there so
—> 0 Page fault. 4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available
in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system
cannot know future requests. The use of Optimal Page replacement is to set up a benchmark
so that other replacement algorithms can be analyzed against it.
3. Least Recently Used: In this algorithm, page will be replaced which is least recently
used.
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.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7 because it is
least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available
in the memory.
4. Most Recently Used (MRU): In this algorithm, page will be replaced which has been used
recently. Belady’s anomaly can occur in this algorithm.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already their so–> 0 page fault
when 3 comes it will take place of 0 because it is most recently used —>1 Page fault
when 0 comes it will take place of 3 —>1 Page fault
when 4 comes it will take place of 0 —>1 Page fault
2 is already in memory so —> 0 Page fault
when 3 comes it will take place of 2 —>1 Page fault
when 0 comes it will take place of 3 —>1 Page fault
when 3 comes it will take place of 0 —>1 Page fault
when 2 comes it will take place of 3 —>1 Page fault
when 3 comes it will take place of 2 —>1 Page fault

You might also like