R19 CSE - OS Unit III
R19 CSE - OS Unit III
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
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
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
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-
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.
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.
Process may become even larger than all of the physical memory.
Data / code should be read from disk whenever required.
123,215,600,1234,76,96
If page size is 100, then the reference string is 1,2,6,12,0,0
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
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
If the partition size and process size are different then poor match occurs and
may use space inefficiently.
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
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.