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

R19 CSE - OS Unit III

The document discusses various memory management strategies, including swapping, contiguous memory allocation, paging, and segmentation. It explains the importance of memory management in optimizing system performance and details techniques such as static and dynamic loading and linking. Additionally, it covers fragmentation issues and the advantages and disadvantages of paging as a memory management method.

Uploaded by

kssnarayana
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 views35 pages

R19 CSE - OS Unit III

The document discusses various memory management strategies, including swapping, contiguous memory allocation, paging, and segmentation. It explains the importance of memory management in optimizing system performance and details techniques such as static and dynamic loading and linking. Additionally, it covers fragmentation issues and the advantages and disadvantages of paging as a memory management method.

Uploaded by

kssnarayana
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/ 35

UNIT-III: Memory Management

Memory-Management Strategies: Introduction, Swapping,


Contiguous memory allocation, Paging, Segmentation
Virtual Memory Management: Introduction, Demand paging, Copy
on-write, Page replacement, Frame allocation, Thrashing, Memory-
mapped files, Kernel memory allocation.
PART A- Memory Management
Introduction
 Main Memory refers to a physical memory ,i.e. internal memory to the
computer.
 The word main is used to distinguish it from external mass storage devices
such as disk drives.
 Main memory is also known as RAM.
 All the programs are loaded into the main memory for execution.
Sometimes complete program is loaded into the memory, but sometimes a
certain part or routine of the program is loaded into the main memory only
when it is called by the program, this mechanism is called Dynamic
Loading, this enhance the performance.
 Also, at times one program is dependent on some other program. In such a
case, rather than loading all the dependent programs, CPU links the
dependent programs to the main executing program when its required.
This mechanism is known as Dynamic Linking.
 Memory Management is the process of controlling and coordinating
computer memory, assigning portions known as blocks to various running
programs to optimize the overall performance of the system.
 Memory management is the functionality of an operating system which
handles or manages primary memory and moves processes back and forth
between main memory and disk during execution.
 Memory management keeps track of each and every memory location,
regardless of either it is allocated to some process or it is free. It checks
how much memory is to be allocated to processes. It decides which
process will get memory at what time. It tracks whenever some memory
gets freed or unallocated and correspondingly it updates the status.
Why Use Memory Management?
Here, are reasons for using memory management:
 It allows you to check how much memory needs to be allocated to
processes that decide which processor should get memory at what time.
 Tracks whenever inventory gets freed or unallocated. According to it will
update the status.
 It allocates the space to application routines.
 It also make sure that these applications do not interfere with each other.
 Helps protect different processes from each other
 It places the programs in memory so that memory is utilized to its full
extent.
Memory Management Techniques
Here, are some most crucial memory management techniques:
 Single Contiguous Allocation

It is the easiest memory management technique. In this method, all types of


computer's memory except a small portion which is reserved for the OS is
available for one application. For example, MS-DOS operating system
allocates memory in this way. An embedded system also runs on a single
application.
 Partitioned Allocation

It divides primary memory into various memory partitions, which is mostly


contiguous areas of memory. Every partition stores all the information for a
specific task or job. This method consists of allotting a partition to a job
when it starts & unallocated when it ends.
 Paged Memory Management

This method divides the computer's main memory into fixed-size units
known as page frames. This hardware memory management unit maps
pages into frames which should be allocated on a page basis.
 Segmented Memory Management

Segmented memory is the only memory management method that does not
provide the user's program with a linear and contiguous address space.
Segments need hardware support in the form of a segment table. It
contains the physical address of the section in memory, size, and other
data like access protection bits and status
Static vs Dynamic Loading
 The choice between Static or Dynamic Loading is to be made at the time of
computer program being developed. If you have to load your program
statically, then at the time of compilation, the complete programs will be
compiled and linked without leaving any external program or module
dependency. The linker combines the object program with other
necessary object modules into an absolute program, which also includes
logical addresses.
 If you are writing a Dynamically loaded program, then your compiler will
compile the program and for all the modules which you want to include
dynamically, only references will be provided and rest of the work will be
done at the time of execution.
 At the time of loading, with static loading, the absolute program (and
data) is loaded into memory in order for execution to start.
 If you are using dynamic loading, dynamic routines of the library are
stored on a disk in reloadable form and are loaded into memory only
when they are needed by the program.
Static vs Dynamic Linking
 As explained above, when static linking is used, the linker combines all
other modules needed by a program into a single executable program to
avoid any runtime dependency.
 When dynamic linking is used, it is not required to link the actual module
or library with the program, rather a reference to the dynamic module is
provided at the time of compilation and linking. Dynamic Link Libraries
(DLL) in Windows and Shared Objects in Unix are good examples of
dynamic libraries.
I. Swapping
Introduction and Definitions
 A process needs to be in memory for execution. But sometimes there is not
enough main memory to hold all the currently active processes in a
timesharing system. So, excess process are kept on disk and brought in to
run dynamically.
 Swapping is a mechanism in which a process can be swapped temporarily
out of main memory (or move) to secondary storage (disk) and make that
memory available to other processes. At some later time, the system
swaps back the process from the secondary storage to main memory.
 Swapping is the process of bringing in each process in main memory,
