Operating system
Operating system
unit 5
Disk Structure
A. Magnetic Disks
Magnetic disks are a key component of a computer's secondary storage. Here's a simplified explanation:
1. Rotational Speed: These disks rotate at speeds ranging from 60 to 200 times per second.
2. Transfer Rate: This is the speed at which data is transferred between the disk and the computer.
3. Positioning Time (Random-Access Time): This consists of two parts:
o Seek Time: The time it takes for the disk arm to move to the desired track or cylinder.
o Rotational Latency: The time it takes for the desired sector to rotate under the disk head.
If the disk head makes contact with the disk surface, it's called a head crash, which can cause significant damage to the
disk.
B. Removable Disks
Some disks are designed to be removable, allowing them to be taken out and replaced.
C. Drive Connection
Drives are connected to the computer through an I/O bus, which allows for data transfer between the disk and the
computer.
Disk scheduling is a method used by the operating system to manage how data is read from and written to the disk
efficiently. Here’s a simplified explanation:
The operating system (OS) ensures that the hardware, including disk drives, is used efficiently. For disks, this
means achieving fast access times and high disk bandwidth.
Access time, the total time to read or write data, has two main parts:
1. Seek Time: This is the time it takes for the disk arm to move the read/write heads to the correct track or
cylinder where the data is located.
2. Rotational Latency: Once the heads are at the correct track, this is the additional time spent waiting for the
disk to rotate the desired sector under the disk head.
The OS aims to minimize seek time because reducing the distance the disk arm has to move (seek distance)
can significantly improve performance.
Seek time is roughly proportional to the seek distance. The shorter the distance, the faster the access.
Seek time seek distance
E. Disk Bandwidth
Disk bandwidth refers to the total amount of bytes transferred divided by the total time taken from the first
request to the completion of the last transfer. Higher disk bandwidth means more data is transferred in less
time, indicating better performance.
Several algorithms are used to manage disk I/O requests efficiently. Here’s a simple explanation of each:
How it works: Selects the request that is closest to the current head position.
Pros: Reduces the total seek time compared to FCFS.
Cons: Can cause starvation for some requests, meaning some requests may wait a long time if there are
always closer requests coming in.
How it works: The disk arm starts at one end of the disk and moves towards the other end, servicing requests
as it goes. Once it reaches the other end, it reverses direction and continues servicing requests.
Pros: More efficient than FCFS and SSTF, as it reduces the back-and-forth movement.
Cons: Can still be inefficient if many requests are at one end of the disk.
1.2.4 CSCAN Scheduling (Circular SCAN)
How it works: Similar to SCAN, but when the head reaches one end of the disk, it immediately returns to the
beginning without servicing any requests on the return trip. It treats the disk as a circular list.
Pros: Provides a more uniform wait time compared to SCAN.
Cons: Slightly more complex than SCAN, but more efficient.
How it works: A version of CSCAN. The arm only goes as far as the last request in each direction before
reversing direction. It does not go all the way to the end of the disk if there are no requests there.
Pros: More efficient than CSCAN because it avoids unnecessary movement to the ends of the disk.
Cons: Similar to CSCAN, but with potentially less movement.
Here's a more detailed and simplified explanation of each disk scheduling algorithm, including important key points.
How It Works:
Requests are serviced in the order they arrive, like a queue at a ticket counter.
Key Points:
Example: If requests come in the order 98, 183, 37, 122, 14, 124, 65, 67, the disk arm will move to each of these
positions in this exact order.
How It Works:
Selects and services the request that is closest to the current head position.
Key Points:
Reduces Seek Time: Minimizes the movement of the disk arm, making it faster than FCFS.
Starvation: Some requests may wait indefinitely if there are always closer requests.
Example: If the current head position is at 50 and requests are at 98, 183, 37, 122, 14, 124, 65, 67, the next request
How It Works:
The disk arm moves from one end of the disk to the other, servicing requests as it goes. Once it reaches the
other end, it reverses direction and continues servicing.
Key Points:
Reduces Back-and-Forth Movement: The disk arm moves in one direction at a time, reducing unnecessary
movement.
Fairer Than SSTF: Ensures that all requests are eventually serviced, but may still have some inefficiency if
many requests are at one end.
Example: If the disk arm is moving from position 0 to 199 and requests are at 98, 183, 37, 122, 14, 124, 65, 67, it will
service requests in the order it encounters them (e.g., 14, 37, 65, 67, 98, 122, 124, 183).
How It Works:
Similar to SCAN, but when the disk arm reaches one end, it immediately returns to the beginning without
servicing any requests on the return trip. Treats the disk as a circular list.
Key Points:
Uniform Wait Time: More uniform wait time than SCAN, as it treats the disk circularly.
Consistent Direction: Reduces wait times for requests at the beginning of the disk after the arm reaches the
end.
Example: If the disk arm is at position 0, moves to 199, and requests are at 98, 183, 37, 122, 14, 124, 65, 67, it will
service requests as it moves towards 199. Once at 199, it jumps back to 0 without servicing and continues.
1.2.5 CLOOK Scheduling (Circular LOOK)
How It Works:
A variant of CSCAN. The disk arm only goes as far as the last request in each direction before reversing.
Key Points:
Optimized Movement: Avoids unnecessary movement to the very end of the disk if there are no requests.
Efficiency: Similar benefits to CSCAN but with potentially less movement.
Example: If requests are at 37, 14, 65, 67, 98, 122, 124, 183, the disk arm will service from the first to the last request
in one direction. If it starts at 0 and the last request is at 183, it will move to 183, then jump back to the beginning (if
the starting point had been 0) and service requests up to the first request on the other side.
When selecting a disk-scheduling algorithm, several factors should be considered to ensure the best performance and
efficiency. Here are the key criteria:
RAID (Redundant Array of Independent Disks) technology enhances data storage reliability and efficiency by using
multiple parallel-running disks. Here’s a breakdown of key RAID levels and related concepts:
1. RAID 0 – Striping
o How It Works: Data is split into blocks and spread across multiple disks.
o Advantages: Increases performance by allowing simultaneous read/write operations on multiple
disks.
o Disadvantages: No redundancy; if one disk fails, all data is lost.
o Best Use: High-performance applications where data loss is acceptable (e.g., gaming, video editing).
2. RAID 1 – Mirroring
o How It Works: Data is copied identically to two or more disks.
o Advantages: High data reliability; if one disk fails, data is still available on the other disk(s).
o Disadvantages: Doubles the storage requirement since the data is duplicated.
o Best Use: Critical data that requires high availability (e.g., system drives, databases).
3. RAID 5 – Striping with Parity
o How It Works: Data and parity information are striped across three or more disks.
o Advantages: Provides redundancy with efficient storage usage; can tolerate one disk failure.
o Disadvantages: Write operations are slower due to parity calculations.
o Best Use: General-purpose data storage where a balance of performance, storage efficiency, and
reliability is needed.
4. RAID 6 – Striping with Double Parity
o How It Works: Similar to RAID 5 but with additional parity information, allowing for two disk
failures.
o Advantages: Higher fault tolerance than RAID 5.
o Disadvantages: Requires more storage for parity and has slower write performance.
o Best Use: Environments requiring high data reliability (e.g., enterprise data storage).
5. RAID 10 – Combining Mirroring and Striping
o How It Works: Data is both striped and mirrored across at least four disks.
o Advantages: Combines the speed of striping (RAID 0) and the redundancy of mirroring (RAID 1).
o Disadvantages: High storage requirement, similar to RAID 1.
o Best Use: High-performance applications requiring high reliability (e.g., database servers).
RAID Controllers
Software RAID: Managed by the operating system (e.g., Windows Server 2012, Mac OS X).
o Advantages: Low cost and flexible.
o Disadvantages: Lower performance compared to hardware RAID, especially in RAID 5 and RAID 6.
Hardware RAID: Managed by a dedicated controller board.
o Advantages: Higher performance, especially for complex RAID levels like RAID 5 and RAID 6.
o Disadvantages: Higher cost.
Hardware RAID
Hardware RAID can be implemented in two main ways, both involving a dedicated RAID processor to manage the
RAID configuration and operations:
Software RAID
Software RAID can be implemented in two main ways, utilizing the system's CPU to manage RAID operations:
RAID (Redundant Array of Independent Disks) levels offer various methods of combining multiple physical disks to
improve performance, redundancy, or both. Here’s a closer look at the most commonly used RAID levels:
RAID 0 (Striping)
Minimum Disks: 2
Mechanism: Data is striped across multiple disks, enhancing read/write performance since data is accessed in
parallel.
Advantages:
o Extremely fast read and write operations.
o Utilizes full storage capacity as no space is reserved for redundancy.
o Simple to implement.
Disadvantages:
o No fault tolerance. If one disk fails, all data in the array is lost.
Ideal Use: Non-critical data storage requiring high-speed access, such as photo editing or video editing
stations.
RAID 1 (Mirroring)
Minimum Disks: 2
Mechanism: Data is duplicated (mirrored) across two disks, providing redundancy.
Advantages:
o High data redundancy; if one disk fails, data is still available on the other disk.
o Simple implementation.
Disadvantages:
o Storage capacity is halved since all data is duplicated.
o Write speed is similar to a single disk, but read speed can be faster since either disk can be read from.
Ideal Use: Mission-critical storage requiring high reliability, such as accounting systems or small server
setups.
Minimum Disks: 3
Mechanism: Data and parity information are striped across all disks. The parity data allows for data recovery
in case of a single disk failure.
Advantages:
o Good balance of performance, storage efficiency, and data redundancy.
o Can withstand the failure of one disk without data loss.
Disadvantages:
o Slightly slower write speeds due to parity calculations.
o Rebuilding a failed disk can be time-consuming and impacts performance.
Ideal Use: General-purpose storage for file and application servers with moderate performance and
redundancy needs.
Minimum Disks: 4
Mechanism: Similar to RAID 5 but with additional parity blocks, allowing for the failure of two disks.
Advantages:
o High fault tolerance, capable of withstanding two disk failures.
o Good read performance.
Disadvantages:
o Slower write performance due to double parity calculations.
o Complex implementation.
Ideal Use: Critical applications and large storage arrays requiring high data redundancy, such as large file
servers.
Minimum Disks: 4
Mechanism: Combines RAID 1 (mirroring) and RAID 0 (striping). Data is mirrored across pairs of disks, and
these pairs are striped.
Advantages:
o High performance and high redundancy.
o Quick rebuild time if a disk fails, since data is copied from the mirror.
Disadvantages:
o Requires more disks (half the storage capacity is used for mirroring).
o More expensive due to the need for more disks.
Ideal Use: High-performance and high-reliability environments, such as database servers.
Summary
Each RAID level offers a different balance of performance, redundancy, and cost:
What It Is: Also known as RAID 10, it combines RAID 1 (mirroring) and RAID 0 (striping).
How It Works:
o First, data is mirrored across pairs of disks (RAID 1).
o Then, these mirrored pairs are striped (RAID 0).
Advantages:
o Performance: High read/write speeds from striping.
o Reliability: Data redundancy from mirroring.
o Better Fault Tolerance: If one disk fails, only that specific disk and its mirror are affected. The rest
of the array is still accessible.
Disadvantages:
o Cost: Doubles the number of disks needed (because of mirroring).
Key Difference:
RAID 0+1: First stripes, then mirrors. If one disk in a stripe fails, the whole stripe is lost, but the mirror stripe
is still available.
RAID 1+0: First mirrors, then stripes. If one disk in a mirror pair fails, only that disk and its mirror are
affected, while the rest of the array remains accessible.
Summary:
RAID 0+1: High performance and redundancy but less fault-tolerant if one disk fails.
RAID 1+0: High performance, redundancy, and better fault tolerance for individual disk failures.
A file system is how an operating system (OS) manages data on a storage device. Here's a simplified breakdown:
Logical Storage Unit: The OS defines files as logical storage units, hiding the physical storage details.
Data Types: Files can contain different types of data, like numeric, character, or binary.
File Control Block (FCB): This is a data structure containing all the information about a file.
Record Structures
Simple Structures:
o Lines: Data arranged in lines.
o Fixed Length: Each record (unit of data) is the same length.
o Variable Length: Records can have different lengths.
Complex Structures:
o Formatted Document: Files with specific formatting (like a Word document).
o Relocatable Load File: Files that can be loaded and executed by the OS.
Files are managed through various operations, similar to functions you can perform on them:
Files can come in various types, each designed for specific purposes. Common file types include:
Files can be accessed in different ways, depending on how the data needs to be read or written.
Description: Combines the features of both sequential and direct access methods.
How It Works:
o Master Index: Points to blocks of a secondary index.
oSecondary Index: Points to the actual file blocks.
oSorted File: The file is kept sorted based on a defined key.
oBinary Search: First, a binary search is performed on the master index to find the block number in
the secondary index. Then, another binary search in the secondary index to find the actual file block.
Common Uses: Effective for large databases where quick access to records is needed.
Summary
These access methods help in managing how data is stored and retrieved from files, optimizing performance based on
different use cases.
1. Text Files:
o Example: Documents such as notes, scripts, or configuration files.
o Computer Use: Text editors (e.g., Notepad, Vim) are used to create and edit these files. Programmers
use text files to write source code, while system administrators use them for configuration files.
2. Binary Files:
o Example: Image files (e.g., .jpg, .png), video files (e.g., .mp4), and compiled program files
(e.g., .bin).
o Computer Use: Applications like media players (VLC, Windows Media Player) or image viewers
(Photoshop, Windows Photo Viewer) open and use binary files. These files contain data in a format
not directly readable by humans but understood by software.
3. Executable Files:
o Example: Programs and applications like web browsers (Chrome.exe, Firefox.exe) or operating
system components.
o Computer Use: The operating system executes these files directly to run applications. They contain
compiled code that the CPU processes to perform tasks.
1. Sequential Access:
o Example: Reading a novel from start to finish or listening to a podcast episode.
o Computer Use: Text editors (e.g., Notepad) read files line-by-line. Compilers process source code
files sequentially to convert them into executable files.
Operations:
Operations: Reading or writing a specific block in a database file, enabling quick lookups, updates, and
deletions.
3. Simulation of Sequential Access on a Direct Access File:
o Example: Reading a specific chapter in a textbook but maintaining the order of pages.
o Computer Use: Emulating sequential access in database management systems where data is accessed
in order but stored in a way that allows random access.
Operations: Keeping track of the current position in a log file stored on a disk, accessed in sequence by a
logging system.
Operations: A library database allows quick lookup of books by title, author, or ISBN through indexes,
combining fast sequential and direct access.
Summary
Sequential Access: Used for applications needing linear data processing, like reading logs or streaming audio.
Direct Access: Employed in systems requiring fast random data retrieval, such as databases and file systems.
Indexed Sequential Access (ISAM): Ideal for large-scale data management in databases, offering a balance of speed
and efficiency for both sequential and random access needs.
File allocation methods determine how disk blocks are allocated to store files. Each method has its advantages and
disadvantages.
Description: Each file is a linked list of disk blocks, which can be scattered anywhere on the disk.
Advantages:
o Simple: Only the starting address is needed.
o No Waste: Efficient use of disk space with no wasted space due to free-space management.
Disadvantages:
o No Random Access: Must follow the links sequentially to access a specific block, making random
access inefficient.
o Mapping: Requires a file-allocation table (FAT) for disk-space allocation (used by MS-DOS and
OS/2).
Example:
o Think of a treasure hunt where each clue (block) leads you to the next location. You must follow each
clue in sequence to reach the final treasure (end of the file).
Description: All pointers to the blocks of a file are brought together into an index block.
Advantages:
o Random Access: Easy to access any part of the file directly through the index.
o No External Fragmentation: Efficient dynamic access without external fragmentation, though there
is an overhead for the index block.
Disadvantages:
o Overhead: Requires additional space for the index table.
Example:
o Imagine a table of contents in a book. The table of contents (index block) tells you exactly which page
to go to for each chapter (file block).
Contiguous Allocation: Simple and efficient for random access but wastes space and limits file growth.
Linked Allocation: Efficient use of space with no waste but does not support random access well.
Indexed Allocation: Supports random access efficiently and avoids external fragmentation but requires extra
space for the index.
Contiguous Allocation
Description: In video editing software, large video files are often stored in contiguous blocks on a disk for quick
access. This ensures that the entire video file is read sequentially without any delay, providing a smooth editing
experience.
Video Editing: When working with large video files, contiguous allocation ensures that the video editor can
access any part of the video file quickly. Since the blocks are stored next to each other, the read/write head of
the hard disk doesn’t have to move much, speeding up the process.
Advantages: Quick access to any part of the video file, making the editing process smoother and more
efficient.
Disadvantages: If the video file needs to grow (e.g., by adding more footage), there might not be enough
contiguous space, requiring the file to be moved, which can be time-consuming.
Example: Think of a large video project stored on a contiguous set of blocks. When you scrub through the video
timeline, the software can quickly read the data without any noticeable delay.
Description: In systems where data is continuously logged, such as server logs or IoT device data, linked allocation is
often used. Data blocks can be scattered across the disk, with each block pointing to the next one in sequence.
Data Logging: As data is generated, it is written to the next available block, regardless of its location on the
disk. Each block contains a pointer to the next block, forming a linked list of data blocks.
Advantages: Efficient use of disk space, as blocks can be used wherever they are available.
Disadvantages: Sequential access can be slow if the data needs to be read or processed in a non-linear
fashion.
Example: Imagine a server logging system that records events. Each log entry is stored in the next available disk
block, and the system follows the pointers to read the entire log file.
Description: In database management systems, indexed allocation is used to manage the storage of database files. The
index block contains pointers to all the blocks that make up a file, allowing for efficient random access.
Database Management: When a database query is executed, the system can quickly access any part of the
database file by using the index block. This allows for efficient retrieval and updating of records.
Advantages: Random access is efficient because the index block provides direct pointers to each data block.
Disadvantages: Additional overhead is required to maintain the index block, which takes up extra space.
Example: Think of a large database storing customer records. The index block acts like a table of contents, pointing
directly to the disk blocks where each customer’s data is stored. When a query is run to find a specific customer, the
system uses the index block to quickly locate and retrieve the relevant data blocks.
Contiguous Allocation: Used in scenarios requiring high-speed sequential access, such as video editing or
streaming applications.
Linked Allocation: Suitable for systems where data is written continuously, like server logs or IoT data
logging, where efficient use of disk space is crucial.
Indexed Allocation: Ideal for applications requiring quick random access to data, such as database
management systems, where an index block provides direct pointers to data blocks.
File Sharing
File sharing in an operating system involves multiple users and systems accessing the same files, which necessitates
proper management of permissions and protections. Here's an overview of key aspects of file sharing:
User IDs:
o Each user is assigned a unique identifier (User ID).
o Permissions and protections can be assigned per user, ensuring security and privacy.
Group IDs:
o Users can belong to one or more groups, each with its own Group ID.
o Group access rights allow users within the same group to share files with specific permissions.
Remote file systems allow files to be accessed over a network, enabling sharing between different systems. This can
be done in several ways:
Manual Access:
o Programs like FTP (File Transfer Protocol) are used to manually transfer files between systems.
Automatic Access:
o Distributed file systems enable seamless access to remote files as if they were local.
Semi-automatic Access:
o The World Wide Web (WWW) provides semi-automatic access, where files can be accessed via web
protocols.
Client-Server Model:
Failure Modes:
Let's consider some real-world examples to illustrate how file sharing mechanisms are used in computer systems:
User IDs:
o Each student and staff member is assigned a unique user ID to log into the lab's computers.
o Permissions are set so that students can access course materials, submit assignments, and use software
necessary for their studies, but they cannot access or modify system files or other users' private data.
Group IDs:
o Students enrolled in a particular course are added to a group corresponding to that course.
o This group ID allows them to access shared folders where professors post lecture notes, assignments,
and other course-related materials.
o Professors have read-write access to these folders, while students have read-only access.
Multiple Users:
o Services like Google Drive, Dropbox, or OneDrive allow users to create individual accounts (User
IDs) and share files with specific individuals or groups (Group IDs).
o Users can set permissions to control who can view, comment, or edit shared documents.
Remote File Systems:
o These cloud services act as remote file systems where files can be accessed and edited from any
device with internet connectivity.
o Users upload files to the cloud (manual access), and the cloud service ensures files are available
across all their devices automatically (distributed file system).
o Sharing links to files via email or messaging platforms provides semi-automatic access.
File protection mechanisms are crucial for ensuring controlled access to files in a computer system. They determine
who can do what with each file based on the type of access being requested. Let's break down the types of operations
that can be controlled and how access control works in real-world scenarios.
1. User Identity: The most common approach is to base access on the identity of the user. Each user has specific
permissions for each file.
2. Access-Control List (ACL):
o Definition: An ACL specifies which users or system processes are granted access to objects, as well
as what operations are allowed on given objects.
o Problem with ACLs: They can be lengthy and cumbersome to manage. Listing all users who can
access a file, especially in large systems, can be tedious and inefficient.
o Solution: Use a condensed version of the access list by recognizing three classifications of users.
To manage access control more efficiently, many systems recognize three categories of users:
Scenario: In a company, each project has a set of files that team members need to access.
Implementation:
o Owner: The project manager who created the file.
o Group: The project team members.
o Universe: Other employees in the company.
File Access Control:
o Owner: Full permissions (read, write, execute, append, delete, list).
o Group: Read and write permissions to collaborate on documents.
o Universe: Read-only access to view project progress.
Scenario: System files in an operating system need to be protected from unauthorized modifications.
Implementation:
o Owner: The system administrator.
o Group: IT support staff.
o Universe: Regular users.
File Access Control:
o Owner: Full control to manage and update system files.
o Group: Read and execute permissions to diagnose issues without modifying files.
o Universe: Execute-only permissions to run necessary system applications without seeing or
modifying the files.
Scenario: In an online document editing platform (e.g., Google Docs), multiple users collaborate on a single
document.
Implementation:
o Owner: The user who created the document.
o Group: Users explicitly invited to collaborate.
o Universe: Any other user on the platform.
File Access Control:
o Owner: Full permissions to manage the document.
o Group: Permissions to read, write, and comment on the document.
o Universe: No access unless explicitly shared.
Summary
File protection and access control are essential for maintaining data integrity and security in computer systems. By
using mechanisms such as ACLs and classifying users into categories like owner, group, and universe, systems can
effectively manage who has access to what files and what they can do with them. Real-world applications, from
corporate file sharing to operating system management and online collaboration tools, all rely on these principles to
function securely and efficiently.
2.6 Recovery
Recovery mechanisms are essential in maintaining the integrity and availability of data within a computer system.
They help ensure that data can be restored in case of corruption, accidental deletion, or system failures. Here are the
key components of recovery mechanisms, explained with real-world examples.
Components of Recovery
1. Consistency Checking
o Definition: Consistency checking involves comparing the data in the directory structure with the
actual data blocks on the disk. This process identifies and attempts to fix any inconsistencies.
o Real-World Example: Think of it as an inventory check in a warehouse. The inventory list (directory
structure) is compared with the actual items on the shelves (data blocks). Any discrepancies are noted
and corrected.
2. Backup Systems
o Definition: Backup systems use specialized programs to copy data from the disk to another storage
device, such as a floppy disk, magnetic tape, another magnetic disk, or optical media.
o Real-World Example: Imagine a business that regularly saves copies of important documents to an
external hard drive. If the main computer's hard drive fails, the business can retrieve the necessary
files from the backup.
3. Data Recovery
o Definition: Data recovery involves restoring lost files or entire disks from backups.
o Real-World Example: Suppose a graphic designer accidentally deletes an important project file. By
using a backup system, the designer can restore the project from the latest backup, minimizing data
loss and work disruption.
Real-World Applications
Scenario: A computer crashes due to a power outage, potentially corrupting file system data.
Implementation: When the system reboots, a consistency checking tool like fsck (file system check) on
Unix-based systems automatically runs. It scans the directory structure and data blocks, identifies
discrepancies, and attempts to fix them.
Outcome: The file system is repaired, ensuring that all files are in a consistent state, and any corrupted files
are either fixed or quarantined.
Scenario: A company performs regular backups of its financial data to protect against data loss.
Implementation: The IT department schedules nightly backups of the company’s financial database to an off-
site server. They use backup software that compresses and encrypts the data before transferring it.
Outcome: In case of a server failure or cyber attack, the company can restore the financial database from the
most recent backup, ensuring business continuity with minimal data loss.
Example 3: Personal Data Recovery from Cloud Backups
Scenario: A user’s laptop is stolen, containing important personal and work-related files.
Implementation: The user has previously set up automatic cloud backups using a service like Google Drive
or Dropbox. All files are regularly synchronized and stored in the cloud.
Outcome: The user purchases a new laptop and restores all the files from the cloud backup, quickly getting
back to work without significant data loss.
Summary
Recovery mechanisms such as consistency checking, backup systems, and data recovery are critical for maintaining
data integrity and availability. Whether in personal computing, corporate environments, or operating systems, these
mechanisms ensure that data can be restored and systems can return to normal operations quickly after a failure or
data loss. Real-world examples demonstrate the practical application and importance of these recovery strategies in
safeguarding data.
Directory structures are essential for organizing files on a computer system. They help in managing data efficiently by
providing a systematic way to store and retrieve files. Here’s a simplified explanation of various directory structures
with real-world examples.
1. Partitions
o Definition: Disks are split into one or more partitions, also known as minidisks or volumes in the PC
world.
o Example: Think of partitions as different sections of a library, each section dedicated to a specific
category of books.
2. Directory Entries
o Definition: Each partition contains information about the files within it, stored in a device directory or
volume table of contents.
o Example: Similar to a library catalog that lists all books available in each section.
1. Single-Level Directory
o Description: All files are contained in a single directory.
o Advantages: Simple and easy to understand.
o Disadvantages: All files must have unique names, making it difficult to manage as the number of
files increases.
o Example: A folder on your desktop where all your documents, pictures, and videos are stored
together.
2. Two-Level Directory
o Description: Each user has their own directory.
o Advantages: Files are organized per user, preventing naming conflicts.
o Example: Each user on a computer has their own "Documents" folder where they store their files.
3. Tree-Structured Directory
o Description: Directories form a tree structure, allowing users to create subdirectories.
o Advantages: Provides a hierarchical structure, making it easier to organize files.
o Example: Your computer’s file system, where you have a "Documents" folder with subfolders like
"Work," "Personal," "Photos," etc.
4. Acyclic Graph Directory
o Description: Directories can share files and subdirectories, forming a graph without cycles.
o Advantages: Allows file sharing among different directories without duplication.
o Example: A shared folder in a network where multiple users can access the same files without having
copies in their own directories.
5. General Graph Directory
o Description: Similar to acyclic graphs but allows cycles.
o Advantages: More flexible in linking directories.
o Disadvantages: Ensuring no cycles can be complex.
o Example: Hyperlinks in a website, where pages can link back to each other forming loops.
Single-Level Directory: A simple USB drive where all files are stored in the root directory without any
folders.
Two-Level Directory: User profiles on a shared computer where each user has a personal space to store their
files.
Tree-Structured Directory: Most modern operating systems like Windows, macOS, and Linux use this
structure, allowing users to create folders and subfolders to organize their files.
Acyclic Graph Directory: Network file systems like NFS (Network File System) where directories can be
shared across different systems.
General Graph Directory: Advanced file systems in enterprise environments where complex file sharing and
linking are required.
Directory implementation refers to how a directory structure is organized and managed internally in a file system.
There are two common methods: Linear List and Hash Table.
Description:
A linear list is the simplest way to implement a directory. It consists of a list of file names, each with a pointer
to the data blocks where the file is stored.
Process: To create a new file, the system searches the entire directory to ensure no file has the same name. If
the name is unique, a new entry is added at the end of the list.
Advantages:
Disadvantages:
Time-consuming, especially for large directories, because it requires a full scan to search for or add files.
Example:
Imagine a classroom attendance list where each student's name is written one after the other. To check if a
student is present or to add a new student, you must go through the list sequentially.
Early computer systems often used this method due to its simplicity. For example, early versions of UNIX
used linear lists for directories.
Description:
A hash table improves upon the linear list by using a hash data structure.
Process: File names are hashed to produce a unique index into a table where the pointer to the data block is
stored. When a new file is created, its name is hashed, and the system checks the corresponding table entry to
ensure uniqueness.
Advantages:
Disadvantages:
Collisions can occur when two file names hash to the same index.
The hash table has a fixed size, which can limit the number of entries.
Example:
Think of a phone book where instead of searching sequentially, each person's name is converted into a number
(hash) that points directly to their phone number. If two names result in the same number, the system must
handle the collision.
Modern file systems, such as those used in Windows NTFS or some Linux filesystems, use hash tables to
improve directory lookup performance.
Summary
Both linear lists and hash tables are methods for implementing directory structures in a file system, each with its own
advantages and disadvantages.
Linear List: Simple but slow, requiring a full scan to find or add files. Suitable for small directories.
Hash Table: Faster with direct access but can suffer from collisions and has a fixed size. Suitable for larger
directories where quick access is needed.
Real-World Usage
Linear List: Early UNIX systems where simplicity was key and directories were small.
Hash Table: Modern operating systems like Windows NTFS and certain Linux filesystems where quick
access to a large number of files is essential.
Free space management is crucial in file systems to efficiently utilize the available disk space. It ensures that space
from deleted files can be reused for new files. Two common methods for managing free space are the Bit Vector (or
Bit Map) and the Linked List.
Description:
Advantages:
Disadvantages:
Requires additional memory to store the bit vector, especially for large disks.
Searching for free blocks might still require scanning a large portion of the bit vector.
Example: Consider a disk with blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 free. The bit map would
be:
Copy code
001111001111110001100000011100000
This bit map indicates that blocks 2 to 5, 8 to 13, 17, 18, and 25 to 27 are free, while the others are allocated.
Some operating systems and file systems, such as early versions of UNIX, use bit maps for free space
management. It's effective for systems where disk space is limited and simplicity is desired.
Description:
In the linked list approach, all free disk blocks are linked together.
A pointer to the first free block is kept in a special location on the disk and cached in memory.
Each free block contains a pointer to the next free block, forming a chain.
Advantages:
Simple to implement.
No need for additional memory to store a bit vector; pointers are stored within the free blocks themselves.
Easy to find the first free block and traverse the list.
Disadvantages:
Not as efficient for finding large contiguous free spaces since blocks may be scattered.
Traversing the list can be time-consuming if the list is long.
Example: Imagine a series of free disk blocks linked together. The first block contains a pointer to the next free block,
which in turn contains a pointer to the next one, and so on.
mathematica
Copy code
Free Block 1 -> Free Block 2 -> Free Block 3 -> ... -> Free Block N
The FAT (File Allocation Table) file system, used in older versions of MS-DOS and Windows, uses a
variation of the linked list method where each entry in the table points to the next block in a file, including
free blocks.
Summary
Both methods are used in various file systems to manage free space:
Bit Vector (Bit Map): Suitable for systems requiring quick checks of block status and easy management of
contiguous free space. Ideal for smaller disks or where memory usage is not a critical issue.
Linked List: Good for systems where memory conservation is important and simple implementation is
preferred. Suitable for disks where free blocks might be scattered.
3.I/O management:
3.1 I/O Hardware: Computers operate a great many kinds of devices.
Most fit into the general categories of storage devices (disks, tapes), transmission devices (network cards,
modems), and humaninterface devices (screen, keyboard, mouse).
Other devices are more specialized, such as the steering of a military fighter jet or a space shuttle.
In these aircraft, a human gives input to the flight computer via a joystick and foot pedals, and the computer
sends output commands that cause motors to move rudders, flaps, and thrusters.
• The device communicates with the machine via a connection point (or port).
• If devices use a common set of wires, the connection is called a bus. A bus is a set of wires and a rigidly defined
protocol that specifies a set of messages that can be sent on the wires.
• In terms of the electronics, the messages are conveyed by patterns of electrical voltages applied to the wires
with defined timings. When device A has a cable that plugs into device B, and device B has a cable that plugs into
device C, and device C plugs into a port on the computer, this arrangement is called a daisy chain.
• A typical PC bus structure is shown in figure. This figure shows a PCI bus (Peripheral Component Interconnect,
the common PC system bus) that connects the processor- memory subsystem to the fast devices . An expansion
bus that connects relatively slow devices such as the keyboard and serial and parallel ports.
• A controller is a collection of electronics that can operate a port, a bus, or a device. A serial-port controller is a
simple device controller. It is a single chip (or portion of a chip) in the computer that controls the signals on the
wires of a serial port. By contrast, a SCSI bus controller is not simple. Because the SCSI protocol is complex, the
SCSI bus controller is often implemented as a separate circuit board (or a host adapter) that plugs into the
computer
1. Connection Points and Ports: Devices communicate with computers through connection points or ports. For
example, USB ports on a computer allow various devices like keyboards, mice, and external drives to connect
and communicate.
2. Buses: When multiple devices share a common set of wires for communication, it forms a bus. A bus has a
defined protocol specifying how messages are transmitted across the wires. An example is the PCI bus used in
PCs to connect components like graphics cards and network adapters to the motherboard.
3. Daisy Chaining: This refers to connecting multiple devices in sequence, where each device connects to the
next, forming a chain. Each device in the chain may communicate using a common bus structure. For
instance, in a SCSI (Small Computer System Interface) setup, multiple devices like hard drives can be daisy-
chained together.
4. Controllers: These manage the operation of ports, buses, or devices. A serial-port controller handles signals
on a serial port, while more complex controllers like SCSI bus controllers manage the intricacies of the SCSI
protocol for devices like high-speed hard drives.
Overview: SCSI is a standard for connecting and transferring data between computers and peripheral devices.
It supports high-speed data transfer rates and allows multiple devices to be connected in a chain or bus
configuration.
Application Example: Imagine a server setup in a data center. The server needs to connect multiple high-
speed hard drives for storing and accessing large amounts of data quickly. A SCSI bus controller (often a
separate circuit board or host adapter) manages the communication between the server and the SCSI devices.
Benefits: SCSI offers faster data transfer rates compared to older interfaces like IDE (Integrated Drive
Electronics). It's commonly used in environments where speed and reliability are critical, such as servers and
high-performance computing systems.
Functionality: Consider a serial port controller in a computer, which manages communication with external
devices like modems or serial printers.
Registers: The serial port controller typically has:
o Data-In Register: Holds incoming data bytes from the external device.
o Data-Out Register: Stores outgoing data bytes to be sent to the external device.
o Status Register: Contains flags such as data-ready, transmission complete, and error indicators.
o Control Register: Allows the processor to set parameters like baud rate, parity settings, and flow
control.
Processor Interaction: To send data through the serial port, the processor writes the data byte to the Data-Out
register. It then uses control bits in the Control Register to initiate the transmission. The Status Register can be
polled by the processor to check when the transmission is complete or if any errors occurred.
the concepts of polling and interrupts in the context of interaction between a host (processor) and a controller, using a
practical example:
Polling:
Concept: Polling involves the host (processor) continuously checking the status of a device by reading
specific bits in its status register until a certain condition is met (e.g., until a busy bit becomes clear).
Example: Consider a printer connected to a computer. The host wants to send a document to the printer:
1. Host Action: The host checks the printer's status register repeatedly to see if the printer is ready to
receive data (polls the busy bit).
2. Write Command: Once the busy bit is clear, the host sets the write command in the controller's
command register and writes a byte into the data-out register, signaling that data transmission can
begin.
3. Controller Response: The controller detects the write command and sets the command-ready bit,
indicating it's ready to receive data. It then sets the busy bit to show it's processing.
4. Data Transmission: The controller reads the command register, sees the write command, and
retrieves the byte from the data-out register. It sends this byte to the printer for printing.
5. Completion: After completing the task, the controller clears the command-ready bit, updates the
status register (clears error bits if successful), and clears the busy bit to indicate it's done.
Interrupts:
Concept: Interrupts allow a device (controller) to notify the processor that it needs attention, avoiding
constant polling. When an interrupt occurs, the processor temporarily stops what it's doing, saves its current
state, and jumps to an interrupt handler routine to service the interrupt.
Example: Imagine a network card in a computer that receives data packets:
1. Interrupt Request: The network card asserts an interrupt request (IRQ) line when it has received a
data packet.
2. CPU Response: The CPU detects the IRQ and suspends its current task. It saves its state and jumps to
a predefined memory location where the interrupt handler for the network card is located.
3. Interrupt Handler: The interrupt handler identifies the source of the interrupt (network card),
processes the received data packet (e.g., stores it in memory), clears the IRQ, and restores the CPU to
its previous state.
4. Resume Execution: After handling the interrupt, the CPU resumes the task it was performing before
the interrupt occurred.
Polling is straightforward but can waste CPU cycles since it involves constant checking.
Interrupts are efficient as they allow the CPU to perform other tasks until needed by a device, reducing CPU
utilization.
In modern computers, interrupts are extensively used for handling various events such as data transmission, keyboard
input, and network operations, optimizing system performance by allowing the processor to multitask efficiently.
Purpose: DMA is used to offload data transfer tasks from the main processor (CPU) to a specialized DMA
controller. This is particularly useful for devices like disk drives that need to transfer large amounts of data
quickly.
Operation:
1. Setup: To initiate a DMA transfer, the host (CPU) sets up a DMA command block in memory. This
block includes:
Source Pointer: Points to the starting address of data to be read from (e.g., from a disk drive).
Destination Pointer: Points to where the data should be written (e.g., to system memory).
Byte Count: Specifies the number of bytes to be transferred.
2. DMA Controller: The DMA controller reads this command block from memory and coordinates the
transfer:
It asserts a DMA-request (DMA-RQ) signal to gain control of the system bus.
Once granted control (DMA-acknowledge), it transfers data directly between the source and
destination addresses specified in the command block.
The CPU is freed from actively managing the data transfer, allowing it to perform other tasks
concurrently.
Handshaking via DMA-request and DMA-acknowledge:
o DMA-request: Signal from the DMA controller to request control of the system bus for data transfer.
o DMA-acknowledge: Signal from the system acknowledging and granting control to the DMA
controller.
Scenario: Imagine a computer system with a DMA controller managing data transfers between a disk drive
and system memory:
1. Setup: The CPU sets up a DMA command block in memory specifying:
Source Pointer: Address in the disk drive's memory where data is stored.
Destination Pointer: Address in the system memory where data should be written.
Byte Count: Number of bytes to transfer.
2. DMA Transfer Initiation: The DMA controller:
Asserts DMA-request to gain control of the system bus.
Receives DMA-acknowledge from the system, granting it control.
Reads data directly from the disk drive via its controller, bypassing the CPU.
Writes data directly to system memory.
3. Completion: Once the transfer is complete, the DMA controller releases control of the bus and
signals completion to the CPU or another controller.
Benefits of DMA:
Efficiency: Reduces CPU overhead by allowing data transfers to occur independently.
Speed: Facilitates faster data transfers between devices and memory.
Multitasking: Enables the CPU to handle other tasks while data transfers are ongoing.
In summary, DMA controllers streamline data movement in computer systems, optimizing performance especially for
devices like disk drives that handle large data volumes. This technology enhances overall system efficiency by freeing
up the CPU for more critical tasks.
Direct Memory Access (DMA) facilitates efficient data transfers in computer systems by offloading the task from the
main processor (CPU) to a specialized DMA controller. Here’s a step-by-step explanation of how DMA transfers data:
Benefits of DMA:
Block Devices:
Interface: Captures all necessary aspects for accessing disk drives and other block-oriented devices.
Commands:
o read(): Reads data from the device.
o write(): Writes data to the device.
o seek(): Specifies which block to transfer next (for random-access devices).
Example:
Disk Drive: Uses block-device interface for operations like reading or writing data blocks.
Character Devices:
Example:
Example:
Web Server: Uses the socket interface to handle client connections, enabling efficient communication over
the network.
Hardware Functions:
Functions Provided:
o Current Time: Provides the current time.
o Elapsed Time: Measures the time elapsed.
o Timers: Sets a timer to trigger specific operations at a given time.
Usage:
Operating Systems and Applications: Heavily rely on these functions for time-sensitive tasks.
Hardware:
Programmable Interval Timer: Used to measure elapsed time and trigger operations.
High-Frequency Counter: Commonly constructs the hardware clock.
Example:
Scheduling Tasks: Operating systems use timers to manage task scheduling and execution timings.
Blocking I/O:
Behavior: Suspends the application's execution until the system call completes.
Process:
o Application moves from the run queue to the wait queue.
o After completion, the application is moved back to the run queue and resumes execution.
Example:
Disk Read Operation: The application waits for the read operation to complete before proceeding.
Nonblocking I/O:
Behavior: Allows the application to continue processing while the I/O operation proceeds.
Use Cases:
o User interfaces receiving keyboard and mouse input while processing data.
o Video applications reading frames from a disk while decompressing and displaying them.
Nonblocking I/O: Returns immediately with available data, even if the full number of bytes requested is not
available.
Asynchronous I/O: Requests a complete transfer that completes at some future time but does not block the
application.
Example:
User Interface: Continues to receive and handle input while updating the display.
Video Application: Reads and processes video frames concurrently.
Scenario:
Operation:
Uses nonblocking I/O to manage incoming client requests without waiting for any single request to complete.
Continues to accept new connections and handle existing ones efficiently.
Benefit:
Efficiency: The server can handle high volumes of concurrent requests, improving performance and
responsiveness.
Purpose:
Determines the optimal order for executing a set of I/O requests to enhance system efficiency and application
response time.
Example:
Benefits:
3.3.2 Buffering
Definition:
A buffer is a memory area that temporarily stores data during transfer between devices or between a device
and an application.
Purposes:
1. Speed Mismatch:
o Handles differences in speed between data producer and consumer.
o Example: Receiving a file via modem (slow) and storing it on a hard disk (fast). A buffer accumulates
bytes from the modem before writing to the disk.
2. Adaptation:
o Adapts between devices with different data-transfer sizes.
o Example: Fragmenting a large message into small network packets for transmission and reassembling
them at the receiving end.
3. Copy Semantics:
o Supports copy semantics for application I/O to ensure data integrity and consistency.
3.3.3 Caching
Definition:
A cache is a fast memory region holding copies of data for efficient access.
Function:
Spooling:
Definition: A buffer that holds output for a device (e.g., a printer) that cannot handle interleaved data streams.
Function: Prevents output from multiple applications from mixing.
Example: Printing – The OS intercepts print jobs, spools them, and sends them to the printer one by one.
Device Reservation:
Ensures that a device is reserved for a particular task, preventing conflicts and ensuring orderly access.
Purpose:
Benefits:
Real-World Examples
I/O Scheduling:
Disk Scheduler: In modern operating systems like Windows and Linux, I/O scheduling algorithms like
SCAN (elevator algorithm) or C-SCAN are used to optimize disk access.
Buffering:
Networking: When downloading a large file, the data is buffered before being written to the disk,
accommodating the speed difference between network transfer and disk write speed.
Caching:
Web Browsers: Frequently accessed web pages and resources are cached to reduce load times.
Spooling:
Printing: Multiple print jobs are spooled by the operating system to be printed sequentially without
interference.
Error Handling:
SCSI Devices: Advanced error reporting allows for specific troubleshooting steps to be taken based on the
detailed error information provided.
These kernel I/O subsystems ensure that the operating system can handle I/O operations efficiently, reliably, and in a
way that maximizes overall system performance and stability.
Unit 4
Memory Management
1.1.1 Introduction
Definition:
A bare machine refers to a computer system without an installed operating system (OS).
Ability to Recover:
Recovery Capability: The system can be restored to the exact state it was at a specific point in time.
Importance: Ensuring that the OS is internally consistent and functional at a particular point in time is crucial
for system reliability and data integrity.
Reasons:
a) System Reinstallation
Definition:
Suitability:
Characteristics:
Steps:
1. Insert installation media.
2. Follow prompts to reinstall the OS.
3. Configure necessary settings and drivers.
Definition:
Characteristics:
Knowledge Requirements:
Partitioning disks.
Configuring network settings.
Setting boot parameters.
Steps:
Real-World Examples
System Reinstallation:
Corporate Environments: IT departments often use reinstallation strategies for standardized office machines
to quickly restore them to a working state using network installation servers or installation media.
Home Users: Individuals use backup software like Acronis True Image or Windows Backup to create regular
system images. In case of system failure, they can restore their system to the last backup state.
Data Centers: Utilize automated backup solutions to create regular snapshots of server states, ensuring quick
recovery in case of hardware failure or data corruption.
Summary
The concept of a bare machine and the strategies for recovering a system to its previous state are essential for
maintaining system integrity and functionality. System reinstallation and backup/restore methods provide different
approaches depending on the needs and environments, ensuring that systems can be quickly and effectively recovered
from failures or disasters.
Overview
Definition:
A resident monitor was an essential software component in early general-use computers from the 1950s to the
1970s, particularly in batch systems using punched card input.
Function:
The resident monitor governed the machine before and after each control card was executed.
It loaded and interpreted each control card.
Acted as a job sequencer for batch operations.
Historical Context
Early Computers:
Evolution:
Resident monitors were eventually replaced by boot monitors, boot loaders, BIOS, and operating system
kernels.
This shift occurred as rewritable base instruction sets became necessary.
Permanent Residency:
Components:
1. Control-Card Interpreter:
o Reads and executes instructions from control cards at the point of execution.
2. Loader:
o Invoked by the control-card interpreter to load system and application programs into memory as
needed.
3. Device Drivers:
o Used by both the control-card interpreter and the loader for I/O operations.
o Shared between the system and application programs to ensure continuity and efficiency.
Key Functions
1. Job Sequencing:
o Ensures jobs are executed in the correct order in a batch processing environment.
2. Memory Management:
o Loads programs into memory and manages their execution.
3. I/O Management:
o Uses device drivers to perform input and output operations.
4. Execution Control:
o Monitors and controls the execution of each job, ensuring smooth transitions between jobs.
Historical Example:
In a 1960s batch processing system, a resident monitor would control the sequence of jobs submitted on
punched cards. It would interpret control cards, load necessary programs, and manage the execution of each
job.
Legacy:
The concept of the resident monitor laid the foundation for modern operating systems.
Modern equivalents include BIOS and boot loaders, which initialize system hardware and load the operating
system kernel.
Summary
The resident monitor was a primitive yet crucial part of early computing, responsible for managing batch job
execution and system control in an era where manual intervention was common. Its evolution into more sophisticated
boot loaders and operating systems reflects the growth of computing technology and the need for more automated and
efficient system management tools.
1.3.1 Introduction
Definition:
Memory management in a multiprogramming system involves managing the allocation and deallocation of
memory spaces to various processes, ensuring efficient and safe use of memory resources.
Purpose:
Processor Sharing: To enable multiple processes to share the processor effectively, several processes must be
loaded into memory simultaneously.
Memory Management Algorithms: Different algorithms are used to manage memory allocation, each
requiring specific hardware support.
Compiler Role: The compiler generates relocatable object code, which allows programs to be loaded at any
location in memory, rather than at a fixed address.
Protection Mechanism:
Memory Isolation: To ensure that a program addresses only its allocated memory space and does not
interfere with other programs, protection mechanisms are implemented.
Summary
Memory management is a critical component of multiprogramming systems, ensuring that multiple processes can
share the processor and memory resources efficiently and safely. By utilizing various memory management
algorithms, hardware support, relocatable object code, and protection mechanisms, the operating system manages the
allocation, protection, and isolation of memory spaces for each process. This ensures both efficient use of memory and
the stability and security of the system.
Real-World Examples
Fixed Partitioning:
Mainframe Computers: Early mainframes used fixed partitioning where memory was divided into fixed-size
partitions, each holding one job.
Paging:
Modern Operating Systems: Systems like Windows and Linux use paging to manage memory, allowing
processes to be allocated memory in discrete pages.
Segmentation:
Multics Operating System: Used segmentation to divide memory into logical segments, making it easier to
manage large programs.
Virtual Memory:
Modern Computers: Use virtual memory to provide the illusion of a large address space, even if physical
memory is limited, improving both security and efficiency.
Overview:
Address binding refers to the process of mapping program instructions and data to specific memory addresses.
A program starts as a binary executable file on disk and must be loaded into memory to execute.
1. Compile Time:
o Absolute Code Generation: If the process location in memory is known at compile time, absolute
code is generated.
o Example: If the process starts at location R, the compiler generates code starting from R.
o Limitation: Requires recompilation if the starting location changes.
2. Load Time:
o Relocatable Code: If the process location is not known at compile time, the compiler generates
relocatable code.
Binding at Load Time: Final binding occurs when the program is loaded into memory.
o
Flexibility: Allows reloading the user code to adjust to a new starting address without recompilation.
o
3. Execution Time:
o Dynamic Binding: If the process can move during execution, binding must be delayed until runtime.
o Runtime Flexibility: Allows processes to change memory segments during execution.
Definitions:
Compile Time & Load Time: Logical and physical addresses are the same.
Execution Time: Logical and physical addresses differ.
Summary
Address binding is a critical concept in memory management, ensuring that programs can be executed from various
memory locations. It can occur at compile time, load time, or execution time, each with its own set of advantages and
trade-offs. Logical addresses, generated by the CPU, are mapped to physical addresses by the MMU, allowing
flexibility and efficient memory utilization without exposing physical addresses to the user program.
Real-World Examples
Address binding techniques ensure efficient and flexible memory management, crucial for the effective operation of
modern computing systems.
Definition:
Dynamic loading improves memory utilization by loading routines into memory only when they are needed.
Mechanism:
1. Routines in Relocatable Load Format:
o All routines are stored on disk in a format that allows them to be loaded at any memory address.
2. Main Program Execution:
o The main program is initially loaded into memory and begins execution.
3. Routine Invocation:
o When a routine needs to call another routine, it checks if the desired routine is already loaded.
o If the routine is not loaded, the relocatable loader loads it into memory.
4. Control Transfer:
o Control is then transferred to the newly loaded routine.
Advantages:
Real-World Application:
Large Applications: Applications with extensive codebases that include rarely used features can benefit from
dynamic loading to reduce their memory footprint.
Dynamic Linking:
Postponed Linking: Similar to dynamic loading, but here the linking of libraries is delayed until runtime.
Usage: Commonly used with system libraries, like language subroutine libraries.
Shared Libraries:
Version Independence: Programs linked before a new library is installed continue to use the older version,
allowing multiple versions of libraries to coexist.
Efficiency: Reduces the memory overhead as multiple programs can share a single copy of a library loaded
into memory.
Advantages:
Memory Savings: Reduces the amount of memory needed for common routines used by multiple programs.
Ease of Updates: Updating a library does not necessitate recompiling dependent programs.
Real-World Application:
Operating Systems: Modern operating systems use shared libraries extensively to manage memory
efficiently and allow for easy updates.
1.3.1.4 Overlays
Concept:
Memory Optimization: Only the necessary instructions and data are kept in memory at any given time.
When different instructions are needed, they replace the currently unused instructions in memory.
Mechanism:
Memory Efficiency: Allows programs larger than the available memory to execute by only keeping necessary
parts in memory.
Simpler Management: Simplifies memory management for large programs with distinct phases or sections.
Real-World Application:
Legacy Systems: Early systems with limited memory used overlays extensively to manage large applications
within constrained memory environments.
Summary
Dynamic loading, dynamic linking, shared libraries, and overlays are techniques that optimize memory utilization,
providing flexibility, efficiency, and improved performance in program execution. These methods allow programs to
dynamically manage their memory needs, reducing the overall memory footprint and enhancing the system's ability to
handle larger and more complex applications. By leveraging these techniques, systems can run more efficiently, with
reduced memory overhead and improved runtime flexibility.
Definition:
Contiguous memory allocation divides memory into partitions where each partition can hold exactly one
process. This ensures that the memory allocated to a process is contiguous.
Partitioning:
Purpose:
Protects the operating system from user processes and user processes from one another.
Mechanism:
1. Base Register:
o Contains the smallest physical address for a process.
o Ensures that all addresses used by a process are above a certain minimum.
2. Limit Register:
o Specifies the range of logical addresses.
o Each logical address must be less than the value in the limit register to ensure it falls within the
process's allocated memory space.
3. MMU (Memory Management Unit):
o Dynamically maps logical addresses to physical addresses using the base and limit registers.
o Provides hardware support for relocation and limit checking, ensuring that processes do not access
memory outside their allocated space.
Hardware Support:
Mechanism:
1. Fixed Partitions:
o Memory is divided into partitions of fixed size.
o The operating system resides in the lowest portion of memory, and the rest is divided among user
processes.
2. Process Classification:
o Processes are classified based on their memory requirements.
o Each class of process has its own Process Queue (PQ).
3. Memory Allocation:
o When a process is selected for execution, it is allocated a partition.
o The number of partitions determines the degree of multiprogramming.
o Each queue corresponds to a memory region, ensuring no competition for memory between different
queues.
Advantages:
Disadvantages:
Wasted Memory: Fixed partition sizes can lead to wasted memory if the process size does not match the
partition size.
Limited Flexibility: Changing partition sizes requires system reconfiguration.
Real-World Application:
Early Operating Systems: Used fixed partitioning to manage memory in a straightforward manner.
Modern Systems: Typically use more advanced memory management techniques but may still use fixed
partitioning for specific tasks or real-time systems.
Summary
Contiguous memory allocation provides a straightforward way to manage memory by dividing it into fixed partitions.
Memory protection is ensured using base and limit registers, which prevent processes from accessing memory outside
their allocated space. Multiprogramming with fixed partitions allows multiple processes to reside in memory
simultaneously, with each partition holding exactly one process. This method offers simplicity and reduces memory
fragmentation but can lead to wasted memory and limited flexibility.
Definition:
Fixed partitioning with swapping enhances the traditional fixed partitioning method by incorporating round-
robin scheduling (RRS) with time quantum. When a process's time quantum expires, it is swapped out to disk,
and the next process in the queue is swapped into memory.
Key Points:
1. Swapping Mechanism:
o Processes are swapped out of memory to disk when their time quantum expires.
o The next process in the process queue is swapped into memory.
Normally, a swapped-out process is swapped back into the same partition, but dynamic relocation can
o
relax this restriction.
2. Handling Memory Requests:
o If a process requests more memory than its partition size, there are several possible policies:
Return Control to User: The user program can decide whether to quit or modify its
operation to fit within the available memory.
Abort the Process: The process is terminated if it exceeds its memory limit.
Dynamic Relocation: Swap the process to the next largest partition queue and allocate it to
that partition when its turn comes.
Challenges:
Example of Fragmentation:
If a new process requiring 8 KB of memory arrives, and memory is fragmented into smaller free partitions, it
cannot be loaded despite there being enough total free space.
Variable Partitioning
Definition:
Variable partitioning allows partition sizes to vary dynamically, addressing the issue of determining the
number and sizes of fixed partitions.
Mechanism:
1. Memory Management:
o Memory is initially considered as one large free block.
o When a process arrives, the OS searches for a free block large enough to accommodate the process.
o Remaining free memory is kept available for future processes.
2. Free Block Management:
o The OS maintains a table or linked list indicating used and free areas in memory.
o When a block becomes free, the OS tries to merge it with neighboring free blocks to form a larger
contiguous block.
Advantages:
Summary
Fixed partitioning with swapping improves upon traditional fixed partitioning by using round-robin scheduling and
swapping processes in and out of memory. It addresses issues of memory requests exceeding partition sizes through
policies like dynamic relocation. However, it still faces challenges with fragmentation and determining partition sizes.
Variable partitioning offers a more flexible approach, dynamically adjusting partition sizes to fit process requirements.
It reduces fragmentation by merging free blocks, making memory management more efficient and adaptable to
varying process needs.
Dynamic Storage Allocation Algorithms
In the context of dynamic storage allocation, there are three primary algorithms used to search for free memory blocks
to allocate for new processes. Each algorithm has its own advantages and disadvantages in terms of speed, memory
utilization, and fragmentation.
a) First Fit
Definition:
Allocates the first free block that is large enough to satisfy the request for memory.
Advantages:
Fast Execution: The algorithm scans the list of free blocks and stops as soon as it finds a block large enough
to accommodate the new process. This makes it faster compared to other algorithms since it doesn't need to
traverse the entire list.
Disadvantages:
Potential Fragmentation: Because it always takes the first suitable block, it can leave small unusable
fragments of memory scattered throughout the list, leading to external fragmentation over time.
b) Best Fit
Definition:
Allocates the smallest free block that is large enough to satisfy the request for memory.
Advantages:
Minimizes Wasted Space: By choosing the smallest possible block that meets the requirement, it leaves the
largest possible remaining free blocks, which can be more easily utilized by future processes. This can
potentially reduce external fragmentation.
Disadvantages:
Slower Execution: The algorithm typically requires scanning the entire list to find the smallest suitable block,
which takes more time compared to the first fit. Alternatively, the list must be kept sorted, which also adds
overhead.
c) Worst Fit
Definition:
Allocates the largest free block that is large enough to satisfy the request for memory.
Advantages:
Reduces Fragmentation in Large Blocks: By allocating the largest block, it can leave medium-sized blocks
available for future processes, potentially reducing fragmentation when large processes need to be allocated.
Disadvantages:
May Waste Larger Blocks: The algorithm can leave smaller blocks that may not be useful for larger future
requests, potentially leading to more fragmentation and inefficient memory use.
Slower Execution: Similar to best fit, this algorithm also requires scanning the entire list or keeping it sorted,
which can be time-consuming.Summary
First Fit: Fast but can lead to fragmentation.
Best Fit: Minimizes wasted space but slower due to searching or sorting.
Worst Fit: Tries to reduce fragmentation in large blocks but can waste larger blocks and also requires
searching or sorting.
Each algorithm has its use cases, and the choice depends on the specific requirements and constraints of the system,
such as the need for speed versus the need for efficient memory utilization.
Compaction is indeed a strategy used in memory management to address the issue of external fragmentation, where
free memory exists in non-contiguous blocks but is unusable due to its dispersion. Here's a detailed explanation of
compaction and how it works:
What is Compaction?
Compaction involves rearranging the contents of memory to place all free memory together in one contiguous block.
This consolidation of free memory blocks allows larger processes to be accommodated, thereby reducing or
eliminating external fragmentation.
1. Dynamic Relocation:
o Compaction requires dynamic relocation, which means that processes may need to be moved within
the memory during runtime.
o The operating system (OS) identifies processes that can be moved to create contiguous free space.
This process requires modifying the memory addresses of these processes dynamically.
2. Swapping:
o One method to achieve compaction is through swapping. Swapping involves moving processes or
parts of processes between main memory (RAM) and secondary storage (usually disk).
o When the OS decides to compact memory, it selects processes that are currently occupying
fragmented memory spaces. These processes are temporarily swapped out to disk.
o Once the processes are swapped out, the OS can then move the remaining processes in memory to
consolidate free memory blocks.
o After compaction, the swapped-out processes are brought back into memory, potentially in a more
contiguous fashion.
3. Optimal Compaction Strategy:
o Selecting an optimal strategy for compaction involves balancing the overhead (time and resources)
required for compaction against the benefits of reducing fragmentation.
o OS designs vary in their compaction strategies. Some systems may perform compaction proactively
during periods of low activity, while others may trigger compaction in response to high fragmentation
levels.
Overhead: Compaction involves significant overhead due to the need for dynamic relocation and possibly
swapping processes in and out of memory.
Complexity: Implementing a compaction algorithm that efficiently manages memory without causing
significant disruption to running processes is challenging.
Algorithm Design: Designing an effective compaction algorithm involves considering factors such as process
priority, memory usage patterns, and the system's workload.
Conclusion:
Overview of Paging
1. Non-Contiguous Allocation:
o Unlike contiguous memory allocation where a process occupies a single contiguous block of memory,
paging divides both physical and logical memory into fixed-size blocks called pages.
o These pages are typically small and of equal size, which simplifies memory management.
2. Address Translation:
o The logical address space of a process is divided into pages of the same size. Similarly, the physical
memory (RAM) is divided into frames, also of the same size as pages.
o The size of a page is determined by the hardware and operating system, typically ranging from 512
bytes to 8,192 bytes.
o Every process generates logical addresses that consist of two parts: a page number (p) and a page
offset (d). The page number acts as an index into a page table, which contains the base address of
each page in physical memory.
3. Page Table:
o Each process has its own page table maintained by the operating system.
o The page table translates logical addresses generated by the CPU into physical addresses by mapping
each logical page number to the corresponding physical frame number.
o The translation process involves adding the page offset to the base address of the frame to determine
the actual physical address in RAM where data is stored.
4. Benefits of Paging:
o Non-Contiguous Memory Allocation: Paging allows processes to be allocated non-contiguously in
physical memory, overcoming the fragmentation issues associated with contiguous allocation.
o Simplified Memory Management: By using fixed-size pages and frames, paging simplifies memory
allocation and management compared to earlier methods that required managing variable-sized
partitions.
o Hardware Support: Paging is typically supported by hardware through a Memory Management
Unit (MMU), which handles the address translation process efficiently.
5. Implementation:
o Paging is managed by the operating system and supported by hardware mechanisms like the MMU.
o When a process is loaded into memory, its pages are allocated to any available physical frames.
o The page table is consulted for every memory access to translate logical addresses to physical
addresses, ensuring correct data retrieval and storage.
1. Memory Organization:
o Physical Memory (RAM): Divided into fixed-sized blocks called frames. Frame sizes typically
range from 512 bytes to 8,192 bytes and are usually a power of 2.
o Logical Memory (Process Address Space): Divided into fixed-sized blocks called pages, matching
the size of frames.
2. Loading Process:
o When a process is initiated or needs to be executed, its pages are loaded into any available frames in
the physical memory (RAM) from the backing store (typically a disk or SSD).
o The backing store is also divided into fixed-sized blocks (equal to the frame size) to correspond with
the frames in physical memory.
3. Address Translation:
o Each address generated by the CPU consists of two parts:
Page Number (p): Identifies which page of the process's address space is being referenced.
Page Offset (d): Specifies the exact location within the page.
o The page number (p) serves as an index into the page table specific to the currently executing
process.
o The page table stores the base address of each page currently residing in physical memory.
o The physical address is formed by combining the base address from the page table with the page
offset (d), providing the exact location in physical memory where the data resides.
Key Benefits and Features
Basics of Paging
Paging is a memory-management scheme where physical memory is divided into fixed-sized blocks called frames,
and logical memory (processes' memory) is divided into blocks of the same size called pages.
Example 1
Given:
Page size (P) = 4 bytes (for simplicity, as per the example provided)
Physical memory has 8 frames (F), numbered from 0 to 7.
a. Show the physical memory implementation using logical memory & page table:
This table shows how logical pages are mapped to physical frames.
Example 2
Given:
Examples Explained
Example 1:
Given:
oMemory reference time without TLB: 200 nanoseconds.
oTime to access data/instruction in memory: 200 nanoseconds.
Calculation:
o With a paged memory system, each memory reference requires accessing the page table (200 ns) and
then accessing the data/instruction in memory (200 ns).
o Total time per memory reference: 200 ns (page table access) + 200 ns (memory access) = 400 ns.
Example 2:
Given:
oHit ratio of TLB: 80% (0.80)
oTime to search TLB (if miss): 20 nanoseconds.
oTime to access data/instruction in memory (if TLB hit): 100 nanoseconds.
Calculation:
o Effective memory access time calculation:
TLB hit time: 0.80 × 120 ns (20 ns TLB search + 100 ns memory access)
TLB miss time: 0.20 × 220 ns (20 ns TLB search + 200 ns page table access + 200 ns
memory access)
o Effective access time = 0.80 × 120 ns + 0.20 × 220 ns = 140 nanoseconds.
Conclusion
Hardware support in paging systems, particularly through TLBs, plays a crucial role in optimizing memory access
times by reducing the overhead associated with page table lookups. TLBs significantly enhance system performance
by minimizing the number of memory accesses required for each data or instruction access, thereby improving overall
system efficiency and responsiveness.
In paging systems, protection mechanisms are crucial for ensuring that processes can only access memory that belongs
to them and that unauthorized access attempts are prevented. Here's how protection is typically implemented:
Summary
Protection mechanisms in paging systems, implemented through protection bits in frames and valid-invalid bits in
page tables, are critical for ensuring secure and efficient memory management. These mechanisms play a key role in
maintaining process isolation, data integrity, and overall system reliability in modern operating systems.
Segmentation is another memory management scheme that differs from paging by dividing a program into segments
of variable sizes rather than fixed-size pages. Here's an overview of segmentation:
Segmentation Basics
Advantages of Segmentation
Challenges of Segmentation
Fragmentation:
o External fragmentation can occur when segments are of varying sizes, leading to inefficient memory
utilization.
Complexity:
o Managing segment tables and ensuring protection and access control can be more complex compared
to paging.
Segmentation is particularly useful for organizing and managing large, complex programs where different parts have
distinct sizes and lifetimes. However, it requires careful management to handle fragmentation and ensure efficient
memory usage.
Hardware support for segmentation involves several components and mechanisms to manage the mapping of logical
segments to physical memory addresses efficiently. Here’s an explanation along with a diagram illustrating the
hardware components involved in segmentation:
1. Segment Table:
o Purpose: Maps logical segments to physical memory locations.
oComponents:
Segment Table Entries: Each entry contains:
Base Address: Starting physical address where the segment resides.
Limit: Specifies the length of the segment.
Segment Table Base Register (STBR): Points to the base address of the segment table in
memory.
Segment Table Length Register (STLR): Indicates the number of segments used by a
program.
2. Segmentation Unit:
o Function: Coordinates the mapping of logical addresses to physical addresses using the segment
table.
3. Segmentation Hardware Components:
o CPU:
Generates logical addresses (segment number and offset).
Utilizes segment registers for efficient address translation.
o Memory Management Unit (MMU):
Translates logical addresses to physical addresses using the segment table.
Interfaces with the CPU to perform address translation on the fly.
Key Points:
Efficiency: Hardware support ensures efficient address translation with minimal overhead.
Protection: Segment table entries include protection bits to enforce memory protection.
Flexibility: Segmentation allows programs to have varying sizes of segments, accommodating different
program structures.
Hardware support for segmentation simplifies memory management by providing efficient mapping of logical
segments to physical memory addresses, thereby supporting complex program structures effectively.
1. Protection
In segmentation, memory protection is managed through various mechanisms associated with each segment. Here's
how it works:
Validation Bit: Each entry in the segment table typically includes a validation bit.
o When the validation bit is set to 0, it indicates that the segment is illegal or not currently in use by the
process.
o When set to 1, it signifies that the segment is valid and accessible.
Read/Write/Execute Privileges: Besides the validation bit, segments can have associated protection bits that
define permissions:
o Read: Determines if the segment can be read from.
o Write: Specifies if the segment can be written to.
o Execute: Controls whether instructions within the segment can be executed.
These protection bits help enforce security and ensure that processes only access memory segments according to their
intended permissions.
Segmentation allows for efficient code and data sharing among processes due to its modular nature:
Code Sharing: Segments can be shared among different processes or instances of the same process.
Data Sharing: Similarly, segments containing shared data (like libraries or common data structures) can be
accessed by multiple processes.
Segment Table: Each process has its own segment table, which can map the same segment number to
different physical addresses if needed.
Protection Bits: By appropriately setting read/write/execute permissions, segments can be shared securely
without compromising data integrity or security.
3. Dynamic Storage-Allocation
Variable-Length Segments: Segmentation supports dynamic memory allocation because segments can vary
in size. This flexibility allows processes to request memory segments of varying lengths as needed.
Memory Allocation: The operating system manages memory allocation by locating free segments of
sufficient size to accommodate new or expanding segments. This dynamic allocation helps optimize memory
usage.
Modularity: Segmentation divides the address space into logical units (segments), making it easier to manage
and share code and data.
Flexibility: Processes can dynamically allocate and deallocate segments, adjusting their memory usage as
required.
Efficiency: Sharing reduces memory duplication, improving overall system efficiency and reducing memory
footprint.
In summary, segmentation provides robust mechanisms for memory protection and efficient sharing of code and data
among processes, leveraging its modular structure and variable-length segments to enhance system performance and
flexibility.
1. Fragmentation in Segmentation
Fragmentation in segmentation occurs due to the variable sizes of segments and how they are allocated and
deallocated over time. There are two main types of fragmentation to consider:
Similarity to Paging: When a long term scheduler needs to allocate memory for all segments of a user
program, it faces a task similar to paging but with segments of variable lengths instead of fixed-sized pages.
Variable-Length Segments: Unlike paging where all pages are of the same size, segments in segmentation
can vary in size, which introduces additional complexity in memory allocation.
c) External Fragmentation
Definition: External fragmentation occurs when there are many small blocks of free memory scattered
throughout the memory space that are individually too small to satisfy a request for a segment.
Causes: Segmentation can lead to external fragmentation because segments of varying sizes may leave
irregularly sized gaps between allocated segments.
Impact: External fragmentation reduces the efficiency of memory usage because even though the total free
memory may be sufficient to accommodate a new segment, the memory cannot be effectively utilized due to
the fragmentation.
Consider a modern multitasking operating system that uses segmentation for memory management. Here’s how
fragmentation might manifest and be managed:
1. Segmentation Overview:
o In this OS, each process is divided into segments such as code, data, and stack, each with different
sizes depending on the needs of the program.
2. Memory Allocation:
o Long Term Scheduler: Similar to paging, the long term scheduler needs to allocate memory for all
segments of a process. However, unlike paging where all pages are uniform in size, segments vary in
size, which complicates allocation.
3. Types of Fragmentation:
o Internal Fragmentation: This occurs when allocated memory is larger than what is actually needed
for a segment. For example, if a data segment requires 500 bytes but is allocated 1 KB, 500 bytes
remain unused within that segment, causing internal fragmentation.
o External Fragmentation: This type of fragmentation occurs when there are many small blocks of
free memory scattered throughout the memory space. These blocks are individually too small to
accommodate a new segment request, even though their combined size might be sufficient. Segments
of varying sizes contribute to irregularly sized gaps between allocated segments, leading to external
fragmentation.
4. Memory Allocation Strategies:
o Best Fit: This algorithm searches for the smallest free block of memory that can accommodate the
segment. It aims to minimize leftover fragmentation by fitting segments into the smallest suitable
gaps.
o First Fit: Allocates the first available free block of memory that is large enough to fit the segment. It
is faster than best fit but can lead to more fragmentation since it might leave larger leftover gaps after
allocation.
5. Example Scenario:
o Scenario: Consider a system where multiple processes are running concurrently, each with its own set
of segments. Process A needs to allocate a new segment for its data storage.
o Implementation Steps:
The OS uses the segment table to manage segments, where each segment descriptor includes
the base address, segment length, and protection information.
When Process A requests memory for a new segment (e.g., data segment), the memory
allocator (using best fit or first fit) searches through the free memory blocks.
If the allocator finds a suitable block (either by best fit or first fit), it allocates the segment and
updates the segment table.
Over time, as segments are deallocated (e.g., when a process finishes execution),
fragmentation can occur where small gaps of free memory remain scattered.
o Fragmentation Mitigation: The OS periodically runs compaction routines to consolidate free
memory blocks, reducing external fragmentation. Compaction involves moving allocated segments
closer together to create larger contiguous blocks of free memory.
6. Impact on System Performance:
o Fragmentation impacts memory efficiency and system performance. External fragmentation, in
particular, can reduce the available memory for new processes or segments, even when sufficient total
memory exists.
o Efficient memory allocation algorithms and periodic compaction are essential to maintain optimal
system performance and memory utilization.
Summary
Segmentation with paging is a memory management scheme that combines the benefits of both segmentation and
paging techniques. Here's how it is typically implemented in a computer system, along with a real-world example:
1. Logical Address Format: In segmentation with paging, the logical address space of a process is divided into
segments. Each segment is further divided into fixed-sized pages.
o Segments: Segments are logical units such as code segments, data segments, stack segments, etc.,
each with its own base address and length specified in the segment descriptor.
o Pages: Within each segment, paging is used to divide the segment into fixed-sized pages (e.g., 4 KB
pages).
2. Segment Descriptor Tables:
o Local Descriptor Table (LDT): This table holds descriptors for segments that are private to a
specific process.
o Global Descriptor Table (GDT): This table holds descriptors for segments that are shared among
multiple processes.
3. Logical Address Translation:
o A logical address in segmentation with paging typically consists of two parts: a segment selector and
an offset within the segment.
o The segment selector points to an entry in either the LDT or GDT, depending on whether the segment
is private to the process or shared among processes.
o The offset specifies the location within the segment.
4. Segmentation and Paging Unit:
o Each segment descriptor includes information about the segment's base address, limit (size), and
access rights (protection bits).
o Paging within each segment is managed using a page table. The page table translates logical page
numbers to physical page frames.
5. Address Translation:
o When a process generates a logical address, the segment selector and offset are used to access the
appropriate segment descriptor.
o The segment descriptor provides the base address of the segment.
o The offset within the segment, combined with the base address, forms a linear address.
o The linear address is then translated into a physical address using the page table entries.
Real-World Example
Let's consider an example scenario to illustrate segmentation with paging:
Scenario:
o Assume a process has several segments: code segment, data segment, and stack segment.
o Each segment is divided into 4 KB pages for efficient memory management.
Implementation Steps:
1. Segmentation: Define segments for the process in the segment descriptor tables (LDT or GDT). Each
segment descriptor specifies the base address, limit, and access rights.
2. Paging: Within each segment, use a page table to manage the mapping between logical pages and
physical memory frames. Each page table entry contains the physical address corresponding to a
logical page number within the segment.
3. Address Translation:
When the CPU generates a logical address (segment selector, offset), it first uses the segment
selector to fetch the segment descriptor from either LDT or GDT.
The segment descriptor provides the base address of the segment.
The offset within the segment is added to the base address to generate a linear address.
The page table associated with the segment translates the linear address into a physical
address.
Protection and Sharing: Segmentation allows for protection and sharing of segments among processes.
Paging within segments ensures efficient memory utilization and management.
Example: In modern operating systems, segmentation with paging is used to manage memory for
applications. Each application might have its own segments (code, data, stack), and within each segment,
paging is used to allocate memory dynamically as needed.
This combined approach of segmentation with paging provides flexibility, protection, and efficient memory
management, making it suitable for modern multi-tasking operating systems and virtual memory systems.
Virtual memory is a pivotal technology in modern computing that enables efficient management and utilization of
memory resources. Here’s a detailed explanation of virtual memory and its benefits:
Virtual memory is a memory management technique that provides an illusion to users and applications that they have
a larger and contiguous address space than the physical memory (RAM) available on the computer. It allows programs
to execute even if they are not entirely loaded into physical memory.
Address Translation: Every memory access generated by a program is translated from virtual addresses to
physical addresses by the hardware with the help of page tables. This translation allows programs to access
data and instructions stored in physical memory or secondary storage transparently.
Page Faults: When a program accesses a page that is not currently in physical memory (page fault), the
operating system intervenes by loading the required page from secondary storage into memory. This process
ensures that programs can execute seamlessly despite their virtual memory addresses not entirely residing in
physical memory.
Real-World Example:
Scenario: Application A and Application B are running concurrently, each requiring more memory than
physically available.
Virtual Memory Utilization: Virtual memory allows both applications to execute by dynamically loading
their required pages into physical memory as needed. If one application references a page not currently in
memory, the operating system handles the page fault by fetching the page from disk, ensuring uninterrupted
execution of the application.
In essence, virtual memory facilitates efficient utilization of physical memory resources, enhances system
performance by reducing I/O operations, and supports the concurrent execution of multiple programs with minimal
impact on response and turnaround times. This capability makes virtual memory a cornerstone of modern operating
systems, enabling them to manage memory effectively across diverse computing environments.
Demand paging is a critical aspect of virtual memory management in operating systems. Let's delve deeper into how
demand paging works, its protection mechanisms, and hardware support for its implementation:
Demand paging is a memory management scheme where pages are loaded into memory only when they are required
by a process. The key idea is to bring in pages on-demand rather than loading the entire process into memory at once.
This approach optimizes memory usage by avoiding unnecessary page reads and reducing the amount of physical
memory required.
Page Usage Prediction: Before swapping a process into memory, the pager (a component of the operating
system) tries to predict which pages will be needed soon based on past behaviors and program structures.
Only these predicted pages are initially brought into memory.
Page Faults: If a process attempts to access a page that is not currently in memory, a page fault occurs. The
operating system then intervenes to bring the required page from secondary storage (like a hard disk) into an
available frame in physical memory.
Protection in demand paging is implemented using several mechanisms to ensure secure and reliable memory access:
1. Check Reference: Verify whether the memory reference causing the fault is valid or invalid.
2. Resolve Page Fault:
If invalid: Abort the process.
If valid but not in memory:
Allocate an empty frame from the free frame list.
Read the required page from secondary storage into the allocated frame.
Update the page table entry to mark the page as valid (v).
Restart the instruction that caused the page fault, allowing the process to continue
execution with the requested page now in memory.
Hardware support is crucial for efficient demand paging implementation, particularly to minimize the overhead of
page table lookups and page faults:
Page Table Base Register (PTBR): Points to the base address of the page table in memory.
Page Table Length Register (PRLR): Indicates the size of the page table.
Translation Look-aside Buffer (TLB): A hardware cache that stores recently accessed page table entries to
speed up address translation. TLB helps reduce the average time taken for memory access by eliminating the
need to access the page table in memory for every memory access.
Consider a scenario where a process is running and accesses a page that is not currently in memory:
1. Page Fault Occurs: The CPU traps to the operating system due to an invalid page table entry.
2. OS Intervention:
o Checks the page table to determine the validity of the page reference.
o Finds that the page is not in memory (invalid bit set).
3. Handling the Page Fault:
o Allocates a free frame from the free frame pool.
o Reads the required page from disk into the allocated frame.
o Updates the page table entry to mark the page as valid (v).
o Resumes the process, allowing it to continue execution with the requested page now available in
memory.
Demand paging ensures efficient memory utilization by loading only necessary pages into memory, thereby
improving overall system performance and responsiveness without requiring large amounts of physical memory.
Real-World Example: Imagine a multitasking operating system running several user programs concurrently:
Scenario: Process A and Process B are both running, each having segments (pages) of code and data.
Demand Paging in Action:
o Process A references a page of its code that is currently not in memory (page fault).
o The OS responds by allocating a free frame, reading the required page from the disk into the frame,
and updating the page table.
o Once loaded, Process A resumes its execution without further interruptions unless it references
another page not currently in memory.
Advantages:
Optimal Memory Usage: Programs can be larger than physical memory size since not all pages need to be
loaded simultaneously.
Increased CPU Utilization: Multiple programs can reside in memory concurrently, improving CPU
utilization and throughput.
Reduced I/O Operations: Only necessary pages are loaded into memory, reducing the number of I/O
operations required to swap processes in and out of memory
The steps for handling a page fault in a demand paging system are crucial for maintaining efficient memory
management and ensuring that processes can execute seamlessly even when not all of their pages are in physical
memory. Here's a detailed breakdown of each step:
1. Check Validity:
o When a memory reference occurs (e.g., load or store instruction), the hardware checks the page table
entry associated with the virtual address to determine its validity.
o If the page table entry indicates that the page is not currently in memory (invalid bit is set), a page
fault is triggered.
2. Process Termination or Paging In:
o If the page reference is invalid (e.g., due to a segmentation fault or unauthorized access), the operating
system may terminate the process to prevent further errors.
o If the page reference is valid but the page is not yet in memory, the operating system initiates the
paging-in process.
3. Allocate a Free Frame:
o The operating system searches for a free frame in physical memory to allocate to the requested page.
o This involves consulting a free frame list or other data structure that tracks available physical memory
frames.
4. Schedule Disk Operation:
o Once a free frame is identified, the operating system schedules a disk read operation to bring the
desired page from secondary storage (e.g., disk) into the newly allocated frame in physical memory.
o The disk read operation involves transferring the page's content from disk to memory, which is
relatively slow compared to memory access speeds.
5. Update Page Tables:
o After the page is successfully loaded into memory, the operating system updates the page table entry
for the process:
Marks the page as valid (v), indicating that it is now resident in physical memory and can be
accessed by the process.
Updates any other necessary data structures or internal tables associated with the process to
reflect the page's presence in memory.
6. Resume Process Execution:
o With the requested page now available in memory, the operating system prepares to resume the
execution of the process that triggered the page fault.
o The instruction that caused the page fault is restarted, allowing the process to continue as though the
page had always been in memory.
Summary of Operations
Fast Operations:
o Checking the address validity and finding a free frame are relatively fast operations.
o These steps ensure that the page fault is handled promptly to minimize disruption to the process.
Slow Operations:
o Swapping out a victim page from memory to make space for the new page (if there are no free frames
available) and swapping in the desired page from disk are slower operations.
o These operations involve disk I/O, which is orders of magnitude slower than accessing memory.
Demand paging ensures efficient use of physical memory by only bringing pages into memory when they are needed
(on demand). This strategy helps in maximizing overall system performance and allows for larger programs to be
executed than the physical memory size would otherwise permit.
By following these steps, demand paging enables effective virtual memory management, improves system
responsiveness, and optimizes memory utilization in modern operating systems.
In demand-paging systems, the performance is measured by the effective access time, which takes into account both
the time taken for memory accesses and the time taken to handle page faults. Here's how the effective access time is
calculated and why minimizing the page-fault rate is crucial:
The effective access time (EAT) is given by: EAT=(1−p)×ma+p×page fault time
Where:
Explanation:
Impact on Performance:
o If the page-fault rate ppp is high, the effective access time EAT\text{EAT}EAT will be closer to
page fault time\text{page fault time}page fault time.
o High ppp means frequent interruptions due to page faults, leading to longer execution times for
processes.
System Responsiveness:
o Minimizing ppp ensures that most memory accesses can be served quickly from physical memory,
optimizing system responsiveness.
Throughput and Utilization:
o Lower ppp allows more processes to run concurrently since they can share physical memory more
effectively without frequent delays due to page faults.
By carefully managing page faults and optimizing the handling of memory requests, demand-paging systems can
achieve efficient memory utilization and maintain high system performance.
a dirty bit or modify bit is a special marker associated with each page in a computer's memory management system. Its
purpose is to track whether a page has been modified (changed or written into) since it was last loaded into memory
Tracking Modifications: The dirty bit is set to "1" by the hardware whenever the page is changed. This helps
the system know if the page content is different from what's stored in the backing store (like disk).
Handling Page Replacement: When the system needs to replace a page in memory to make space for
another, it checks the dirty bit of the page. If the dirty bit is set ("1"), it means the page has been modified, and
the system needs to write it back to the backing store to keep the data updated. If the dirty bit is not set ("0"),
the page hasn't been modified since it was loaded, so it can be replaced without needing to write it back.
Page replacement algorithms are used in virtual memory management to decide which page to evict from physical
memory (RAM) when a new page needs to be brought in and all memory frames are already occupied. The goal is to
minimize the number of page faults (instances where a requested page is not in memory).
Basic Operation:
Importance:
Minimizing Page Faults: The primary objective of page replacement algorithms is to reduce the number of
page faults. Efficient algorithms can significantly improve the overall performance of a system by ensuring
that the most frequently accessed pages remain in memory.
Evaluation: Page replacement algorithms are evaluated based on their ability to handle different types of
memory reference patterns, such as sequential, random, or localized accesses. Algorithms are tested using
reference strings, which simulate real-world memory access patterns.
1. FIFO (First-In-First-Out): Evicts the oldest page that was brought into memory first.
2. LRU (Least Recently Used): Evicts the page that has not been referenced for the longest period of time.
3. LFU (Least Frequently Used): Evicts the page that has been referenced the least number of times.
4. Optimal: Selects the page that will not be used for the longest period in the future (ideal but not practical due
to needing future knowledge).
5. Clock (Second-Chance): Similar to FIFO but uses a clock hand to give pages a second chance before
replacement.
Evaluation:
Performance Metrics: Algorithms are evaluated based on metrics such as the number of page faults,
throughput (number of processes completed per unit time), and CPU utilization.
Simulation: To determine which algorithm performs best for a specific workload or system, simulations are
run using trace files or generated reference strings.
Page replacement algorithms are fundamental in ensuring efficient utilization of physical memory resources in modern
operating systems, balancing the trade-offs between complexity and performance.
The FIFO (First-In-First-Out) page replacement algorithm is one of the simplest methods used to manage memory in
virtual memory systems. Here’s how it works and an example to illustrate its operation:
Basic Concept
Selection Criteria: FIFO selects the page that was brought into memory first as the victim for replacement.
This means it evicts the oldest page in memory.
Implementation: Pages are stored in a queue or circular buffer in the order they are brought into memory.
When a page fault occurs and a new page needs to be brought in but all frames are occupied, FIFO removes
the page that has been in memory the longest.
Assume there are 3 frames available. Let's walk through the reference string and track the memory content and page
faults using the FIFO algorithm:
Belady’s Anomaly
Belady’s Anomaly refers to a phenomenon where increasing the number of frames may lead to an increase in page
faults rather than a decrease.
Let’s apply the FIFO method with different numbers of frames and observe if Belady’s Anomaly occurs:
In this case, FIFO does not exhibit Belady's Anomaly as increasing the number of frames results in fewer or the same
number of page faults.
Basic Concept:
LRU replaces the page that has not been used for the longest period of time in main memory.
It requires maintaining a record of the time of last use for each page, typically using a timestamp or a counter.
Advantages:
Reduces Frequency of Page Faults: LRU tends to perform better than FIFO because it replaces pages that
are least recently used, which often matches the pattern of future memory accesses better.
Close to Optimal Performance: LRU approximates OPT (Optimal) in performance in many cases, especially
when programs exhibit temporal locality (reusing recently accessed pages).
Disadvantages:
Implementation Complexity: LRU requires additional hardware support (e.g., timestamps, counters) to track
and maintain the last use times of pages.
High Overhead: Updating and maintaining the last use times can be computationally expensive, especially in
systems with a large number of pages or when implemented in software.
Example: Consider the reference string: 5, 7, 6, 0, 7, 1, 7, 2, 0, 1, 7, 1, 0. With 3 frames, LRU would result in 9 page
faults. Memory content after each reference:
[5], [ ], [ ]
[5], [7], [ ]
[5], [7], [6]
[0], [7], [6]
[0], [7], [6]
[0], [7], [1]
[0], [7], [1]
[0], [7], [2]
[0], [7], [2]
[0], [7], [2]
[0], [7], [2]
[0], [7], [2]
[0], [7], [2]
Basic Concept:
OPT selects the page that will not be used for the longest period of time in the future, based on the optimal
(but impractical to implement) knowledge of future memory accesses.
OPT serves as a benchmark for comparing other page replacement algorithms.
Advantages:
Optimal Page Replacement: OPT has the lowest possible page fault rate among all page replacement
algorithms because it always selects the best page to replace.
Disadvantages:
Impractical to Implement: OPT requires future knowledge of memory access patterns, which is not feasible
in real systems.
High Overhead: Even though OPT is used as a benchmark, its implementation in practical systems is
impossible due to the requirement of knowing future references.
Example: For the reference string: 5, 7, 6, 0, 7, 1, 7, 2, 0, 1, 7, 1, 0, OPT would result in 7 page faults with 3 frames.
Summary
FIFO: Simple and easy to implement but may suffer from Belady's Anomaly where increasing frames can
increase page faults.
LRU: More sophisticated than FIFO, reduces page faults by replacing the least recently used pages, but
requires additional overhead to maintain last use information.
OPT: Theoretically optimal but impractical due to the need for future knowledge. Used as a benchmark for
comparing other algorithms.
1. Simple Implementation: FIFO is straightforward to implement because it only requires a queue or circular
buffer to maintain the order of page frames.
2. Low Overhead: Compared to more complex algorithms like LRU (Least Recently Used) or OPT (Optimal),
FIFO has lower computational overhead because it does not require tracking page usage history or predicting
future accesses.
3. No Additional Hardware Requirements: FIFO does not need additional hardware support like timestamps
or reference bits, making it suitable for systems with limited resources.
1. Belady's Anomaly: FIFO can suffer from Belady's Anomaly, where increasing the number of page frames
may unexpectedly increase the number of page faults. This contradicts the common expectation that more
memory should reduce page faults.
2. Poor Performance with Certain Access Patterns: FIFO does not consider the frequency or recency of page
accesses. As a result, it may evict pages that are actively being used, leading to a higher page fault rate
compared to more sophisticated algorithms like LRU or OPT.
3. Suboptimal Replacement Strategy: Since FIFO always replaces the oldest page in memory, it may not
select the best page to replace in terms of minimizing future page faults. This can result in unnecessary page
swaps and reduced overall system performance.
Thrashing in operating systems occurs when the system is excessively busy swapping pages in and out of physical
memory, leading to a severe degradation in performance. Here’s a simple explanation with a real-world example:
Understanding Thrashing
Definition: Thrashing happens when a computer's virtual memory system is overcommitted, and the system spends a
significant amount of time swapping pages between main memory (RAM) and secondary storage (usually disk), rather
than executing instructions.
Example:
Imagine you have a computer running several memory-intensive applications simultaneously, such as video editing
software, a web browser with multiple tabs, and a large spreadsheet. Each application requires a certain amount of
RAM to operate efficiently. If the combined memory requirements of all running applications exceed the physical
RAM available (due to insufficient RAM or excessive multitasking), the operating system must constantly swap pages
between RAM and the hard disk to accommodate the demands of each application.
Effects of Thrashing:
1. Reduced Performance: The system becomes sluggish because a significant portion of CPU time is spent on
managing page swaps instead of executing application instructions.
2. Increased Response Time: Applications respond slowly to user input because they are waiting for data to be
swapped into memory.
3. Poor Utilization: CPU utilization drops because the processor spends more time waiting for data from the
disk rather than executing tasks.
Preventing Thrashing
1. Optimal Frame Allocation: Allocate enough physical memory (frames) to each process based on its memory
requirements and workload. This prevents frequent page faults and unnecessary page swaps.
2. Working Set Model: This model identifies the set of pages that a process actively uses during its execution.
By keeping these pages in memory, the operating system can reduce page faults and improve performance.
3. Page Replacement Algorithms: Use efficient page replacement algorithms like LRU (Least Recently Used)
or OPT (Optimal) to minimize page faults and reduce thrashing when page faults do occur.
Real-World Implementation
In a real-world scenario, modern operating systems continuously monitor memory usage and dynamically adjust
resource allocation to prevent thrashing. For example, if a user opens multiple applications on a desktop computer
with limited RAM, the OS may prioritize memory allocation based on the active application and user interaction. If
memory demands exceed physical limits, the system may reduce background processes or virtual memory usage to
maintain performance.
Overall, understanding and managing thrashing is crucial for maintaining system responsiveness and efficiency,
especially in environments with high multitasking demands or limited hardware resources.
The working set model is a concept in operating systems used to prevent thrashing by dynamically adjusting the
number of frames allocated to a process based on its working set. Here’s a breakdown of the model and its
application:
Definition: The working set of a process is defined as the set of pages that the process is actively using at any given
time. The idea is based on the principle of locality in program execution, where programs tend to reference a relatively
small portion of their address space over short periods.
Parameters:
Working Set Window (∆): This parameter determines the size of the window or interval over which page
references are observed to calculate the working set.
Working Set Size (WSS): The number of distinct pages in the working set.
Example Calculation: Assume a working set window size (∆) of 10 and consider the following reference string:
2,1,5,7,7,5,1,3,42, 1, 5, 7, 7, 5, 1, 3, 42,1,5,7,7,5,1,3,4
In this example, the working set changes over time as the program's locality of reference shifts.
1. Thrashing Prevention:
o By monitoring the working set of each process, the operating system can dynamically adjust the
number of frames allocated to prevent thrashing.
o Thrashing Condition: If the total demand (sum of all working set sizes) exceeds the available frames
nnn, the system is thrashing.
o System Stability: If the total demand DDD is less than nnn, the system can increase the degree of
multiprogramming (number of concurrent processes) without performance degradation.
2. Implementation Challenges:
o Choosing an appropriate value for Δ\DeltaΔ is critical. A smaller Δ\DeltaΔ may not capture the entire
working set, leading to under-allocation of frames. A larger Δ\DeltaΔ may overlap working sets of
different processes, reducing efficiency.
o Efficiently tracking and updating the working set requires additional computational overhead,
especially in systems with large numbers of processes and frequent context switches.
Real-World Application
In modern operating systems, the working set model is used alongside other memory management techniques to
optimize resource utilization. For example, Windows uses a variant of the working set model to manage memory
pages efficiently, ensuring that active processes receive adequate resources to maintain performance while inactive or
background processes are allocated fewer resources.
Understanding and implementing the working set model is crucial for balancing system responsiveness and resource
utilization, particularly in environments with variable workloads and multitasking demands.
Cache memory is a small, high-speed storage component placed between the processor (CPU) and main memory
(RAM) to improve the overall speed of memory access. It operates on the principle of locality, which predicts that if a
processor accesses a particular memory location, it is likely to access nearby locations soon afterward.
Key Points:
Advantages:
o Dramatically reduces the average time to access data.
o Mitigates the speed gap between the fast processor and slower main memory.
o Enhances overall system performance by minimizing latency.
Challenges:
o Increased complexity in cache management and coherence.
o Expensive to manufacture due to its high-speed nature and proximity to the CPU.
o Requires efficient algorithms for cache replacement (e.g., LRU, FIFO) to maintain optimal
performance.
Real-World Application:
Modern CPUs utilize multiple levels of cache (L1, L2, L3) with varying sizes and speeds to balance between speed,
size, and cost. Efficient cache management algorithms and hardware techniques are crucial for optimizing the use of
cache memory and improving system performance in various computing environments.
The Locality of Reference principle is a fundamental concept in computer science and memory management,
describing the tendency of programs to access the same set of memory locations repetitively over short periods of
time. This principle underpins the efficient design of caching systems, virtual memory, and other memory
management strategies. Here’s a breakdown of its key aspects:
Key Points:
1. Definition:
o Locality of Reference refers to the observation that memory accesses by a program tend to cluster in
specific regions of the memory at any given time.
o There are two main types of locality:
Temporal Locality: The principle that if a memory location is accessed, it is likely to be
accessed again in the near future.
Spatial Locality: The principle that if a memory location is accessed, nearby memory
locations are also likely to be accessed soon afterward.
2. Examples:
o Temporal Locality: When a program executes a loop, it repeatedly accesses the same set of
instructions and data within the loop until the loop terminates.
o Spatial Locality: When a program accesses elements of an array, it often accesses nearby elements
sequentially due to the linear storage of arrays in memory.
3. Causes:
o Iterative Structures: Programs frequently contain loops and subroutines where instructions and data
are accessed repetitively.
o Data Structures: Access patterns in data structures such as arrays, linked lists, and trees exhibit
locality due to the linear or hierarchical arrangement of elements.
4. Impact on Performance:
o Efficient memory management techniques leverage locality of reference to improve performance:
Caching: Caches exploit both temporal and spatial locality to store frequently accessed data
and instructions close to the processor, reducing access latency.
Virtual Memory: Virtual memory systems use demand paging to bring pages into physical
memory based on expected future references, guided by locality of reference.
Optimization: Compilers and runtime environments optimize code and data layout to
enhance locality, reducing cache misses and improving execution speed.
5. Real-World Application:
o Modern processors and operating systems are designed with the locality of reference principle in
mind:
CPUs feature multiple levels of caches (L1, L2, L3) that exploit temporal and spatial locality
to speed up memory access.
Operating systems use paging algorithms and memory allocation strategies that prioritize
locality to minimize page faults and improve overall system performance.
The structure and types of page tables play a crucial role in virtual memory management within operating systems.
Here’s an explanation of each type mentioned:
A. Hierarchical Paging
Hierarchical paging is a method of organizing the page table into multiple levels to efficiently manage large address
spaces. Here’s how it typically works:
Hashed page tables are designed to handle large virtual address spaces efficiently by using a hash function. Here’s
how it works:
1. Hashing Technique:
o Hash Function: Virtual page numbers are hashed to generate a hash value.
o Hash Table: Contains chains of elements (typically linked lists) that hash to the same location.
2. Search Mechanism:
o Lookup: When a virtual address needs translation:
Compute the hash of the virtual page number.
Traverse the chain to find the matching virtual page number.
Retrieve the corresponding physical frame from the hash table.
3. Advantages:
o Efficient for large address spaces.
o Reduces memory overhead compared to full-sized page tables.
1. Entry Structure:
o Each entry in the inverted page table contains:
Virtual Page Number: Identifies the virtual page stored in that physical frame.
Process ID: Indicates which process owns the virtual page.
Offset: Offset within the page.
2. Address Translation:
o Lookup: To translate a virtual address:
Hash or index directly into the inverted page table using the virtual page number.
Retrieve the physical frame and process ID.
3. Efficiency Considerations:
o Memory Usage: Saves memory by only storing entries for pages currently in physical memory.
o Search Time: Efficient lookup but can become slower with large numbers of entries due to potential
collisions in hashing.