OS Nduk 03
OS Nduk 03
LECTURE NOTES
UNIT-I
Introduction: Concept of Operating Systems, Generations of Operating systems, Types of
Operating Systems, OS Services, System Calls, Structure of an OS - Layered, Monolithic, Microkernel Operating
Systems, Concept of Virtual Machine. Case study on UNIX and WINDOWS Operating System.
Processes: Definition, Process Relationship, Different states of a Process, Process State transitions, Process
Control Block (PCB), Context switching
Thread: Definition, Various states, Benefits of threads, Types of threads, Concept of Multithreads.
UNIT-II
Process Scheduling: Foundation and Scheduling objectives, Types of Schedulers, Scheduling criteria: CPU
utilization, Throughput, Turnaround Time, Waiting Time, Response Time; Scheduling algorithms: Pre-emptive
and Non-pre-emptive, FCFS, SJF, RR; Multiprocessor scheduling: Real Time scheduling: RM and EDF.
Inter-process Communication: Critical Section, Race Conditions, Mutual Exclusion, Hardware
Solution, Strict Alternation, Peterson’s Solution, The Producer/Consumer Problem, Semaphores, Event
Counters, Monitors, Message Passing, Classical IPC Problems: Reader’s & Writer Problem, Dinning
Philosopher Problem etc.
UNIT-III
Memory Management: Basic concept, Logical and Physical address map, Memory allocation: Contiguous
Memory allocation – Fixed and variable partition–Internal and External fragmentation and Compaction; Paging:
Principle of operation – Page allocation – Hardware support for paging, protection and sharing, Disadvantages
of paging.
Virtual Memory: Basics of Virtual Memory – Hardware and control structures – Locality of reference, Page
fault, Working Set, Dirty page/Dirty bit – Demand paging, Page Replacement algorithms: Optimal, First in First
Out (FIFO), Second Chance (SC), Not recently used (NRU) and Least Recently used (LRU).
UNIT-IV
File Management: Concept of File, Access methods, File types, File operation, Directory structure, File System
structure, Allocation methods (contiguous, linked, indexed), Free-space management (bit vector, linked list,
grouping), directory implementation (linear list, hash table), efficiency and performance.
I/O Hardware: I/O devices, Device controllers, Direct memory access Principles of I/O Software: Goals of
Interrupt handlers, Device drivers, Device independent I/O software.
UNIT-V
Deadlocks: Definition, Necessary and sufficient conditions for Deadlock, Deadlock Prevention,
Deadlock Avoidance: Banker’s algorithm, Deadlock detection and Recovery.
Disk Management: Disk structure, Disk scheduling - FCFS, SSTF, SCAN, C-SCAN, Disk reliability, Disk
formatting, Boot-block, Bad blocks.
TEXT BOOKS:
1. Operating System Concepts Essentials, 9th Edition by AviSilberschatz, Peter Galvin,Greg Gagne, Wiley
Asia Student Edition.
2. Operating Systems: Internals and Design Principles, 5th Edition, William Stallings, Prentice Hall of
India.
REFERENCE BOOKS:
1. Operating System: A Design-oriented Approach, 1st Edition by Charles Crowley, Irwin Publishing
2. Operating Systems: A Modern Perspective, 2nd Edition by Gary J. Nutt, AddisonWesley
3. Design of the Unix Operating Systems, 8th Edition by Maurice Bach, Prentice-Hallof India
4. Understanding the Linux Kernel, 3rd Edition, Daniel P. Bovet, Marco Cesati, O'Reilly and Associates
OUTCOMES:
At the end of the course the students are able to:
✓ Create processes and threads.
✓ Develop algorithms for process scheduling for a given specification of CPU utilization, Throughput,
Turnaround Time, Waiting Time, Response Time.
✓ For a given specification of memory organization develop the techniques for optimally allocating
memory to processes by increasing memory utilization and for improving the access time.
✓ Design and implement file management system.
✓ For a given I/O devices and OS (specify) develop the I/O management functions in OS as part of a
uniform device abstraction by performing operations for synchronization between CPU and I/O
controllers.
Introduction
Operating System (OS) refers to a system software that manages computer hardware, software resources, and
provides common services for computer programs. It helps programs run and allows users to interact with the
computer easily, without having to worry about the details of how the hardware works.
Popular Operating Systems
Windows: A widely used OS developed by Microsoft, known for its user-friendly interface.
macOS: The operating system developed by Apple for its desktop and laptop devices.
Linux: A free and open-source OS, widely used for server environments and by developers.
Android: A Linux-based OS used primarily for mobile devices.
iOS: The operating system used by Apple for its mobile devices, like iPhones and iPads.
why an OS is important
✓ Resource Management -The OS manages the computer’s resources like the CPU, memory, and storage,
making sure everything runs smoothly without conflicts. It also allows multiple programs to run at the
same time without interfering with each other.
✓ User Interface -The OS provides a way for you to interact with the computer, either through a Graphical
User Interface (GUI) (like in Windows) or a Command Line Interface (CLI) (like in Linux).
✓ File Management - The OS organizes and stores your files, ensuring they’re safe and easy to find when
needed. It helps you create, move, or delete files.
✓ Security and Privacy - The OS keeps your data safe by controlling who can access what on the computer.
It includes features like passwords, encryption, and firewalls to protect against unauthorized access and
malware.
✓ Process Management - The OS decides which tasks get to use the CPU and when, allowing multiple
programs to run at once without issues.
✓ Hardware Abstraction - The OS lets programs interact with hardware without needing to know the
details of how each device works. This helps applications work on different computers with different
hardware.
✓ System Stability and Reliability - The OS makes sure the system works smoothly, helps prevent crashes,
and provides ways to recover from errors, like system restore or backup features.
✓ Networking and Communication - The OS helps computers connect to each other over networks,
supporting things like internet access, file sharing, and email communication.
Objectives of Operating System
The objectives of an OS revolve around ensuring efficient, secure, and reliable operation of the computer or
device while providing a user-friendly interface.
Below are the primary objectives of an OS
1. Efficient Resource Management - The OS must manage the system's resources (CPU, memory, storage, and
I/O devices) in a way that optimizes the overall performance of the system.
2. Process Management - To manage processes (programs in execution) effectively, ensuring fair access to the
CPU and other system resources.
3. Memory Management - The OS must manage the system's memory (RAM) to ensure that each process gets
the necessary amount of memory and that memory is used optimally.
4. File System Management - To provide an organized and efficient way of storing, retrieving, and managing
files and directories on storage devices.
5. Security and Protection - To protect system resources from unauthorized access, theft, and malware.
6. User Interface (UI) Management - The OS should provide a user-friendly interface for interacting with the
computer, whether it’s graphical (GUI) or command-line (CLI).
7. Error Detection and Recovery -To detect and handle errors that occur during system operations, whether in
hardware or software.
8. System Stability and Reliability - The OS must ensure that the system remains stable under heavy workloads,
multiple users, and various types of applications.
Functions of an OS
1. Process Management - Process management involves the handling of processes, which are programs in
execution.
Key tasks include;
✓ Process Scheduling - Decides which process gets CPU time and for how long (via techniques like round-
robin or priority-based scheduling).
✓ Process Creation and Termination - Manages the creation, execution, and termination of processes.
✓ Multitasking: Allows multiple processes to run concurrently by efficiently sharing CPU time.
✓ Process Synchronization and Communication - Ensures that processes coordinate and communicate
without conflicts, especially in multi-threaded environments.
2. Memory Management - OS is responsible for managing the computer's memory (RAM) and ensuring each
process has enough memory to execute.
Key tasks include;
✓ Memory Allocation - Allocates memory to different processes based on their needs.
✓ Memory Protection - Ensures that one process cannot access the memory space allocated to another
process.
✓ Virtual Memory - Uses a portion of the hard drive as additional memory when physical RAM is full,
creating an illusion of a larger memory pool.
✓ Paging and Segmentation - Divides memory into smaller, manageable chunks (pages or segments) for
efficient use and allocation.
3. File System Management - The OS manages data storage, organization, retrieval, and manipulation of files
on storage devices.
Key tasks include;
✓ File Creation and Deletion - Allows users and programs to create, delete, or modify files.
✓ File Organization - Organizes files in directories or folders, providing structure for easy access.
✓ File Access and Protection - Ensures that files are accessed by authorized users and applications only,
and prevents unauthorized access or data corruption.
✓ File System Maintenance - Keeps track of where each file is stored on the disk and ensures efficient
storage and retrieval.
4. Device Management - OS manages input and output (I/O) devices such as printers, keyboards, mice, storage
devices, and displays.
Key tasks include;
✓ Device Drivers - OS uses drivers to communicate with hardware devices, ensuring that they function
correctly and efficiently.
✓ Device Allocation - Manages access to shared devices and allocates them to different processes when
needed.
✓ Input/output Operations - Handles data transfer between the CPU and peripheral devices, ensuring
smooth operation.
✓ Buffering - Uses memory buffers (small memory areas) to temporarily hold data being transferred
between devices (e.g., between RAM and disk drives).
5. Security and Access Control - OS ensures the system’s data and resources are secure and accessible only by
authorized users or processes.
Key tasks include;
✓ User Authentication - Verifies user identities using passwords, biometrics, or other authentication
methods.
✓ Access Control - Manages who can access or modify files, memory, and hardware devices.
✓ Encryption - Protects sensitive data by converting it into unreadable formats that can only be deciphered
with the correct key.
✓ Security Auditing - Keeps track of user activities and system operations to detect any potential breaches
or unauthorized access.
6. User Interface (UI) Management - The OS provides an interface that allows users to interact with the system
easily.
Key tasks include;
✓ Graphical User Interface (GUI) - Provides a visual interface with icons, windows, and menus (e.g.,
Windows, macOS).
✓ Command Line Interface (CLI) - Allows users to interact with the system by typing text-based
commands (e.g., Linux terminal).
✓ User Input Handling - Manages input from devices like keyboards, mice, and touchscreens.
7. Networking - The OS enables communication between computers and devices over a network, allowing for
data sharing and resource sharing.
Key tasks include;
✓ Network Configuration - Configures network settings for communication.
✓ Data Transmission - Manages the sending and receiving of data packets across a network using
protocols like TCP/IP.
✓ Remote Access - Allows users to access resources on other systems over a network (e.g., file sharing,
remote desktop).
✓ Network Security - Protects data and resources from unauthorized access over the network by
implementing firewalls, encryption, and secure protocols.
8. Error Detection and Handling - OS detects and handles errors or problems that occur in the system to ensure
stability and minimize disruptions.
Key tasks include;
✓ Error Detection - Identifies faults in hardware, software, or system operations.
✓ Error Recovery - Takes corrective actions to resolve issues, such as restarting processes or reloading
system configurations.
✓ Logging and Reporting - Keeps logs of system errors and events for troubleshooting and analysis.
9. System Resource Allocation - OS manages the allocation of resources like CPU time, memory, and devices
among competing processes.
Key tasks include;
✓ Resource Scheduling - Determines which processes or tasks get access to system resources at any given
time (e.g., which process gets the CPU next).
✓ Fairness and Efficiency - Ensures resources are allocated in a fair and efficient manner, optimizing
performance while preventing starvation of any process.
Historical development of OS
The development of operating systems (OS) is a journey that spans several decades, evolving from simple
systems to the complex, multitasking platforms we use today.
Milestones in the evolution of Operating Systems
1. Early Beginnings (1940s - 1950s)
First Generation (1940s)
The earliest computers were hardware-centric, and no operating systems were used at the time. Programs were
written directly in machine language and run manually on the hardware.
Batch Processing Systems (1950s)
In the 1950s, the first batch processing systems were introduced. Users would submit their jobs (programs) to
an operator, who would load them onto the computer in batches for execution, reducing the need for user
intervention. UNIVAC I and IBM 701 were some of the early systems that utilized batch processing.
2. Mainframe Era and Multiprogramming (1960s)
Multiprogramming and Time-sharing
As computing systems became more complex, the need for better resource management grew. The concept of
multiprogramming was introduced to allow multiple programs to run simultaneously by sharing the CPU. The
OS would switch between programs to maximize efficiency e.g. IBM’s OS/360 was a significant example of an
OS that supported multiprogramming.
Time-sharing Systems
In the late 1960s, time-sharing systems were developed, allowing multiple users to access a computer
concurrently. The idea was that the computer would quickly switch between users, giving the illusion of
dedicated machine access. E.g. CTSS (Compatible Time-Sharing System) developed at MIT was one of the first
examples of a time-sharing system.
3. UNIX and the Rise of Multi-user Systems (1970s - 1980s)
UNIX (1969)
UNIX, developed by Ken Thompson and Dennis Ritchie at AT&T Bell Labs, was a revolutionary operating
system that introduced concepts like multitasking, multi-user capabilities, and a portable environment. UNIX
allowed multiple users to interact with the system simultaneously and was highly portable, meaning it could be
adapted to run on different hardware platforms.
1970s UNIX became more widely used in academic and research institutions.
The Birth of PC Operating Systems (1980s)
With the rise of personal computers, there was a need for operating systems customized to individual users rather
than mainframe users e.g. MS-DOS (Microsoft Disk Operating System), released by Microsoft in 1981 for IBM
PCs, became one of the most popular operating systems for personal computers.
Apple’s Macintosh OS introduced a graphical user interface (GUI) with the release of Mac OS in 1984, which
was a significant milestone in the development of user-friendly operating systems.
CP/M (Control Program for Microcomputers) was one of the first popular OS for microcomputers before MS-
DOS.
4. Graphical User Interfaces and Modern OS (1990s)
Microsoft Windows
Microsoft transitioned from its command-line MS-DOS to Windows, which introduced the Graphical User
Interface (GUI), making computers more accessible to the general public. Windows 95 (released in 1995) was a
major milestone, offering a GUI and support for multitasking.
Linux (1991)
Linus Torvalds released the Linux kernel in 1991, based on the principles of UNIX. Linux quickly gained
traction in academic, development, and server environments. It’s an open-source OS, allowing users to modify
the code, and it eventually became widely adopted in server and embedded systems.
Linux distributions (e.g., Ubuntu, Red Hat) have evolved to cater to different needs, from desktop to cloud
computing.
Mac OS X (2001)
Apple’s transition from the classic Mac OS to Mac OS X (based on UNIX) brought a more powerful, stable, and
feature-rich OS to Apple's desktop and laptop computers. This OS integrated both the GUI and UNIX’s powerful
command-line features.
5. Mobile Operating Systems and Cloud Computing (2000s - 2010s)
Mobile OS Growth
Apple’s iOS (2007) - The introduction of the iPhone led to the development of iOS, a mobile operating system
that revolutionized smartphones.
Google’s Android (2008) - Android, based on the Linux kernel, became the dominant mobile OS globally. Its
open-source nature and support for a wide variety of devices made it extremely popular.
Cloud-based OS - With the increasing reliance on the internet and cloud computing, the concept of a cloud
operating system began to take shape. These OSs focus on virtualizing resources and providing online storage
and applications, rather than relying entirely on local hardware.
5. Virtualization and Specialized OS (2010s - Present)
Virtualization
With the rise of cloud computing and large-scale server farms, virtualization technologies became crucial.
Virtualization allows multiple virtual machines (VMs) to run on a single physical machine, each with its own
OS. This has led to the rise of specialized OS for virtualized environments, such as VMware ESXi or Microsoft
Hyper-V.
Embedded and Real-time OS
Specialized operating systems have been developed for specific purposes, such as embedded systems and real-
time applications. These systems are designed for critical applications that require immediate responses, such as
in automobiles, medical devices, robots, and IoT devices.
e.g. RTOS (Real-Time Operating Systems) like VxWorks and FreeRTOS.
7. The Future: AI, Edge Computing, and Autonomous Systems
✓ AI-driven OS - The role of Artificial Intelligence (AI) in OS development is growing, with the aim to
make systems smarter, more responsive, and capable of automating system management tasks.
✓ Edge Computing - Operating systems for edge devices (like the internet of things (IoT) and autonomous
devices) are evolving to support real-time processing and data management closer to the data source.
✓ Quantum Computing - Although still in the early stages, the development of operating systems for
quantum computers is underway. These OSs will handle unique challenges associated with quantum
computation, such as qubits and quantum entanglement.
OS structure
The structure of an Operating System (OS) defines how its components are organized and interact with each
other. The structure determines how the OS manages hardware resources, performs tasks, and provides services
to applications and users.
Key OS Structures
1. Monolithic Structure
In a monolithic OS structure, everything that makes the operating system work is packed together into one big
unit. All the different parts, like managing processes, memory, files, and device drivers, are tightly connected
and run in the same area of the computer's memory.
Since everything runs in kernel mode (mode of operation in a computer's CPU that gives the operating system
full access to the hardware), it means the operating system has complete control over the hardware, like the CPU
and memory, and can do anything without restrictions.
In simple terms, it’s like a single big team (the OS) doing all the work in the background, with full access to all
the tools and resources of the computer, all in one shared space.
Advantages
✓ High performance due to direct communication between components.
✓ Simple design for smaller, less complex systems.
Disadvantages
✓ Difficult to maintain and extend as the codebase grows.
✓ Lack of modularity, making debugging and upgrading harder.
✓ A bug in one part of the kernel can affect the entire OS.
2. Microkernel Structure
The microkernel design minimizes the functions running in kernel mode. Only essential services (like process
management, communication, and basic hardware abstraction) run in the kernel, while other services such as
device drivers, file systems, and network protocols run in user space. The OS communicates with external
services via inter-process communication (IPC).
Advantages
✓ Improved modularity, making it easier to maintain, extend, and debug.
✓ Greater stability and security since failures in user space do not crash the entire system.
✓ Easier to port to new hardware.
Disadvantages
✓ Potential performance overhead due to increased context switching and inter-process communication.
3. Layered Structure
In a layered OS structure, the OS is divided into layers, each providing a specific service or function. The layers
interact only with adjacent layers. For example, the top layer might be responsible for the user interface, the
middle layers handle system services, and the bottom layers manage hardware resources.
Advantages
✓ Easier to manage and understand due to clear separation of concerns.
✓ Layers can be developed and modified independently, making the OS more flexible.
Disadvantages
✓ Performance overhead due to layer communication.
✓ Layers that depend on each other may lead to inefficiencies in certain cases.
4. Client-Server Model
The OS follows a client-server architecture, where services are provided by servers and requested by clients. The
client makes requests for services, and the server responds to them. This structure is often used in distributed
systems (a group of computers or devices that work together to accomplish a common goal, even though they
are physically located in different places) where resources are shared across multiple computers.
Advantages
✓ Decentralized approach makes it scalable and fault-tolerant.
✓ Better for distributed computing and resource sharing across networks.
✓ Can isolate client systems from failure in the server.
Disadvantages
✓ Communication overhead due to network calls.
✓ Complexity in managing the interaction between clients and servers.
6. Hybrid Structure
A hybrid OS structure combines elements of both monolithic and microkernel architectures. The idea is to get
the best of both worlds—keeping the performance benefits of monolithic systems while ensuring the modularity
and stability of a microkernel system.
Advantages
✓ Better modularity and isolation of services compared to monolithic systems.
✓ Potential for high performance by keeping critical services in kernel space while using user space for
non-essential services.
Disadvantages
✓ Can be complex to implement and maintain.
✓ May not achieve the level of isolation and security of a pure microkernel.
6. Exokernel
The Exokernel architecture is an extreme minimalist approach where the OS kernel provides only the lowest-
level abstractions for hardware access, while applications manage hardware resources directly. The kernel does
not provide typical OS services like memory management or process scheduling—those tasks are handled by
libraries and applications themselves.
Advantages
✓ Maximizes performance by minimizing the kernel's interference with application-level operations.
✓ Allows applications to have more direct control over resources.
Disadvantages
✓ Complex application design since applications must manage resources manually.
✓ Lack of OS-provided protections and abstractions might lead to security and stability issues.
Types of OS
Operating Systems (OS) come in different types, each designed to meet specific needs based on the environment
or type of system. The following are the main types of operating systems;
1. Batch Operating System
In a batch OS, jobs (or tasks) are collected together and processed in batches without interaction from the user
during execution. Jobs are processed one after another in a sequence, without any user intervention once the job
starts. Example: Early mainframe systems like IBM’s OS/360 used batch processing.
2. Multitasking / Time-Sharing Operating System
A multitasking OS can run multiple programs (tasks) at the same time by rapidly switching between them. In
time-sharing, the OS allocates small time slots to each task, so it seems like they are running simultaneously.
The OS switches between tasks so fast that it gives the impression that several tasks are happening at once.
Example: Windows, Linux, and macOS are modern multitasking OSs.
3. Real-Time Operating System (RTOS)
An RTOS is designed for systems that need to perform tasks with very strict timing requirements. These tasks
must be completed within a certain time frame to avoid failure. The OS prioritizes tasks based on urgency. If a
task is critical, it gets done immediately. Used in medical equipment, automobile control systems, and robotics.
Example RTOSs: VxWorks, FreeRTOS.
4. Single-User, Single-Tasking Operating System
A single-user, single-tasking OS allows one user to run one program at a time. The OS is very simple and doesn’t
have to manage multiple users or tasks. Example: Early MS-DOS or Mac OS 9 (in its earlier versions).
5. Single-User, Multi-Tasking Operating System
A single-user, multi-tasking OS allows one user to run multiple programs at the same time. The OS can switch
between multiple tasks, allowing the user to work on several applications at once (e.g., browse the internet while
writing a document). Example: Windows, macOS, and Linux.
6. Multi-User Operating System
A multi-user OS allows multiple users to access and use the system’s resources at the same time. The OS
manages user access and ensures that multiple users can work independently without interfering with each other.
Example: UNIX, Linux, and Windows Server.
7. Distributed Operating System
A distributed OS manages a group of separate computers (called nodes) that work together as one system.
It allows resources (like processing power, memory, and storage) to be shared across multiple machines, so users
can access the resources as though they were on a single system. Example: Google’s Android, Cloud-based
systems, and Hadoop.
8. Network Operating System (NOS)
A network OS is designed to manage network resources and allow computers on a network to communicate and
share resources. It provides features for managing users, network connections, and file sharing, ensuring that
multiple computers can connect and work together.
Example: Windows Server, Novell NetWare, Linux with networking capabilities.
9. Embedded Operating System
An embedded OS is a lightweight OS used in devices like appliances, cars, and consumer electronics. It’s tailored
for specific tasks. The OS is optimized to run on hardware with limited resources (like memory and processing
power) and is designed for real-time, continuous operation. Example: Android (in smart devices), RTOS in
medical equipment, embedded Linux in smart TVs and cars.
10. Mobile Operating System
A mobile OS is designed specifically for smartphones, tablets, and other mobile devices. It allows for efficient
use of battery, touch-based interaction, and mobile networking. Example: iOS (for iPhones), Android (for many
smartphones), Windows Phone (older).
11. Cloud Operating System
A cloud OS is designed to manage and run applications on the cloud, meaning over the internet, rather than on
local machines. It helps distribute workloads and storage across many servers, and allows users to access and
interact with the system via the internet. Example: Chrome OS (Google), Amazon Web Services (AWS), and
Microsoft Azure.
Properties of OS
1. Multitasking
The OS allows multiple processes (or tasks) to run at the same time. The OS allocates small time slices to each
process, switching rapidly between them, so it seems like they are running simultaneously.
Advantages
✓ Increased productivity - Users can run multiple applications at once (e.g., browsing the internet while
listening to music).
✓ Efficient CPU usage - The CPU is kept busy by quickly switching between tasks, rather than being idle.
Disadvantages
✓ Resource contention - Multiple tasks competing for resources (memory, CPU) can slow down the
system.
✓ Complexity - Managing multiple tasks simultaneously can complicate scheduling and resource
allocation.
2. Multiprogramming
The OS keeps multiple programs in memory and manages them so that the CPU is always busy. It loads several
programs into memory, and when one program waits for input or I/O, the OS switches to another program that
can use the CPU.
Advantages
✓ Improved CPU utilization - While one program waits for input/output (I/O), another can use the CPU.
✓ Reduced CPU idle time -The system is more responsive and efficient because the CPU is rarely idle.
Disadvantages
✓ Memory management issues - As multiple programs are loaded into memory; the OS has to carefully
manage memory to avoid overloading.
✓ Context switching overhead - Frequent switching between programs can lead to performance loss.
3. Resource Management
The OS manages system resources such as CPU, memory, storage, and devices, allocating them efficiently to
running programs. The OS tracks all resources and assigns them based on priority and availability, ensuring fair
access to all processes.
Advantages
✓ Efficient resource use - The OS prevents overuse or wastage of resources, ensuring the system runs
smoothly.
✓ Fair distribution - Ensures that no single process monopolizes system resources, and every process gets
a fair share.
Disadvantages
✓ Overhead - Managing resources requires additional processing time and complexity, which can reduce
performance.
✓ Complex management - As the system grows, managing resources becomes increasingly difficult and
prone to errors.
4. Concurrency
Concurrency refers to the ability of a system to execute multiple tasks (processes or threads) simultaneously.
The OS allows multiple processes to run at the same time even on a single-core CPU by rapidly switching
between tasks. The OS divides CPU time into small time slices and switches between processes quickly, giving
the illusion of parallel execution.
Difficulties with Concurrency in Operating Systems
While concurrency improves efficiency and resource utilization, it also introduces several challenges. Here are
the main difficulties associated with concurrency:
1. Race Conditions - Occurs when multiple processes or threads access shared resources simultaneously,
leading to unpredictable outcomes. Example: Two processes updating the same bank account balance at the
same time may result in incorrect data.
2. Deadlocks - Happens when two or more processes are waiting for resources held by each other,
creating a circular dependency where none can proceed. Example: Process A locks Resource 1 and waits for
Resource 2, while Process B locks Resource 2 and waits for Resource 1.
3. Starvation - A process may be indefinitely delayed from execution because other higher-priority
processes keep getting scheduled. Example: A low-priority thread never gets CPU time because high-priority
threads continuously execute.
4. Synchronization Issues - Requires proper mechanisms (like semaphores, locks, and monitors) to
control access to shared resources. Incorrect synchronization may lead to inconsistent data and system crashes.
5. Context Switching Overhead - Switching between processes or threads requires saving and restoring
states, consuming CPU time and reducing performance. Too many context switches can slow down the system
rather than improving efficiency.
6. Resource Contention - Multiple processes competing for limited system resources (CPU, memory,
I/O) can lead to performance degradation. Example: Two processes heavily using the disk may slow down I/O
operations.
7. Debugging Complexity - Bugs in concurrent programs are often non-deterministic, meaning they
occur unpredictably and are hard to reproduce. Example: A race condition may only happen under specific
timing conditions, making it difficult to detect and fix.
8. Load Balancing Issues - Distributing tasks evenly across processors in a multicore or distributed
system is challenging. Poor load balancing can lead to some processors being idle while others are overloaded.
9. Priority Inversion - A low-priority process holds a resource needed by a high-priority process, causing
delays. Example: A low-priority thread with a lock prevents a high-priority thread from progressing.
10. Fault Tolerance and Recovery - If a process or thread fails, the system must ensure that other
concurrent processes remain unaffected. Implementing proper rollback and recovery mechanisms adds
complexity.
Solutions to Concurrency Challenges
✓ Synchronization Mechanisms - Semaphores, mutexes, monitors, and condition variables.
✓ Deadlock Prevention & Detection - Avoid circular dependencies, use timeouts, or apply the banker’s
algorithm.
✓ Efficient Scheduling Algorithms - Ensuring fair CPU time distribution and preventing starvation.
✓ Minimizing Context Switching - Using lightweight threads and optimizing scheduling policies.
✓ Proper Testing & Debugging - Tools like race condition detectors, logging, and simulation techniques.
Advantages
✓ Efficiency in task execution - Processes can make progress even while waiting for other processes to
complete.
✓ Better utilization of resources - The CPU is kept busy by switching tasks instead of waiting for one to
finish.
Disadvantages
✓ Synchronization issues - If processes share resources, the OS needs mechanisms (like locks) to avoid
data corruption or conflicts.
✓ Increased complexity - Managing concurrent processes is complex and requires careful synchronization.
5. Security and Protection
The OS protects the system from unauthorized access and ensures that processes are isolated from each other to
prevent interference. The OS uses authentication methods (like passwords) and access control mechanisms to
limit user privileges and protect data.
Advantages
✓ Data security - Protects sensitive information from unauthorized access or malicious attacks.
✓ System stability - Processes are isolated from each other, preventing one program from crashing the
system or corrupting other data.
Disadvantages
✓ Performance overhead - Security measures (like encryption and access control) can slow down system
performance.
✓ Complex configuration - Setting up proper security and protection mechanisms requires expert
knowledge and constant updates.
6. Error Detection and Handling
The OS detects and manages errors in both hardware and software to ensure the system operates smoothly. The
OS monitors the system for faults and takes corrective action, such as logging errors, showing messages, or
recovering from failures.
Advantages
✓ Increased system reliability - Errors are handled effectively, reducing the risk of system crashes.
✓ Prevention of data loss - The OS may perform automatic backups or save work to prevent data loss
during unexpected shutdowns.
Disadvantages
✓ Error recovery complexity - Handling errors can be complex, especially for critical system failures like
hardware malfunctions.
✓ Performance impact - Constant error checking can consume resources and slow down system
performance.
7. Virtualization
Virtualization is the process of creating a software-based (or virtual) representation of computing resources, such
as hardware, storage, network, or an entire operating system. The OS can simulate multiple environments, such
as virtual machines, to allow multiple operating systems or processes to run on the same hardware.
Types of Virtualizations
✓ Hardware Virtualization (Server Virtualization) - Uses a hypervisor (such as VMware, Hyper-V, or
KVM) to create and manage multiple virtual machines (VMs) on a single physical server.
✓ Operating System Virtualization - Allows multiple user-space instances (containers) to run on a single
OS kernel.
✓ Storage Virtualization - Combines multiple storage devices into a single logical storage unit.
✓ Network Virtualization - Abstracts physical network resources into software-defined networks (SDN).
✓ Desktop Virtualization - Hosts an entire desktop environment on a remote server, allowing users to
access it from any device.
✓ Application Virtualization - Runs applications in isolated environments without installing them on the
host system.
Benefits of Virtualization
✓ Improved Resource Utilization as it maximizes the use of available CPU, memory, and storage.
✓ Cost Savings - Reduces the need for physical hardware, power, and cooling.
✓ Scalability - Easily deploy and scale applications as business needs grow.
✓ Disaster Recovery - Snapshots and backups of VMs enable quick recovery.
✓ Security & Isolation - Each VM or container is independent, minimizing security risks.
Advantages
✓ Resource efficiency - Virtualization allows multiple systems or applications to share hardware resources
efficiently.
✓ Isolation - Virtual machines are isolated from each other, so if one crashes, the others are unaffected.
Disadvantages
Overhead - Virtualization requires extra resources to manage the virtual machines, which can reduce overall
performance.
Complex management - Managing virtual environments and ensuring that they work together properly can be
difficult.
Modern OS
A modern operating system (OS) is a sophisticated piece of software that manages computer hardware and
software resources, providing common services for computer programs. Some key
Characteristics of modern operating systems
1. Object-Oriented Design: This approach uses the principles of object-oriented programming to design
and build the OS.
2. Multitasking and Multithreading: These features allow multiple tasks and threads to run concurrently,
improving efficiency and performance.
3. Symmetric Multiprocessing: This enables multiple processors to share the same memory and I/O
facilities, enhancing processing power.
4. Distributed Operating System: This type of OS manages a group of distinct computers and makes
them appear to be a single computer.
5. Microkernel Architecture: This design minimizes the kernel's size by running most services in user
space, increasing modularity and security
Job Control
Job control refers to the management and execution of jobs (processes) within an operating system. It involves
overseeing the initiation, scheduling, and termination of jobs, and controlling their execution, especially in
environments where multiple jobs are running simultaneously. Job control is often used in batch processing
systems and interactive systems, where the user or OS has control over the processes/jobs.
Key Concepts of Job Control
i. Job Scheduling
The OS is responsible for determining the order in which jobs (processes) are executed. This involves placing
jobs in a queue and scheduling them for execution based on certain criteria like priority, time, and resources.
ii. Job Execution
After a job is scheduled, the OS allocates system resources (like CPU, memory) and executes it. Job control also
involves switching between jobs when the CPU is available.
iii. Job Termination
After the completion of a job, it is terminated by the OS, and resources are freed up for other jobs to use.
iv. Job Control Commands
In interactive environments like UNIX/Linux, job control is managed through specific commands in the
command line interface (CLI).
v. Foreground Jobs
Jobs that are running in the current terminal window and taking control of the user’s input.
vi. Background Jobs
Jobs that are running but not taking up the user’s terminal. These are typically managed in the background.
Common commands for job control in UNIX/Linux:
fg: Brings a background job to the foreground.
bg: Sends a job to the background.
jobs: Lists all jobs.
kill: Terminates a job.
Types of Job Control
1. Interactive Job Control
In interactive systems (e.g., UNIX or Linux), job control allows users to interact with processes, sending them
to the background, bringing them to the foreground, or stopping them. The user has direct control over jobs
running on the system through commands.
2. Batch Job Control
In batch processing systems, where jobs are processed automatically in sequence, the OS queues jobs for
execution without user interaction. The OS manages the scheduling, resource allocation, and job completion
without needing continuous input from users.
Advantages of Job Control
✓ Efficient Resource Utilization - Job control ensures that system resources are effectively allocated to
running jobs, especially in systems that handle multiple jobs simultaneously.
✓ User Flexibility - Users can pause, resume, or prioritize jobs as needed, allowing for better control over
job execution. In an interactive session, users can handle background tasks while working on other tasks
in the foreground.
✓ Increased Productivity - Job control allows users to run long-running tasks in the background while still
interacting with the system. This increases overall system productivity, as users can multitask efficiently
✓ Process Management - The OS can manage processes and prioritize tasks, ensuring that high-priority
tasks get the resources they need to run, while lower-priority jobs are placed in the background or
executed later.
Disadvantages of Job Control
✓ Complexity - In systems with many jobs running, job control can become complex to manage, especially
when dealing with large numbers of background tasks. It may require significant attention to ensure jobs
are executed properly.
✓ Overhead - Job control can add overhead to the system. Tracking jobs, switching between foreground
and background tasks, and managing resources for each job takes up additional resources, which can
slow down system performance.
✓ Potential for Errors - Mismanagement of jobs, like mistakenly stopping or terminating the wrong job,
can result in system instability or data loss. Incorrect handling of job priorities and resource allocation
can lead to inefficiencies or conflicts.
Importance of Job Control
✓ Efficient Resource Utilization – Prevents system overload and optimizes CPU usage.
✓ Multitasking – Allows users to run multiple applications simultaneously.
✓ Flexibility – Users can suspend, resume, and terminate processes as needed.
✓ System Stability – Helps prevent system crashes by managing runaway processes.
Process Management
Process Management is a fundamental function of the operating system (OS) that manages the creation,
scheduling, and termination of processes.
Process management ensures that the system allocates resources efficiently, ensures process synchronization,
and manages the execution of processes in a multi-tasking environment.
Process Creation - When a new program is initiated, the OS creates a process. The OS allocates resources (like
memory, CPU time) to the new process, sets up its process control block (PCB), and adds it to the process queue
for execution.
Process Termination - Once a process has completed its task, the OS releases its resources and terminates it.
The process is removed from the process table, and any remaining resources are freed for other processes.
Process lifecycle
The process lifecycle describes the various states a process undergoes from creation to termination.
✓ New - The process is being created. The operating system sets up the necessary structures (like the
Process Control Block, PCB).
✓ Ready - The process is ready to run but is waiting for CPU time. It's placed in the ready queue and
waits for the scheduler to assign it the CPU.
✓ Running - The process is actively being executed by the CPU.
✓ Waiting/Blocked - The process cannot continue until some external event occurs, like I/O
completion or resource availability. It's placed in the waiting queue.
✓ Terminated/Exit - The process has finished execution and is terminated. The OS will clean up its
resources, and its PCB is deleted.
State Transitions
A process moves through different states as it executes, depending on CPU scheduling, I/O operations, and
system resource availability. The key state transitions in a process lifecycle are as follows;
✓ New → Ready – The process is loaded into memory and prepared for execution.
✓ Ready → Running – The CPU scheduler selects the process for execution.
✓ Running → Waiting – The process requests an I/O operation and enters the waiting state.
✓ Waiting → Ready – The process resumes execution once the I/O operation is complete.
✓ Running → Terminated – The process finishes execution or is terminated by the system.
A PCB is a data structure used by the operating system to store information about a process. It serves as the
representation of a process within the system, allowing the OS to manage and track the state and execution of
processes. Each process in an operating system has its own PCB, which contains critical data needed to manage
and control the process.
3. Program Counter (PC) - The address of the next instruction to be executed by the process. The program
counter helps resume execution where the process last left off after a context switch.
4. CPU Registers - The values of the CPU registers when the process is not running but is stored for later
resumption. These include the accumulator, index registers, and stack pointers, which are saved during
context switching.
5. Memory Management Information - Details about the memory allocated to the process. This includes;
✓ Base and limit registers - Used to define the range of memory that the process can access.
✓ Page tables - If virtual memory is used, it holds information for mapping virtual addresses to
physical memory locations.
6. Process Priority - The priority of the process, which is used by the scheduler to decide the order in
which processes should be executed. Processes with higher priority may be given preference over those
with lower priority.
7. Scheduling Information - This includes information related to the scheduling of the process, such as:
✓ Scheduling queue pointers - Pointers to the next and previous processes in the scheduling
queue.
✓ Priority information - Used by scheduling algorithms to decide which process to execute next.
8. I/O Status Information - Information about the I/O devices the process is using (like files, printers,
etc.). It also includes pointers to I/O queues and device-specific information.
9. Accounting Information - Data used for accounting purposes, such as:
✓ CPU usage time - The amount of time the process has used the CPU.
✓ Real-time clock information - Time elapsed during the process execution.
✓ Resource usage information - The amount of memory, files, or I/O devices used by the process.
✓ The PCB plays a crucial role in process management as OS needs to maintain a record of the process's
state, memory, scheduling, and other essential data to effectively control the process.
✓ PCB holds the necessary information about the current process when the CPU switches from one process
to another. It ensures that the process state is saved and restored correctly.
✓ The OS uses the PCB to allocate and deallocate system resources such as memory, CPU time, and I/O
devices.
✓ The PCB stores scheduling information such as process priority and queue pointers, which the OS uses
to determine which process to run next.
✓ The PCB enables the operating system to monitor the progress of processes.
Importance of PCB
✓ The PCB allows the OS to track and manage the state of a process, ensuring that the process can be
resumed from where it left off after interruptions.
✓ The PCB holds the necessary scheduling information, which is used by the OS to decide when to execute
a process and which process to execute next.
✓ The PCB helps manage process synchronization by storing information about IPC mechanisms and
shared resources.
✓ The OS uses the PCB to control and manage processes, ensuring smooth execution and preventing
conflicts or errors in multi-processing environments.
3. Process Scheduling
The OS uses process scheduling algorithms to decide which process gets to use the CPU and for how long.
Scheduling ensures that CPU time is divided fairly and efficiently among all active processes.
Scheduling Criteria
There are many different criteria to check when considering the "best" scheduling algorithm, they are:
CPU Utilization
To make out the best use of CPU and not to waste any CPU cycle, CPU would be working most of the time
(Ideally 100% of the time) Considering a real system, CPU usage should range from 40% (lightly loaded) to
90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit time or rather say total amount of work done in a unit of
time. This may range from 10/second to 1/hour depending on the specific processes.
Response Time
Amount of time it takes from when a request was submitted until the first response is produced. Remember, it
is the time till the first response and not the completion of process execution (final response). CPU utilization
and Throughput are maximized and other factors are reduced for proper optimization.
Burst Time
Arrival Time
Completion Time
It is the amount of time taken to execute a particular process Is the interval from time of submission (Arrival
time) of the process to the time of completion of the process (completion time).
Turnaround time = Completion time – Arrival time Turnaround time = Waiting time + Burst time
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has been waiting in the ready
queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get into the CPU
✓ First-Come, First-Served (FCFS) - Processes are executed in the order they arrive.
✓ Shortest Job Next (SJN) - The process with the shortest execution time is executed first.
✓ Round Robin (RR) - Processes are assigned fixed time slices, and they rotate through execution.
✓ Priority Scheduling - Processes are assigned priorities, and the highest-priority process is executed
first.
4. Process States
A process can be in one of several states, depending on its execution status. The main states are:
5. Threading
A thread, also known as a "lightweight process," is the smallest unit of execution within a process. Threads are
a way for a program to split itself into two or more simultaneously (or pseudo-simultaneously) running tasks.
Here's an overview of threads and their importance in Operating Systems:
A thread is a sequence of programmed instructions that can be managed independently by a scheduler, which
is part of the operating system.
Types of Threads
✓ User-level Threads: Managed by the user-level libraries, not by the kernel. They are fast to create and
manage but lack the advantages of kernel-level threads.
✓ Kernel-level Threads: Managed by the operating system kernel. They can take advantage of
multiprocessor architectures but are slower to create and manage.
Benefits of Threads
✓ Efficiency: Threads share resources of the process they belong to, such as memory and file handles,
allowing for efficient communication and data sharing.
✓ Responsiveness: In a multithreaded program, if a part of a process is blocked or performing a lengthy
operation, the rest of the process can still run.
✓ Resource Sharing: Threads within the same process share the same address space and resources, which
allows for easy data sharing and synchronization.
✓ Utilization of Multi-core Processors: Threads can run on multiple processors, improving the
performance of multi-threaded applications.
Multithreading Models
✓ Many-to-One: Many user-level threads map to one kernel thread. This model is efficient but has
limitations in terms of concurrency.
✓ One-to-One: Each user-level thread maps to a kernel thread. This model provides high concurrency but
is resource-intensive.
✓ Many-to-Many: Many user-level threads map to many kernel threads. This model offers the best
balance between concurrency and resource utilization.
Thread Operations
Thread Lifecycle
✓ Thread Cancellation. Thread cancellation means terminating a thread before it has finished working.
There can be two approaches for this, one is Asynchronous cancellation, which terminates the target
thread immediately. The other is Deferred cancellation allows the target thread to periodically check if
it should be cancelled.
✓ Signal Handling. Signals are used in UNIX systems to notify a process that a particular event has
occurred. Now in when a Multithreaded process receives a signal, to which thread it must be delivered?
It can be delivered to all, or a single thread.
✓ fork() System Call. fork() is a system call executed in the kernel through which a process creates a
copy of itself. Now the problem in Multithreaded process is, if one thread forks, will the entire process
be copied or not?
✓ Security Issues because of extensive sharing of resources between multiple threads. There are many
other issues that you might face in a multithreaded process, but there are appropriate solutions available
for them.
✓ Race Conditions - When two or more threads access shared resources simultaneously, leading to
unpredictable results.
✓ Deadlocks: When two or more threads are waiting for each other to release resources, causing them to
be stuck indefinitely.
✓ Starvation - When a thread is perpetually denied necessary resources to proceed.
Why process management is important
✓ Schedules processes in a way that maximizes CPU utilization by managing when and how long processes
are executed.
✓ Fair Allocation of Resources.
✓ Process management enables multitasking.
✓ Process management ensures that processes are isolated from each other, preventing one process from
accidentally or maliciously interfering with another process’s memory or execution.
✓ Process Synchronization and Coordination
✓ Deadlock Detection and Prevention
✓ Optimized System Performance
✓ Support for Multithreading
✓ System Stability and Reliability
✓ Support for User Interaction
Process/CPU Scheduling
Process scheduling or CPU scheduling is the technique used by an operating system (OS) to determine the
order in which processes are executed by the CPU. Since most modern systems support multitasking, multiple
processes are often competing for CPU time, and the OS must decide which process to allocate the CPU to at
any given moment.
The primary goal of CPU scheduling is to maximize system efficiency and ensure fair allocation of CPU time to
processes, all while maintaining good system performance.
Why is Process Scheduling Important?
✓ Maximize CPU utilization as it keeps the CPU as busy as possible.
✓ Maximize throughput as it increases the number of processes completed per unit time.
✓ Minimize turnaround time as it reduces the total time taken for a process to complete.
✓ Minimize waiting time by decreasing the time a process spends in the ready queue.
✓ Minimize response time by reducing the time it takes for a process to start responding.
The CPU is busy most of the time and is the bottleneck. The process doesn't need much interaction with external
devices like hard drives or networks.
Characteristics
Characteristics
✓ Low CPU utilization but high I/O device usage.
✓ Frequently waiting for I/O operations to complete.
✓ Can benefit from faster storage devices (e.g., SSDs) or better network speeds.
Pros
✓ Better at sharing CPU time fairly.
✓ Makes systems more responsive because high-priority tasks get immediate attention.
Cons
✓ The OS has to spend time switching between tasks, which can slow things down.
✓ More complicated to manage.
Non-preemptive scheduling
In non-preemptive scheduling, once a process starts running, it keeps running until it finishes or voluntarily
gives up the CPU (e.g., to wait for something like input or data). The OS doesn't interrupt a process. The process
runs to completion unless it asks to stop or wait for something else. It works well when you have predictable
tasks that don’t need to be interrupted.
Pros
✓ Simpler and faster to set up because there’s no need to interrupt processes.
✓ More predictable because processes aren’t interrupted.
Cons
✓ A long process can block shorter or more important ones from running.
✓ Less responsive when tasks need to be prioritized.
Comparison
Feature Preemptive Non-preemptive
Interrupts The OS can interrupt processes No interruptions, runs to finish
Fairness Fairer to all tasks Can be unfair if long tasks block short ones
Complexity More complex to manage Easier to manage
Responsiveness Faster response time Slower response if long tasks run
Context Switching
Context switching is when the operating system (OS) switches from running one process (task) to another. The
OS saves the state of the current process and loads the state of the next process, allowing the computer to handle
multiple tasks at once.
A process is like a big box that contains everything needed to run a program, including its code and memory.
A thread is a small task inside that box. Multiple threads can work together within the same box (process).
Types of Threads
✓ User-Level Threads: Managed by the program itself (without help from the operating system). These
threads are lighter and faster but can’t use multiple processors very well.
✓ Kernel-Level Threads: Managed by the operating system. These can be run on different processors, so
they are better for multitasking.
✓ Hybrid Threads (Combination of Both) - In some systems, both user-level and kernel-level threads are
combined, forming a hybrid model.
Deadlock Avoidance
In contrast to deadlock prevention, deadlock avoidance does not try to eliminate the conditions but aims to
ensure that the system will never enter an unsafe state where a deadlock is possible.
A safe state is one where the system can allocate resources to processes without causing a deadlock. In an unsafe
state, deadlock is possible, and the system may enter that state under certain conditions.
Banker's Algorithm
The Banker's Algorithm is a deadlock avoidance strategy that determines whether a process can be safely
allocated resources without causing a deadlock. It checks if resources can be allocated in a way that ensures all
processes can eventually complete.
Steps in the Banker's Algorithm:
✓ Check if the system is in a safe state.
✓ Ensure there are enough resources for each process to complete.
✓ If a process is requesting resources, determine whether granting that request would still leave the system
in a safe state.
Advantages
Helps to avoid deadlocks by ensuring that resource allocation does not lead to an unsafe state.
Disadvantages
Can be slow and computationally expensive due to frequent checks of resource allocation.
1. Process Termination - Abort one or more processes involved in the deadlock. The process chosen for
termination may be based on factors like priority or resource usage.
Advantages
The deadlock is resolved quickly by killing the processes.
Disadvantages
Terminating processes may lead to data loss or inconsistency.
2. Resource Preemption - Resources are forcibly taken from processes involved in the deadlock. These
resources can be allocated to other processes to break the cycle.
Advantages
Can recover from deadlock without terminating processes.
Disadvantages
Complex to implement and can cause data inconsistency if not done carefully.
3. Rollback - The system can periodically make a record of the state of each process and when deadlock
occurs, roll everything back to the last checkpoint, and restart, but allocating resources differently so that
deadlock does not occur.
What is a Livelock? There is a variant of deadlock called livelock. This is a situation in which two or more
processes continuously change their state in response to changes in the other process(es) without doing any
useful work. This is similar to deadlock in that no progress is made but differs in that neither process is blocked
or waiting for anything.
2. Race Condition
A race condition happens when two processes try to access the same resource at the same time, leading to
incorrect or unpredictable results. Synchronization helps avoid race conditions by controlling when each process
can access the resource.
Memory Management
Memory management is a critical function of an operating system (OS) that involves the process of managing
the computer's memory resources. It ensures that each running program (process) has enough memory to operate
efficiently while preventing one program from interfering with others. Proper memory management is essential
for system stability, performance, and multitasking.
Functions of Memory Management
1. Memory Allocation - When a program starts, the OS allocates memory for it to use. The OS determines
how much memory the program needs and reserves that portion of the system's RAM (Random Access
Memory) for its use.
✓ Static Allocation -This is when memory is allocated at compile time and remains fixed during
the program's execution.
✓ Dynamic Allocation - Memory is allocated during the execution of the program, and the
program can request more memory or release it as needed.
2. Tracking and Organizing Memory (Memory Partitioning) - The OS keeps track of which memory
locations are in use and which are free. It divides the memory into smaller, manageable sections
(partitions) for processes to use. Memory can be partitioned in several ways:
✓ Fixed Partitioning - Memory is divided into equal-sized partitions. Each process is allocated a
partition, but the memory might not always be fully utilized.
✓ Dynamic Partitioning - The OS allocates variable-sized partitions based on the needs of the
process. This is more flexible than fixed partitioning but can lead to fragmentation.
3. Virtual Memory - Virtual memory allows the system to run larger programs than can fit into physical
RAM by swapping data between RAM and disk storage (usually a swap file or swap space).
✓ It creates the illusion that each process has its own contiguous block of memory, even though
the process may be scattered across different physical locations. This makes multitasking more
efficient.
✓ Virtual memory is typically managed through a combination of paging or segmentation.
4. Paging - Paging is a memory management scheme that eliminates the need for contiguous memory
allocation. The OS divides the physical memory into small, fixed-size blocks called pages. It also divides
logical memory (used by the program) into blocks of the same size. When a process needs memory, the
OS assigns it a page from the free memory pool. Pages can be swapped in and out of physical memory
to/from secondary storage (disk).
5. Segmentation - Unlike paging, which divides memory into fixed-size pages, segmentation divides
memory into segments of different lengths based on the logical structure of the program. Segmentation
can be combined with paging to manage memory in a more flexible way.
6. Memory Protection - Memory protection ensures that one process cannot access or modify the memory
space of another process. This is important for security and stability, as it prevents bugs in one program
from crashing others. The OS uses techniques like base and limit registers to define the boundaries of
each process's memory, ensuring that processes stay within their allocated space.
7. Garbage Collection (in certain environments) - In some programming languages and environments
(like Java or Python), the OS or runtime system takes care of freeing up memory that is no longer needed
by a process. This is called garbage collection. It helps manage memory dynamically and automatically,
but it can have performance overhead.
8. Swapping and Thrashing
Swapping occurs when the OS moves data between RAM and disk (swap space) to free up memory for active
processes. This can slow down performance if too much data is swapped in and out.
Thrashing happens when the system spends more time swapping data in and out of memory than executing
processes, often caused by insufficient physical memory or excessive multitasking.
✓ Fragmentation - Fragmentation occurs in a dynamic memory allocation system when most of the free
blocks are too small to satisfy any request. It is generally termed as inability to use the available memory.
In such situation processes are loaded and removed from the memory.
✓ Efficient Allocation - The OS needs to find the most efficient ways to allocate memory to processes
while preventing fragmentation and ensuring that memory is used optimally.
✓ Balancing Performance - Memory management techniques (like virtual memory and paging) can
impact system performance. For example, excessive swapping can cause slowdowns.
✓ For efficient utilization of resources - Memory is a limited resource, i.e., it’s of limited size in computer
systems, and proper management ensures its efficient utilization.
✓ Program Stability - Poor memory management can lead to instability in computer programs. Effective
memory management helps identify and prevent memory leaks, improving program stability.
✓ Performance optimization - Efficient memory management will have a significant impact on system
performance. It helps reduce access latency and improves overall program execution speed.
✓ Security and Protection - Memory management plays an important role in enforcing security and
protecting the integrity of computer systems. It enables memory isolation between different processes,
preventing unauthorized access to or modification of memory regions.
✓ Dynamic Resource Allocation - Modern computer systems often require the dynamic allocation of
memory resources. Memory management allows programs to request memory dynamically during
runtime, enabling flexibility and adaptability in resource utilization.
✓ Prevents thrashing - Thrashing occurs when the OS spends most of its time swapping data in and out
of memory instead of executing tasks, leading to significant performance slowdowns. Proper memory
management techniques, such as setting up efficient virtual memory handling, can help avoid thrashing
by ensuring that only necessary data is swapped.
Memory allocation
Memory allocation is a process by which computer programs are assigned memory or space. The operating
system (OS) must decide how to allocate memory efficiently to avoid fragmentation and ensure that all processes
have the necessary resources to run.
1. First Fit
2. Best Fit
3. Worst Fit
The largest hole that is big enough is allocated to program.
There are several memory allocation techniques, each with its own strengths and weaknesses.
In contiguous memory allocation, each process is allocated a single contiguous block of memory. The OS assigns
a portion of memory that is contiguous (i.e., all the memory required for a process is in one continuous block).
Advantages
✓ Simple to implement.
✓ Access to memory is fast because there’s no need to search through fragmented memory.
Disadvantages
✓ External Fragmentation - As processes are loaded and unloaded, free memory is scattered in small
chunks, making it harder to find a large enough block for new processes.
✓ Fixed size allocation - This can lead to inefficient use of memory if the process does not fully utilize
the allocated memory.
✓ Difficult to handle processes with dynamic or changing memory needs.
✓ Fixed Partitioning - The memory is divided into fixed-sized partitions. Each process is assigned one
partition.
✓ Dynamic Partitioning - The OS allocates a variable-sized block of memory to a process based on its
requirements.
Paged memory allocation is a technique that divides the physical memory into fixed-size blocks called pages.
The logical memory (used by processes) is also divided into blocks of the same size. A page table is used to map
the logical pages to physical pages in memory.
Advantages
Disadvantages
✓ Internal Fragmentation - If the process doesn’t fully fill a page, there will be wasted space within the
page.
✓ Overhead - The OS must maintain page tables to track the mapping between virtual and physical pages.
In segmented memory allocation, the memory is divided into variable-sized segments, each of which represents
a logical division of the program. For example, a program might have segments for code, data, and the stack.
Advantages
✓ More flexible than paging since segments can be of varying sizes, which matches the program’s logical
structure (e.g., code, data, stack).
✓ Allows better protection and sharing of data, as individual segments can be protected or shared
independently.
Disadvantages
✓ External Fragmentation - Segments can end up being scattered in physical memory, leading to
inefficient memory usage.
✓ Complexity - The OS must manage the segment table, which maps logical segments to physical memory
locations.
Virtual memory allocation is a technique where the OS uses a combination of RAM and disk space to create
the illusion of a larger memory space than is physically available. This allows programs to use more memory
than the system has in physical RAM by swapping data in and out of disk storage.
Advantages
Disadvantages
✓ Swapping Overhead - If the system relies too heavily on swapping between RAM and disk, it can
significantly slow down performance (known as "thrashing").
✓ Disk I/O - Reading and writing to disk is much slower than accessing RAM, which can cause delays
when the system runs out of physical memory.
In the buddy system, memory is allocated in blocks that are powers of two (e.g., 1KB, 2KB, 4KB, 8KB). The
system divides memory into partitions that are all rounded up to the nearest power of two.
Advantages
✓ Simple to implement and fast for allocation and deallocation.
✓ It minimizes external fragmentation by merging "buddies" (pairs of adjacent free blocks) when memory
is freed.
Disadvantages
✓ Internal Fragmentation - If a process needs a small amount of memory but is allocated a larger block,
there will be wasted space within that block.
6. Slab Allocation
Slab allocation is a memory allocation method used in kernel memory management, particularly in operating
systems like Linux. It manages memory for objects of the same size (e.g., buffers, queues, or other kernel objects)
in a cache-like structure.
Advantages
✓ Reduces fragmentation by pre-allocating memory blocks of a fixed size for specific types of objects.
✓ Efficiently handles memory allocation and deallocation for objects of the same size.
Disadvantages
✓ Not as flexible as other techniques for general-purpose memory management, as it's designed for
specialized use cases.
Buddy System Simple, minimizes fragmentation. Internal fragmentation, fixed block sizes.
Efficient for objects of the same size, reduces Less flexible for general-purpose memory
Slab Allocation
fragmentation. allocation.
Fragmentation
Fragmentation refers to the condition where memory is inefficiently used, leading to wasted space. It occurs
when free memory is scattered throughout the system in small, non-contiguous blocks, making it difficult to
allocate large chunks of memory for new processes or tasks. Fragmentation can significantly reduce the
efficiency of memory utilization, leading to performance degradation and wasted resources.
1. External Fragmentation
External fragmentation occurs when free memory is split into small, non-contiguous blocks. This happens when
processes are allocated memory and later released, leaving gaps between the remaining free blocks. While the
total amount of free memory might be sufficient to satisfy a new allocation, the fragmented blocks may not be
large enough to fulfill the new memory request.
Causes
✓ When processes are loaded and unloaded into memory, they leave gaps (or free spaces) between them.
Over time, these gaps accumulate, creating small blocks of free memory.
✓ Fixed-size allocation (such as in contiguous memory allocation) is particularly prone to external
fragmentation because as processes come and go, small unused gaps form between them.
2. Internal Fragmentation
Internal fragmentation occurs when memory is allocated in fixed-size blocks, but the allocated block is larger
than the memory actually needed by the process. As a result, unused space is left within the allocated block,
leading to wasted memory.
Causes
✓ Fixed-size partitions or blocks are allocated to processes, but some processes may not require the full
block of memory. For example, if a process needs 60 KB of memory but is allocated 64 KB (due to
block size limitations), the extra 4 KB is wasted and cannot be used by other processes.
The impact of fragmentation varies depending on the memory allocation method used:
External Fragmentation - Contiguous allocation methods (like fixed and dynamic partitioning) are especially
vulnerable to external fragmentation because processes are allocated contiguous blocks of memory, and over
time, gaps form.
Internal Fragmentation - Less of a concern in contiguous allocation but still possible if fixed-sized partitions
are used (as in fixed partitioning).
Internal Fragmentation - Paging can still suffer from internal fragmentation within individual pages if a process
doesn’t use the full space in a page. For example, if a page is 4 KB and a process needs only 2 KB, 2 KB of
memory in that page is wasted.
External Fragmentation - Like contiguous allocation, segmented memory allocation can suffer from external
fragmentation because segments can vary in size and may end up scattered in memory. This makes it difficult to
find large enough free segments.
Internal Fragmentation - Less of a problem, since segments are allocated based on the program’s logical
structure and can vary in size.
4. Buddy System
External Fragmentation - The buddy system minimizes external fragmentation by merging adjacent free blocks
(called "buddies") when memory is freed. However, fragmentation can still occur if the required memory size is
not an exact power of two.
Internal Fragmentation - The buddy system can lead to internal fragmentation because memory is allocated in
fixed sizes (powers of two), which might not exactly match a process's needs.
5. Virtual Memory
External Fragmentation - Virtual memory systems use paging or segmentation, which helps eliminate
external fragmentation by breaking memory into fixed-size pages or segments. Thus, the system can efficiently
use available physical memory.
Internal Fragmentation - Can occur when the system allocates pages or segments larger than needed. However,
because pages are typically smaller (e.g., 4 KB), the fragmentation within individual pages is usually less severe.
Summary of Fragmentation
✓ External fragmentation occurs when there’s enough total free memory, but it’s scattered in small, non-
contiguous chunks, making it difficult to allocate large blocks of memory.
✓ Internal fragmentation occurs when allocated memory blocks are larger than needed, leaving unused
space within those blocks.
✓ Memory allocation techniques such as paging and segmentation are designed to minimize
fragmentation and make memory management more efficient.
✓ Compaction and garbage collection are techniques used to address external fragmentation by moving
processes around to create larger contiguous memory blocks, but they can be time-consuming and
resource-intensive.
Swapping
Swapping is a memory management technique used by operating systems to manage the allocation of physical
memory (RAM) when there is not enough available space for all the processes that are running. When physical
memory is full, the operating system swaps out some of the data or processes from RAM to secondary storage
(such as a hard disk or solid-state drive) to free up space. Later, when the swapped-out processes are needed
again, they are swapped back into RAM.
Swapping allows the system to run processes that require more memory than is physically available by making
use of virtual memory. It enables the operating system to use disk space as an extension of RAM, allowing for
multitasking even with limited physical memory.
When there are more processes in memory than the available physical RAM, the operating system identifies
less-active or low-priority processes to swap out. These processes are transferred to swap space (a dedicated
area on the disk). The OS frees up memory so that new or more important processes can be loaded into RAM.
2. Swapping Back
When a swapped-out process needs to be executed again, the OS will swap it back into physical memory,
possibly swapping out another process to make room. This is done based on a swap algorithm that determines
which process should be swapped out or swapped in.
Concepts in Swapping
Swap Space - The area on the disk where data is stored when it's swapped out of RAM. This can be a designated
swap partition (a part of the hard drive set aside for this purpose) or a swap file (a file within the file system used
for swapping).
Swap In and Swap Out - "Swapping out" refers to moving data from RAM to swap space on disk, and
"swapping in" refers to moving data back from swap space to RAM.
Page Swapping - In modern operating systems that use virtual memory, memory is divided into pages. If a
page of memory is swapped out, it is stored in swap space, and when needed again, it is swapped back into RAM.
Thrashing: If the system is swapping too frequently (i.e., constantly moving processes in and out of memory),
it can lead to a performance problem called thrashing. This happens when the system spends more time swapping
than executing processes, causing significant slowdowns.
Types of Swapping
1. Single-Process Swapping
In this approach, the entire process is swapped in or out of memory at once. This is typically used in older or
simpler operating systems. The OS can swap out a process entirely if it’s not actively in use, freeing up memory
for other processes.
In modern operating systems, memory is divided into pages (fixed-size blocks of memory). Only pages that are
not actively being used are swapped out, and only the necessary pages are swapped in when the process needs
them. This allows for more efficient use of memory and minimizes the amount of data that needs to be swapped
at any given time.
3. Demand Paging
In demand paging, instead of swapping the entire process, only the pages of memory that the process requires
are loaded into RAM when needed. If a process tries to access a page that is not currently in RAM, a page fault
occurs, and the OS loads the page from the swap space into memory. This reduces the amount of data that needs
to be swapped and allows for more efficient memory use.
4. Swap Scheduling
The operating system uses various algorithms to determine which process to swap in or out. Common strategies
include:
✓ Least Recently Used (LRU): The OS swaps out the process or page that hasn’t been used for the longest
time.
✓ First-In, First-Out (FIFO): The first process or page that was swapped in is the first one to be swapped
out when space is needed.
✓ Optimal Page Replacement: A theoretical algorithm that swaps out the page that will not be used for
the longest period of time. Although ideal, it is not practical due to its need for future knowledge of
memory accesses.
Advantages of Swapping
1. Enables Multitasking - Swapping allows the system to run more processes than can fit in physical
memory, making multitasking feasible even with limited RAM.
2. Efficient Use of Physical Memory - Swapping helps to free up physical memory by transferring
inactive or low-priority processes to disk. This allows the OS to allocate memory to the most critical
processes that require more immediate access to resources.
3. Support for Large Applications - Swapping allows larger programs to run on systems with limited
RAM by relying on disk space to hold parts of the program that are not currently being used.
4. Virtual Memory - With swapping and virtual memory, the OS can provide the illusion of a larger
address space than what is physically available in RAM. This creates a more flexible environment for
running applications that require more memory than what is installed on the system.
Disadvantages of Swapping
1. Performance Overhead - Accessing data in swap space is much slower than accessing data in physical
RAM because disk storage is slower than RAM.
2. Disk Space Usage - Swapping requires sufficient disk space, which can be an issue on systems with
limited storage.
3. Thrashing - When the system is overburdened with too many processes competing for memory, the OS
may spend most of its time swapping processes in and out of memory instead of actually executing them.
This condition is known as thrashing.
✓ Increase RAM - Adding more physical memory reduces the need for swapping.
✓ Use Efficient Algorithms - Operating systems can optimize swap scheduling algorithms (like LRU or
FIFO) to minimize swapping overhead.
✓ Limit Active Processes - Running fewer processes at the same time can reduce the likelihood of
thrashing and excessive swapping.
✓ Use Large Swap Space or SSDs - Increasing swap space or using faster storage (e.g., SSDs instead of
HDDs) can improve swapping performance.
Paging and Segmentation are both memory management techniques used by operating systems to divide and
manage memory efficiently. Although both are used to handle memory allocation, they have different approaches
and are often used in combination in modern systems. Let's dive into each technique:
1. Paging
Paging is a memory management scheme that eliminates the problem of external fragmentation by dividing
physical memory and logical memory into fixed-sized blocks called pages (for logical memory) and frames (for
physical memory).
✓ Logical Memory (the process's memory) is divided into fixed-size blocks called pages.
✓ Physical Memory (RAM) is divided into fixed-size blocks called frames.
✓ The size of a page and a frame is the same, which allows logical pages to be mapped to physical frames.
✓ The page table is used to keep track of the mapping between pages in logical memory and frames in
physical memory.
Paging Process
Step 1: Logical Address Generation - When a process is executed, the CPU generates a logical (virtual) address.
This address is divided into two parts:
✓ Page number (high-order bits): Identifies the page in the logical address space.
✓ Offset (low-order bits): Identifies the location within that page.
Step 2: Page Table Lookup - The page number is used to index into the page table to get the corresponding
frame number in physical memory.
Step 3: Physical Address Calculation - The frame number from the page table and the offset from the logical
address are combined to generate the physical address.
Step 4: Access Memory - The memory management unit (MMU) uses the physical address to access the physical
memory location, and the process continues its execution.
Advantages of Paging
✓ No External Fragmentation - Since memory is divided into fixed-size blocks, there is no fragmentation
of free space between processes.
✓ Efficient Use of RAM - Physical memory can be allocated in a non-contiguous manner, allowing for
better utilization of memory.
✓ Supports Virtual Memory - Virtual memory allows processes to use more memory than the available
physical RAM by swapping pages in and out of disk storage.
Disadvantages of Paging
✓ Internal Fragmentation - Since the size of a page is fixed, there may be wasted space within a page if
a process doesn't fully use the allocated space.
✓ Overhead - Maintaining and managing page tables requires extra memory and can cause some
performance overhead. The system must also manage page faults and swapping.
✓ Slow Memory Access - Accessing memory requires looking up the page table to find the corresponding
frame, which adds some overhead.
Example of Paging
Let’s say the logical memory has 3 pages (Page 0, Page 1, Page 2), and the physical memory has 3 frames (Frame
0, Frame 1, Frame 2). The page table might look like this:
Page 0 Frame 1
Page 1 Frame 0
Page 2 Frame 2
This means that Page 0 from logical memory is stored in Frame 1 in physical memory, Page 1 in Frame 0, and
Page 2 in Frame 2.
2. Segmentation
Segmentation is another memory management scheme used by operating systems, and it is quite different from
paging. While paging divides memory into fixed-size blocks (pages), segmentation divides memory into
variable-sized segments, each representing a logical unit of a program, such as functions, arrays, or stacks.
In segmentation, the logical memory of a process is divided into segments that vary in size. Each segment
contains a specific part of the process, like:
Components of Segmentation
1. Logical Address (Segmented Address) - A logical address in a segmented memory system consists
of two parts:
✓ Segment number - Identifies which segment of the program the address belongs to (such as code,
data, stack).
✓ Offset - Specifies the exact location within that segment.
2. Segment Table - The operating system maintains a segment table for each process. The segment
table stores the base address (starting address) of each segment in physical memory and the limit (the
size) of each segment.
This table helps in converting logical addresses into physical addresses by adding the base to the offset.
1. Logical Address Generation - The CPU generates a logical address in the form of (Segment
Number, Offset) when a process is executed.
2. Segment Table Lookup -The operating system uses the segment number to look up the base and
limit in the segment table.
✓ The base gives the starting address of the segment in physical memory.
✓ The limit ensures that the offset is within the bounds of the segment.
3. Check Validity
If the offset is less than the segment's limit, the address is valid, and the process continues. If the offset
exceeds the segment's limit, a segmentation fault occurs, indicating an invalid memory access.
4. Physical Address Calculation - If the address is valid, the physical address is calculated by adding
the base address of the segment to the offset.
5. Access the Memory - The memory management unit (MMU) uses the physical address to access the
appropriate location in physical memory.
Example of Segmentation
✓ Code Segment - Starts at physical address 0x1000 and has a size of 0x200.
✓ Data Segment - Starts at physical address 0x2000 and has a size of 0x300.
✓ Stack Segment - Starts at physical address 0x3000 and has a size of 0x100.
When a process generates the logical address (Segment 1, Offset 0x50) (for example, accessing the code segment),
the operating system does the following:
The segment table says that Segment 1 (Code Segment) starts at physical address 0x1000 and has a size of
0x200.
2. Check Validity
The offset 0x50 is within the limit (less than 0x200), so the address is valid.
Benefits of Segmentation
✓ Logical Structure - Segmentation provides a more natural way of organizing memory based on
logical divisions of a program (like code, data, stack, etc.).
✓ Protection and Sharing - Segments can be protected (e.g., code can be marked read-only) and shared
between processes (e.g., a shared library might be mapped into the code segment of multiple
processes).
✓ Dynamic Memory Allocation - Since segments can grow or shrink as needed, segmentation is more
flexible for handling different-sized blocks of memory.
Drawbacks of Segmentation
✓ External Fragmentation - Since segments vary in size, there may be gaps (fragments) in physical
memory that can’t be used efficiently, especially as segments are allocated and deallocated.
✓ Complexity - Managing segment tables and ensuring that offsets do not exceed segment limits can be
more complex than paging.
✓ Paging divides memory into fixed-size pages, while segmentation divides memory into variable-sized
segments.
✓ Paging avoids fragmentation but introduces internal fragmentation, while segmentation avoids
internal fragmentation but can suffer from external fragmentation.
✓ Paging is simpler in terms of memory management but doesn’t reflect the logical structure of a
program, while segmentation provides a more logical organization of memory but can lead to
fragmentation.
In modern operating systems, both paging and segmentation are often used together. This combination allows
for the advantages of both techniques while minimizing their respective drawbacks.
Segmented Paging - In this approach, the system first divides memory into segments (e.g., code, data, stack),
and then each segment is divided into pages. This technique combines the benefits of segmentation (logical
organization) with paging (elimination of external fragmentation).
Virtual Memory - Many systems implement virtual memory using segmented paging, where each process is
divided into segments, and within each segment, memory is allocated in pages. This allows processes to use
more memory than is physically available by swapping pages and segments in and out of disk storage
Virtual memory
Virtual Memory is a memory management technique that gives the illusion to users and applications that they
have access to a large, contiguous block of memory, even though the physical memory (RAM) is limited. It
enables a computer to run programs that require more memory than is physically available in the system. Virtual
memory allows an operating system to use secondary storage (usually a hard disk or solid-state drive) to extend
the apparent size of physical memory.
The virtual address space is the range of memory addresses that an application or process can use. Each process
is given its own virtual address space, which is mapped to physical memory by the operating system. The
virtual address space includes memory for the process's code, data, stack, and heap.
2. Physical Memory
Physical memory refers to the actual RAM installed on the machine. Virtual memory uses paging or
segmentation to map virtual addresses to physical memory locations.
3. Address Translation
Virtual memory relies on a memory management unit (MMU) to translate virtual addresses into physical
addresses. This is done using page tables (in paging) or segment tables (in segmentation). When a process
generates a virtual address, the MMU translates it into the corresponding physical address in RAM. If the
required data is not in physical memory (because it has been swapped out), a page fault occurs, and the operating
system swaps data between RAM and secondary storage.
4. Swap Space
Swap space (also called paging file or swap file) is a portion of the disk used as "virtual" memory. When physical
RAM is full, data is temporarily swapped to this space on disk to free up RAM for other processes. Swapping
is the process of moving data between RAM and swap space. Virtual memory allows processes to exceed the
physical memory limits by swapping in and out of swap space.
Each process is assigned its own virtual address space, which is divided into fixed-size blocks (pages) or
variable-size blocks (segments). These virtual addresses are mapped to physical memory addresses, but they
may not be contiguous in RAM. The operating system and hardware (MMU) manage this mapping. When a
process accesses a page or segment that is not currently in RAM (a page fault), the OS loads the required data
from the disk (swap space) into physical memory.
In paging systems, memory is divided into fixed-size pages (in virtual memory) and frames (in physical
memory). A page table keeps track of which page is mapped to which frame. When a process accesses a page
that is not currently in RAM (a page fault), the operating system swaps that page into memory from disk. The
page table helps the OS know where the pages are stored and translates virtual addresses to physical addresses.
In a segmented system, memory is divided into segments based on the program's structure, such as code, data,
stack, etc. Each segment has its own base address and length. The virtual address consists of a segment number
and an offset, and the segment table is used to translate virtual addresses to physical addresses. Like paging,
segmentation can also use secondary storage (swap space) when there is insufficient physical memory.
4. Demand Paging
Demand paging is a strategy where pages are loaded into memory only when they are needed (on-demand),
rather than loading the entire process into memory at once. This helps reduce memory usage and ensures that
only the required pages are loaded into physical memory, improving overall system performance.
5. Page Fault
A page fault occurs when a process tries to access a page that is not currently in physical memory.
When a page fault happens, the operating system:
6. Thrashing
Thrashing occurs when the system spends more time swapping data between memory and disk than actually
executing processes. This typically happens when the system is overloaded with too many processes or not
enough physical memory is available. Thrashing significantly degrades system performance, as it causes high
disk I/O and excessive paging.
1. Enables Multitasking
2. Efficient Use of RAM
3. Memory Protection
4. Simplified Programming
5. Support for Larger Applications
1. Performance Overhead
2. Increased Disk Usage
3. Complexity
Consider a system with 4 GB of RAM and 8 GB of swap space. If an application requests 6 GB of memory:
✓ The operating system can use virtual memory to provide the application with 6 GB of memory, even
though only 4 GB is available in RAM.
✓ The OS will swap data between physical memory and swap space on the disk to ensure that the
application gets the memory it needs.
✓ If the application doesn't need the entire 6 GB at once, only the pages that are being used will be loaded
into RAM, while the rest will be in swap space.
Demand paging
Demand Paging is a memory management technique used in virtual memory systems where pages of a process
are loaded into physical memory only when they are needed, i.e., when the process accesses them. This allows
for more efficient use of physical memory by not loading all pages of a program into RAM at once. Instead,
pages are brought into memory on-demand, as the program references them.
The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all at
once. Rather they are swapped in only when the process needs them (On demand). This is termed as lazy
swapper, although a pager is a more accurate term.
1. Lazy Loading - Pages are not loaded into memory at the start of a process's execution. Instead, they are
loaded only when the process accesses them. This reduces the need to load all pages into memory at
once, saving both memory and time.
2. Page Faults - When a process tries to access a page that is not currently in physical memory, a page
fault occurs. The operating system handles this page fault by loading the required page into memory
from secondary storage (e.g., disk or swap space).
3. Efficiency - Demand paging makes better use of system resources. Only the necessary pages are loaded
into memory, and unneeded pages are swapped out to disk or never loaded at all. It allows programs to
run with a smaller footprint in RAM, making it possible to run more processes concurrently.
4. Page Table -The operating system maintains a page table for each process, which maps the process's
virtual addresses to physical addresses. If a page is not in memory, the page table entry will indicate that
the page is on disk, and the OS will load it into RAM when needed.
When the process requires any of the page that is not loaded into the memory, a page fault trap is triggered and
following steps are followed,
1. The memory address which is requested by the process is first checked, to verify the request made by
the process.
3. In case the request by the process is valid, a free frame is located, possibly from a free-frame list,
where the required page will be moved.
4. A new operation is scheduled to move the necessary page from disk to the specified memory location.
(This will usually block the process on an I/O wait, allowing some other process to use the CPU in the
meantime.)
5. When the I/O operation is complete, the process's page table is updated with the new frame number,
and the invalid bit is changed to valid
1. Initial Program Execution - When a program starts running, only a small portion of its memory
(typically the code and a few required pages) is loaded into physical memory. The rest is not loaded yet.
2. Page Access and Page Fault - As the process executes, it accesses different parts of its address space.
If the process accesses a page that is not in RAM, the MMU (Memory Management Unit) detects the
page fault. The page fault triggers an interrupt, and the operating system takes control.
3. Handling the Page Fault -The operating system examines the page table to determine which page
caused the fault. It checks if the page is on disk (in the swap space or secondary storage). If it is, the OS
finds a free frame in physical memory, swaps out a page (if necessary), and loads the required page from
disk into the available frame.
4. Resuming Execution - After the page is loaded into memory, the process can continue its execution
from where it left off. The OS updates the page table to reflect that the page is now in memory, and the
page fault is cleared.
5. Lazy Loading - Only the pages that are actually used by the program are loaded into memory. Pages
that are never accessed are not loaded, saving both disk space and RAM.
The OS first identifies the page fault and the page that needs to be loaded.
The OS may need to swap out an existing page from physical memory to make room for the new page. The
chosen page to swap out is typically based on the page replacement algorithm (e.g., Least Recently Used,
FIFO).
The required page is loaded from secondary storage (swap space) into the free frame in RAM. The page table is
updated to reflect the new location of the page in memory.
2. Swap Space
The swap space on disk is used to store pages that are not currently in memory. If the physical memory becomes
full, the OS will swap pages in and out of memory to keep the most needed pages in RAM.
When physical memory is full, and a new page must be loaded, the operating system needs to decide which
existing page to swap out. Several algorithms exist to manage this decision:
Optimal - The page that will be used furthest in the future is swapped out (though this is impractical to implement
as it requires future knowledge).
Page Replacement Algorithms are techniques used by operating systems to decide which page to swap out when
new pages need to be loaded into memory and there is no free space available. These algorithms are crucial for
managing virtual memory efficiently, particularly in a system that uses demand paging, where pages are brought
into memory only when needed. The goal of page replacement algorithms is to minimize the number of page
faults, reduce swapping overhead, and keep the system running smoothly.
FIFO is one of the simplest page replacement algorithms. It works by replacing the oldest page in memory, the
page that has been in memory the longest, when a new page needs to be loaded.
How it works
Advantages
✓ Simple to implement.
Disadvantages
✓ Belady’s Anomaly - FIFO can lead to more page faults as the number of frames increases
(counterintuitive behavior).
✓ It doesn’t consider the frequency or recency of page accesses, so it can lead to inefficient page
replacements.
The LRU algorithm replaces the page that has not been used for the longest time, assuming that pages used most
recently will likely be used again soon.
How it works
✓ Each page in memory is tracked by a timestamp or counter of when it was last accessed.
✓ When a page is accessed, its timestamp is updated to the current time.
✓ When a page needs to be replaced, the page with the oldest timestamp (the least recently used page) is
replaced.
Advantages
✓ More efficient than FIFO because it takes into account how recently pages were used.
Disadvantages
✓ Requires extra overhead to maintain the order of pages, which can be costly in terms of time and space.
✓ It is not always perfect; there are situations where LRU does not perform optimally.
The Optimal page replacement algorithm replaces the page that will not be used for the longest period of time
in the future. This algorithm provides the lowest possible page fault rate, but it requires future knowledge about
page accesses.
How it works
✓ The OS looks ahead to see which page will be used furthest in the future (or not used at all).
✓ The page that will be needed the farthest in the future (or never again) is replaced.
Advantages
Disadvantages
✓ It requires future knowledge of page accesses, which is impractical and cannot be used in real-world
scenarios.
✓ It's used mainly as a benchmark to compare the performance of other algorithms.
4. Clock (Second-Chance)
The Clock algorithm is a more practical approximation of LRU. It uses a circular queue (like a clock) and gives
each page a "second chance" before replacing it.
How it works
✓ Pages in memory are arranged in a circular queue with a reference bit associated with each page.
✓ When a page is accessed, its reference bit is set to 1.
✓ When a page needs to be replaced, the algorithm checks the pages in the queue:
✓ The clock hands move one step at a time, checking pages for replacement.
Advantages
Disadvantages
The LFU algorithm replaces the page that is least frequently used, i.e., the page that has been accessed the fewest
times in the past.
How it works
✓ A counter is maintained for each page, which tracks how many times it has been accessed.
✓ When a page is accessed, its counter is incremented.
✓ When a page fault occurs and a replacement is needed, the page with the lowest access counter is
replaced.
Advantages
Disadvantages:
✓ It requires maintaining access counters, which can be inefficient in terms of both space and time.
✓ Pages that are used frequently initially but then become less relevant are not replaced as quickly.
In practice, exact implementation of LRU can be expensive because it requires maintaining the order of accesses
for each page. Some approximations are made to reduce the overhead of tracking exact access history.
How it works
✓ Approximations of LRU use algorithms like Clock or Aging, which reduce the complexity of
implementing LRU while still providing decent performance.
Comparison of Algorithms
Can be effective, but may fail with High (requires access Good for systems with stable
LFU
bursty access patterns counting) access patterns
Optimal Device Access - Devices like hard drives, printers, and network interfaces are shared by multiple
processes. Without proper I/O management, multiple processes might try to access these devices simultaneously,
leading to conflicts, inefficiencies, and delays
Maximizing Throughput - I/O operations (e.g., disk reads/writes, network communication) can be slow, so
managing how data is transferred to and from devices can help in maximizing throughput.
2. Improved Performance
Device Scheduling - Device scheduling algorithms like First-Come-First-Serve (FCFS), Shortest Seek Time
First (SSTF), and SCAN help prioritize and order I/O requests. By minimizing seek times, wait times, and
optimizing access to devices, the OS enhances the overall performance of the system.
DMA (Direct Memory Access) - With DMA, data can be transferred directly between memory and devices
without involving the CPU, freeing up the CPU for other tasks and reducing bottlenecks. This improves system
performance, especially in I/O-intensive
3. Device Independence
Abstraction Layer - Device I/O management provides an abstraction layer between hardware devices and user
programs, enabling them to access devices without needing to know the specifics of the device hardware.
Uniform Access - The OS ensures uniform access to devices, meaning that different types of devices (disk,
keyboard, printer, etc.) are all handled in a consistent manner.
I/O Bound Processes - Many applications are I/O bound, meaning that they spend most of their time waiting
for I/O operations to complete (e.g., waiting for data from a disk or network). Effective I/O management helps
reduce the time these processes spend waiting for device access, thus improving their overall execution time and
responsiveness.
Concurrency - By efficiently managing multiple I/O operations, the OS can allow multiple processes to access
devices concurrently, even when some devices are slower or have limited bandwidth. This enables efficient
parallel execution, preventing bottlenecks and delays.
Reliability - Devices are prone to errors, such as hardware failures, read/write errors, and communication
breakdowns. Effective I/O management helps detect these errors, handle them appropriately, and, in some cases,
recover from failures without disrupting the entire system
Graceful Degradation - If a device fails or becomes unavailable, I/O management allows the system to continue
operating by managing the failure and notifying the user without crashing the entire system.
Permission Management - I/O management helps enforce access control policies, ensuring that only authorized
processes or users can access certain devices.
Audit Trails - I/O management also provides the capability to log and monitor I/O activities, which is useful for
auditing purposes and for detecting suspicious or unauthorized access to devices.
1. Device Scheduling
Scheduling determines the order in which requests to the I/O devices are processed. Because multiple processes
can request access to I/O devices simultaneously, the operating system must manage these requests effectively.
2. Device Drivers
A device driver is a specialized program that allows the operating system to communicate with specific
hardware devices. It provides an interface between the OS and the hardware.
3. Buffering
Buffering is the process of storing data in memory temporarily before it is transferred to the device or from the
device.
4. Spooling
Spooling is a technique where data is temporarily stored in a queue (usually in memory or on disk) before being
processed by the device. It is commonly used for devices like printers where the data is queued up to be printed
sequentially.
5. Error Handling
The operating system must detect and handle errors that may occur during I/O operations, such as hardware
failures, disk errors, or communication problems.
The OS provides an I/O scheduler to handle requests that come from multiple processes. Requests are managed
in a queue and served based on scheduling policies and priority.
7. Access Control
The OS ensures that only authorized processes or users can access certain devices or I/O operations.
1. Block Devices
These devices store data in fixed-size blocks (e.g., hard drives, SSDs). The OS manages the data in blocks,
allowing for efficient random access to data.
2. Character Devices
These devices transmit data one character at a time, typically with no buffering and sequential access.
3. Network Devices
Network interfaces enable communication over a network. The OS manages network traffic, including sending
and receiving data over Ethernet or Wi-Fi.
4. Hybrid Devices
Some devices combine features of both block and character devices. For example, some modern storage devices
like flash drives or optical drives may have characteristics of both.
1. Device Communication
refer to the various techniques used by an operating system to transfer data between the computer system
(CPU, memory) and I/O devices (such as hard drives, printers, keyboards, etc.). These methods determine how
data is sent to and received from devices.
In Programmed I/O, the CPU directly controls the data transfer between the system and the device. The CPU
sends commands to the device and waits for the device to complete the operation.
Process
✓ The CPU continuously checks (polls) the device to see if it's ready for data transfer.
✓ The data is transferred one word (or byte) at a time.
✓ This method is simple but inefficient, as it requires continuous CPU involvement.
2. Interrupt-Driven I/O
In this method, the device notifies the CPU when it is ready to transfer data by generating an interrupt. The
CPU can perform other tasks and only gets interrupted when the device needs attention.
Process
✓ The device sends an interrupt signal to the CPU when it is ready for data transfer.
✓ The CPU then handles the interrupt, performs the data transfer, and resumes its previous task.
Advantage
It frees up the CPU to perform other tasks while waiting for I/O operations to complete.
DMA allows certain devices to directly access system memory without involving the CPU, significantly
improving the efficiency of data transfer.
Process
✓ The device communicates with the DMA controller, which manages the data transfer directly between
memory and the device.
✓ The CPU initiates the transfer, but the DMA controller performs the actual data transfer without
further CPU intervention.
✓ DMA transfers large blocks of data more efficiently than Programmed I/O and Interrupt-Driven I/O.
Advantage
Reduces CPU load and increases data transfer speed, especially for large data sets.
4. Memory-Mapped I/O
In Memory-Mapped I/O, devices are treated as if they are part of the computer’s memory space. Instead of
using special I/O instructions, the CPU uses normal memory instructions to read and write data to and from I/O
devices.
Process
Advantage
✓ Allows for faster communication, as it uses the standard memory access mechanisms and can take
advantage of the system's cache.
Serial Communication:
In serial communication, data is transmitted one bit at a time over a single communication line.
Advantage
Parallel Communication:
In parallel communication, multiple bits are transmitted simultaneously over multiple communication lines.
Advantage
Faster data transfer over short distances (but limited by the number of lines and signal integrity over long
distances).
6. Bus-Based Communication
This is a method where multiple devices share a common communication path (or bus) to exchange data. The
system’s CPU, memory, and devices are connected to a bus, which carries data between them.
Process
✓ Data is transferred to and from devices using the bus, with devices and the CPU taking turns to send
and receive data.
Advantage
It allows multiple devices to communicate with each other, but can become a bottleneck if multiple devices try
to access the bus simultaneously.
Synchronous Communication:
In synchronous communication, data is transferred between devices in sync with a clock signal. Both the
sender and receiver must be synchronized in time for proper data transfer.
Advantage
Faster data transfer with low latency, as both devices are synchronized.
Asynchronous Communication:
In asynchronous communication, data is transferred without synchronization between sender and receiver.
Each byte of data is sent with start and stop bits, allowing the receiver to detect the beginning and end of data
transmission.
Advantage
Simpler and more flexible but slower due to the overhead of start and stop bits.
Ports are the physical interfaces that allow data to pass between the system and external devices. They can be
used for input or output operations. Common types of I/O ports include USB, HDMI, Ethernet, and audio
jacks.
The interface defines the signaling standards, electrical characteristics, and protocols used to communicate.
Interfaces like PCIe (Peripheral Component Interconnect Express), SATA (Serial ATA), and Thunderbolt
ensure that data can be transferred correctly and at the desired speed.
To transfer data between devices and memory, different mechanisms can be used:
✓ Programmed I/O (PIO): In this method, the CPU is directly involved in the data transfer. The CPU
must execute specific instructions to read or write data to/from devices. It's simple but inefficient because
the CPU is heavily involved, resulting in high CPU usage.
✓ Interrupt-Driven I/O: The CPU does not constantly poll the I/O device. Instead, when a device is ready
for data transfer, it sends an interrupt to the CPU, which suspends its current task and handles the
interrupt. This is more efficient than PIO because the CPU does not waste time checking for device
readiness.
✓ Direct Memory Access (DMA): DMA allows I/O devices to directly transfer data to and from memory
without CPU intervention. The DMA controller manages the data transfer, significantly improving
efficiency by freeing up the CPU for other tasks.
4. Data Buffers
Buffers are memory areas used to temporarily store data before it is transferred between devices or between a
device and memory. Buffers help smooth out the data flow and prevent the system from being overwhelmed by
continuous I/O operations.
For instance, write buffers temporarily hold data before writing it to disk, while input buffers store data
received from a device until it can be processed by the CPU.
Devices often operate at different speeds, so synchronization mechanisms are essential to ensure that data is
transferred correctly. A clock controls the timing of data transmission to ensure proper data synchronization
between the CPU and peripherals.
Handshaking is a technique used in parallel or serial communication to indicate when the sender and receiver
are ready to exchange data. This ensures proper coordination and prevents data corruption or loss.
Error detection mechanisms are vital to identify and handle errors that might occur during I/O operations. Some
common error detection techniques are;
✓ Parity Bit A single bit added to a binary data sequence to help detect errors by ensuring the total number
of 1s in the sequence is either even (even parity) or odd (odd parity).
✓ Checksum A value calculated from a set of data and appended to it, used to detect errors during
transmission by comparing the computed checksum at the receiver with the original checksum.
✓ Cyclic Redundancy Check (CRC) An error-detection technique that uses polynomial division to
generate a checksum (CRC code) appended to data, allowing the receiver to detect errors by performing
the same division and checking for discrepancies.
Error correction can be used to automatically fix errors in data transfer. For example, ECC (Error Correcting
Code) memory can detect and correct single-bit errors.
7. Bus Architecture
Buses are the communication pathways that transfer data between the CPU and I/O devices. The bus is
responsible for carrying data, control signals, and power between the components. Common bus architectures
include:
✓ System Bus - Connects the CPU, memory, and other components on the motherboard.
✓ Expansion Bus - Connects additional devices like network cards or sound cards (e.g., PCI, PCIe).
The bus uses a shared architecture to transmit data to multiple devices, but bus contention can occur when
multiple devices try to use the bus at the same time. This is why efficient bus arbitration is needed.
8. Multiplexing
Multiplexing is a technique where multiple devices share the same communication channel by sending data in
a controlled, alternating manner. It can be used to send multiple data streams over a single channel, improving
the efficiency of the system.
✓ Time Division Multiplexing (TDM): Allocates specific time slots to different devices for data
transmission.
✓ Frequency Division Multiplexing (FDM): Divides the available frequency spectrum into multiple
channels, allowing devices to transmit in parallel without interference.
9. Device Controllers
Device controllers are specialized hardware components that manage the operation of I/O devices. They serve
as intermediaries between the CPU and the device, ensuring that data is transferred in the correct format and
timing.
Many I/O devices, especially those on portable devices, require power management to reduce energy
consumption. I/O hardware must support power-saving modes (such as sleep or standby) and efficiently manage
the activation/deactivation of devices to ensure optimal battery life.
Each I/O device is typically assigned a unique address on the system to distinguish it from other devices. This
can be done using a bus address (for devices connected via a bus, such as PCIe) or a memory-mapped address
(where I/O devices appear as part of the system’s memory).
Proper addressing allows the CPU to communicate with the correct device. I/O ports are often used for simple
devices like keyboards, mice, and printers, while more complex devices use more sophisticated addressing
schemes.
Interrupts are signals sent by I/O devices to the CPU to indicate that they require attention. Interrupt handling
ensures that the CPU can handle multiple I/O requests efficiently without continuously polling the devices.
Interrupt prioritization is critical, especially in systems with multiple I/O devices. The OS typically uses an
interrupt vector to identify the appropriate interrupt service routine (ISR) for each device.
Device Controllers
A device controller is a crucial piece of hardware responsible for managing communication between the
operating system (OS) and peripheral devices (like hard drives, printers, network cards, etc.). It acts as an
intermediary that handles data transfer, timing, error checking, and other functions required to interface the CPU
with I/O devices.
The controller is responsible for facilitating data transfer between the I/O device (e.g., hard disk, keyboard,
printer) and the computer's memory. This may involve managing data buffers, initiating read/write operations,
and ensuring proper sequencing of data.
2. Command Interpretation:
The controller receives commands from the CPU or OS and translates them into low-level instructions that the
device can understand. For example, a disk controller receives a request to read data from a specific sector and
translates that into a format the hard drive understands.
3. Buffer Management:
Controllers typically include small buffers (temporary storage areas) that hold data before it’s written to or read
from the device. Buffers are crucial in handling differences in data transfer rates between the CPU and the device.
Device controllers often implement error-checking mechanisms, such as Parity Bits or Cyclic Redundancy
Checks (CRC), to detect data corruption during transfers. If an error is detected, the controller may initiate a
recovery process, such as retrying the operation, or it might inform the OS so corrective action can be taken.
5. Interrupt Generation:
When a device completes a task (e.g., reading data or printing a page), the device controller sends an interrupt
signal to the CPU to notify it that the task has been completed. This allows the CPU to pause its current operations
and process the interrupt, which may involve reading data from the device or starting another task.
The controller also monitors the device's operational status, including whether it's ready to send or receive data.
There are various types of device controllers, depending on the type of device they manage. Some of the common
ones include:
1. Disk Controllers
These controllers manage hard disk drives (HDDs), solid-state drives (SSDs), and other storage devices.
They handle low-level disk operations like reading and writing data to the disk, managing disk buffers, and
executing disk scheduling algorithms.
NIC controllers manage data transmission between the computer and network devices (e.g., Ethernet cards or
wireless network adapters).
These controllers manage the framing of network packets, transmission and reception of data, and error detection
(such as checksums).
These controllers manage the data flow between the CPU and the GPU, which is responsible for rendering
images and video.
They handle memory management, display output, and facilitate operations like rendering 3D graphics or video
decoding.
4. Printer Controllers
These controllers’ interface with printers to manage the print queue, data formatting, and communication
between the computer and the printer.
They may also provide features like error detection for paper jams or low ink levels.
These controllers are responsible for managing data from input devices such as keyboards, mice, and
touchscreens.
They interpret signals from the input devices and send them to the CPU for further processing.
6. Audio Controllers
Audio controllers manage the data flow between the system's memory and audio output devices like speakers or
headphones.
These controllers are responsible for ensuring proper sound playback and input (e.g., from a microphone).
1. Initiating a Request
✓ When the OS or CPU needs to communicate with a device (e.g., reading a file from a disk), it sends a
command to the appropriate device controller.
✓ The controller takes the command and prepares the necessary hardware to perform the action (e.g.,
positioning a disk head, opening a network socket).
2. Data Transfer
✓ Once the device controller is ready, it manages the transfer of data. If Direct Memory Access (DMA) is
being used, the controller will coordinate the transfer of data directly to or from the system's memory,
bypassing the CPU.
✓ If the controller uses the CPU for data transfer, it will request the CPU’s attention to read or write data.
✓ After the data transfer is completed (or if an error occurs), the device controller sends an interrupt to the
CPU to signal that the operation has finished or that there’s an issue that needs attention.
✓ The CPU then checks the status of the operation and may initiate further actions (e.g., notifying the user
or retrying the operation).
Device controllers play a significant role in overall system performance, as they directly impact:
1. Throughput: Efficient controllers allow faster data transfer, increasing the throughput of devices like
storage drives, network adapters, and GPUs.
2. Latency: Good controllers reduce the delay between the initiation of a request and the completion of a
task, improving system responsiveness.
3. CPU Utilization: By offloading tasks such as data transfer (using DMA), controllers can reduce CPU
load, allowing the CPU to perform other tasks more efficiently.
Communication with Input/output (I/O) devices is a critical part of any computer system. I/O devices allow the
system to interact with the external world, enabling operations like reading user input, displaying output,
transferring files, and network communication. The way in which a computer communicates with these devices
is determined by several methods and protocols, depending on the nature of the device and the hardware interface
it uses.
Below are the main ways communication occurs between the system and I/O devices:
Programmed I/O is a method where the CPU is directly involved in data transfer between an I/O device and
memory. In this model, the CPU sends commands to the device, waits for the device to be ready, and then
manually handles the transfer of data.
How it Works
✓ The CPU sends a command to the device controller to perform an I/O operation (e.g., read or write data).
✓ The CPU waits for the device to be ready (often by polling the device).
✓ Once the device is ready, the CPU moves data between memory and the device, byte by byte or word
by word.
✓ The CPU continually checks for completion or status updates through a process known as polling.
2. Interrupt-Driven I/O
In interrupt-driven I/O, instead of the CPU continually polling the I/O device, the device sends an interrupt
signal to the CPU when it is ready for communication. The CPU can then suspend its current tasks to handle the
I/O request.
How it Works
✓ The I/O device sends an interrupt to the CPU when it needs attention (e.g., the device is ready to send
or receive data).
✓ The CPU stops its current execution and jumps to an interrupt service routine (ISR), which processes
the I/O request (e.g., reads the input data or sends output).
✓ After handling the interrupt, the CPU resumes its normal operations.
✓ The interrupt service routine often includes checking the status of the device, reading/writing data, and
clearing the interrupt flag.
Direct Memory Access (DMA) is an advanced technique where an external controller manages data transfer
directly between I/O devices and memory, without involving the CPU. This method is used to improve
performance by offloading the data transfer work from the CPU.
How it Works
✓ The CPU issues a command to the DMA controller, specifying the source, destination, and the number
of bytes to be transferred.
✓ The DMA controller takes over the task of transferring data between the device and memory.
✓ Once the DMA transfer is complete, the DMA controller sends an interrupt to the CPU to notify it that
the operation has finished.
✓ This allows the CPU to perform other tasks while the data is being transferred.
In Memory-Mapped I/O, devices are assigned specific memory addresses, and the CPU communicates with
I/O devices as though they were part of the system's memory. Instead of using separate I/O instructions, the CPU
accesses I/O devices through regular memory access instructions.
How it Works
In modern systems, most devices communicate via buses, which are shared communication pathways that allow
data to flow between the CPU, memory, and I/O devices. Examples of buses include PCIe (Peripheral
Component Interconnect Express), USB (Universal Serial Bus), SATA (Serial ATA), and Thunderbolt.
How it Works
✓ Devices connected to a bus can send data to or receive data from the CPU or memory. The bus handles
the physical communication, while the controller and CPU manage the logic.
✓ The CPU may initiate a request to access data from a device on the bus, or the device may initiate
communication through interrupts.
✓ Bus communication often relies on bus arbitration, a mechanism that ensures only one device
communicates with the CPU at a time.
The communication protocols are critical for ensuring that data sent between the CPU and I/O devices is
properly formatted, transmitted, and received. Some common I/O communication protocols include:
✓ Serial Protocols (e.g., RS-232, USB): Used for serial data transmission, where data is sent one bit at a
time over a single communication channel.
✓ Parallel Protocols (e.g., IEEE 1284 for printers): Used for sending multiple bits simultaneously over
multiple channels (older systems).
✓ Network Protocols (e.g., TCP/IP): Used for communication over networks (e.g., Ethernet, Wi-Fi).
✓ Storage Protocols (e.g., SATA, SCSI): Specialized protocols for connecting storage devices and
managing data transfers.
Direct Memory Access (DMA) is a system feature that allows peripherals (like disk drives, network cards, sound
cards, etc.) to communicate directly with the computer's memory without involving the CPU for each byte of
data transfer. This enables faster data transfers and reduces CPU overhead, allowing the CPU to focus on other
tasks while I/O operations take place in parallel.
The general process of how DMA works involves the following steps:
1. CPU Setup
The CPU initiates the DMA operation by providing the DMA controller with details about the data transfer. This
includes the memory address (source or destination), the number of bytes to transfer, and the direction (from
memory to device or vice versa).
2. DMA Controller
Once set up, the DMA controller takes over the data transfer task. It will handle all of the necessary data
movement without further CPU intervention.
The DMA controller is responsible for transferring data between the I/O device and the main memory directly.
It might involve using system buses like the memory bus to move data.
3. Data Transfer
The DMA controller controls the bus and directly moves data between the device and memory. Depending on
the system's architecture, the DMA controller might use block transfers, cycle stealing, or burst transfers (see
below).
4. Interrupt to CPU
Once the transfer is complete, the DMA controller sends an interrupt signal to the CPU, notifying it that the
operation has finished, and the data is ready for further processing.
5. Completion
The CPU, upon receiving the interrupt, checks the status and processes the data in memory as required.
There are several modes of DMA operation, which determine how the DMA controller interacts with the CPU
and memory:
1. Burst Mode
In this mode, the DMA controller gains exclusive control of the system bus and transfers all the data in one
continuous burst. The DMA controller transfers data without letting the CPU access the system bus until the
entire block of data has been transferred.
2. Cycle Stealing
In cycle stealing, the DMA controller only steals the bus for one cycle to transfer a single piece of data at a time
(usually one byte or one word). After each transfer, the CPU regains control of the bus for a cycle.
3. Block Mode:
In block mode, the DMA controller transfers data in larger blocks, but it allows the CPU to retain control of the
bus for other operations once the block transfer is completed. It strikes a balance between burst and cycle stealing.
A DMA Controller (DMAC) is a special hardware component that manages the DMA process. It consists of the
following components:
1. DMA Channels
A DMA controller can manage multiple DMA channels, allowing it to handle simultaneous transfers to and
from different devices.
2. Address Registers
These registers hold the source and destination addresses for data transfer. They specify where the data should
be read from and where it should be written to.
3. Counter Registers:
These registers hold the count of how many bytes or words need to be transferred during the DMA operation.
4. Control Register:
The control register defines the specific DMA operation to be performed, such as the direction of data transfer
(read or write), transfer mode (burst, cycle stealing, or block), and the triggering mechanism.
5. Status Registers:
These registers provide feedback to the CPU, indicating whether the DMA operation is in progress, completed,
or if an error occurred during the transfer.
Advantages of DMA
✓ Reduced CPU Overhead: DMA frees the CPU from manually moving data, allowing it to perform
other tasks, thus improving system efficiency.
✓ Faster Data Transfers: Since the DMA controller handles the data transfer directly, it is much faster
than having the CPU handle each byte individually.
✓ Efficient Use of System Resources: DMA allows better use of system resources by offloading the
memory and I/O data transfer to dedicated hardware.
Disadvantages of DMA
✓ Complexity: DMA systems introduce additional complexity to the computer’s architecture and require
DMA controllers and specialized hardware.
✓ Potential for Conflicts: Since the DMA controller can take control of the system bus, conflicts can arise
if multiple devices need to access the bus at the same time. Bus arbitration is needed to resolve such
conflicts.
✓ Limited by Bus Bandwidth: The speed of DMA transfers is constrained by the bandwidth of the
system's bus. If multiple devices share the bus, this can become a bottleneck.
Polling and interrupts are two primary methods used for communication between a computer’s central
processing unit (CPU) and its input/output (I/O) devices. Both methods are designed to manage the transfer
of data between the CPU and peripheral devices, but they differ significantly in how they handle the
communication process. Below, we explore both concepts in detail.
Polling
Polling is a method where the CPU actively checks (or "polls") I/O devices at regular intervals to see if they
require attention or are ready for data transfer. It involves the CPU repeatedly querying the status of an I/O
device to determine whether an operation is complete or if the device is ready to send or receive data.
1. The CPU checks the status of a device, such as whether it is ready to send data or if there is data available
to read.
2. The CPU repeats this process at fixed intervals, continuously checking the device’s status.
3. If the device is ready for data transfer (e.g., data is available to read or space is available to write), the
CPU proceeds with the I/O operation.
4. If the device is not ready, the CPU goes back to checking the device status in the next polling cycle.
Interrupts
Interrupts are a method where the I/O device sends a signal (known as an interrupt) to the CPU when it requires
attention. The CPU responds by temporarily halting its current execution, saving its state, and jumping to an
Interrupt Service Routine (ISR), a special routine designed to handle the device's request. Once the interrupt
is handled, the CPU resumes its previous tasks.
1. The device signals the CPU by sending an interrupt request (IRQ) when it needs attention (e.g., when
new data is available, or a task is completed).
2. The CPU stops executing its current instructions and temporarily stores its state (this process is called
context saving).
3. The CPU then jumps to the Interrupt Service Routine (ISR), which is a special piece of code dedicated
to handling the interrupt.
4. The ISR processes the interrupt, communicates with the device (e.g., reading data or acknowledging the
completion of a task), and may update the device’s status.
5. After handling the interrupt, the CPU restores its previous state and continues execution from where it
left off.
Simpler to implement, but inefficient in terms of CPU More complex, requiring interrupt handling and
Complexity
utilization. context switching.
Aspect Polling Interrupts
Power Less efficient, as polling consumes power even when no More power-efficient, as the CPU is not
Efficiency data transfer is needed. constantly active.
Suitability Good for simple, low-priority tasks. Better for time-sensitive or real-time systems.
Polling is best suited for scenarios where the device operates at a predictable and low frequency, and the CPU
can afford to check for updates regularly without significant performance degradation.
Interrupts are more appropriate for time-sensitive operations where immediate action is required when a device
needs attention.
Hybrid Approach
Polling + Interrupts
In many systems, polling and interrupts are used together to balance efficiency and responsiveness.
For example;
A device may be polled for status updates at regular intervals when it’s known that it’s unlikely to need attention.
However, the device may also be configured to interrupt the CPU for more urgent events, allowing the CPU
to handle critical requests immediately.
This hybrid approach can optimize performance by using polling when the CPU has spare cycles and interrupts
when a more immediate response is required.
I/O (Input/Output) software is responsible for managing and controlling data transfer between the computer's
CPU and its peripherals (such as hard drives, keyboards, mice, printers, etc.). The goal of I/O software is to make
communication with hardware devices more efficient, transparent to users, and easy to manage from a software
perspective. It abstracts the underlying hardware complexities and provides a simpler interface for the system
and applications to interact with I/O devices.
Here are the principles of I/O software that guide its development and implementation;
1. Abstraction
Abstraction is a core principle in I/O software, as it hides the low-level hardware details and provides a higher-
level, more user-friendly interface.
✓ Device Independence I/O software ensures that users and applications interact with devices without
needing to understand the details of how the devices function.
✓ Uniform Interface: I/O software provides uniform access to devices through a common set of APIs or
system calls, even though devices may have different hardware characteristics and protocols.
2. Efficiency
Efficiency in I/O software involves ensuring that data transfers are fast, that resources (especially CPU and
memory) are used optimally, and that the overall performance of the system is not hindered by I/O operations.
✓ Minimize CPU Involvement: As much as possible, I/O operations should be designed to minimize CPU
intervention. Techniques like Direct Memory Access (DMA) allow peripherals to transfer data to
memory without involving the CPU, increasing system performance.
✓ Buffering: Buffering involves temporarily storing data in a memory area (buffer) before it is transferred
to or from a device. Buffering minimizes I/O wait times and allows for more efficient use of system
resources, like CPU and memory.
✓ Caching: Caching stores frequently used data in faster, more readily accessible memory (like CPU cache
or RAM), reducing the time required for repeated I/O operations. Caching is common in disk I/O (e.g.,
disk cache) and network communication.
In modern systems, I/O devices may need to handle multiple requests from different processes concurrently.
Proper synchronization and management of concurrent I/O operations are crucial to ensure that resources are
allocated fairly and efficiently.
✓ Concurrency Control: The I/O software needs to manage the simultaneous requests of multiple
processes that might access the same device. This includes ensuring that devices are not used by multiple
processes simultaneously unless supported.
✓ Synchronization Mechanisms: Techniques like semaphores, mutexes, or locks are used to synchronize
access to shared I/O devices, preventing race conditions (where two processes try to access the same
resource at the same time) and ensuring data integrity.
Effective error handling and recovery are essential principles of I/O software. Devices may malfunction,
connections may be interrupted, or data may be corrupted during transmission. I/O software needs to detect,
report, and handle errors gracefully without crashing the entire system.
Error Detection - The system must be able to identify when something goes wrong during an I/O operation.
This can include detecting errors like data corruption, hardware failure, or communication breakdowns.
Error Reporting - When an error is detected, I/O software should report it back to the user or the system, often
through appropriate error codes or messages.
Error Recovery - I/O software should attempt to recover from errors automatically, if possible, or provide
mechanisms for users or administrators to address the issue. This may involve retrying operations, rolling back
changes, or offering alternative solutions.
5. Device Independence
Device independence refers to the ability of the I/O software to provide a uniform interface to devices, regardless
of the specific hardware involved.
✓ Abstracting Hardware Details - The OS should allow applications to perform I/O operations without
worrying about the specific device being used (e.g., hard disk, USB drive, or network printer). This is
achieved through the use of device drivers, which provide a standard interface between the OS and the
hardware.
✓ Device Drivers - Device drivers are specialized software components that communicate directly with
hardware devices. They translate generic system calls from applications into device-specific instructions
and vice versa.
I/O software must support multitasking (executing multiple tasks or processes simultaneously) and
multiprogramming (running multiple programs concurrently). These capabilities are essential for improving
the system's responsiveness and throughput.
✓ Non-blocking, I/O - In multitasking environments, I/O software must provide mechanisms for non-
blocking operations, where the CPU does not need to wait for an I/O operation to complete before
proceeding with other tasks.
✓ Asynchronous I/O - I/O operations can be performed asynchronously, allowing processes to initiate
I/O and continue executing other code without waiting for the I/O to finish. Once the I/O completes, the
system informs the process (often via a callback or interrupt).
I/O software also plays a crucial role in ensuring that access to devices and resources is properly controlled and
secure.
✓ Authentication and Authorization - The I/O software should verify whether the requesting user or
process is authorized to perform the requested I/O operation.
✓ Access Control - Security mechanisms ensure that only authorized applications or users can read or
write to certain devices or files.
✓ Data Encryption - For sensitive data, I/O software may employ encryption techniques to ensure that
data is secure during transfer, particularly in network communications.
8. Buffer Management
Buffer management is a technique used to temporarily store data in memory during I/O operations. This helps
smooth out the differences in speed between the CPU, memory, and I/O devices, preventing the system from
becoming bottlenecked by slow peripherals.
✓ Double Buffering - This technique involves using two buffers, one for reading data and one for writing
data, allowing for continuous data transfer without interruption.
✓ Ring Buffers - A ring buffer is a circular queue that efficiently manages continuous streams of data,
commonly used in streaming media or real-time systems.
Disk Clocks
In the context of disk, I/O systems, disk clocks refer to the mechanisms that control and synchronize the
operations of a disk drive, specifically the timing of the various components involved in reading from and
writing to a disk. These components include the disk platters, read/write heads, and disk controller. Disk
clocks ensure that the movement of the read/write head, the rotation of the disk, and the execution of I/O
operations are coordinated to achieve efficient and correct data access.
The disk is composed of spinning platters, which rotate at a constant speed (typically measured in RPMs –
Revolutions Per Minute).
Disk clocks help manage the timing of the rotational latency, which refers to the time it takes for the desired
disk sector to come under the read/write head after the disk has rotated.
2. Seek Time
The time it takes for the read/write head to move to the track containing the data you want to access is known
as seek time.
Disk clocks help manage the movement of the head to ensure the proper track is aligned with the head, facilitating
efficient data retrieval.
Once the head is in position and the required sector is under the head, data transfer occurs, which is controlled
by disk clocks.
The transfer rate depends on the data rate of the disk (how quickly data can be read or written from the disk)
and the speed of the disk’s rotation.
1. Rotational Latency
Rotational latency refers to the time it takes for the disk platter to rotate and bring the required sector under the
read/write head. Disk clocks help manage and synchronize this timing.
The average rotational latency for a disk can be calculated as half the time taken for one full rotation (since, on
average, the desired sector will be halfway around the disk).
2. Seek Time
The seek time is the time it takes for the read/write head to move to the correct track. It involves mechanical
movement and is influenced by the clock that controls the head’s positioning.
Disk clocks are responsible for coordinating this movement, ensuring that the head is moved in a manner that
minimizes the overall time required to locate the data.
The data transfer rate is the speed at which data is read or written to the disk once the head is in position.
Disk clocks help control the flow of data between the disk and the system, ensuring that the data transfer is
carried out at the optimal rate without overloading the system.
4. Access Time
The total access time for a disk I/O operation is the sum of the seek time and the rotational latency. The disk
clock plays a role in both of these components, ensuring efficient access time by synchronizing the movement
and timing of disk components.
5. Synchronization
Disk clocks also synchronize the interaction between the disk controller, the platters, and the read/write heads.
This ensures that the data is read from or written to the correct location on the disk at the correct time, preventing
errors and improving efficiency.
The performance of a disk system is heavily influenced by the timing provided by disk clocks. Efficient timing
allows the disk to minimize seek time and rotational latency, leading to faster data access.
Disk clocks also ensure that the data transfer happens in sync with the disk’s rotational speed, allowing for
continuous, uninterrupted reading and writing of data.
Traditional hard disk drives (HDDs) use spinning magnetic disks and rely on precise timing mechanisms to
control the rotational speed and seek operations.
The disk clock in HDDs ensures that the disk platters spin at a constant rate and the read/write heads move to
the appropriate tracks to read/write data.
SSDs also rely on internal clocks to coordinate data placement, garbage collection, wear leveling, and other
operations that manage data storage within NAND flash memory.
In computing, terminals and virtual devices are fundamental concepts that involve how users interact with a
computer system and how software or hardware manages those interactions. Here’s an overview of both:
1. Terminals
A terminal is an input/output (I/O) device that allows users to interact with a computer system. Originally,
terminals were physical devices, but with modern systems, they are often emulated as software, allowing for
communication between users and computers.
Types of Terminals
1. Physical Terminals
These were hardware devices with a keyboard (for input) and a display (for output). Terminals were often used
to access mainframe or minicomputer systems before the advent of personal computers.
Dumb Terminals - Simple devices with no processing power; they could only send user input to the mainframe
and display output from the mainframe. They relied completely on the mainframe's processing power.
Smart Terminals - These terminals had minimal processing power and could perform some local processing,
like handling certain control commands or formatting.
Modern terminals are virtual (emulated by software) and allow users to interact with the system through text-
based interfaces, like command-line interfaces (CLI) or shells.
Virtual terminals exist on operating systems like Unix, Linux, and Windows. For example, Xterm (on Unix-
based systems) or Console/Command Prompt (on Windows) are examples of software emulating terminal
functionality.
Graphical Terminals - While traditional terminals were text-based, virtual terminals can now also support basic
graphics and mouse input in some environments.
Functions of a Terminal
✓ Input - The terminal allows users to input commands, which are sent to the computer for execution.
✓ Output - The terminal displays the output generated by the system, such as the results of commands or
system messages.
✓ Interaction with Operating System - Terminals allow users to interact with the operating system
through command-line tools, text editors, file manipulation utilities, etc.
2. Virtual Devices
A virtual device is a software abstraction that behaves like a physical device but does not correspond to actual
hardware. Virtual devices are often used to provide functionality that mimics real-world devices (e.g., printers,
disks, network devices), but they operate in a virtualized environment.
Virtual devices are used in virtualization, cloud computing, and emulation scenarios, where you need to
simulate the behavior of hardware without having physical devices connected to the system.
✓ Emulation of Hardware - Virtual devices emulate the behavior of physical devices, such as hard drives,
printers, network cards, etc. They provide an interface to software as if they were real hardware, even
though no physical hardware is involved.
✓ Software Management - Virtual devices are managed by software, often within a hypervisor (for virtual
machines) or an operating system that abstracts the real hardware. They perform tasks similar to physical
devices but entirely within the software realm.
✓ Isolation - Virtual devices allow for the isolation of system resources, such as in virtualized
environments (e.g., in cloud computing or when running virtual machines). Each virtual machine (VM)
can have its own virtual devices that behave like real hardware, but they don’t interfere with each other’s
operation.
1. Virtual Disk
A virtual disk is a software-emulated disk that appears to the system as a real hard disk, but it may exist as a
file or a set of files in a virtualized environment.
In virtual machines (VMs), a virtual disk is typically a file stored on the host machine's physical disk but
behaves like a real disk for the guest operating system running inside the VM.
A virtual network device (or virtual NIC) emulates a network card for virtual machines. It allows VMs to
communicate over a network, just as physical network interfaces allow real machines to communicate over a
network.
3. Virtual Printers
A virtual printer may simulate a physical printer. For example, PDF printers allow users to "print" a document
to a file (like a PDF) instead of a physical printer.
Virtual printers can be used for debugging, test printing, or creating digital versions of physical documents.
A virtual USB device can emulate the behavior of a physical USB device, allowing software or virtual
environments to interact with USB devices without needing the actual hardware.
In VMware or VirtualBox, virtual machines are created, each with its own virtual network interface card
(vNIC), virtual hard drive, virtual graphics adapter, and more. These virtual devices behave as if they are
real physical devices but are managed entirely by the virtualization software.
2. Cloud Environments
In cloud computing platforms like Amazon Web Services (AWS), virtual devices such as virtual machines,
virtual networks, and virtual storage are provisioned for users, but they do not correspond to physical
hardware.
A terminal is an I/O device for interacting A virtual device emulates a physical device
Definition
with the computer. via software.
Examples in Accessing a virtual machine through a Virtual devices like virtual disks, virtual
Virtualization virtual terminal (CLI). network adapters, etc., within VMs.
Disk Scheduling
Disk scheduling refers to the method by which an operating system manages the order in which disk I/O
operations are serviced. Since the disk is a mechanical device with limited speed (especially in Hard Disk Drives
(HDDs), which have moving parts), efficient scheduling of requests can improve the overall performance of a
system by minimizing delays in data access.
In particular, disk scheduling focuses on minimizing seek time (the time it takes for the disk's read/write head
to reach the correct track) and rotational latency (the time it takes for the desired disk sector to rotate under the
read/write head).
1. Seek Time: The time it takes for the read/write head to move to the correct track.
2. Rotational Latency: The time it takes for the disk platter to rotate to the correct position so that the data
is under the read/write head.
3. Data Transfer Time: The time it takes to actually read or write the data once the head is in position.
The goal of disk scheduling algorithms is to reduce the total access time (which is the sum of seek time, rotational
latency, and data transfer time).
Several algorithms are used to determine the order in which disk I/O requests are serviced. Each algorithm aims
to optimize disk performance by reducing seek time or improving throughput.
FCFS is the simplest disk scheduling algorithm. It handles requests in the order they arrive, without any
reordering.
Pros
✓ Simple to implement.
✓ Fair, as each request is handled in the order it is received.
Cons
✓ Can result in long seek times because there’s no consideration of the disk's current position or the
location of other requests.
SSTF selects the request that is closest to the current position of the disk head, minimizing the seek time for
each individual request.
Pros
✓ Generally, more efficient than FCFS, as it minimizes the movement of the disk head.
Cons
✓ Starvation: Requests far from the current head position might be delayed indefinitely if there are always
closer requests.
✓ Does not optimize rotational latency (focuses only on seek time).
In SCAN, the disk head moves in one direction, serving requests until it reaches the end of the disk. Once it
reaches the end, it reverses direction and serves requests on the return trip. It’s often compared to the motion of
an elevator.
Pros
Cons
✓ Can still result in some long waiting times for requests at the ends of the disk.
C-SCAN is a variation of SCAN where the disk head only moves in one direction, serving requests along the
way. When it reaches the end of the disk, it immediately jumps back to the beginning without serving any
requests on the return trip.
Pros
Cons
✓ Still suffers from some inefficiency in serving requests that are far from the current head position.
5. LOOK
The LOOK algorithm is similar to SCAN, but instead of scanning all the way to the end of the disk, the disk
head only moves to the farthest request in the current direction. Once it reaches that request, it reverses direction.
Pros
✓ More efficient than SCAN because it doesn't waste time traveling to the end of the disk if there are no
requests.
Cons
✓ Similar to SCAN, it can have inefficiencies in serving requests located at the farthest ends of the disk.
6. N-Step SCAN
N-Step SCAN divides the request queue into N smaller queues. The head services one queue in a single scan and
then moves to the next queue after completing the current one. This prevents the disk head from going back and
forth unnecessarily between tracks.
Pros
Cons
V-SCAN is a variation of the SCAN algorithm where the disk head’s movement is more adaptable. The
algorithm adjusts the direction of the disk head based on certain conditions, allowing it to change behavior based
on request density.
Pros
Cons
Minimizes seek time for each Starvation of requests far High-demand systems where seek
SSTF
individual request. from the head. time is critical.
Reduces starvation, more Inefficiency for requests at Systems with evenly distributed
SCAN
efficient than FCFS and SSTF. the ends of the disk. I/O requests.
More predictable than SCAN, Still inefficiency for far-off Systems where predictable
C-SCAN
avoids end inefficiency. requests. performance is important.
Algorithm Pros Cons Typical Use Case
Efficient in terms of not going Inefficiency for requests at Environments where head
LOOK
to the end if unnecessary. the ends. movement needs to be minimized.
N-Step Prevents excessive back-and- Can still have inefficiency if High-volume I/O systems with
SCAN forth movement. not well-tuned. burst traffic.
File Management
File management is an essential aspect of operating systems that involves organizing, storing, retrieving,
naming, and securing files on a storage medium like hard drives, SSDs, or network storage. The system must
efficiently manage files to ensure that data is stored in a way that can be easily accessed, updated, and maintained.
File management not only handles the physical storage of data but also provides a logical structure that allows
users and programs to access, manage, and manipulate files in a convenient manner.
1. File
A file is a collection of data stored on a storage medium. It could be text, an image, an application, or any other
type of data. Each file has a name and is identified by a file path in the file system.
2. File System
A file system is a system that controls how data is stored and retrieved. Without a file system, data would be a
large mass of bytes with no way to differentiate or access specific pieces of information. Examples: NTFS
(Windows), ext4 (Linux), HFS+ (MacOS).
3. Directory Structure
The file system often organizes files into a hierarchical directory structure (also known as folders or
directories), where directories contain files or other directories. This structure allows for the efficient
organization and quick access to data.
1. File Creation
The file management system must support creating new files, assigning names, and setting file properties (such
as access permissions and location).
2. File Access
The file system allows for the reading and writing of files. It manages how data is retrieved (sequential or random
access) and controls the methods for updating or modifying a file’s content.
3. File Naming
Each file in the system must have a unique name. The file management system supports naming conventions
and ensures that filenames do not conflict with each other in the same directory.
4. File Deletion
Files can be deleted when they are no longer needed. The file system ensures that when a file is deleted, its data
is properly removed from the storage medium.
5. File Renaming
Files can be renamed within the system, and the file management system handles the update of the file’s name
and its location.
6. File Attributes
7. File Storage:
o File management involves deciding where to store files on the physical storage medium. This
can include managing disk space, fragmentation, and file placement to optimize access speed
and storage efficiency.
8. File Security
The file system must ensure that files are securely stored and access is controlled according to permissions.
Access control lists (ACLs) and other security mechanisms help protect files from unauthorized access.
9. File Backup
A file management system may also involve creating backups of files to ensure data recovery in case of a failure
or accidental deletion.
Different operating systems and devices may use different file systems, each with specific characteristics.
FAT32 is an extended version of FAT and supports larger disks and files but is still limited compared to newer
file systems.
NTFS is the default file system for modern versions of Windows. It supports larger file sizes, better security (file
permissions, encryption), and better performance, including journaling to improve system reliability.
ext4 is the most commonly used file system for Linux. It supports larger file sizes, better performance,
journaling, and efficient data storage. It’s the successor of ext3.
HFS+ is used by macOS and provides features like journaling and case-insensitive file naming.
APFS is a newer file system introduced by Apple for macOS and iOS. It improves on HFS+ by offering better
encryption, faster file access, and better handling of SSDs.
exFAT is used for external drives, such as USB flash drives, SD cards, etc., providing compatibility across
multiple platforms and supporting large files and drives.
ZFS is a high-performance file system and logical volume manager designed by Sun Microsystems. It’s used
for large-scale storage solutions and supports high reliability, high storage capacities, and advanced features like
snapshots and data integrity checking.
These are the methods by which data is stored on the disk for easy retrieval.
✓ Contiguous Allocation - In this method, each file occupies a set of contiguous blocks on the disk. It
allows for fast access since the blocks are located next to each other, but it can cause fragmentation
over time.
✓ Linked Allocation - Files are stored in scattered blocks on the disk, with each block containing a pointer
to the next. This eliminates fragmentation but increases access time due to non-contiguous block
locations.
✓ Indexed Allocation - An index block is used to store the addresses of the file's blocks. This allows
random access to the file’s data but requires extra space for the index block.
File access can be organized into different methods to allow for efficient reading or writing of data.
✓ Sequential Access - Data is read or written in a specific sequence. Used in applications where reading
data in order is sufficient, such as in audio or video playback.
✓ Direct/Random Access - Data can be accessed in any order without following a sequence. This is used
in databases or any system where files are frequently updated.
✓ Indexed Access - A combination of direct and sequential access, often used in databases, where an index
is maintained to quickly locate data within a file.
1. Fragmentation
Internal Fragmentation occurs when fixed-size blocks of storage are allocated, but the file doesn’t completely
use the allocated block space.
External Fragmentation occurs when free storage space is scattered across the disk, causing inefficient use of
disk space and requiring more seek time to access scattered file parts.
File systems can become corrupted due to power failures, hardware malfunctions, or software bugs. Corruption
can lead to data loss or the inability to access files.
3. Access Control
Ensuring that users only have access to the files they are authorized to use is crucial for system security. This
can be complicated in multi-user systems and requires strict management of file permissions.
4. Data Consistency
The file system needs to ensure that files remain consistent, even in the event of a system crash. Features like
journaling or atomic operations ensure that changes to files are correctly written or rolled back.
In multi-user systems, file management takes on an even more critical role. It must provide:
1. Access Control - Different users may need different access rights for the same file (e.g., read, write,
execute permissions). This requires robust security mechanisms to ensure files are protected from
unauthorized access.
2. Concurrency - When multiple users or processes access the same file simultaneously, the file
management system must ensure that changes are made correctly without data corruption (e.g., using
file locks or synchronization mechanisms).
3. File Sharing - Users may need to share files, so the file system must allow efficient sharing while
maintaining access control and data integrity.
1. Efficient Storage and Retrieval - A well-managed file system ensures that files are stored in an
organized way, enabling quick and efficient retrieval.
2. Data Integrity and Security - Proper file management ensures data is protected from corruption,
unauthorized access, and accidental deletion. Security features like permissions and encryption
safeguard sensitive information.
3. Organization - Files are classified into directories (folders), helping users easily navigate and organize
data, making large volumes of information manageable.
4. Improved System Performance - Proper management minimizes fragmentation, improving access
times and reducing the time it takes to locate and read files.
5. File Sharing and Collaboration - File management supports easy access and sharing of files across
networks, promoting collaboration while ensuring appropriate access control.
6. Data Backup and Recovery - Regular backup and recovery mechanisms are a core part of file
management, ensuring important data can be restored in case of a failure.
7. Concurrency in Multi-User Systems - File management systems control access to files by multiple
users or processes, ensuring no data corruption occurs from simultaneous access.
8. Support for Large-Scale Data - Modern file systems handle large amounts of data efficiently, enabling
the system to scale as data needs grow without performance loss.
9. Troubleshooting and Maintenance - Organized files and directories make it easier for system
administrators to troubleshoot and maintain the system.
10. Version Control and History - In collaborative environments, file management can include version
control, allowing users to keep track of changes and revert to earlier versions when necessary.
File Structure
The file structure refers to the way data is organized and stored within a file. It defines how information is stored
on a storage medium (such as a hard disk, SSD, or cloud storage) and how it can be accessed or modified. The
structure of a file is important because it affects the efficiency, speed, and complexity of data access, retrieval,
and storage operations.
There are several types of file structures used in different applications, depending on the nature of the data and
how it is accessed. Below are the common file structures:
A flat file is a simple file structure that stores data in a sequential, plain-text format without any complex
organization or hierarchy. Each record is stored one after another, with minimal metadata or indexing.
Advantages
✓ Simple to implement.
✓ Easy to read and write in basic text editors.
Disadvantages
✓ Inefficient for large datasets.
✓ No built-in mechanism for indexing, searching, or retrieving specific records.
In a hierarchical file structure, data is stored in a tree-like arrangement where files are organized into
directories and subdirectories. Files can be organized logically, where one directory can contain other
directories or files. This is the most common structure used by modern operating systems, such as Windows or
Linux.
Advantages
Disadvantages
✓ Searching for files can become inefficient if the directory structure grows too large.
✓ Can become complex to maintain if not organized well.
This structure is used in databases, where files store tabular data in rows and columns. Data is structured in
relations (tables), and relationships between data elements are represented in the table design. Each record is
identified by a primary key, and indexes are used to speed up data retrieval.
Advantages
Disadvantages
In an indexed file structure, a separate index is created for faster access to data. The index contains pointers
(references) to the data records, making it easier to find specific records. Indexes can be created based on
different criteria (e.g., alphabetical, numerical), allowing for faster search operations. This structure is
commonly used in databases and file systems like NTFS.
Advantages
Disadvantages
✓ Requires additional storage for the index.
✓ Maintenance of indexes can become complex if the data is frequently updated.
In a sequential file structure, records are stored in a sequential order, typically based on a key field. Data can
only be accessed sequentially, i.e., from the beginning to the end of the file. This is often used for applications
that involve logging, backups, or simple record processing.
Advantages
Disadvantages
A hashed file structure uses a hash function to generate a unique address for each record based on a key field
(e.g., employee ID or account number). The hash function helps to locate the record directly without needing
to search through the entire file, enabling fast access. This structure is commonly used for files that require fast
lookups, such as caching systems or authentication databases.
Advantages
Disadvantages
✓ Collisions can occur when two keys produce the same hash value, requiring handling mechanisms like
chaining or open addressing.
✓ Not ideal for sequential processing.
This structure uses multiple levels of indexes to speed up access to large files. The first level of index points to
another index file, which in turn points to the actual data records. This method allows fast access to very large
files while reducing the size of the primary index.
Advantages
Disadvantages
✓ Adds complexity to the file structure and index management.
✓ Requires more storage for additional indexing.
A file system is a way of organizing, storing, and managing data on a storage device such as a hard disk, SSD,
or external storage. Implementing a file system involves designing the methods and structures that allow an
operating system (OS) to efficiently store and retrieve files. A file system is responsible for organizing data into
files and directories, tracking metadata, handling file access, and ensuring data security.
A File Control Block (FCB) is a data structure used by the operating system to store information about a file.
Each file in the system has an associated FCB, which contains metadata about the file. Attributes typically stored
in an FCB include:
✓ File name
✓ File type
✓ File size
✓ Date of creation
✓ Permissions (read/write/execute)
✓ File location (pointer to the data blocks)
The FCB is used by the operating system to track and manage the file during its lifecycle.
File allocation refers to how the operating system assigns space for files on storage devices. There are several
methods to allocate space for files:
a. Contiguous Allocation:
In contiguous allocation, each file is stored in a single, contiguous block of memory. This makes accessing files
fast, as the file’s data is located next to each other.
Advantages
Disadvantages
✓ Leads to fragmentation, making it difficult to allocate contiguous space as the disk fills up.
✓ File sizes must be known in advance, or resizing becomes difficult.
b. Linked Allocation
In linked allocation, files are stored in non-contiguous blocks, with each block pointing to the next block in the
file using a pointer.
Advantages
Disadvantages
✓ Slower access time, as the OS must follow the pointers to access the data.
✓ Difficulty in direct access to data blocks (especially for random access).
c. Indexed Allocation:
Indexed allocation uses an index block that contains pointers to all the data blocks of a file. The index block is
stored separately from the file data.
Advantages
Disadvantages
✓ The index block can become large for very large files, requiring additional overhead.
✓ Extra space is needed for the index blocks.
3. Directory Structure
The directory structure organizes files within a file system. The operating system maintains directories that
help users and programs locate files.
a. Single-Level Directory
Advantages
✓ Simple to implement.
✓ Easy for small systems or applications with minimal file organization.
Disadvantages
Each user has their own directory, and files are stored in the user’s directory.
Advantages
✓ Improved organization.
✓ Separation of users’ files.
Disadvantages
c. Hierarchical Directory
Advantages
Disadvantages
Free space management is the process of keeping track of available disk space (i.e., unused blocks) for
allocating new files. The OS needs to know which blocks on the storage device are free, so it can assign them to
new files.
a. Bit Vector
The disk is divided into blocks, and a bit vector is maintained where each bit corresponds to a block on the disk.
A "1" means the block is used, and "0" means it is free.
b. Linked List
Free blocks are organized in a linked list, and each block points to the next available block.
c. Grouping
Blocks are grouped together into clusters, and a single pointer points to the group of free blocks.
5. Access Control and Security
File systems implement access control mechanisms to ensure only authorized users can access or modify files.
Access control often involves the use of permissions.
File Permissions
Each file has an associated set of permissions (read, write, execute) that define the operations allowed for each
user.
ACLs are lists that specify what operations each user or group can perform on a file. It is a more fine-grained
control over file access compared to basic file permissions.
Step 1: Design Data Structures: Design structures for storing metadata about files (e.g., File Control Block,
directory structure, index blocks, etc.).
Step 2: Implement File Allocation Methods: Choose an allocation method (contiguous, linked, or indexed)
based on system requirements.
Step 3: Develop Directory Structure: Implement a directory structure to manage files. This can be flat,
hierarchical, or a more complex multi-level system.
Step 4: Implement Free Space Management: Develop mechanisms to track free and used space on the storage
device (e.g., bit vector, linked list).
Step 5: Handle File Operations: Implement system calls for file creation, deletion, reading, writing, and
modification.
Step 6: Implement Access Control: Integrate file permissions and security mechanisms to control file access.
Step 7: Test and Optimize: Test the file system’s performance and optimize for speed and efficiency,
particularly for large file systems.
File access techniques refer to the methods used by an operating system or application to retrieve and manipulate
data stored in files. The choice of file access technique impacts the efficiency, speed, and ease of file operations
such as reading, writing, and searching data within files. Below are the primary file access techniques used in
various systems:
1. Sequential Access
Sequential access means reading or writing files one record or byte at a time, in a linear sequence from the
beginning to the end.
2. Direct/Random Access
Direct access, also known as random access, allows the program to read or write data at any location within
the file without reading through previous data. Data can be accessed directly using a file pointer or index,
allowing for non-sequential reading or writing.
3. Indexed Access
Indexed access involves maintaining an index that maps file records to specific locations within the file. The
index makes it easier to locate and retrieve data efficiently.
4. Hashed Access
Hashed access uses a hash function to map a key to a specific location within a file. This technique is often
used in hash tables or hash-based indexing for efficient lookups. The hash function calculates the address of
the data directly, allowing fast access to specific records without searching through the file.
5. Content-Based Access
Content-based access refers to accessing files based on their content rather than their structure or order. This
approach often involves searching through the file for specific patterns or keywords.
Memory-mapped file access maps a file (or a portion of a file) directly into the virtual memory of a process.
The operating system treats the file as if it were part of the program’s memory space, allowing direct memory
access to file data.
7. Buffered Access
Buffered access involves reading or writing files in large blocks (buffers) instead of byte-by-byte or record-by-
record. The system loads a large block of data into memory, processes it, and then writes it back to the disk.
File allocation refers to the way data is stored in a file on a disk or storage device. Efficient file allocation is
essential for minimizing fragmentation, optimizing performance, and maximizing disk space utilization. The file
system uses different techniques to allocate space for files, and each has its own advantages and disadvantages
based on the specific needs of the system.
Contiguous allocation involves allocating a contiguous block of memory or disk space to a file. In this method,
a file's data blocks are stored next to each other in a continuous block, making accessing the file efficient.
How it Works
✓ The operating system allocates a series of adjacent blocks for a file when it is created.
✓ The file's size and starting location are stored in the File Control Block (FCB), and the blocks are
accessed sequentially.
2. Linked Allocation
Linked allocation involves storing file data in blocks scattered throughout the disk, with each block containing
a pointer to the next block. A linked list is formed for each file.
How it Works
✓ Each block contains a pointer to the next block of the file. The last block points to a special value (such
as NULL) to indicate the end of the file.
✓ The operating system only needs to track the address of the first block in the file (i.e., the head of the
linked list) in the File Control Block (FCB).
3. Indexed Allocation
Indexed allocation uses an index block (or table) to store the addresses of all the data blocks in a file. The index
block contains pointers to the actual file data blocks, and the file system uses this index to access the data
efficiently.
How it Works
✓ An index block is created for each file. This index contains pointers to the actual data blocks of the file.
✓ The file system uses the index block to retrieve data. If the file grows, a new index block may be created,
and pointers are updated.
Multilevel index allocation is an extension of indexed allocation where multiple levels of indexing are used for
large files. The first-level index block points to second-level index blocks, and the second-level index blocks
point to actual data blocks.
How it Works
The file system uses multiple levels of indexing to accommodate large files. For example:
The File Allocation Table (FAT) is a well-known file allocation method used by older file systems (e.g., FAT12,
FAT16, and FAT32). It combines the principles of linked allocation with a central table to map file blocks.
How it Works
✓ A FAT is a table stored on the disk that contains an entry for each disk block. Each entry in the table
points to the next block of the file. If a block is the last one, it contains a special value (e.g., 0xFFFF or
0xFFFFFFF) indicating the end of the file.
✓ The operating system uses the FAT to locate the blocks of a file.
6. Hybrid Allocation
Hybrid allocation combines the strengths of multiple allocation methods. For example, a file system might use
contiguous allocation for small files and indexed or linked allocation for larger files. This allows for better
performance in specific use cases.
How it Works
File protection and security are essential aspects of an operating system's file management system, aimed at
safeguarding data from unauthorized access, modification, corruption, or destruction. These measures ensure the
confidentiality, integrity, and availability of files and protect them from threats, both internal and external.
Below is an overview of file protection and security concepts, techniques, and strategies used in modern
systems.
1. File Protection
File protection refers to the mechanisms that prevent unauthorized users or processes from accessing, altering,
or deleting files. These mechanisms typically focus on user permissions and access controls.
Access control defines who can access a file and what operations they can perform on it (such as read, write,
execute). Some common techniques include:
1. User Authentication
Users must prove their identity before gaining access to files. This is typically done through usernames and
passwords, biometric methods, or two-factor authentication.
3. File Permissions
Unix-like Systems (Linux, macOS) use read (r), write (w), and execute (x) permissions, which are granted to
the owner, group, and others. Windows uses more granular permission settings, allowing for more specific
access control over files and directories.
RBAC assigns permissions based on a user’s role within an organization (e.g., admin, user, guest). Permissions
are granted to roles, and users inherit those permissions based on their role.
MAC enforces access restrictions based on security labels or classifications (e.g., top secret, confidential). Only
users with the appropriate clearance level can access certain files. SELinux (Security-Enhanced Linux) is an
example of an operating system that implements MAC.
B. File Locking
File locking prevents concurrent access to a file by multiple processes to avoid data corruption. A process must
obtain a lock before modifying a file and release the lock once done.
Shared locks allow multiple processes to read the file but prevent writing.
Exclusive locks prevent any other process from accessing the file while it is being modified.
Advisory locking is typically used in Unix-like systems, where a process is responsible for respecting the lock.
C. File Encryption
Encryption ensures that files are stored in a format that cannot be read without the appropriate decryption key.
It provides confidentiality by making files unreadable to unauthorized users.
✓ Symmetric encryption uses a single key for both encryption and decryption (e.g., AES).
✓ Asymmetric encryption uses a public key for encryption and a private key for decryption (e.g., RSA).
✓ File encryption is widely used in full-disk encryption (e.g., BitLocker on Windows) or for individual
file protection.
Regular backups of critical files are an essential protection measure. Backups ensure that files can be restored
in case of corruption, accidental deletion, or security breaches (e.g., ransomware attacks).
Backup systems may include features such as versioning (storing multiple versions of a file) and encrypted
backups for additional protection.
2. File Security
File security encompasses techniques aimed at preventing unauthorized access, data breaches, and malicious
activities that can compromise file integrity or confidentiality.
A. Data Integrity
Ensuring data integrity means preventing unauthorized modification of files and ensuring that the data is
reliable and accurate.
A checksum or hash function generates a unique value (hash) for a file based on its content. This value can be
used to verify if the file has been altered.
SHA (Secure Hash Algorithm) and MD5 are commonly used hash functions for verifying file integrity.
2. Digital Signatures
Digital signatures are cryptographic methods that confirm the authenticity and integrity of a file. A file is signed
by the sender using their private key, and the recipient can verify it using the sender’s public key.
3. File Versioning
Some file systems (e.g., Google Drive or Git version control) automatically store multiple versions of a file,
allowing users to restore previous versions in case of errors, corruption, or malicious modification.
B. Malware Protection
Malicious software (malware) is a significant threat to file security. Malware, such as viruses, worms, or
ransomware, can damage, steal, or encrypt files.
Antivirus software scans files for known malware signatures, identifies suspicious activities, and helps in the
prevention and removal of malware infections.
2. Ransomware Protection
Ransomware encrypts files and demands a ransom for decryption. Some protection measures against
ransomware include using backups, enabling File History in Windows, and using ransomware detection tools.
Behavioral-based detection can help identify new or unknown ransomware variants based on their activity
patterns.
3. Sandboxing
Sandboxing isolates potentially dangerous programs or files from the rest of the system to prevent malware
from spreading or affecting other parts of the system.
C. Intrusion Detection and Prevention
File security often relies on detecting and preventing intrusions into the system by unauthorized users or
malicious actors.
An IDS monitors file access patterns, system behavior, and network activity to detect abnormal behavior or
unauthorized access attempts.
File Integrity Checkers (e.g., AIDE) can be used to monitor critical system files and alert administrators about
unauthorized changes.
An IPS goes a step further by actively blocking detected threats, stopping potential attacks before they can
compromise file security.
1. Unauthorized Access
Countermeasure - Implement strong authentication, access control policies, and encryption for sensitive files.
2. Data Corruption
Countermeasure - Use file integrity checking tools, regularly back up files, and ensure data is protected against
power outages and hardware failures.
3. Data Breaches
Countermeasure - Encrypt sensitive files, use secure file transfer methods (e.g., SFTP, HTTPS), and ensure
that only authorized users have access.
4. Ransomware
Countermeasure - Keep backups updated, use endpoint protection software, and avoid opening suspicious
email attachments or links.
5. Physical Theft
Countermeasure - Use full-disk encryption (e.g., BitLocker, FileVault) and lock physical devices to prevent
unauthorized access.
B. File Security Policies
Organizations often create file security policies to ensure consistent protection across all systems:
Data Classification - Categorizing files based on sensitivity (e.g., public, internal, confidential).
Access Control - Limiting access to files based on the user's role and need to know.
Auditing and Logging - Keeping track of file access activities to detect unauthorized actions or potential
breaches.
Training and Awareness - Ensuring that users understand the importance of file security and follow proper
procedures to protect files.
Cloud services like Google Drive, Dropbox, and OneDrive offer convenience but also introduce new security
concerns.
Measures like end-to-end encryption, two-factor authentication, and data access controls are critical for
protecting files in the cloud.
Blockchain technology can be used to ensure file integrity by creating an immutable ledger of file access and
modifications. Each change to a file can be recorded in a blockchain, providing a verifiable history of the file’s
lifecycle.