running it for a while and then putting it back to the disk.
 Swapping is a method in which the process should be swapped
temporarily from the main memory to the backing store. It will be later
brought back into the memory for continue execution.
 Backing store is a hard disk or some other secondary storage device that
should be big enough in order to accommodate copies of all memory
images for all users. It is also capable of offering direct access to these
memory images.
 Though performance is usually affected by swapping process but it helps
in running multiple and big processes in parallel and that's the
reason Swapping is also known as a technique for memory
compaction.
The total time taken by swapping process includes the time it takes to move the
entire process to a secondary disk and then to copy the process back to
memory, as well as the time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard disk
where swapping will take place has a data transfer rate around 1 MB per second.
The actual transfer of the 1000K process to or from memory will take
2048KB / 1024KB per second
= 2 seconds
= 2000 milliseconds
Now considering in and out time, it will take complete 4000 milliseconds plus
other overhead where the process competes to regain main memory.
Benefits of Swapping

 It offers a higher degree of multiprogramming.

 Allows dynamic relocation. For example, if address binding at execution


time is being used, then processes can be swap in different locations. Else
in case of compile and load time bindings, processes should be moved to
the same location.
 It helps to get better utilization of memory.

 Minimum wastage of CPU time on completion so it can easily be applied to


a priority-based scheduling method to improve its performance.
II. Contiguous Memory Allocation
What is Memory allocation?
Memory allocation is a process by which computer programs are assigned
memory or space.
Partition Allocation
Memory is divided into different blocks or partitions. Each process is allocated
according to the requirement. Partition allocation is an ideal method to avoid
internal fragmentation.
Below are the various partition allocation schemes :
 First Fit: In this type fit, the partition is allocated, which is the first
sufficient block from the beginning of the main memory.
 Best Fit: It allocates the process to the partition that is the first smallest
partition among the free partitions.
 Worst Fit: It allocates the process to the partition, which is the largest
sufficient freely available partition in the main memory.
 Next Fit: It is mostly similar to the first Fit, but this Fit, searches for the first
sufficient partition from the last allocation point .
Main memory usually has two partitions
 Low Memory − Operating system resides in this memory.
 High Memory − User processes are held in high memory.
Operating system uses the following memory allocation mechanism.

S.N. Memory Allocation & Description

1 Single-partition allocation
In this type of allocation, relocation-register scheme is used to
protect user processes from each other, and from changing
operating-system code and data.
Relocation register contains value of smallest physical address
whereas limit register contains range of logical addresses. Each
logical address must be less than the limit register.

2 Multiple-partition allocation
In this type of allocation, main memory is divided into a number of
fixed-sized partitions where each partition should contain only one
process. When a partition is free, a process is selected from the input
queue and is loaded into the free partition. When the process
terminates, the partition becomes available for another process.
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
 Processes are stored and removed from memory, which creates free
memory space, which are too small to use by other processes.
 After sometimes, that processes not able to allocate to memory blocks
because its small size and memory blocks always remain unused is called
fragmentation. This type of problem happens during a dynamic memory
allocation system when free blocks are quite small, so it is not able to fulfill
any request.
 Fragmentation occurs in a dynamic memory allocation system when most
of the free blocks are too small to satisfy any request. It is generally termed
as inability to use the available memory.
Types of Fragmentation
1. External fragmentation
2. Internal fragmentation
External Fragmentation
 In such situation processes are loaded and removed from the memory. As a
result of this, free holes exists to satisfy a request but is non contiguous i.e. the
memory is fragmented into large no. Of small holes. This phenomenon is
known as 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
 External fragmentation can be reduced by rearranging memory contents to
place all free memory together in a single block .
Internal Fragmentation
 Memory block assigned to process is bigger. Some portion of memory is left
unused, as it cannot be used by another processes
 Also, at times the physical memory is broken into fixed size blocks and
memory is allocated in unit of block sizes. The memory allocated to a space
may be slightly larger than the requested memory. The difference between
allocated and required memory is known as Internal fragmentation i.e. the
memory that is internal to a partition but is of no use
 The internal fragmentation can be reduced by assigning the smallest partition,
which is still good enough to carry the entire process
The following diagram shows how fragmentation can cause waste of memory
and a compaction technique can be used to create more free memory out of
fragmented memory
 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 .
III. Paging
What is Paging?
In a memory management system that takes advantage of paging, the OS reads
data from secondary storage in blocks called , all of which have identical size.
The physical region of memory containing a single page is called a frame. When
paging is used, a frame does not have to comprise a single physically contiguous
region in secondary storage. This approach offers an advantage over earlier
memory management methods, because it facilitates more efficient and faster use
of storage
 Non-contiguous memory allocation is a memory allocation technique.
 It allows to store parts of a single process in a non-contiguous fashion.
Thus, different parts of the same process can be stored at different places in the
main memory
Paging is a storage mechanism that allows OS to retrieve processes from the
secondary storage into the main memory in the form of pages. In the Paging
method, the main memory is divided into small fixed-size blocks of physical
memory, which is called frames. The size of a frame should be kept the same as
that of a page to have maximum utilization of the main memory and to avoid
external fragmentation. Paging is used for faster access to data, and it is a logical
concept.
Paging is a method of writing to, and reading it from, for use in , also known as
main memory. Paging plays a role in memory management for a
computer's operating system
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 that's set up to emulate the computer's RAM. Paging technique plays an
important role in implementing virtual memory
 Non-contiguous memory allocation is a memory allocation technique.
 It allows to store parts of a single process in a non-contiguous fashion.
 Thus, different parts of the same process can be stored at different places
