Os - Unit-3
Os - Unit-3
I. Memory-Management Strategies
1. Introduction
2. Swapping
3. Contiguous memory allocation
4. Paging
5. Segmentation
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 1
UNIT-3
I. Memory-Management Strategies
1. Introduction
Program must be brought (from disk) into memory and placed within a
process for it to be run.
Main memory and registers are only storage CPU can access directly
Memory unit only sees a stream of “addresses + read requests” , or “ address +
data and write requests”
MICROPROCESSOR
Register access in one CPU clock (or less). Main memory can take many cycles,
causing a stall. Control
Cache sits between main memory and CPU ALUregisters.
Protection of memory required to ensure correct operation Unit
CACHE
RA
M
DISK
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 2
Base and Limit Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to be sure it is
between base and limit for that user
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 3
Address Binding
Programs on disk, ready to be brought into memory to execute form an input
queue
o Without support, must be loaded into address 0000
Further, addresses represented in different ways at different stages of a
program’s life
o Source code addresses usually symbolic
o Compiled code addresses bind to relocatable addresses
i.e. “14 bytes from beginning of this module”
o Linker or loader will bind relocatable addresses to absolute addresses
i.e. 74014
o Each binding maps one address space to another
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 4
Logical vs. Physical Address Space
Dynamic Linking
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 5
Static linking – system libraries and program code combined by the loader
into the binary program image
Dynamic linking –linking postponed until execution time
Small piece of code, stub, used to locate the appropriate memory-resident
library routine
Stub replaces itself with the address of the routine, and executes the routine
Operating system checks if routine is in processes’ memory address
o If not in address space, add to address space
Dynamic linking is particularly useful for libraries
System also known as shared libraries
Consider applicability to patching system libraries
o Versioning may be needed
2. Swapping
a. A process can be swapped temporarily out of memory to a backing store, and then
brought back into memory for continued execution
i. Total physical memory space of processes can exceed physical memory
b. Backing store – fast disk large enough to accommodate copies of all memory images
for all users; must provide direct access to these memory images
c. Roll out, roll in – swapping variant used for priority-based scheduling algorithms;
lower-priority process is swapped out so higher-priority process can be loaded and
executed
d. Major part of swap time is transfer time; total transfer time is directly proportional to
the amount of memory swapped
e. System maintains a ready queue of ready-to-run processes which have memory
images on disk
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 6
a. If next processes to be put on CPU is not in memory, need to swap out a process and
swap in target process
b. Context switch time can then be very high
c. 100MB process swapping to hard disk with transfer rate of 50MB/sec
i. Swap out time = 100MB / 50 MB/Sec = 2 Sec= 2000 ms
ii. Plus swap in of same sized process
iii. Total context switch swapping component time of 4000ms (4 seconds)
d. Other constraints as well on swapping
i. Pending I/O – can’t swap out as I/O would occur to wrong process
ii. Or always transfer I/O to kernel space, then to I/O device
Known as double buffering, adds overhead
e. Standard swapping not used in modern operating systems
i. But modified version common
Swap only when free memory extremely low
Advantages of Swapping :
a. It helps the CPU to manage multiple processes within a single main memory.
b. It helps to create and use virtual memory.
c. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore, processes
do not have to wait very long before they are executed.
d. It improves the main memory utilization.
Disadvantages of Swapping :
a. If the computer system loses power, the user may lose all information related to the
program in case of substantial swapping activity.
b. If the swapping algorithm is not good, the composite method can increase the number of
Page Fault and decrease the overall processing performance.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 7
Dynamic Storage-Allocation Problem
First-fit and best-fit better than worst-fit in terms of speed and storage utilization
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.
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 –
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 8
All the memory allocation strategies suffer from external fragmentation, though
first and best fits experience the problems more so than worst fit. External
fragmentation means that the available memory is broken up into lots of little pieces,
none of which is big enough to satisfy the next memory requirement, although the
sum total could.
The amount of memory lost to fragmentation may vary with algorithm, usage
patterns, and some design decisions such as which end of a hole to allocate and
which end to save on the free list.
Statistical analysis of first fit, for example, shows that for N blocks of allocated
memory, another 0.5 N will be lost to fragmentation.
Internal fragmentation also occurs, with all memory allocation strategies. This is
caused by the fact that memory is allocated in blocks
of a fixed size, whereas the actual memory needed will rarely be that exact size. For
a random distribution of memory requests, on the
memory request, because on the average the last allocated block will be only half
full.
o Note that the same effect happens with hard drives, and that modern hardware
gives us increasingly larger drives and memory at the expense of ever larger
block sizes, which translates to more memory lost to internal fragmentation.
o Some systems use variable size blocks to minimize losses due to internal
fragmentation.
If the programs in memory are relocatable, ( using execution- timeaddress binding ),
then the external fragmentation problem can be reduced via compaction, i.e. moving all
processes down to one end of physical memory. This only involves updating the
relocation register for each process, as all internal work is done using logical
addresses.
4. Paging
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.
Divide physical memory into fixed-sized blocks called frames
a. Size is power of 2, between 512 bytes and 16 Mbytes
Divide logical memory into blocks of same size called pages
Keep track of all free frames
To run a program of size N pages, need to find N free frames and load program
Set up a page table to translate logical to physical addresses
Backing store likewise split into pages
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 9
Address generated by CPU is divided into:
a. Page number (p) – used as an index into a page table which contains base
address of each page in physical memory
b. Page offset (d) – combined with base address to define the physical memory
address that is sent to the memory
page unit page offset
number
p d
m -n n
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 11
5. Segmentation
4
1
3 2
4
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 12
Segmentation Architecture
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 13
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compared to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. It is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 14
II. Virtual Memory Management
1. Introduction,
Virtual Memory is a storage scheme that provides user an illusion of having a very big main
memory. This is done by treating a part of secondary memory as the main 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.
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
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 15
Virtual address space – logical view of how process is stored in memory
o Usually start at address 0, contiguous addresses until end of space
o Meanwhile, physical memory organized in page frames
o MMU must map logical to physical
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 16
2. Demand paging
The process of loading the page into memory on demand (whenever page fault occurs) is
known as demand paging.
The process includes the following steps :
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.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 17
Valid-Invalid Bit
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 18
Page Fault
Page faults is an error. A page fault will happen if a program tries to access a piece of
memory that does not exist in physical memory (main memory). The fault specifies the
operating system to trace all data into virtual memory management and then relocate it from
secondary memory to its primary memory, such as a hard disk.
Page fault occurs when
Operating system looks at another table to decide:
o Invalid reference Þ abort
o Just not in memory
Find free frame
Swap page into frame via scheduled disk operation
Reset tables to indicate page now in memory
Set validation bit = v
Restart the instruction that caused the page fault
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 19
The procedure of page fault handling in the OS:
1. Firstly, an internal table for this process to assess whether the reference was valid or
invalid memory access.
2. If the reference becomes invalid, the system process would be terminated.
Otherwise, the page will be paged in.
3. After that, the free-frame list finds the free frame in the system.
4. Now, the disk operation would be scheduled to get the required page from the disk.
5. When the I/O operation is completed, the process's page table will be updated with
a new frame number, and the invalid bit will be changed. Now, it is a valid page
reference.
6. If any page fault is found, restart these steps from starting.
Example
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
EAT = (1 – p) x 200 + p (8 milliseconds)
= 200 – p x 200 + p x 8,000,000
= 200 + p x 7,999,800
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 20
3. Copy on-write
Copy-on-Write (COW) allows both parent and child processes to initially share the
same pages in memory
o If either process modifies a shared page, only then is the page copied
COW allows more efficient process creation as only modified pages are copied
The CoW is basically a technique of efficiently copying the data resources in the computer
system. In this case, if a unit of data is copied but is not modified then "copy" can mainly
exist as a reference to the original data.
But when the copied data is modified, then at that time its copy is created(where new bytes
are actually written )as suggested from the name of the technique. The main use of this
technique is in the implementation of the fork system call in which it shares the virtual
memory/pages of the Operating system.
Recall in the UNIX(OS), the fork() system call is used to create a duplicate process of the
parent process which is known as the child process.
The CoW technique is used by several Operating systems like Linux, Solaris, and Windows
XP. Let us take an example where Process A creates a new process that is Process B, initially
both these processes will share the same pages of the memory.
Now, let us assume that process A wants to modify a page in the memory. When the Copy-
on-write(CoW) technique is used, only those pages that are modified by either process are
copied; all the unmodified pages can be easily shared by the parent and child process.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 21
Whenever it is determined that a page is going to be duplicated using the copy-on-write
technique, then it is important to note the location from where the free pages will be
allocated. There is a pool of free pages for such requests; provided by many operating
systems.
And these free pages are allocated typically when the stack/heap for a process must expand or
when there are copy-on-write pages to manage.
These pages are typically allocated using the technique that is known as Zero-fill-on-
demand. And the Zero-fill-on-demand pages are zeroed-out before being allocated and thus
erasing the previous content.
4. Page replacement
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 22
1. Find the location of the desired page on disk
2. Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a
victim frame
- Write victim frame to disk if dirty
3. Bring the desired page into the (newly) free frame; update the page and
frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing EAT
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 23
Page and Frame Replacement Algorithms
Frame-allocation algorithm determines
o How many frames to give each process
o Which frames to replace
Page-replacement algorithm
o Want lowest page-fault rate on both first access and re-access
Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page faults on
that string
o String is just page numbers, not full addresses
o Repeated access to the same page does not cause a page fault
o Results depend on number of frames available
In all our examples, the reference string of referenced page numbers is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 24
First-In-First-Out (FIFO) Algorithm
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 25
Page-Buffering Algorithms
5. Frame allocation
The main memory of the operating system is divided into various frames. The process is
stored in these frames, and once the process is saved as a frame, the CPU may run it. As a
result, the operating system must set aside enough frames for each process. As a result, the
operating system uses various algorithms in order to assign the frame.
Demand paging is used to implement virtual memory, an essential operating system feature.
It requires the development of a page replacement mechanism and a frame allocation system.
If OS is running multiple processes, the frame allocation techniques are utilized to define
how many frames to allot to each one.
Fixed Allocation
1. Equal allocation – For example, if there are 100 frames (after allocating frames for the
OS) and 5 processes, give each process 20 frames
For example, if there are 100 frames (after allocating frames for the OS) and 5
processes, give each process 20 frames
For instance, in a system with 62 frames, if there is a process of 10KB and another
process of 127KB, then the first process will be allocated (10/137)*62 = 4 frames and
the other process will get (127/137)*62 = 57 frames.
6. 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.
In computer science, thrash is the poor performance of a virtual memory (or paging) system
when the same pages are being loaded repeatedly due to a lack of main memory to keep them
in memory. Depending on the configuration and algorithm, the actual throughput of a system
can degrade by multiple orders of magnitude.
In computer science, thrashing occurs when a computer's virtual memory resources are
overused, leading to a constant state of paging and page faults, inhibiting most application-
level processing. It causes the performance of the computer to degrade or collapse. The
situation can continue indefinitely until the user closes some running applications or the active
processes free up additional virtual memory resources.
To know more clearly about thrashing, first, we need to know about page fault and swapping.
o Page fault: We know every program is divided into some pages. A page fault occurs
when a program attempts to access data or code in its address space but is not
currently located in the system RAM.
o Swapping: Whenever a page fault happens, the operating system will try to fetch that
page from secondary memory and try to swap it with one of the pages in RAM. This
process is called swapping.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 28
Algorithms during Thrashing
Whenever thrashing starts, the operating system tries to apply either the Global page
replacement Algorithm or the Local page replacement algorithm.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 29
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 30
7. Memory-mapped files
A sequential read of a file on disk using the standard system calls open(), read(), and write().
Each file access requires a system call and disk access. This approach, known as memory
mapping a file, allows a part of the virtual address space to be logically associated with the
file. Memory mapping a file can improve the performance of CPU processing.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 31
Shared Memory via Memory-Mapped I/O
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 and most likely
contains free pages scattered throughout physical memory.
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 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.
2. Pages allocated to user-mode processes do not necessarily have to be in contiguous
physical memory.
Buddy System
The buddy system allocates memory from a fixed-size segment consisting of physically
contiguous pages.
Memory is allocated from this segment using a power-of-2 allocator, which satisfies
requests in units sized as a power of 2 (4 KB, 8 KB, 16 KB, and so forth). A request in
units not appropriately sized is rounded up to the next highest power of 2.
For example, a request for 11 KB is satisfied with a 16-KB segment.
Let’s consider a simple example. Assume the size of a memory segment is initially 256
KB and the kernel requests 21 KB of memory.
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 32
The segment is initially divided into two buddies—which we will call AL and AR — each
128 KB in size.
One of these buddies is further divided into two 64-KB buddies BL and BR. However, the
next-highest power of 2 from 21 KB is 32 KB so either
BL or BR is again divided into two 32-KB buddies, CL and CR. One of these buddies is used
to satisfy the 21-KB request.
Slab Allocator.
Alternate strategy is Slab Allocator.
Slab is one or more physically contiguous pages
Cache consists of one or more slabs
Single cache for each unique kernel data structure
o Each cache filled with objects – instantiations of the data structure
When cache created, filled with objects marked as free
When structures stored, objects marked as used
If slab is full of used objects, next object allocated from empty slab
o If no empty slabs, new slab allocated
Benefits include no fragmentation, fast memory request satisfaction
Prepared by Mr. Isaac Paul P, Assoc Prof & HOD, Dept of CSE, RISE Krishna Sai Gandhi Groups 33