Unit-3 (OS)
Unit-3 (OS)
Chapter 1
Swapping
Swapping is a process of swap a process temporarily into a secondary memory
from the main memory, which is fast as compared to secondary memory.
A swapping allows more processes to be run and can be fit into memory at one time.
The main part of swapping is transferred time and the total time directly
proportional to the amount of memory swapped. Swapping is also known as roll-out,
roll in, because if a higher priority process arrives and wants service, the memory
manager can swap out the lower priority process and then load and execute the
higher priority process. After finishing higher priority work, the lower priority
process swapped back in memory and continued to the execution process.
1|Page
Contiguous Memory Allocation
The main memory should oblige 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.
Here, in this diagram 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.
3|Page
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.
4|Page
Internal fragmentation:
Paging:
5|Page
Example:
If Logical Address = 31 bits, then Logical Address Space = 2 31 words = 2 G
words (1 G = 230)
If Logical Address Space = 128 M words = 2 7 * 220 words, then Logical
Address = log2 227 = 27 bits
If Physical Address = 22 bits, then Physical Address Space = 2 22 words = 4
M words (1 M = 220)
If Physical Address Space = 16 M words = 2 4 * 220 words, then Physical
Address = log2 224 = 24 bits
6|Page
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.
A process is divided into Segments. The chunks that a program is divided into
which are not necessarily all of the same sizes are called segments. Segmentation
gives user’s view of the process which paging does not give. Here the user’s view is
mapped to physical memory.
8|Page
Translation of Two dimensional Logical Address to one dimensional Physical Address.
9|Page
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
There is no internal fragmentation.
Segment Table consumes less space in comparison to Page table in paging.
Disadvantage of Segmentation
As processes are loaded and removed from the memory, the free memory
space is broken into little pieces, causing External fragmentation.
10 | P a g e
Chapter- 2
Virtual Memory
Introduction:
In this scheme, User can load the bigger size processes than the available main
memory by having the illusion that the memory is available to load the process.
Instead of loading one big process in the main memory, the Operating System loads
the different parts of more than one process in the main memory. By doing this, the
degree of multiprogramming will be increased and therefore, the CPU utilization will
also be increased.
In modern world, virtual memory has become quite common these days. In this
scheme, whenever some pages needs to be loaded in the main memory for the
execution and the memory is not available for those many pages, then in that case,
instead of stopping the pages from entering in the main memory, the OS search for
the RAM area that are least used in the recent times or that are not referenced and
copy that into the secondary memory to make the space for the new pages in the
main memory.
Since all this procedure happens automatically, therefore it makes the computer
feel like it is having the unlimited RAM.
Let us assume 2 processes, P1 and P2, contains 4 pages each. Each page size is
1 KB. The main memory contains 8 frame of 1 KB each. The OS resides in the first
two partitions. In the third partition, 1 st page of P1 is stored and the other frames are
also shown as filled with the different pages of processes in the main memory.
The page tables of both the pages are 1 KB size each and therefore they can be fit in
one frame each. The page tables of both the processes contain various information
that is also shown in the image.
The CPU contains a register which contains the base address of page table that
is 5 in the case of P1 and 7 in the case of P2. This page table base address will be
added to the page number of the Logical address when it comes to accessing the
actual corresponding entry.
11 | P a g e
Advantages of Virtual Memory
Demand Paging
Every process in the virtual memory contains lots of pages and in some cases;
it might not be efficient to swap all the pages for the process at once. Because it
might be possible that the program may need only a certain page for the application
to run. Let us take an example here, suppose there is a 500 MB application and it
may need as little as 100MB pages to be swapped, so in this case, there is no need to
swap all pages at once.
The demand paging system is somehow similar to the paging system with
swapping where processes mainly reside in the main memory (usually in the hard
disk). Thus demand paging is the process that solves the above problem only by
swapping the pages on Demand. This is also known as lazy swapper ( It never swaps
the page into the memory unless it is needed).
12 | P a g e
Swapper that deals with the individual pages of a process are referred to
as Pager.
Demand Paging is a technique in which a page is usually brought into the main
memory only when it is needed or demanded by the CPU. Initially, only those pages
are loaded that are required by the process immediately. Those pages that are never
accessed are thus never loaded into the physical memory.
Valid-Invalid Bit
Some form of hardware support is used to distinguish between the pages that are
in the memory and the pages that are on the disk. Thus for this purpose Valid-Invalid
scheme is used:
With each page table entry, a valid-invalid bit is associated
(where 1 indicates in the memory and 0 indicates not in the memory)
Initially, the valid-invalid bit is set to 0 for all table entries.
1. If the bit is set to "valid", then the associated page is both legal and is in
memory.
13 | P a g e
2. If the bit is set to "invalid" then it indicates that the page is either not valid or
the page is valid but is currently not on the disk.
For the pages that are brought into the memory, the page table is set as usual.
But for the pages that are not currently in the memory, the page table is
either simply marked as invalid or it contains the address of the page on the
disk.
During the translation of address, if the valid-invalid bit in the page table entry is 0
then it leads to page fault.
The above figure is to indicates the page table when some pages are not in the
main memory.
The components that are involved in the Demand paging process are as follows:
Main Memory
CPU
Secondary Memory
Interrupt
Physical Address space
Logical Address space
Operating System
Page Table
1. If a page is not available in the main memory in its active state; then a request
may be made to the CPU for that page. Thus for this purpose, it has to generate
an interrupt.
2. After that, the Operating system moves the process to the blocked state as an
interrupt has occurred.
14 | P a g e
3. Then after this, the Operating system searches the given page in the Logical
address space.
4. And finally with the help of the page replacement algorithms, replacements are
made in the physical address space. Page tables are updated simultaneously.
5. After that, the CPU is informed about that update and then asked to go ahead
with the execution and the process gets back into its ready state.
When the process requires any of the pages that are not loaded into the memory,
a page fault trap is triggered and the following steps are followed,
In some cases when initially no pages are loaded into the memory, pages in such
cases are only loaded when are demanded by the process by generating page faults.
It is then referred to as Pure Demand Paging.
15 | P a g e
In the case of pure demand paging, there is not even a single page that is
loaded into the memory initially. Thus pure demand paging causes the page
fault.
When the execution of the process starts with no pages in the memory, then
the operating system sets the instruction pointer to the first instruction of the
process and that is on a non-memory resident page and then in this case the
process immediately faults for the page.
After that when this page is brought into the memory then the process
continues its execution, page fault is necessary until every page that it needs is
in the memory.
And at this point, it can execute with no more faults.
This scheme is referred to as Pure Demand Paging: means never bring a page
into the memory until it is required.
Copy on Write
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.
16 | P a g e
Page Replacement
Page replacement is needed in the operating systems that use virtual memory
using Demand Paging. As we know that in Demand paging, only a set of pages of a
process is loaded into the memory. This is done so that we can have more processes
in the memory at the same time.
When a page that is residing in virtual memory is requested by a process for its
execution, the Operating System needs to decide which page will be replaced by this
requested page. This process is known as page replacement and is a vital component
in virtual memory management.
A Page Fault occurs when a program running in CPU tries to access a page that
is in the address space of that program, but the requested page is currently not
loaded into the main physical memory, the RAM of the system.
17 | P a g e
Since the actual RAM is much less than the virtual memory the page faults
occur. So whenever a page fault occurs, the Operating system has to replace an
existing page in RAM with the newly requested page. In this scenario, page
replacement algorithms help the Operating System in deciding which page to replace.
The primary objective of all the page replacement algorithms is to minimize the
number of page faults.
FIFO algorithm is the simplest of all the page replacement algorithms. In this,
we maintain a queue of all the pages that are in the memory currently. The oldest
page in the memory is at the front-end of the queue and the most recent page is at
the back or rear-end of the queue.
Whenever a page fault occurs, the operating system looks at the front-end of
the queue to know the page to be replaced by the newly requested page. It also adds
this newly requested page at the rear-end and removes the oldest page from the
front-end of the queue.
Initially, all of the slots are empty so page faults occur at 3,1,2.
Page faults = 3
18 | P a g e
Page faults = 3
When page 6 comes, it is not present and a page fault occurs. Since there are
no empty slots, we remove the front of the queue, i.e 3.
Page faults = 4
When page 5 comes, it is also not present and hence a page fault occurs. The
front of the queue i.e 1 is removed.
Page faults = 5
When page 1 comes, it is not found in memory and again a page fault occurs.
The front of the queue i.e 2 is removed.
Page faults = 6
When page 3 comes, it is again not found in memory, a page fault occurs,
and page 6 is removed being on top of the queue
Advantages
Disadvantages
Poor performance
Doesn’t use the frequency of the last used time and just simply replaces the
oldest page.
Suffers from Belady’s anomaly.
Example:
19 | P a g e
Let’s take the same page reference string 3, 1, 2, 1, 6, 5, 1, 3 with 3-page
frames as we saw in FIFO. This also helps you understand how Optimal Page
replacement works the best.
Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and
take the empty slots.
Page faults = 3
Page faults = 3
When page 6 comes, it is not in the memory, so a page fault occurs and 2 is
removed as it is not going to be used again.
Page faults = 4
When page 5 comes, it is also not in the memory and causes a page fault.
Similar to above 6 is removed as it is not going to be used again.
Page faults = 5
When page 1 and page 3 come, they are in the memory so no page fault
occurs.
Advantages
Excellent efficiency
Less complexity
Easy to use and understand
Simple data structures can be used to implement
Used as the benchmark for other algorithms
Disadvantages
20 | P a g e
More time consuming
Difficult for error handling
Need future awareness of the programs, which is not possible every time
The least recently used page replacement algorithm keeps the track of usage of
pages over a period of time. This algorithm works on the basis of the principle of
locality of a reference which states that a program has a tendency to access the same
set of memory locations repetitively over a short period of time. So pages that have
been used heavily in the past are most likely to be used heavily in the future also.
In this algorithm, when a page fault occurs, then the page that has not been
used for the longest duration of time is replaced by the newly requested page.
Example: Let’s see the performance of the LRU on the same reference string of 3, 1,
2, 1, 6, 5, 1, 3 with 3-page frames:
Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault and
take the empty slots.
Page faults = 3
Page faults = 3
When page 6 comes, it is not in the memory, so a page fault occurs and the
least recently used page 3 is removed.
Page faults = 4
When page 5 comes, it again causes a page fault and page 1 is removed as it
is now the least recently used page.
Page faults = 5
21 | P a g e
When page 1 comes again, it is not in the memory and hence page 2 is
removed according to the LRU.
Page faults = 6
When page 3 comes, the page fault occurs again and this time page 6 is
removed as the least recently used one.
Now in the above example, the LRU causes the same page faults as the FIFO,
but this may not always be the case as it will depend upon the series, the number of
frames available in memory, etc. In fact, on most occasions, LRU is better than FIFO.
Advantages
Disadvantages
This is the Last in First Out algorithm and works on LIFO principles. In this
algorithm, the newest page is replaced by the requested page. Usually, this is done
through a stack, where we maintain a stack of pages currently in the memory with
the newest page being at the top. Whenever a page fault occurs, the page at the top
of the stack is replaced.
Example: Let’s see how the LIFO performs for our example string of 3, 1, 2, 1, 6, 5,
1, 3 with 3-page frames:
22 | P a g e
Initially, since all the slots are empty, page 3,1,2 causes a page fault and
takes the empty slots.
Page faults = 3
Page faults = 3
When page 6 comes, the page fault occurs and page 2 is removed as it is on
the top of the stack and is the newest page.
Page faults = 4
When page 5 comes, it is not in the memory, which causes a page fault, and
hence page 6 is removed being on top of the stack.
Page faults = 5
When page 1 and page 3 come, they are in memory already, hence no page
fault occurs.
If we notice, this is the same number of page faults as of the Optimal page
replacement algorithm. So we can say that for this series of pages, this is the best
algorithm that can be implemented without the prior knowledge of future references.
23 | P a g e
Advantages
Simple to understand
Easy to implement
No overhead
Disadvantages
Does not consider Locality principle, hence may produce worst performance
The old pages may reside in memory forever even if they are not used
This algorithm, as the name suggests, chooses any random page in the memory
to be replaced by the requested page. This algorithm can behave like any of the
algorithms based on the random page chosen to be replaced.
Example: Suppose we choose to replace the middle frame every time a page fault
occurs. Let’s see how our series of 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames perform
with this algorithm:
Initially, since all the slots are empty, page 3,1,2 causes a page fault and
takes the empty slots
Page faults = 3
Page faults = 3
When page 6 comes, the page fault occurs, we replace the middle element i.e 1
is removed.
Page faults = 4
When page 5 comes, the page fault occurs again and middle element 6 is
removed
Page faults = 5
24 | P a g e
When page 1 comes, there is again a page fault and again the middle element 5
is removed
Page faults = 6
As we can see, the performance is not the best, but it's also not the worst. The
performance in the random replacement algorithm depends on the choice of the page
chosen at random.
Advantages
Disadvantages
It Can not be analyzed, may produce different performances for the same series
It Can suffer from Belady’s anomaly
Conclusion
FIFO page replacement algorithm replaces the oldest page in the memory
Optimal page replacement algorithm replaces the page which will be referred
farthest in the future
LRU page replacement algorithm replaces the page that has not been used for
the longest duration of time
25 | P a g e
Frame Allocation
26 | P a g e
1. Local replacement: When a process needs a page which is not in the memory,
it can bring in the new page and allocate it a frame from its own set of allocated
frames only.
Advantage: The pages in memory for a particular process and the page fault
ratio is affected by the paging behavior of only that process.
Disadvantage: A low priority process may hinder a high priority process by
not making its frames available to the high priority process.
Thrashing
Thrashing is when the page fault and swapping happens very frequently at a
higher rate, and then the operating system has to spend more time swapping these
pages. This state in the operating system is known as thrashing. Because of
thrashing, the CPU utilization is going to be reduced or negligible.
The basic concept involved is that if a process is allocated too few frames,
then there will be too many and too frequent page faults. As a result, no valuable
work would be done by the CPU, and the CPU utilization would fall drastically.
The long-term scheduler would then try to improve the CPU utilization by
loading some more processes into the memory, thereby increasing the degree of
multiprogramming. Unfortunately, this would result in a further decrease in the CPU
utilization, triggering a chained reaction of higher page faults followed by an
increase in the degree of multiprogramming, called thrashing.
27 | P a g e
Algorithms during Thrashing
Whenever thrashing starts, the operating system tries to apply either the
Global page replacement Algorithm or the Local page replacement algorithm.
Since global page replacement can bring any page, it tries to bring more
pages whenever thrashing is found. But what actually will happen is that no
process gets enough frames, and as a result, the thrashing will increase more
and more. Therefore, the global page replacement algorithm is not suitable
when thrashing happens.
Causes of Thrashing
Elimination of Thrashing
Thrashing has some negative impacts on hard drive health and system
performance. Therefore, it is necessary to take some actions to avoid it. To resolve
the problem of thrashing, here are the following methods, such as:
28 | P a g e
o Adjust the swap file size:If the system swap file is not configured correctly,
disk thrashing can also happen to you.
o Increase the amount of RAM: As insufficient memory can cause disk
thrashing, one solution is to add more RAM to the laptop. With more memory,
your computer can handle tasks easily and don't have to work excessively.
Generally, it is the best long-term solution.
o Decrease the number of applications running on the computer: If there
are too many applications running in the background, your system resource
will consume a lot. And the remaining system resource is slow that can result
in thrashing. So while closing, some applications will release some resources
so that you can avoid thrashing to some extent.
o Replace programs: Replace those programs that are heavy memory
occupied with equivalents that use less memory.
The Local Page replacement is better than the Global Page replacement, but
local page replacement has many disadvantages, so it is sometimes not helpful.
Therefore below are some other techniques that are used to handle thrashing:
1. Locality Model
A locality is a set of pages that are actively used together. The locality
model states that as a process executes, it moves from one locality to
another. Thus, a program is generally composed of several different localities
which may overlap.
2. Working-Set Model
29 | P a g e
The accuracy of the working set is dependent on the value of parameter
A. If A is too large, then working sets may overlap. On the other hand, for
smaller values of A, the locality might not be covered entirely.
If D is the total demand for frames and WSS i is the working set size for
process i,
D = ⅀ WSSi
Now, if 'm' is the number of frames available in the memory, there are
two possibilities:
If there are enough extra frames, then some more processes can be
loaded into the memory. On the other hand, if the summation of working set
sizes exceeds the frames' availability, some of the processes have to be
suspended (swapped out of memory).
This technique prevents thrashing along with ensuring the highest degree of
multiprogramming possible. Thus, it optimizes CPU utilization.
A more direct approach to handle thrashing is the one that uses the Page-Fault
Frequency concept.
The problem associated with thrashing is the high page fault rate, and thus,
the concept here is to control the page fault rate.
If the page fault rate is too high, it indicates that the process has too few
frames allocated to it. On the contrary, a low page fault rate indicates that the
process has too many frames.
30 | P a g e
Upper and lower limits can be established on the desired page fault rate, as
shown in the diagram.
If the page fault rate falls below the lower limit, frames can be removed from
the process. Similarly, if the page faults rate exceeds the upper limit, more frames
can be allocated to the process.
In other words, the graphical state of the system should be kept limited to the
rectangular region formed in the given diagram.
If the page fault rate is high with no free frames, some of the processes can
be suspended and allocated to them can be reallocated to other processes. The
suspended processes can restart later.
Memory mapping refers to process ability to access files on disk the same way
it accesses dynamic memory. It is obvious that accessing RAM is much faster than
accessing disk via read and write system calls. This technique saves user
applications IO overhead and buffering but it also has its own drawbacks as we will
see later.
The operating system utilizes virtual memory techniques to do the trick. The
OS splits the memory mapped file into pages (similar to process pages) and loads
the requested pages into physical memory on demand. If a process references an
address (i.e. location within the file) that does not exists, a page fault occurs and
the operating system brings the missing page into memory.
Memory mapped files sound like an efficient method to access files on disk. Is
it a good option always? That is not necessarily the case. Here are few scenarios
where memory mapping is appealing.
Advantages
Memory mapping is an excellent technique that has various benefits.
Examples below.
Efficiency: when dealing with large files, no need to read the entire file into
memory first.
Fast: accessing virtual memory is much faster than accessing disk.
Sharing: facilitates data sharing and interprocess communication.
Simplicity: dealing with memory as opposed to allocating space, copying data
and deallocating space.
31 | P a g e
Disadvantages
The drawbacks of memory mapping are:
Memory mapping is generally good for binary files; however reading formatted
binary file types with custom headers such as TIFF can be problematic.
Memory mapping text files is not such an appealing task as it may require
proper text handling and conversion.
The notion that a memory mapped file has always better performance should
not be taken for granted. Recall that accessing file in memory may generate a
lot of page faults which is bad.
Memory footprint is larger than that of traditional file IO. In other words, user
applications have no control over memory allocation.
Expanding file size is not easy to implement because a memory mapped file is
assumed to be fixed in size.
Difference between memory mapped file and shared memory
Named pipes allow one process to communicate with another process in real
time on the same computer or through a network. It is based on client server
communication model with a sender and a listener.
Behind the scenes, named pipes may implement an IPC shared memory.
Example – If the request of 25Kb is made then block of size 32Kb is allocated.
32 | P a g e
Four Types of Buddy System –
If the partition size and procees 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 ablock 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 atleast 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
rquests 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 –
33 | P a g e
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 deallocate 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?
34 | P a g e
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.
35 | P a g e
In linux, a slab may in one of three possible states:
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 –
36 | P a g e