in the main memory.
Techniques
There are two popular techniques used for non-contiguous memory allocation

Paging- Features
Paging is a fixed size partitioning scheme.
 In paging, secondary memory and main memory are divided into equal fixed
size partitions.
 The partitions of secondary memory are called as pages.
 The partitions of main memory are called as frames.

 Each process is divided into parts where size of each part is same as page
size.
 The size of the last part may be less than the page size.

 The pages of process are stored in the frames of main memory depending
upon their availability.
Example
Consider a process is divided into 4 pages P 0, P1, P2 and P3.
 Depending upon the availability, these pages may be stored in the

main memory frames in a non-contiguous fashion as shown in the following


figure

Translating Logical Address into Physical Address
 CPU always generates a logical address.

 A physical address is needed to access the main memory.

Following steps are followed to translate logical address into physical address
Step-01:
CPU generates a logical address consisting of two parts
1. Page Number
2. Page Offset

 Page Number specifies the specific page of the process from which CPU
wants to read the data.
 Page Offset specifies the specific word on the page that CPU wants to read .

Step-02:
For the page number generated by the CPU,
 provides the corresponding frame number (base address of the frame)
where that page is stored in the main memory.
Step-03:
The frame number combined with the page offset forms the required physical
address.

 Frame number specifies the specific frame where the required page is
stored.
 Page Offset specifies the specific word that has to be read from that page.

Diagram
The following diagram illustrates the above steps of translating logical address
into physical address-
Advantages
 Easy to use memory management algorithm
 No need for external Fragmentation
 Swapping is easy between equal-sized pages and page frames.
 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
 It allows to store parts of a single process in a non-contiguous fashion.

Disadvantages
 There is an overhead of maintaining a page table for each process.

 The time taken to fetch the instruction increases since now two memory
accesses are required.
 Complex memory management algorithm

 Multi-level paging may lead to memory reference overhead.

 Paging reduces external fragmentation, but still suffer from internal


fragmentation.
 Page table requires extra memory space, so may not be good for a system
having small RAM.
Example – I
Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages (size is power of 2, between
512 bytes and 8192 bytes). The size of the process is measured in the number of
pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical)
memory called frames and the size of a frame is kept the same as that of a page
to have optimum utilization of the main memory and to avoid external
fragmentation.
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
 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
Example-II
For example, if the main memory size is 16 KB and Frame size is 1 KB. Here, the
main memory will be divided into the collection of 16 frames of 1 KB each.
There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB
each. Here, all the processes are divided into pages of 1 KB each so that
operating system can store one page in one frame.
At the beginning of the process, all the frames remain empty so that all the pages
of the processes will get stored in a contiguous way.

In this example you can see that A2 and A4 are moved to the waiting state after
some time. Therefore, eight frames become empty, and so other pages can be
loaded in that empty blocks. The process A5 of size 8 pages (8 KB) are waiting in
the ready queue.
In this example, you can see that there are eight non-contiguous frames which is
available in the memory, and paging offers the flexibility of storing the process
at the different places. This allows us to load the pages of process A5 instead of
A2 and A4.
IV. structure of the Page Table
Page Table
 Page Table is a data structure used by the virtual memory system to store
the mapping between logical addresses and physical addresses.
 Logical addresses are generated by the CPU for the pages of the processes
therefore they are generally used by the processes.
 Physical addresses are the actual frame address of the memory. They are
generally used by the hardware or more specifically by RAM subsystems.
 Paging is a non-contiguous memory allocation technique.
 The logical address generated by the CPU is translated into the physical
address using the page table.
Characteristics
 Page table is stored in the main memory.
 Number of entries in a page table = Number of pages in which the process
is divided.
 Page Table Base Register (PTBR) contains the base address of page table.
 Each process has its own independent page table.
Working

 Page Table Base Register (PTBR) provides the base address of the page
table.
 The base address of the page table is added with the page number
referenced by the CPU.
 It gives the entry of the page table containing the frame number where the
referenced page is stored.
Page Table Entry
A page table entry contains several information about the page.
 The information contained in the page table entry varies from operating
system to operating system.
 The most important information in a page table entry is frame number.
In general, each entry of a page table contains the following information

1. Frame Number
 Frame number specifies the frame where the page is stored in the main
memory.
 The number of bits in frame number depends on the number of frames in
the main memory.
2. Present / Absent Bit
 This bit is also sometimes called as valid / invalid bit.
 This bit specifies whether that page is present in the main memory or not.
 If the page is not present in the main memory, then this bit is set to 0
otherwise set to 1.
3. Protection Bit
 This bit is also sometimes called as “Read / Write bit“.
 This bit is concerned with the page protection.
 It specifies the permission to perform read and write operation on the
page.
 If only read operation is allowed to be performed and no writing is
allowed, then this bit is set to 0.
 If both read and write operation are allowed to be performed, then this bit
