Operating System Unit 7,6,5
Operating System Unit 7,6,5
Directory Implementation
Linked list
Directory implementation using a linked list is a way of storing directory information in a linked list data structure.
A linked list is a data structure that consists of a series of nodes, each of which contains data and a pointer to the
next node in the list.
Linked lists can be used to implement a variety of data structures, such as queues, stacks, etc.
Characteristics
1. When a new file is created, the entire list is checked whether the new file name matches an existing file
name or not. In case, it doesn't exist, the file can be created at the beginning or at the end.
2. In order to delete a file, we first search the directory with the name of the file to be deleted. After
searching we can delete that file by releasing the space allocated to it.
Disadvantages
1. Linked lists are not very efficient for searching for data. This is because we have to do a linear search to
find the node that contains the desired data.
2. Linked lists require more memory than other data structures, such as arrays. This is because each node in
a linked list needs to store a pointer to the next node.
Hash Table
Directory implementation using a hash table is a method where we combine a linked list with a hash data
structure. This approach helps overcome the drawbacks of using a simple linked list for directories.
In this method, each file in the directory is represented by a key-value pair. The key is generated using a hash
function, which is a mathematical function that takes a string as input and returns an integer as output.
The hash function is designed to distribute the keys throughout the hash table. This ensures that each key is stored
in a unique location in the hash table.
To find a file in the directory, we use the file's name as the key to look up the corresponding value in the hash
table. The value is a pointer to the file's data block.
Directory implementation using the hash table is a more efficient way to search for files than linked list
implementation. This is because the hash table allows us to look up the file's location directly, without having to
search through the entire list of files.
Disadvantages
1. Hash tables must be a fixed size. This means that if we have more files than the hash table can store, we
will need to create a new hash table. This can be time-consuming.
2. Collisions occur when two or more files have the same hash key.
An access control list (ACL) is a list of permissions that are granted to users or groups for a specific object. The
object can be a file, folder, or other resource. The permissions can include read, write, execute, and delete.
ACLs are used to control access to resources and to protect them from unauthorized access. They are typically
used in operating systems, file systems, and database systems.
1. Discretionary access control (DAC): DAC allows the owner of an object to specify the permissions that
are granted to other users.
2. Mandatory access control (MAC): MAC does not allow the owner of an object to specify the permissions.
The permissions are determined by the system administrator.
Let's say we have a file called "report.txt" and we want to define the access permissions for different users
User C: No access
With this ACL, User A has both read and write permissions, so they can view and modify the contents of
"report.txt". User B has read-only permission, allowing them to view the file but not make any changes. User C,
on the other hand, has no access to the file.
Whenever a user tries to access "report.txt", the operating system checks the ACL to determine if they have the
necessary permissions. If the user's credentials match an entry in the ACL and the requested operation is allowed,
access is granted. Otherwise, access is denied.
Advantages
Disadvantages
Overall, ACLs are a powerful tool for controlling access to resources in an operating system. They can help
protect data and resources from unauthorized access, but they should be used with care to avoid performance
problems.
An Access Control Matrix (ACM) is a table that shows which users or groups have access to which objects and
what operations they can perform on those objects. It is a more comprehensive way to control access than ACLs.
However, it is also more complex and difficult to manage.
This ACM shows that User1 has read, write, and execute permissions for File1. User2 has read and write
permissions for File2. User3 has execute permissions for File3.
The ACM can be used to control access to files, folders, printers, and other resources. It can also be used to
control access to applications and services.
The ACM is a powerful tool for controlling access to resources in an operating system. However, it can be
complex to implement and manage. It is important to carefully consider the needs of the system before
implementing an ACM.
Advantages
Overall, ACMs are a powerful tool for controlling access to resources in an operating system. However, they
should be used with care to avoid performance problems and to ensure that they are properly implemented and
managed.
ACL ACM
An access control list (ACL) is a list of permissions that An Access Control Matrix (ACM) is a table that shows
are granted to users or groups for a specific object. which users or groups have access to which objects
The object can be a file, folder, or other resource. and what operations they can perform on those
objects.
Easier to manage. Difficult to manage.
Simpler More complex
Commonly used in file systems, databases, and Commonly used in multi-level security systems
networks
Defines the access rights each user has to a particular Defines a subject's access rights (read, write, execute)
system object on an object
Represents permissions as a list of rules attached to Represents permissions in a table-like structure
each object
Let's say we have a file called "report.txt" and we Here is an example of an ACM:
want to define the access permissions for different Object | User | Permission
users ------- | --------| --------
User A: Read and Write permissions File1 | User1| Read, Write, Execute
User B: Read-only permission File2 | User2| Read, Write
User C: No access File3 | User3| Execute
With this ACL, User A has both read and write This ACM shows that User1 has read, write, and
permissions, so they can view and modify the contents execute permissions for File1. User2 has read and
of "report.txt". User B has read-only permission, write permissions for File2. User3 has execute
allowing them to view the file but not make any permissions for File3.
changes. User C, on the other hand, has no access to
the file.
Free space management is the process of keeping track of unused space on a disk so that it can be allocated to
files when needed.
When a file is created, the operating system looks for free space in a list and assigns it to the file. When a file is
deleted, the space it occupied is marked as free and added back to the list.
The operating system uses various methods to manage free space, including:
In this method, each block of memory is represented by a bit. If a bit is set to 1, it means the block is free, and if
it is set to 0, it means the block is occupied by a file. This method is simple, consumes less memory, and is efficient
in finding free space. However, it can be less efficient when the disk size is large because the operating system
needs to search through all the blocks until it finds a free one.
Advantages
Disadvantages
---------- | --------
0 |1
1 |1
2 |0
3 |1
4 |0
5 |1
6 |1
7 |0
8 |1
9 |1
10 |0
In this example, blocks 0, 2, 5, 7, and 9 are free, while the remaining blocks are allocated.
To allocate a block, the operating system simply finds the first "1" bit in the bit vector. To free a block, the
operating system simply changes the corresponding bit from "1" to "0".
b. Linked List
In this approach, the free blocks on the disk are linked together using pointers. Each free block contains the
address of the next free block, and the last pointer in the list points to null, indicating the end of the linked list.
To allocate a block, the operating system simply removes the first block from the list. To free a block, the operating
system simply adds the block to the end of the list.
Linked lists are a more efficient way to manage free space on large disks than bit vectors. This is because the
operating system does not need to scan the entire list to find a free block. Instead, it can simply start at the head
of the list and keep going until it finds a free block.
Advantages
Disadvantages
I-Node
An I-node (index node) is a data structure in a file system that contains metadata about a file. Each file in the file
system has a unique I-node associated with it.
- Size of file
+--------------------+
| I-node |
+---------------------+
| File type |
| File size |
| Ownership |
| Permissions |
| Timestamps |
+----------------------+
The I-node contains metadata fields for things like file type, size, ownership, permissions, etc.
It also contains pointers that point to the data blocks on the disk where the actual file contents are stored.
The I-node acts as a central location to look up information about a file. When a process wants to access a file, it
looks up the file's I-node to get the metadata and data block locations for that file.
This I-node structure allows the file system to efficiently manage and locate files on disk. Each file has a unique I-
node number that identifies it within the file system.
FAT
File Allocation Table (FAT) is a way to organize and manage files on storage devices like hard drives, USB drives,
and memory cards.
It works by keeping track of the location of each file on the device by using a table.
There are three main types of FAT file systems: FAT12, FAT16, and FAT32. FAT12 was the original version designed
for small disks, FAT16 supports larger disks, and FAT32 is the most recent version that can handle even larger
disks.
Here are some of the key features of the FAT file system:
1. It uses a table to map file names to their physical location on the disk.
2. It is a simple and efficient file system.
3. It is compatible with many different operating systems.
4. It is easy to implement.
5. It supports large disk sizes.
6. It uses a journaling mechanism to minimize the risk of data corruption.
Here are some of the limitations of the FAT file system:
Overall, the FAT file system is a simple and efficient file system that is widely used today. It is a good choice for
use on removable storage devices such as USB drives and SD cards. However, it may not be the best choice for
use on hard drives, as it can be vulnerable to fragmentation.
Unit - 6
Q. When programmed I/0 is more suitable than other I/0 handling techniques?
Programmed I/O is suitable for simple I/O operations involving low-speed devices and small data transfers. Since
the CPU is directly responsible for data movement with PIO, it adds minimal overhead for straightforward, low-
volume I/O tasks.
I/O software is a set of programs that control the interaction between the computer and its I/O devices. It is
responsible for transferring data between the devices and the computer's memory, as well as managing the use
of these devices by multiple programs.
Goals
• To make it easy for application programs to access I/O devices. This means that the I/O software should
be designed in such a way that it is not dependent on the specific hardware devices that are being used.
This makes it possible to write application programs that can access any I/O device without having to be
modified for each different device.
• To provide a uniform way of transferring data between I/O devices and memory. This means that the
I/O software should provide a way for application programs to transfer data to and from I/O devices
without having to worry about the specific details of the hardware devices.
• To handle errors that occur during I/O operations. This means that the I/O software should be able to
detect and handle errors that occur during I/O operations, such as a bad sector on a disk drive or a
communication error.
• To manage the use of I/O devices by multiple programs. This means that the I/O software should be able
to schedule the use of I/O devices by multiple programs so that they do not interfere with each other.
Programmed I/O Interrupt-driven I/O
Data transfer is initiated by instructions in the Data transfer is initiated by interrupt requests.
program.
Slower compared to interrupt-driven I/O Faster compared to programmed I/O
Relatively easy to program and understand Can be tricky and complicated to understand
The performance of the system is severely degraded The performance of the system is enhanced to some
extent
CPU is not required to stay in the loop. It can do other CPU stays in the loop to know if the device is ready for
work until it is interrupted by the command indicating transfer and has to continuously monitor the
the readiness of the device for data transfer. peripheral device.
Not suitable for devices with slow data transfer rates Suitable for devices with slow data transfer rates
When the CPU is using programmed I/O, it has to stay When the CPU is using interrupt-driven I/O, it does not
in a loop to wait for the I/O device to be ready for data have to stay in a loop to wait for the I/O device to be
transfer. This means that the CPU is not able to do any ready for data transfer. Instead, the CPU can do other
other work during this time, even if there are other work until it is interrupted by the I/O device. This
tasks that are waiting to be processed. This can lead to means that the CPU's time is not wasted, and it can be
the wastage of CPU cycles. used to process other tasks.
Programmed I/O
Advantages:
Disadvantages:
Interrupt-driven I/O
Advantages:
Disadvantages:
Programmed I/O
• Easy to program and understand: Programmed I/O is relatively easy to program and understand because
the programmer has complete control over the data transfer process. The programmer simply writes a
program that loops until the I/O device is ready for data transfer. This can be a simple and efficient way
to transfer data, especially for devices with fast data transfer rates.
• Less chance of errors: Programmed I/O is less prone to errors than interrupt-driven I/O because the
programmer has complete control over the data transfer process. There is no need to worry about the
I/O device interrupting the CPU at an unexpected time.
• Simpler to debug: Programmed I/O is simpler to debug than interrupt-driven I/O because the programmer
has complete control over the data transfer process. If there is an error, the programmer can simply step
through the program and see where the error occurred.
Interrupt-driven I/O
• Efficient use of CPU: Interrupt-driven I/O is more efficient than programmed I/O because the CPU does
not have to stay in a loop waiting for the I/O device to be ready for data transfer. The CPU can do other
work until it is interrupted by the I/O device. This can improve the overall performance of the system.
• Improves system performance: Interrupt-driven I/O can also improve system performance because it
allows the CPU to respond to other requests more quickly. This is because the CPU is not waiting for the
I/O device to be ready for data transfer.
• Suitable for devices with slow data transfer rates: Interrupt-driven I/O is suitable for devices with slow
data transfer rates because it allows the CPU to do other work while the I/O device is transferring data.
This can help to reduce the overall time it takes to transfer data.
• More complex to program: Interrupt-driven I/O is more complex to program than programmed I/O
because the programmer needs to take care of the interrupt handling mechanism. The programmer needs
to write code that will handle the interrupt and transfer the data. This can be a more challenging task than
simply writing a program that loops until the I/O device is ready for data transfer.
More prone to errors: Interrupt-driven I/O is more prone to errors than programmed I/O because the
programmer needs to take care of the interrupt-handling mechanism. If the programmer makes a mistake in the
interrupt handling code, it can cause the system to crash.
Difficult to debug: Interrupt-driven I/O can be difficult to debug because it is not always clear what caused the
interrupt. If the system crashes, the programmer may need to step through the program and the interrupt
handling code to see what went wrong.
Overall, interrupt-driven I/O is more efficient than programmed I/O, but it is also more complex to program and
debug. The best choice for a particular application will depend on the specific requirements of the application.
Advantages:
• Faster data transfer: DMA can transfer data much faster than programmed I/O because the CPU does
not have to stay in a loop waiting for the I/O device to be ready for data transfer.
• Efficient use of CPU: DMA allows the CPU to do other work while the I/O device is transferring data. This
can improve the overall performance of the system.
• Less overhead: DMA does not require the CPU to be involved in the data transfer process, which can
reduce the overhead on the CPU.
• Suitable for devices with slow data transfer rates: DMA is suitable for devices with slow data transfer
rates because it allows the CPU to do other work while the I/O device is transferring data. This can help
to reduce the overall time it takes to transfer data.
Disadvantages:
• More complex to program: DMA is more complex to program than programmed I/O because the
programmer needs to take care of the DMA controller. The programmer needs to write code that will
initialize the DMA controller and transfer the data. This can be a more challenging task than simply
writing a program that loops until the I/O device is ready for data transfer.
• More prone to errors: DMA is more prone to errors than programmed I/O because the programmer
needs to take care of the DMA controller. If the programmer makes a mistake in the DMA controller
code, it can cause the system to crash.
• Can cause data corruption: If the DMA controller is not properly synchronized with the I/O device, it can
cause data corruption.
Overall, DMA is a more efficient way to transfer data than programmed I/O, but it is also more complex to
program and can cause data corruption if not used properly. The best choice for a particular application will
depend on the specific requirements of the application.
1. FCFS
FCFS (First Come First Serve) is the simplest disk scheduling algorithm. Its services request in the order they arrive.
This is the fairest algorithm, but it may not be the most efficient, as it may cause the disk arm to move a long way
to service a request.
For example, let's say we have a disk with 10 tracks and the requests are 1, 5, 3, 7, 9, 2, 4, 6, 8, 0. In FCFS, the
requests will be serviced in the following order: 1, 5, 3, 7, 9, 2, 4, 6, 8, 0.
• It may not be the most efficient. If the requests are not evenly distributed, the disk arm may have to
move a long way to service a request.
• It can cause starvation. If there are a lot of requests, some requests may never be serviced.
2. SSTF
SSTF stands for Shortest Seek Time First. It is a disk scheduling algorithm that services the request that is closest
to the current position of the disk arm. This can reduce the total seek time, but it may cause starvation, where
some requests are never serviced.
For example, let's say we have a disk with 10 tracks and the requests are 1, 5, 3, 7, 9, 2, 4, 6, 8, 0. In SSTF, the
requests will be serviced in the following order: 1, 3, 5, 7, 9, 0, 2, 4, 6, 8.
• It can be more efficient than FCFS, especially if the requests are not evenly distributed.
• It can reduce the total seek time.
• It can cause starvation. If there are a lot of requests, some requests may never be serviced.
• It can be more complex to implement than FCFS.
SSTF is a greedy algorithm. This means that it always chooses the request that is closest to the current position of
the disk arm, without considering the requests that will come later.
3. SCAN
SCAN stands for Scan. It is a disk scheduling algorithm that services requests in a single direction, from one end of
the disk to the other. When the disk arm reaches the end of the disk, it reverses direction and services the requests
in the opposite direction. This can achieve a high throughput, but it may cause some requests to wait a long time
for service.
For example, let's say we have a disk with 10 tracks and the requests are 1, 5, 3, 7, 9, 2, 4, 6, 8, 0. In SCAN, the
requests will be serviced in the following order: 9, 8, 7, 6, 5, 1, 2, 3, 4, 0.
• It can achieve a high throughput. This is because the disk arm is always moving, even when there are no
requests to service.
• It can be more efficient than FCFS, especially if the requests are evenly distributed.
• It can cause some requests to wait a long time for service. This is because the disk arm may have to pass
by a request multiple times before it can be serviced.
• It can be more complex to implement than FCFS.
SCAN is a greedy algorithm. This means that it always chooses the next request that is closest to the current
position of the disk arm, without considering the requests that will come later.
4. C-SCAN
C-SCAN stands for Circular SCAN. It is a disk scheduling algorithm that is similar to SCAN, but the disk arm does
not stop at the end of the disk. Instead, it continues to move in the same direction until it reaches the request
that is farthest away from it. This can reduce the amount of time that the disk arm is idle, but it may cause some
requests to wait a longer time for service.
For example, let's say we have a disk with 10 tracks and the requests are 1, 5, 3, 7, 9, 2, 4, 6, 8, 0. In C-SCAN, the
requests will be serviced in the following order: 9, 8, 7, 6, 5, 1, 0, 2, 3, 4.
• It can achieve a high throughput. This is because the disk arm is always moving, even when there are no
requests to service.
• It can be more efficient than FCFS and SCAN, especially if the requests are evenly distributed.
C-SCAN is a greedy algorithm. This means that it always chooses the next request that is closest to the current
position of the disk arm, without considering the requests that will come later.
5. LOOK
LOOK stands for LOOK Disk Scheduling Algorithm. It is a disk scheduling algorithm that is similar to SCAN, but the
disk arm does not move past the last request in its path. Instead, it turns around and starts servicing requests in
the opposite direction from the next request. This can reduce the amount of time that the disk arm is idle, but it
may cause some requests to wait a longer time for service.
For example, let's say we have a disk with 10 tracks and the requests are 1, 5, 3, 7, 9, 2, 4, 6, 8, 0. In LOOK, the
requests will be serviced in the following order: 1, 3, 5, 7, 9, 0, 2, 4, 6, 8.
• It can be more efficient than FCFS and SCAN, especially if the requests are evenly distributed.
• It can reduce the amount of time that the disk arm is idle.
• It can cause some requests to wait a longer time for service. This is because the disk arm may have to pass
by a request multiple times before it can be serviced.
• It can be more complex to implement than FCFS and SCAN.
LOOK is a greedy algorithm. This means that it always chooses the next request that is closest to the current
position of the disk arm, without considering the requests that will come later.
6. C-LOOK
C-LOOK stands for Circular LOOK Disk Scheduling Algorithm. It is a disk scheduling algorithm that is similar to LOOK,
but the disk arm does not stop at the last request in its path. Instead, it continues to move in the same direction
until it reaches the first request in its path. This can reduce the amount of time that the disk arm is idle, but it may
cause some requests to wait a longer time for service.
For example, let's say we have a disk with 10 tracks and the requests are 1, 5, 3, 7, 9, 2, 4, 6, 8, 0. In C-LOOK, the
requests will be serviced in the following order: 1, 3, 5, 7, 9, 0, 2, 4, 6, 8.
• It can be more efficient than LOOK and SCAN, especially if the requests are evenly distributed.
• It can reduce the amount of time that the disk arm is idle.
• It can cause some requests to wait a longer time for service. This is because the disk arm may have to pass
by a request multiple times before it can be serviced.
• It can be more complex to implement than LOOK and SCAN.
C-LOOK is a greedy algorithm. This means that it always chooses the next request that is closest to the current
position of the disk arm, without considering the requests that will come later.
UNIT – 5
Memory Management
Memory management is the process of managing the computer's main memory. It is responsible for allocating
and de-allocating memory to processes, as well as keeping track of which processes are using which memory.
Memory management is essential for ensuring that the computer's memory is used efficiently and that no process
is denied access to the memory that it needs.
• To allocate and de-allocate memory to processes: When a process is created, it needs to be allocated
some memory in which to run. When the process terminates, the memory that it was using needs to be
de-allocated so that it can be used by other processes.
• To keep track of used memory space by processes: Memory management needs to keep track of which
processes are using which memory. This is important so that the operating system can prevent two
processes from accessing the same memory at the same time.
• To minimize fragmentation issues: Fragmentation occurs when there are small, unused blocks of memory
scattered throughout the main memory. This can make it difficult to allocate large blocks of memory to
processes, and it can also slow down the performance of the computer. Memory management techniques
can be used to minimize fragmentation and improve the performance of the computer.
• To proper utilization of main memory: Memory management is responsible for ensuring that the
computer's main memory is used efficiently. This means that the operating system needs to allocate
memory to processes in a way that minimizes the amount of unused memory.
Memory hierarchy
Memory hierarchy is a way of organizing memory in a computer system so that the most frequently accessed data
is stored in the fastest memory. This helps to improve the performance of the system by reducing the amount of
time it takes to access data.
Level 0: Registers
Registers are the fastest type of memory in a computer system. They are located inside the CPU and are directly
accessible by the CPU. Registers are typically used to store data that is being actively used by the CPU, such as the
instruction pointer, the program counter, and the stack pointer.
Cache memory is a small, fast memory that is located close to the CPU. It stores copies of data that is frequently
accessed from main memory. Cache memory is much faster than main memory, but it is also much smaller.
Main memory is the largest type of memory in a computer system. It is slower than cache memory, but it is much
larger. Main memory is used to store data that is not being actively used by the CPU, but that may need to be
accessed soon.
Secondary storage is the slowest type of memory in a computer system. It is used to store large amounts of data
that is not being actively used by the CPU. Secondary storage devices include hard drives, CDs, and DVDs.
Tertiary storage is even slower than secondary storage. It is used to store very large amounts of data that is not
being actively used by the CPU. Tertiary storage devices include magnetic tapes and optical disks.
The data is organized in the memory hierarchy in such a way that the most frequently accessed data is stored in
the fastest memory. This is done because the time it takes to access data is proportional to the distance that the
data has to travel.
For example, if a piece of data is stored in registers, it can be accessed very quickly because the registers are
located inside the CPU. However, if the same piece of data is stored in tertiary storage, it will take much longer to
access because tertiary storage is located much further away from the CPU.
The memory hierarchy is a very important concept in computer architecture. It helps to improve the performance
of the system by reducing the amount of time it takes to access data.
Example:
Imagine that you have a desk with a drawer. The drawer is like the registers. It's the fastest way to access the
things that you need most often, such as the instruction pointer, the program counter, and the stack pointer.
The top of your desk is like the cache memory. It's not as fast as the drawer, but it's still faster than the main
memory, which is like the bottom of your desk. The main memory is where the data and instructions that are
currently being used by the CPU are stored.
The floor is like the secondary storage. It's the slowest way to access things, but it's the largest. Secondary storage
devices, such as hard drives, CDs, and DVDs, are used to store large amounts of data that is not being actively used
by the CPU.
And the tertiary storage is like the basement. It's even slower than the secondary storage, but it's the largest of
all. Tertiary storage devices, such as magnetic tapes and optical disks, are used to store very large amounts of data
that are not being actively used by the CPU.
When you need to access something, you'll first look in the drawer. If it's not there, you'll look on the top of your
desk. If it's not there, you'll look on the floor. And if it's not there, you'll look in the basement. And if it's not there,
you'll have to go find it somewhere else.
The memory hierarchy works in a similar way. The CPU first looks for the data it needs in the registers. If it's not
there, it looks in the cache memory. If it's not there, it looks in the main memory. And if it's not there, it has to go
to the secondary storage. And if it's not there, it has to go to the tertiary storage.
Logical Address Space: A logical address is an address that is generated by the CPU while a program is running. It
is also called a virtual address because it does not exist physically in the memory unit. The logical address is used
as a reference to access the physical memory location. The set of all logical addresses generated by a program
perspective is called Logical Address Space.
Physical Address Space: A physical address is an address that identifies a physical location in memory. It is
computed by the Memory Management Unit (MMU) and is used to access the data or instruction stored at that
location. The set of all physical addresses corresponding to the logical addresses in a Logical address space is called
Physical Address Space.
A memory management system is a part of the operating system that manages the computer's memory. It ensures
that different programs and processes can run smoothly in the computer's memory without conflicts.
Function:
• Allocating memory to processes: The memory management system must allocate memory to processes
when they are created. The amount of memory allocated to each process depends on the size of the
process and the available memory.
• Freeing memory: When a process terminates, the memory management system must free the memory
that the process was using. This memory can then be allocated to other processes.
• Tracking memory usage: The memory management system must track the memory usage of each
process. This information is used to ensure that no process is using more memory than it needs.
• Preventing conflicts: The memory management system must prevent conflicts between processes that
are trying to use the same memory block. This is done by keeping track of which processes are using which
memory blocks.
• Managing memory fragmentation: Memory fragmentation occurs when there are small, unused memory
blocks scattered throughout the memory. This can make it difficult to allocate memory to new processes.
The memory management system can use techniques such as compaction to reduce memory
fragmentation.
There are several classes of memory management systems that are used to achieve these goals:
Memory management with swapping is a technique used in operating systems to manage memory when the
amount of memory needed by all the processes is greater than the amount of physical memory available. In this
technique, processes are swapped in and out of memory as needed.
When a process is swapped out, it is temporarily stored on a secondary storage device, such as a hard drive. When
the process needs to be executed again, it is swapped back into memory.
Swapping can be used to improve the performance of a system by allowing more processes to be run than would
be possible if all the processes had to be kept in memory at all times. However, swapping can slow down the
system due to the movement of data between main memory and secondary storage.
In detail, here are the steps involved in memory management with swapping:
• When a process is created, the operating system checks to see if there is enough space in memory for the
process. If there is not enough space, the process is swapped out to secondary storage.
• When the process needs to be executed, the operating system swaps the process back into memory.
• When the process terminates, the operating system swaps the process out of memory and frees up the
memory space.
The operating system uses a variety of techniques to manage swapping, such as:
Swapping policy: This determines which processes are swapped in and out of memory. There are many different
swapping policies, such as the first-in, first-out (FIFO) policy, the least recently used (LRU) policy, and the optimal
policy.
Swapping algorithm: This determines how the processes are swapped in and out of memory. There are many
different swapping algorithms, such as the block swapping algorithm and the page swapping algorithm.
Swapping buffer: This is a special area of memory that is used to store the swapped processes. The swapping
buffer must be large enough to store all the swapped processes.
Memory management with bitmaps is a technique used in operating systems to track the free and allocated
memory in a system. In this technique, the memory is divided into small units, and each unit is represented by a
bit in a bitmap. A bit of 0 indicates that the unit is free, and a bit of 1 indicates that it's in use.
Advantages:
• Simple to implement.
• Efficient for small memory systems.
• Easy to manage memory fragmentation.
Disadvantages:
In simple language, here is how memory management with linked lists works:
Advantages:
Disadvantages:
Memory management with linked lists is a more efficient technique for tracking free and allocated memory in
large memory systems than bitmaps. However, it is more complex to implement and more difficult to search for
free blocks of memory when the memory is divided into small units.
Each entry in the list specifies a hole (H) and Process (P), the address at which it starts, the length, and a pointer
to the next entry.
Memory management without swapping is a technique used in operating systems to manage memory when the
amount of memory needed by all the processes is less than or equal to the amount of physical memory available.
In this technique, processes are not swapped out of memory.
• When a process is created, the operating system checks to see if there is enough space in memory for the
process. If there is enough space, the process is loaded into memory.
• When a process terminates, the memory that the process was using is freed up.
• The operating system continues to load and unload processes from memory as needed.
• In detail, here are the advantages and disadvantages of memory management without swapping:
Advantages:
• No need to swap processes in and out of memory, which can slow down the system.
• All processes are always in memory, which can improve the performance of the processes.
Disadvantages:
• The operating system must be able to keep track of all the processes in memory, which can be complex.
• If there is not enough space in memory for a new process, the operating system must either refuse to load
the process or terminate another process to free up space.
Memory management without swapping is a simple and efficient technique for managing memory when the
amount of memory is sufficient. However, it can be complex to implement and may not be suitable for systems
with a large number of processes.
Uniprogramming / Monoprogramming is a process management technique where only one process is allowed to
run in the CPU at a time. This means that the CPU is idle whenever the currently running process is waiting for I/O
or some other event.
Multiprogramming is a process management technique where multiple processes are allowed to be in memory
at the same time, and the CPU is switched between them. This allows the CPU to be utilized more efficiently, even
if some of the processes are waiting for I/O or other events.
CMA is a memory allocation method where a process is allocated a continuous block of memory. This means that
the process's memory is all located in one place, without any gaps.
CMA is a simple and efficient memory allocation method. It is easy to implement and understand, and it can be
used to allocate memory to processes of any size.
However, CMA can also lead to fragmentation. Fragmentation occurs when there are small, unused blocks of
memory scattered throughout the main memory. This can happen when a process is removed from memory and
the space it was using is not given to another process.
Fragmentation can make it difficult to allocate memory to new processes. If there are no large, contiguous blocks
of free memory available, then a new process may not be able to be loaded into memory.
CMA can also be inflexible. It is not possible to load a process into memory if the process is larger than the largest
free block of memory. This can be a problem if a process is growing or if there are many small processes in
memory.
Advantages:
Disadvantages:
Single Contiguous Memory Allocation (SCMA) is a memory allocation method where the entire main memory is
divided into two parts: one part for the operating system and one part for user processes. The operating system
is always loaded into the same part of memory, and user processes are loaded into the other part of memory.
SCMA is the simplest form of Contiguous Memory Allocation. It is easy to implement and understand, but it can
lead to internal fragmentation. Internal fragmentation occurs when a partition is allocated to a process, but the
process only uses part of the partition. The unused space in the partition is wasted.
SCMA can be used in systems with a small number of user processes. However, it is not a good choice for systems
with a large number of user processes or for systems where memory efficiency is important.
Advantages:
Disadvantages:
It is a memory allocation method where the main memory is divided into multiple partitions, each of which can
be allocated to a different process. It supports multiprogramming and utilizes memory space and CPU time.
Fixed partitioning is a memory allocation method where the main memory is divided into a fixed number of
partitions, each of which can be allocated to a different process. This is the simplest type of multiple partitioning.
In fixed partitioning, the size of each partition is fixed and cannot be changed. This means that the operating
system must know the size of each process before it can be loaded into memory. If a process is too large to fit into
a partition, it cannot be loaded into memory.
Fixed partitioning is simple to implement and understand. However, it can lead to internal fragmentation. Internal
fragmentation occurs when a partition is allocated to a process, but the process only uses part of the partition.
The unused space in the partition is wasted.
Advantages:
• Easy to implement: Fixed partitioning is a simple memory allocation method that is easy to understand
and implement. The operating system simply divides the main memory into a fixed number of partitions,
and each partition can be allocated to a different process.
• Little OS overhead: Fixed partitioning requires very little overhead from the operating system. The
operating system does not need to track the free memory in each partition, and it does not need to move
processes around in memory.
Disadvantages:
• Not suitable for systems with a large number of user processes or for systems where memory efficiency
is important
• Internal fragmentation: This occurs when a partition is allocated to a process, but the process only uses
part of the partition. The unused space in the partition is wasted. For example, if a partition is 100MB in
size and a process is allocated 50MB of memory, then there will be 50MB of internal fragmentation in the
partition.
• External fragmentation: This occurs when there are small, unused blocks of memory scattered throughout
the main memory. This can happen when a process is removed from memory and the space it was using
is not given to another process. For example, if a partition of 100MB is removed from memory, and then
a process requests 50MB of memory, the operating system may not be able to find two adjacent 50MB
blocks of free memory.
• Limit on process size: The size of a process that can be loaded into memory is limited by the size of the
smallest partition. For example, if the smallest partition is 64MB, then no process larger than 64MB can
be loaded into memory.
b) Variable partitioning (Dynamic Partitioning)
It is a memory allocation method where the size of each partition can be changed. This means that the operating
system does not need to know the size of each process before it can be loaded into memory. The operating system
can simply allocate a partition that is large enough to hold the process.
Variable partitioning can avoid internal fragmentation. This is because the operating system can allocate a
partition that is exactly the size of the process that is being loaded. However, variable partitioning can still lead to
external fragmentation. This is because there may be small, unused blocks of memory scattered throughout the
main memory when a process is removed from memory.
Advantages
• No internal fragmentation: This occurs when a partition is allocated to a process, but the process only
uses part of the partition. The unused space in the partition is wasted. Variable partitioning avoids internal
fragmentation because the operating system can allocate a partition that is exactly the size of the process
that is being loaded.
• No limitation on the size of the process: The size of a process that can be loaded into memory is not limited
by the size of the smallest partition. This means that any process, regardless of its size, can be loaded into
memory as long as there is enough free memory available.
• Dynamic degree of multiprogramming: The degree of multiprogramming is the number of processes that
are actively running in the system at the same time. Variable partitioning can dynamically adjust the
degree of multiprogramming depending on the availability of free memory. This means that more
processes can be run in the system if there is more free memory available
Disadvantages
• External fragmentation: This occurs when there are small, unused blocks of memory scattered throughout
the main memory. This can happen when a process is removed from memory and the space it was using
is not given to another process. For example, if a process that is 100MB in size is removed from memory,
and then a process that is 50MB in size is requested, the operating system may not be able to find two
adjacent 50MB blocks of free memory.
This can be a problem because it can prevent the operating system from loading new processes into memory.
• Complex memory allocation: Variable partitioning is more complex to implement than fixed partitioning.
This is because the operating system needs to keep track of the free memory in each partition and it needs
to be able to dynamically adjust the size of the partitions. This can make it more difficult for the operating
system to allocate memory to processes efficiently.
The operating system has to keep track of all the partitions and their sizes. This can be a lot of work, especially if
there are a large number of partitions.
Fragmentation is a problem that occurs when there are small, unused blocks of memory scattered throughout
the main memory. This can happen when a process is removed from memory and the space it was using is not
given to another process. For example, if a process that is 100MB in size is removed from memory, and then a
process that is 50MB in size is requested, the operating system may not be able to find two adjacent 50MB blocks
of free memory.
Internal fragmentation: This occurs when a process is allocated a memory block that is larger than the size of the
process.
External fragmentation: This occurs when there are small, unused blocks of memory scattered throughout the
main memory.
• Compaction: This is the process of merging all the free memory blocks together into one large block. This
can be done by moving the processes that are currently in memory around.
• Paging: This is a method of memory management where the operating system divides the main memory
into small blocks called pages. Each process is divided into pages as well. When a process is loaded into
memory, only the pages that are needed are actually loaded. This can help to reduce fragmentation
because the unused pages can be stored in secondary memory.
• Segmentation: This is a method of memory management where the main memory is divided into
segments. Each process is divided into segments as well. When a process is loaded into memory, the entire
process is loaded, but only the segments that are needed are made accessible. This can help to reduce
fragmentation because the unused segments can be stored in secondary memory.
Internal fragmentation is a problem that occurs in memory allocation when a process is allocated a memory block
that is larger than the size of the process. The unused space in the memory block is wasted.
Causes
• Allocating memory blocks in excess of the process's size: This is the most common cause of internal
fragmentation. It occurs when the operating system allocates a memory block that is larger than the size
of the process that is being allocated.
• Allocating memory blocks in non-contiguous locations: This can also cause internal fragmentation. It
occurs when the operating system allocates memory blocks to processes in non-contiguous locations. This
can happen when the operating system is trying to minimize the amount of fragmentation, or when there
are not enough contiguous memory blocks available.
• Compaction: Compaction is the process of moving processes around in memory so that the unused space
is consolidated into larger blocks. This can help to reduce internal fragmentation, but it can also be a time-
consuming process.
• Paging: Paging is a memory management technique that divides memory into small blocks called pages.
Each process is divided into pages as well. When a process is loaded into memory, only the pages that are
needed are actually loaded. This can help to reduce internal fragmentation because the unused pages can
be stored in secondary memory.
External fragmentation is a problem that occurs in memory allocation when there are small, unused blocks of
memory scattered throughout the main memory. This can happen when a process is removed from memory and
the space it was using is not given to another process.
• Removal of a process: When a process is removed from memory, the space it was using is not given to
another process. This can happen if there are no other processes that need the space, or if the space is
not contiguous with the space that is needed by another process.
• Splitting of a process: When a process is split into two or more smaller processes, the unused space
between the processes is wasted.
• Allocation of memory blocks in non-contiguous locations: This can also cause external fragmentation. It
occurs when the operating system allocates memory blocks to processes in non-contiguous locations. This
can happen when the operating system is trying to minimize the amount of fragmentation, or when there
are not enough contiguous memory blocks available.
• Compaction: Compaction is the process of moving processes around in memory so that the unused space
is consolidated into larger blocks. This can help to reduce external fragmentation, but it can also be a time-
consuming process.
• Paging: Paging is a memory management technique that divides memory into small blocks called pages.
Each process is divided into pages as well. When a process is loaded into memory, only the pages that are
needed are actually loaded. This can help to reduce external fragmentation because the unused pages
can be stored in secondary memory.
• Segmentation: Segmentation is a memory management technique that divides memory into large blocks
called segments. Each process is divided into segments as well. When a process is loaded into memory,
the entire process is loaded, but only the segments that are needed are made accessible. This can help to
reduce external fragmentation because the unused segments can be stored in secondary memory.
Internal Fragmentation External Fragmentation
It occurs in the worst fit memory allocation method. It occurs in the best fit and first fit memory allocation
method.
It occurs in the best fit and first fit memory allocation External fragmentation occurs with segmentation
method. and dynamic partitioning.
The solution to internal fragmentation is the best-fit The solution to external fragmentation is compaction
block. and paging.
Cause: Allocating a memory block that is larger than Cause: Removal of a process, splitting of a process, or
the size of the process allocation of memory blocks in non-contiguous
locations
Unused space within an allocated memory block Unused space scattered throughout the main
memory
Reallocation
When a process is running, it needs to access memory locations to read and write data. However, the location of
the process in memory can change at any time. This can happen when the process is swapped out to disk to make
room for another process, or when the operating system needs to move the process around to improve memory
usage.
The relocation problem is the problem of ensuring that the process can still access its memory locations even if
its location in memory changes. This is solved by using base and limit registers.
The base register contains the address of the start of the process's memory space. The limit register contains the
size of the process's memory space. When the process makes a memory access, the operating system checks to
see if the address is within the bounds of the process's memory space. If it is, the access is allowed. If it is not, the
access is denied.
For example, suppose a process's base register contains the address 0x10000 and its limit register contains the
value 0x10000. This means that the process can only access memory locations between 0x10000 and 0x1ffff. If
the process tries to access a memory location outside of this range, the operating system will deny the access.
The base and limit registers are a simple but effective way to solve the relocation problem. They are used in most
multitasking operating systems.
Protection
The protection problem is related to the relocation problem. It is concerned with ensuring that each process can
only access its own memory space and not the memory space of other processes. This is important to prevent one
process from accidentally or intentionally corrupting the data of another process.
For example, suppose there are two processes, A and B. Process A has its own memory space, and Process B has
its own memory space. Process A should not be able to access the memory space of Process B, and vice versa.
The protection problem is solved using a technique called memory protection. Memory protection is a set of
mechanisms that allow the operating system to control which processes have access to which memory locations.
One way to implement memory protection is to use the base and limit registers. The base register contains the
address of the start of the process's memory space. The limit register contains the size of the process's memory
space. When the process makes a memory access, the operating system checks to see if the address is within the
bounds of the process's memory space. If it is, the access is allowed. If it is not, the access is denied.
In addition to the base and limit registers, the operating system can also use other mechanisms to implement
memory protection. These mechanisms include:
Permissions: The operating system can assign permissions to each memory location. These permissions can specify
whether a process can read, write, or execute the memory location.
Segmentation: The operating system can divide memory into segments. Each process can only access the
segments that it is authorized to access.
Paging: The operating system can divide memory into pages. Each page can be stored in a different location in
physical memory. The operating system can use paging to control which pages of a process are in memory at any
given time.
The protection problem is an important problem in multitasking operating systems. It is important to ensure that
each process can only access its own memory space and not the memory space of other processes. This helps to
prevent one process from accidentally or intentionally corrupting the data of another process.
Coalescing is the process of merging two adjacent free blocks of memory into a single larger free block. This can
be done when a process terminates and its memory is released back to the operating system. Coalescing can help
to reduce fragmentation, which is the problem of having small, unused blocks of memory scattered throughout
the main memory.
For example, suppose we have three free blocks of memory, each 100 bytes long. If two of these blocks are
adjacent, coalescing them would result in a single free block of memory that is 200 bytes long.
Compaction is the process of moving all the processes in memory so that the free blocks are contiguous. This can
be done by moving the processes down in memory, or by moving the free blocks up in memory. Compaction can
help to eliminate fragmentation, but it can be a time-consuming process.
For example, suppose we have three processes, each 100 bytes long, and two free blocks of memory, each 100
bytes long. If we compact the memory, we can move the processes down so that they are all together, and the
free blocks will be contiguous.
Coalescing and compaction can be used together to improve memory utilization. Coalescing can be used to reduce
fragmentation, and compaction can be used to eliminate fragmentation.
Non-contiguous memory allocation is a memory allocation technique where the blocks of memory allocated to a
process are not necessarily adjacent to each other. This is in contrast to contiguous memory allocation, where the
blocks of memory allocated to a process are contiguous. Non-contiguous memory allocation can be used to
improve memory utilization, but it can also make it more difficult for the operating system to manage memory.
In non-contiguous memory allocation, the operating system maintains a table called a page table for each process.
The page table contains the address of each block of memory allocated to the process. When the process needs
to access a memory location, the operating system looks up the address in the page table to find the physical
address of the memory location.
Non-contiguous memory allocation can be implemented using a technique called paging. In paging, the memory
is divided into small blocks called pages. Each page can be stored in a different location in physical memory. The
operating system keeps track of which pages are in memory and which pages are not in memory. When a process
needs to access a page that is not in memory, the operating system brings the page into memory.
a. First Fit
The first fit algorithm searches the list of memory blocks for the first block that is large enough to accommodate
the process. If the operating system finds a block that is large enough, it allocates the block to the process and
splits the block into two, with the first part being allocated to the process and the second part being left as a hole.
If the operating system does not find a block that is large enough, the process is not allocated memory and is
placed in a waiting state.
b. Worst Fit
The worst fit algorithm always uses the largest hole available to allocate memory to a process. This means that it
may leave behind smaller holes that cannot be used for other processes. This can lead to fragmentation, which is
the problem of having small, unused blocks of memory scattered throughout the main memory.
c. Quick Fit
The quick fit algorithm maintains separate lists for different sizes of memory blocks. When a process needs to be
allocated memory, the algorithm searches the list for the smallest block that is large enough. This can be faster
than first fit or best fit if the process size is close to the size of one of the blocks in the list. However, it can lead to
more fragmentation than first fit or best fit.
Virtual Memory
Virtual memory is a memory management technique used by operating systems to provide an illusion of having
more physical memory than is actually available. It allows programs to use more memory than what is physically
installed in the computer.
In virtual memory, each program is allocated a contiguous block of virtual memory addresses, which are divided
into fixed-size units called pages. These virtual pages are mapped to physical pages in the computer's RAM.
(yo sab kura Paging topic ma xa, kna ki paging haru Virtual memory kai Concept ho)
When a program accesses a virtual memory address, the OS checks if the corresponding physical page is in RAM.
If present, the program can access it directly. If not present, a page fault occurs and the OS retrieves the required
page from disk to RAM.
Paging
Paging is a memory management technique used in virtual memory systems where the process or tasks is divided
into fixed-size blocks called pages.
Physical memory is divided into frames (page frame), and the page size and frame size are typically the same to
make the mapping between virtual and physical memory easier.
- Pages of virtual memory are mapped to pages of physical memory through a page table maintained by the
operating system.
- When a process accesses a virtual page, the CPU uses the page table to translate the virtual address to the
physical address of the corresponding page in RAM.
- If the requested page is not in RAM, a page fault occurs. The OS then moves a page from disk into RAM using a
page replacement algorithm.
- Pages can be moved between RAM and disk as needed. This gives the illusion that processes have far more
memory than what is physically available.
So in summary, paging divides memory into uniform pages that are mapped between virtual and physical
addresses via a page table. This enables virtual memory capabilities.
Page table
A page table is a data structure used by an operating system's memory management unit (MMU) to map virtual
addresses to physical addresses.
- The page table stores the mapping between virtual pages and physical pages of memory. It associates each virtual
page with a physical frame.
- Page table contains an entry for each page in a process's virtual address space. The entry specifies the physical
location of the page. The page table entry contains things like
• frame number: frame no. in which the current looking page is present.
• present/absent bits: It says whether page you looking is present or not. ‘0’ if not present (page fault
occurs) and ‘1’ if present.
• protection bits: represent what kind of protection you want on page. (Read, write etc)
• referenced bits: says whether this page has been referred in last clock cycle.
• modified bits: it says if the page has been modified or not.
- The MMU consults the page table on every memory access to translate the virtual address to a physical one.
- The operating system is responsible for initializing and maintaining the page tables as pages move between
memory and disk.
- When a page fault occurs, the OS updates the page table entry to point to the new physical page that was loaded
from disk.
- Without a page table, the CPU would not be able to virtualize memory addresses or allow processes to run in
virtual memory.
So, in summary, a page table is a critical data structure that maps virtual pages to physical pages, enabling virtual
memory and address translation.
Types of Page table
Hierarchical page table
• To map virtual addresses to physical addresses, a page table is used. Since the page table can be very
large, it is stored in a hierarchical manner using multiple levels of page tables.
• At the top is the page directory, which maps large regions of virtual addresses (called page directory
entries) to the next level page tables.
• Page tables then map smaller regions (called page table entries [PTE]) to the actual physical frame
numbers where the pages reside.
• This hierarchical approach saves space by allowing sharing of page tables between processes when
possible
Inverted page table
In a normal page table, each process has its own page table containing entries for all its virtual pages. This can
use a lot of memory.
- An inverted page table has a single page table for the entire system, with one entry per physical frame in RAM.
The entry stores which process and virtual page is mapped to that physical frame.
This reduces memory usage compared to individual page tables, as the inverted table size is fixed based on
physical RAM size, not virtual address spaces.
- This technique is called inverted paging as indexing is done based on the frame number not logical page
number.
So in summary, an inverted page table provides a shared system-wide page table to reduce memory overhead
compared to per-process page tables.
- When a process starts, none of its pages are loaded into memory initially. Only its page table is populated
(created).
- The first access to a virtual page causes a page fault, as the page is not present in RAM.
- On a page fault, the OS loads the requested page from the swap area (usually disk) into an available physical
frame.
- Future accesses to that page will not cause faults, as it is now resident in memory.
- Pages are loaded on an as-needed basis. Pages that are not immediately needed remain swapped out to disk.
- However, it can lead to more page faults initially until frequently used pages are memory-resident.
So in summary, demand paging lazily loads pages on first access rather than all at once, improving memory
usage for processes not fully utilizing their allocated memory.
Page faults
A page fault is a type of exception that occurs when a process tries to access a virtual memory page that is not
currently mapped to a physical page in RAM.
- Page faults occur when a process accesses a virtual memory page that is not present in physical memory
(RAM).
- This happens because not all of a process's virtual address space can fit into physical RAM at once.
- Page faults are handled by the memory management unit (MMU) and addressed by the operating system's
virtual memory subsystem.
- When a page fault happens, the OS suspends the process and loads the requested page from secondary
storage (usually disk) into RAM.
- Once loaded, the process resumes and now has access to the page without any fault.
- Page faults enable demand paging and virtual memory by lazily loading pages only when needed rather than all
at once.
So in summary, page faults trigger the loading of virtual pages from disk to RAM on demand to support virtual
memory.
Trashing
Thrashing is a situation in which a computer system is constantly swapping pages in and out of memory, causing
the CPU to spend most of its time waiting for I/O operations. This can happen when a system has too many
processes running and not enough physical memory to hold them all.
In other words-
Thrashing is a condition when the system is spending a major portion of its time in managing page faults rather
than actual processing.
• The page fault rate increases dramatically as the OS is constantly swapping pages in and out.
• The system spends more time servicing page faults rather than actual process execution
• To avoid thrashing, the OS may kill processes or reduce their priority when physical memory is
low.
• Page replacement algorithms aim to reduce thrashing by prioritizing pages that will stay in
memory
Overall, Thrashing is when the page fault rate overwhelms the CPU due to insufficient RAM, degrading system
performance significantly. The OS tries to prevent it through memory management techniques.
Segmentation
Segmentation is a memory management technique used in computer systems to organize and manage memory
in a more flexible and structured manner. In segmentation, memory is divided into segments or blocks, each
representing a distinct logical unit such as a code segment, data segment, stack segment, or user-defined
segment. This technique allows programs to be divided into separate parts based on their functional
requirements, facilitating efficient memory allocation and protection.