is set to 1.
4. Reference Bit
 Reference bit specifies whether that page has been referenced in the last
clock cycle or not.
 If the page has been referenced recently, then this bit is set to 1 otherwise
set to 0.
5. Caching Enabled / Disabled
 This bit enables or disables the caching of page.
 Whenever freshness in the data is required, then caching is disabled using
this bit.
 If caching of the page is disabled, then this bit is set to 1 otherwise set to 0.
6. Dirty Bit
 This bit is also sometimes called as “Modified bit“.
 This bit specifies whether that page has been modified or not.
 If the page has been modified, then this bit is set to 1 otherwise set to 0.
V. Segmentation
 Segmentation is a memory management technique in which each job is
divided into several segments of different sizes, one for each module that
contains pieces that perform related functions. Each segment is actually a
different logical address space of the program.
 When a process is to be executed, its corresponding segmentation are
loaded into non-contiguous memory though every segment is loaded into
a contiguous block of available memory.
 Segmentation memory management works very similar to paging but here
segments are of variable-length where as in paging pages are of fixed
size.
 A program segment contains the program's main function, utility functions,
data structures, and so on. The operating system maintains a segment
map table for every process and a list of free memory blocks along with
segment numbers, their size and corresponding memory locations in main
memory.
 For each segment, the table stores the starting address of the segment and
the length of the segment. A reference to a memory location includes a
value that identifies a segment and an offset.
 Like Paging, Segmentation is another non-contiguous memory allocation
technique.
 In segmentation, process is not divided blindly into fixed size pages.
 Rather, the process is divided into modules for better visualization
Characteristics
Segmentation is a variable size partitioning scheme.
 In segmentation, secondary memory and main memory are divided into
partitions of unequal size.
 The size of partitions depend on the length of modules.
 The partitions of secondary memory are called as segments
Example
Consider a program is divided into 5 segments as

Segment Table

 Segment table is a table that stores the information about each segment of
the process.
 It has two columns.
 First column stores the size or length of the segment.
 Second column stores the base address or starting address of the segment
in the main memory.
 Segment table is stored as a separate segment in the main memory.
 Segment table base register (STBR) stores the base address of the segment
table.
For the above illustration, consider the segment table is -
Here,
 Limit indicates the length or size of the segment.
 Base indicates the base address or starting address of the segment in the
main memory.
 In accordance to the above segment table, the segments are stored in the
main memory as-

Translating Logical Address into Physical Address


 CPU always generates a logical address.
 A physical address is needed to access the main memory .
Following steps are followed to translate logical address into physical address-
Step-01:
CPU generates a logical address consisting of two parts-
1. Segment Number
2. Segment Offset
 Segment Number specifies the specific segment of the process from which
CPU wants to read the data.
 Segment Offset specifies the specific word in the segment that CPU wants to
read.
Step-02:
For the generated segment number, corresponding entry is located in the
segment table.
 segment offset is compared with the limit (size) of the segment.
Now, two cases are possible-
Case-01: Segment Offset >= Limit
 If segment offset is found to be greater than or equal to the limit, a trap is
generated.
Case-02: Segment Offset < Limit
 If segment offset is found to be smaller than the limit, then request is
treated as a valid request.
 The segment offset must always lie in the range [0, limit-1],
 Then, segment offset is added with the base address of the segment.
 The result obtained after addition is the address of the memory location
storing the required word.
The following diagram illustrates the above steps of translating logical
address into physical address-
Advantages
 It allows to divide the program into modules which provides better
visualization.
 Segment table consumes less space
 It solves the problem of internal fragmentation.
 Disadvantages
 There is an overhead of maintaining a segment table for each process.
 The time taken to fetch the instruction increases since now two memory
accesses are required.
 Segments of unequal size are not suited for swapping.
 It suffers from external fragmentation as the free space gets broken down
into smaller pieces with the processes being loaded and removed from the
main memory.

PART-B Virtual Memory Management


Introduction to 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.
 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.
 Following are the situations, when entire program is not required to be
loaded fully in main memory.
 User written error handling routines are used only when an error
occurred in the data or computation.
 Certain options and features of a program may be used rarely.
 Many tables are assigned a fixed amount of address space even
though only a small amount of the table is actually used.
 The ability to execute a program that is only partially in memory
would counter many benefits.
 Less number of I/O would be needed to load or swap each user
program into memory.
 A program would no longer be constrained by the amount of
physical memory that is available.
 Each user program could take less physical memory, more
programs could be run the same time, with a corresponding
increase in CPU utilization and throughput.
 Modern microprocessors intended for general-purpose use, a
memory management unit, or MMU, is built into the hardware. The
MMU's job is to translate virtual addresses into physical addresses.
A basic example is given below

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

I. Demand Paging
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.
Fault rate
Fault rate is a frequency with which a designed system or component fails. It is
expressed in failures per unit of time. It is denoted by the Greek letter λ
(lambda).
For example:
Let's assume that an OS requires 300 MB of memory to store all the running
programs. However, there's currently only 50 MB of available physical memory
stored on the RAM.
 The OS will then set up 250 MB of virtual memory and use a program called
the Virtual Memory Manager(VMM) to manage that 250 MB.
 So, in this case, the VMM will create a file on the hard disk that is 250 MB in
size to store extra memory that is required.
 The OS will now proceed to address memory as it considers 300 MB of real
memory stored in the RAM, even if only 50 MB space is available.
 It is the job of the VMM to manage 300 MB memory even if just 50 MB of
real memory space is available.
Advantages
Following are the advantages of Demand Paging
 Large virtual memory.

 More efficient use of memory.

 There is no limit on degree of multiprogramming.

 Virtual memory helps to gain speed when only a particular segment of the
program is required for the execution of the program.
 It is very helpful in implementing a multiprogramming environment.

 It allows you to run more applications at once.

 It helps you to fit many large programs into smaller programs.

 Common data or code may be shared between memory.

 Process may become even larger than all of the physical memory.
 Data / code should be read from disk whenever required.

 The code can be placed anywhere in physical memory without requiring


relocation.
 More processes should be maintained in the main memory, which
increases the effective use of CPU.
 Each page is stored on a disk until it is required after that, it will be
removed.
 It allows more applications to be run at the same time.

 There is no specific limit on the degree of multiprogramming.

 Large programs should be written, as virtual address space available is


more compared to physical memory.
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.
 Applications may run slower if the system is using virtual memory.
 Likely takes more time to switch between applications.
 Offers lesser hard drive space for your use.
 It reduces system stability.
 It allows larger applications to run in systems that don't offer enough
physical RAM alone to run them.
 It doesn't offer the same performance as RAM.
 It negatively affects the overall performance of a system.
 Occupy the storage space, which may be used otherwise for long term
data storage
 II.Copy on Write
 Copy on Write or simply COW is a resource management technique. One
of its main use is in the implementation of the fork system call in which it
shares the virtual memory(pages) of the OS.
 In UNIX like OS, fork() system call creates a duplicate process of the parent
process which is called as the child process.
 The idea behind a copy-on-write is that when a parent process creates a
child process then both of these processes initially will share the same
pages in memory and these shared pages will be marked as copy-on-write
which means that if any of these processes will try to modify the shared
pages then only a copy of these pages will be created and the
modifications will be done on the copy of pages by that process and thus
not affecting the other process.
 Suppose, there is a process P that creates a new process Q and then
process P modifies page 3.
The below figures shows what happens before and after process P modifies
page 3.

III. Page-Replacement Algorithms


 Page replacement algorithms are the techniques using which an Operating
System decides which memory pages to swap out, write to disk when a page
of memory needs to be allocated. Paging happens whenever a page fault
occurs and a free page cannot be used for allocation purpose accounting to
reason that pages are not available or the number of free pages is lower than
required pages.
 When the page that was selected for replacement and was paged out, is
referenced again, it has to read in from disk, and this requires for I/O
completion. This process determines the quality of the page replacement
algorithm: the lesser the time waiting for page-ins, the better is the algorithm.
 A page replacement algorithm looks at the limited information about
accessing the pages provided by hardware, and tries to select which pages
should be replaced to minimize the total number of page misses, while
balancing it with the costs of primary storage and processor time of the
algorithm itself.
 There are many different page replacement algorithms.
 We evaluate an algorithm by running it on a particular string of memory
reference and computing the number of page faults
Reference String
 The string of memory references is called reference string. Reference strings are
generated artificially or by tracing a given system and recording the address of
each memory reference. The latter choice produces a large number of data,
where we note two things.
 For a given page size, we need to consider only the page number, not the entire
address.
 If we have a reference to a page p, then any immediately following references to
page p will never cause a page fault. Page p will be in memory after the first
reference; the immediately following references will not fault.
 For example, consider the following sequence of addresses –

123,215,600,1234,76,96
 If page size is 100, then the reference string is 1,2,6,12,0,0

First In First Out (FIFO) algorithm


FIFO (First-in-first-out) is a simple implementation method.
In this method, memory selects the page for a replacement that has been in the
virtual address of the memory for the longest time
Features:
 Whenever a new page loaded, the page recently comes in the memory is
removed. So, it is easy to decide which page requires to be removed as its
identification number is always at the FIFO stack.
 The oldest page in the main memory is one that should be selected for
replacement first.
 Easy to implement, keep a list, replace pages from the tail and add new
pages at the head.

Optimal Page algorithm


The optimal page replacement method selects that page for a replacement for
which the time to the next reference is the longest.
Features:
 Optimal algorithm results in the fewest number of page faults. This
algorithm is difficult to implement.
 An optimal page-replacement algorithm method has the lowest page-fault
rate of all algorithms. This algorithm exists and which should be called MIN
or OPT.
 Replace the page which unlike to use for a longer period of time. It only
uses the time when a page needs to be used.
 An optimal page-replacement algorithm has the lowest page-fault rate of
all algorithms. An optimal page-replacement algorithm exists, and has
been called OPT or MIN.
 Replace the page that will not be used for the longest period of time. Use
the time when a page is to be used.

Least Recently Used (LRU) algorithm


The full form of LRU is the Least Recently Used page. This method helps OS to find
page usage over a short period of time. This algorithm should be implemented
by associating a counter with an even- page.
How does it work?
 Page, which has not been used for the longest time in the main memory, is
the one that will be selected for replacement.
Features:
 The LRU replacement method has the highest count. This counter is also
called aging registers, which specify their age and how much their
associated pages should also be referenced.
 The page which hasn't been used for the longest time in the main memory
is the one that should be selected for replacement.
 It also keeps a list and replaces pages by looking back into time.
 Easy to implement, keep a list, replace pages by looking back into time .
Page Buffering algorithm
 To get a process start quickly, keep a pool of free frames.
 On page fault, select a page to be replaced.
 Write the new page in the frame of free pool, mark the page table and
restart the process.
 Now write the dirty page out of disk and place the frame holding replaced
page in free pool.
Least frequently Used(LFU) algorithm
 The page with the smallest count is the one which will be selected for
replacement.
 This algorithm suffers from the situation in which a page is used heavily
during the initial phase of a process, but then is never used again.
Most frequently Used(MFU) algorithm
 This algorithm is based on the argument that the page with the smallest
count was probably just brought in and has yet to be used.
IV.Thrashing
Thrashing in Operating System (OS)
When a program need space larger than RAM or it need space when RAM is full,
Operating System will try to allocate space from secondary memory and behaves
like it has that much amount of memory by serving to that program. This concept
is called virtual memory. To know about thrashing we first need to know what is
page fault and swapping.
Page fault and swapping: We know every program divided into some pages.
When a program need a page which is not in RAM that is called page fault.
Whenever a page fault happens, operating system will try to fetch that page from
secondary memory and try to swap it with one of the page in RAM. This is called
swapping.
If this page fault and then swapping happening very frequently at higher rate,
then operating system has to spend more time to swap these pages. This state is
called thrashing. Because of this, CPU utilization is going to be reduced
Cause of Thrashing-
1. High degree of multiprogramming: The CPU scheduler sees the
decreasing CPU utilization and increases the degree of multiprogramming
as a result. The new process tries to get started by taking frames from
running processes, causing more page faults and a longer queue for the
paging device. As a result, CPU utilization drops even further, and the CPU
scheduler tries to increase the degree of multiprogramming even more
2. Lack of frames: If a process has less number of frames then less pages of
that process will be able to reside in memory and hence it would result in
more frequent swapping. This may lead to thrashing. Hence sufficient
amount of frames must be allocated .
Recovery
 Do not allow the system to go into thrashing by instructing the long term
scheduler not to bring the processes into memory after the threshold.
 If the system is already in thrashing then instruct the mid term schedular to
suspend some of the processes so that we can recover the system from
thrashing.
Effect of Thrashing
Whenever thrashing starts, operating system tries to apply either Global page
replacement Algorithm or Local page replacement algorithm.
Global Page Replacement
Since global page replacement can access to bring any page, it tries to bring
more pages whenever thrashing found. But what actually will happen is, due to
this, no process gets enough frames and by result thrashing will be increase
more and more. So global page replacement algorithm is not suitable when
thrashing happens.
Local Page Replacement
Unlike global page replacement algorithm, local page replacement will select
pages which only belongs to that process. So there is a chance to reduce the
thrashing. But it is proven that there are many disadvantages if we use local page
replacement. So local page replacement is just alternative than global page
replacement in thrashing scenario.
Techniques to Handle Thrashing
We already seen Local replacement is better than Global replacement to avoid
thrashing. But it also has disadvantages and not suggestable. Some more
techniques are
Working Set Model
This model is based on locality. What locality is saying, the page used recently
can be used again and also the pages which are nearby this page will also be
used. Working set means set of pages in the most recent D time. The page which
completed its D amount of time in working set automatically dropped from it. So
accuracy of working set depends on D we have chosen. This working set model
avoid thrashing while keeping the degree of multiprogramming as high as
possible.
Page Fault Frequency

It is some direct approach than working set model. When thrashing occurring we
know that it has few number of frames. And if it is not thrashing that means it has
too many frames. Based on this property we assign an upper and lower bound for
the desired page fault rate. According to page fault rate we allocate or remove
pages. If the page fault rate become less than the lower limit, frames can be
removed from the process. Similarly, if the page fault rate become more than the
upper limit, more number of frames can be allocated to the process. And if no
frames available due to high page fault rate, we will just suspend the processes
and will restart them again when frames available.
V. Memory-mapped files
Consider a sequential read of a file on disk using the standard system calls
openQ, readO, and writeQ. Each file access requires a system call and disk
access. Alternatively, we can use the virtual memory techniques discussed so far
to treat file I/O as routine memory accesses. This approach, known as memory
mapping a file, allows a part of the virtual address space to be logically
associated with the file.
Basic Mechanism
Memory mapping a file is accomplished by mapping a disk block to a page (or
pages) in memory. Initial access to the file proceeds through ordinary demand
paging, resulting in a page fault. However, a page-sized portion of the file is
read from the file system into a physical page (some systems may opt to read in
more than a page-sized chunk of memory at a time).
Subsequent reads and writes to the file are handled as routine memory accesses,
thereby simplifying file access and usage by allowing the system to manipulate
files through memory rather than incurring the overhead of using the readQ and
writeO system calls. Note that writes to the file mapped in memory are not
necessarily immediate (synchronous) writes to the file on disk. Some systems
may choose to update the physical file when the operating system periodically
checks whether the page in memory has been modified. When the file is closed,
all the memory-mapped data are written back to disk and removed from the
virtual memory of the process

Some operating systems provide memory mapping only through a specific


system call and use the standard system calls to perform all other file I/O.
However, some systems choose to memory-map a file regardless of whether the
file was specified as memory-mapped. Let's take Solaris as an example. If a file is
specified as memory-mapped (using the mmapO system call), Solaris maps the
file into the address space of the process.
If a file is opened and accessed using ordinary system calls, such as openO,
read(), and write(), Solaris still memory-maps the file; however, the file is
mapped to the kernel address space. Regardless of how the file is opened, then,
Solaris treats all file I/O as memory-mapped, allowing file access to take place
via the efficient memory subsystem. Multiple processes may be allowed to map
the same file concurrently, to allow sharing of data. Writes by any of the
processes modify the data in virtual memory and can be seen by all others that
map the same section of the file. Given our earlier discussions of virtual memory,
it should be clear how the sharing of memory-mapped sections of memory is
implemented:
Processes can communicate using shared memory by having the communicating
processes memory-map the same file into their virtual address spaces. The
memory mapped file serves as the region of shared meniory between the
communicating processes (Figure 9.24). In the following section, we illustrate
support in the Win32 API for shared memory using memory-mapped files.

Shared Memory in the Win32 API


The general outline for creating a region of shared, memory using memory
mapped files in the Win32 API involves first creating a file mapping for the file to
be mapped and then establishing a view of the mapped file in a process's virtual
address space. A second process can then open and create a view of the
mapped file in its virtual address space. The mapped file represents the shared-
memory object that will enable communication to take place between the
processes. We next illustrate these steps in more detail. In this example, a
producer process first creates a shared-memory object using the memory-
mapping features available in the Win32 API.
The producer then writes a message to shared memory. After that, a consumer
process opens a mapping tp the shared-memory object and reads the message
written by the consumer. To establish a memory-mapped file, a process first
opens the file to be mapped with the CreateFileO function, which returns a
HANDLE to the opened file. The process then creates a mapping of this file
HANDLE using the CreateFileMappingO function.

The consumer then reads from shared memory the message "Shared memory
message" that was written by the producer process. Finally, both processes
remove the view of the mapped file with a call to UnmapViewOfFileO. We
provide a programming exercise at the end of this chapter using shared memory
with memory mapping in the Win32 API
Memory-Mapped I/O
In the case of I/O, as mentioned in Section 1.2.1, each I/O controller includes
registers to hold commands and the data being transferred. Usually, special I/O
instructions allow data transfers between these registers and system memory. To
allow more convenient access to I/O devices, many computer architectures
provide memory-mapped I/O. In this case, ranges of memory addresses are set
aside and are mapped to the device registers.
Reads and writes to these memory addresses cause the data to be transferred to
and from the device registers. This method is appropriate for devices that have
fast response times, such as video controllers. In the IBM PC, each location on
the screen is mapped to a memory location. Displaying text on the screen is
almost as easy as writing the text into the appropriate memory-mapped
locations. Memory-mapped I/O is also convenient for other devices, such as the
serial and parallel ports used to connect modems and printers to a computer.
The CPU transfers data through these kinds of devices by reading and writing a
few device registers, called an I/O port. To send out a long string of bytes
through a memory-mapped serial port, the CPU writes one data byte to the data
register and sets a bit in the control register to signal that the byte is available.
The device takes the data byte and then clears the bit in the control register to
signal that it is ready for the next byte. Then the CPU can transfer the next byte.
If the CPU uses polling to watch the control bit, constantly looping to see whether
the device is ready, this method of operation is called programmed I/O (PIO). If
the CPU does not poll the control bit, but instead receives an interrupt when the
device is ready for the next byte, the data transfer is said to be interrupt driven.
VI. Kernel memory allocation
Allocating Kernel Memory
When a process running in user mode requests additional memory, pages are
allocated from the list of free page frames maintained by the kernel. This list is
typically populated using a page-replacement algorithm such as those discussed
in Section 9.4 and most likely contains free pages scattered throughout physical
memory, as explained earlier. Remember, too, that if a user process requests a
single byte of memory, internal fragmentation will result, as the process will be
granted, an entire page frame. Kernel memory, however, is often allocated from
a free-memory pool different from the list used to satisfy ordinary user-mode
processes. There are two primary reasons for this:
1. The kernel requests memory for data structures of varying sizes, some of
which are less than a page in size. As a result, the kernel must use memory
conservatively and attempt to minimize waste due to fragmentation. This is
especially important because many operating systems do not subject kernel
code or data to the paging system.
2. Pages allocated to user-mode processes do not necessarily have to be in
contiguous physical memory. However, certain hardware devices interact
directly with physical memory—-without the benefit of a virtual memory
interface—and consequently may require memory residing in physically
contiguous pages. In the following sections, we examine two strategies for
managing free memory that is assigned to kernel processes.
Buddy System
Buddy allocation system is an algorithm in which a larger memory block is
divided into small parts to satisfy the request. This algorithm is used to give
best fit. The two smaller parts of block are of equal size and called as buddies.
In the same manner one of the two buddies will further divide into smaller parts
until the request is fulfilled. Benefit of this technique is that the two buddies can
combine to form the block of larger size according to the memory request.

Example – If the request of 25Kb is made then block of size 32Kb is allocated
Four Types of Buddy System –
1. Binary buddy system
2. Fibonacci buddy system
3. Weighted buddy system
4. Tertiary buddy system

Why buddy system?

If the partition size and process size are different then poor match occurs and
may use space inefficiently.

It is easy to implement and efficient then dynamic allocation.

Binary buddy system –

The buddy system maintains a list of the free blocks of each size (called a free
list), so that it is easy to find a block of the desired size, if one is available. If no
block of the requested size is available, Allocate searches for the first nonempty
list for blocks of at least the size requested. In either case, a block is removed
from the free list.
Example – Assume the size of memory segment is initially 256kb and the kernel
requests 25kb of memory. The segment is initially divided into two buddies. Let
we call A1 and A2 each 128kb in size. One of these buddies is further divided
into two 64kb buddies let say B1 and B2. But the next highest power of 25kb is
32kb so, either B1 or B2 is further divided into two 32kb buddies(C1 and C2)
and finally one of these buddies is used to satisfy the 25kb request. A split block
can only be merged with its unique buddy block, which then reforms the larger
block they were split from.
Fibonacci buddy system –
This is the system in which blocks are divided into sizes which are fibonacci
numbers. It satisfy the following relation:
Zi = Z(i-1)+Z(i-2)
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 144, 233, 377, 610.
The address calculation for the binary and weighted buddy systems is straight
forward, but the original procedure for the Fibonacci buddy system was either
limited to a small, fixed number of block sizes or a time consuming computation.
Advantages –
In comparison to other simpler techniques such as dynamic allocation, the buddy
memory system has little external fragmentation.
 The buddy memory allocation system is implemented with the use of a
binary tree to represent used or unused split memory blocks.
 The buddy system is very fast to allocate or de allocate memory.
 In buddy systems, the cost to allocate and free a block of memory is low
compared to that of best-fit or first-fit algorithms.
 Other advantage is coalescing.
 Address calculation is easy.
What is coalescing?
It is defined as how quickly adjacent buddies can be combined to form larger
segments this is known as coalescing.

For example, when the kernel releases the C1 unit it was allocated, the system
can coalesce C1 and C2 into a 64kb segment. This segment B1 can in turn be
coalesced with its buddy B2 to form a 128kb segment. Ultimately we can end up
with the original 256kb segment.
Drawback –
The main drawback in buddy system is internal fragmentation as larger block of
memory is acquired then required. For example if a 36 kb request is made then
it can only be satisfied by 64 kb segment and remaining memory is wasted.

Slab Allocation

A second strategy for allocating kernel memory is known as slab allocation. It


eliminates fragmentation caused by allocations and deallocations. This method
is used to retain allocated memory that contains a data object of a certain type
for reuse upon subsequent allocations of objects of the same type. In slab
allocation memory chunks suitable to fit data objects of certain type or size are
preallocated. Cache does not free the space immediately after use although it
keeps track of data which are required frequently so that whenever request is
made the data will reach very fast. Two terms required are:
 Slab – A slab is made up of one or more physically contiguous pages. The
slab is the actual container of data associated with objects of the specific kind
of the containing cache.
 Cache – Cache represents a small amount of very fast memory. A cache
consists of one or more slabs. There is a single cache for each unique kernel
data structure.

Example –
 A separate cache for a data structure representing processes descriptors
 Separate cache for file objects
 Separate cache for semaphores etc.
Each cache is populated with objects that are instantiations of the kernel data
structure the cache represents. For example the cache representing
semaphores stores instances of semaphore objects, the cache representing
process descriptors stores instances of process descriptor objects.
Implementation –
The slab allocation algorithm uses caches to store kernel objects. When a cache
is created a number of objects which are initially marked as free are allocated
to the cache. The number of objects in the cache depends on size of the
associated slab.
Example – A 12 kb slab (made up of three contiguous 4 kb pages) could store
six 2 kb objects. Initially all objects in the cache are marked as free. When a
new object for a kernel data structure is needed, the allocator can assign any
free object from the cache to satisfy the request. The object assigned from the
cache is marked as used.
In linux, a slab may in one of three possible states:
1. Full – All objects in the slab are marked as used
2. Empty – All objects in the slab are marked as free
3. Partial – The slab consists of both
The slab allocator first attempts to satisfy the request with a free object in a
partial slab. If none exists, a free object is assigned from an empty slab. If no
empty slabs are available, a new slab is allocated from contiguous physical
pages and assigned to a cache.
Benefits of slab allocator –
 No memory is wasted due to fragmentation because each unique kernel data
structure has an associated cache.
 Memory request can be satisfied quickly.
 The slab allocating scheme is particularly effective for managing when objects
are frequently allocated or deallocated. The act of allocating and releasing
memory can be a time consuming process. However, objects are created in
advance and thus can be quickly allocated from the cache. When the kernel has
finished with an object and releases it, it is marked as free and return to its
cache, thus making it immediately available for subsequent request from the
kernel.

You might also like