0% found this document useful (0 votes)
11 views69 pages

Operating System Mca

Uploaded by

Sindhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views69 pages

Operating System Mca

Uploaded by

Sindhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

UNIT1

Defining of Operating System


An Operating System (OS) is a software program that manages and coordinates the hardware
resources of a computer and provides an interface for users and applications to interact with
the system. The OS acts as an intermediary between the user and the computer hardware,
ensuring that different applications and users can run smoothly without interfering with one
another.
Key functions of an operating system include:
Process Management: Controls and schedules tasks or processes, ensuring efficient CPU
utilization.
Memory Management: Manages the computer's memory, allocating and freeing memory
space as needed by programs.
File System Management: Organizes and manages data storage, providing access to files and
directories on storage devices.
Device Management: Handles communication and data transfer between hardware devices
(e.g., printers, disks) and software.
Security and Access Control: Protects data and resources from unauthorized access and
ensures system integrity.
Examples of operating systems include Windows, Linux, macOS, and Android.

Clustered System
Cluster systems are similar to parallel systems because both systems use multiple CPUs. The
primary difference is that clustered systems are made up of two or more independent systems
linked together. They have independent computer systems and a shared storage media, and all
systems work together to complete all tasks. All cluster nodes use two different approaches to
interact with one another, like message passing interface (MPI) and parallel virtual machine
(PVM).

1
Cluster operating systems are a combination of software and hardware clusters. Hardware
clusters aid in the sharing of highperformance disks among all computer systems, while
software clusters give a better environment for all systems to operate. A cluster system
consists of various nodes, each of which contains its cluster software.

There are two clusters available to make a more efficient cluster. These are as follows:
• Software Cluster
• Hardware Cluster

Software Cluster
The Software Clusters allows all the systems to work together.
Hardware Cluster
It helps to allow highperformance disk sharing among systems.
Types of Clustered Operating System
There are mainly three types of the clustered operating system:
• Asymmetric Clustering System
• Symmetric Clustering System
• Parallel Cluster System

Asymmetric Clustering System


In the asymmetric cluster system, one node out of all nodes is in hot standby mode, while the
remaining nodes run the essential applications. Hot standby mode is completely failsafe and
also a component of the cluster system. The node monitors all server functions; the hot
standby node swaps this position if it comes to a halt.

2
Symmetric Clustering System
Multiple nodes help run all applications in this system, and it monitors all nodes
simultaneously. Because it uses all hardware resources, this cluster system is more reliable
than asymmetric cluster systems.
Parallel Cluster System
A parallel cluster system enables several users to access similar data on the same shared
storage system. The system is made possible by a particular software version and other apps.
Advantages of Clustered os:
• High Availability
• Scalability
• Load Balancing
• Fault Tolerance
• Improved Performance
• Cost Efficiency

Operating Systems Structures


The Operating System (OS) Structure refers to the organization of an OS's components and
how they interact with each other to manage hardware resources and provide services to
applications and users. Different operating systems use different structural models to achieve
these goals, each with its own advantages and tradeoffs.
Types of Operating Systems Structures
• Simple/Monolithic Structure
• MicroKernel Structure
• HybridKernel Structure
• ExoKernel Structure
• Layered Structure
• Modular Structure

Simple/Monolithic structure
Such operating systems do not have welldefined structures and are small, simple, and limited.
The interfaces and levels of functionality are not well separated. MSDOS is an example of
such an operating system.

3
MicroKernel Structure
This structure designs the operating system by removing all nonessential components from
the kernel and implementing them as system and user programs. This results in a smaller
kernel called the microkernel. Advantages of this structure are that all new services need to be
added to user space and does not require the kernel to be modified. Thus it is more secure and
reliable as if a service fails, then rest of the operating system remains untouched. Mac OS is
an example of this type of OS.
HybridKernel Structure
Hybridkernel structure is nothing but just a combination of both monolithickernel structure
and microkernel structure. Basically, it combines properties of both monolithic and
microkernel and make a more advance and helpful approach. It implement speed and design
of monolithic and modularity and stability of microkernel structure.
Advantages of HybridKernel Structure
• It offers good performance as it implements the advantages of both structure in it.
• It supports a wide range of hardware and applications.

ExoKernel Structure
Exokernel is an operating system developed at MIT to provide applicationlevel management
of hardware resources. By separating resource management from protection, the exokernel
architecture aims to enable applicationspecific customization. Due to its limited operability,
exokernel size typically tends to be minimal.
Advantages of ExoKernel
• Support for improved application control.
• Separates management from security.
• It improves the performance of the application.

4
Layered Structure
An OS can be broken into pieces and retain much more control over the system. In this
structure, the OS is broken into a number of layers (levels). The bottom layer (layer 0) is the
hardware, and the topmost layer (layer N) is the user interface. These layers are so designed
that each layer uses the functions of the lowerlevel layers. This simplifies the debugging
process, if lowerlevel layers are debugged and an error occurs during debugging, then the
error must be on that layer only, as the lowerlevel layers have already been debugged.

Modular Structure
It is considered as the best approach for an OS. It involves designing of a modular kernel.
The kernel has only a set of core components and other services are added as dynamically
loadable modules to the kernel either during runtime or boot time. It resembles layered
structure due to the fact that each kernel has defined and protected interfaces, but it is more
flexible than a layered structure as a module can call any other module.

Operating System Operations

5
The major operations of the operating system are process management, memory
management, device management and file management.

Process Management
The operating system is responsible for managing the processes i.e assigning the processor to
a process at a time. This is known as process scheduling. The different algorithms used for
process scheduling are FCFS (first come first served), SJF (shortest job first), priority
scheduling, round robin scheduling etc.
There are many scheduling queues that are used to handle processes in process management.
When the processes enter the system, they are put into the job queue. The processes that are
ready to execute in the main memory are kept in the ready queue. The processes that are
waiting for the I/O device are kept in the device queue.
Memory Management
Memory management plays an important part in operating system. It deals with memory and
the moving of processes from disk to primary memory for execution and back again.
The activities performed by the operating system for memory management are −
The operating system assigns memory to the processes as required. This can be done using
best fit, first fit and worst fit algorithms.
All the memory is tracked by the operating system
Device Management
There are many I/O devices handled by the operating system such as mouse, keyboard, disk
drive etc. There are different device drivers that can be connected to the operating system to
handle a specific device. The device controller is an interface between the device and the
device driver. The user applications can access all the I/O devices using the device drivers,
which are device specific codes.

6
File Management
Files are used to provide a uniform view of data storage by the operating system. All the files
are mapped onto physical devices that are usually non volatile so data is safe in the case of
system failure.
The files can be accessed by the system in two ways i.e. sequential access and direct access

Sequential Access
The information in a file is processed in order using sequential access. The files records are
accessed on after another. Most of the file systems such as editors, compilers etc. use
sequential access.
Direct Access
In direct access or relative access, the files can be accessed in random for read and write
operations. The direct access model is based on the disk model of a file, since it allows
random accesses.
Process Management
Process management is a key part of an operating system. It controls how processes are
carried out, and controls how your computer runs by handling the active processes.
The OS is responsible for managing the start, stop, and scheduling of processes, which are
programs running on the system. The operating system uses a number of methods to prevent
deadlocks, facilitate interprocess communication, and synchronize processes.
A process in memory is divided into several distinct sections, each serving a different
purpose. Here’s how a process typically looks in memory:

Text Section: A Process, sometimes known as the Text Section, also includes the current
activity represented by the value of the Program Counter.
Stack: The stack contains temporary data, such as function parameters, returns addresses, and
local variables.
Data Section: Contains the global variable.

7
Heap Section: Dynamically memory allocated to process during its run time.
Characteristics of a Process:
A process has the following attributes.
Process Id: A unique identifier assigned by the operating system.
Process State: Can be ready, running, etc.
CPU Registers: Like the Program Counter (CPU registers must be saved and restored when a
process is swapped in and out of the CPU)
Accounts Information: Amount of CPU used for process execution, time limits, execution ID,
etcI/O Status Information: For example, devices allocated to the process, open files, etc

States of Process
A process is in one of the following states:
New: Newly Created Process (or) beingcreated process.
Ready: After the creation process moves to the Ready state, i.e. the process is ready for
execution.
Running: Currently running process in CPU (only one process at a time can be under
execution in a single processor).
Wait (or Block): When a process requests I/O access.
Complete (or Terminated): The process completed its execution.
Suspended Ready: When the ready queue becomes full, some processes are moved to a
suspended ready state
Suspended Block: When the waiting queue becomes full.

Memory Management in Operating System


The term memory can be defined as a collection of data in a specific format. It is used to store
instructions and process data. The memory comprises a large array or group of words or
bytes, each with its own location. The primary purpose of a computer system is to execute
programs. These programs, along with the information they access, should be in the main
memory during execution.
o Memory Allocation
o First Fit
o Best Fit
o Worst Fit
o Fragmentation
o Internal Fragmentation

8
o External Fragmentation
o Paging
o
Why Memory Management is Required?
Allocate and deallocate memory before and after process execution.
To keep track of used memory space by processes.
To minimize fragmentation issues.
To proper utilization of main memory.
To maintain data integrity while executing of process.
Logical and Physical Address Space
Logical Address Space: An address generated by the CPU is known as a “Logical Address”. It
is also known as a Virtual address. Logical address space can be defined as the size of the
process. A logical address can be changed.
Physical Address Space: An address seen by the memory unit (i.e the one loaded into the
memory address register of the memory) is commonly known as a “Physical Address”. A
Physical address is also known as a Real address.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different types of
loading :
Static Loading: Static Loading is basically loading the entire program into a fixed address. It
requires more memory space.
Dynamic Loading: The entire program and all data of a process must be in physical memory
for the process to execute. So, the size of a process is limited to the size of physical memory.
Swapping
When a process is executed it must have resided in memory. Swapping is a process of
swapping a process temporarily into a secondary memory from the main memory, which is
fast compared to secondary memory. A swapping allows more processes to be run and can be
fit into memory at one time. The main part of swapping is transferred time and the total time
is directly proportional to the amount of memory swapped. Swapping is also known as
rollout, or roll because if a higher priority process arrives and wants service, the memory
manager can swap out the lower priority process and then load and execute the higher priority
process.

9
Storage Management
Storage Management is defined as it refers to the management of the data storage
equipment’s that are used to store the user/computer generated data. Hence it is a tool or set
of processes used by an administrator to keep your data and storage equipment’s safe.
Storage management key attributes:
1. Performance
2. Reliability
3. Recoverability
4. Capacity
Feature of Storage management: There is some feature of storage management which is
provided for storage capacity. These are given below:

Storage management is a process that is used to optimize the use of storage devices.
Storage management must be allocated and managed as a resource in order to truly benefit a
corporation.
Storage management is generally a basic system component of information systems.
It is used to improve the performance of their data storage resources.
Advantage of storage management:
• It becomes very simple to manage a storage capacity.
• It generally reduces the time consumption.
• It improves the performance of system.

10
• In virtualization and automation technologies, it can help an organization improve its
agility.

Protection and security


Protection and security requires that computer resources such as CPU, softwares, memory
etc. are protected. This extends to the operating system as well as the data in the system. This
can be done by ensuring integrity, confidentiality and availability in the operating system.
The system must be protect against unauthorized access, viruses, worms etc.
Threats to Protection and Security
A threat is a program that is malicious in nature and leads to harmful effects for the system.
Some of the common threats that occur in a system are −
Virus
Viruses are generally small snippets of code embedded in a system. They are very dangerous
and can corrupt files, destroy data, crash systems etc. They can also spread further by
replicating themselves as required.
Trojan Horse
A trojan horse can secretly access the login details of a system. Then a malicious user can use
these to enter the system as a harmless being and wreak havoc.
Trap Door
A trap door is a security breach that may be present in a system without the knowledge of the
users. It can be exploited to harm the data or files in a system by malicious people.
Worm
A worm can destroy a system by using its resources to extreme levels. It can generate
multiple copies which claim all the resources and don't allow any other processes to access
them. A worm can shut down a whole network in this way.
Denial of Service

11
These type of attacks do not allow the legitimate users to access a system. It overwhelms the
system with requests so it is overwhelmed and cannot work properly for other user.
Protection and Security Methods
The different methods that may provide protect and security for different computer systems
are −
Authentication
This deals with identifying each user in the system and making sure they are who they claim
to be. The operating system makes sure that all the users are authenticated before they access
the system. The different ways to make sure that the users are authentic are:
Username/ Password
Each user has a distinct username and password combination and they need to enter it
correctly before they can access the system.
User Key/ User Card
The users need to punch a card into the card slot or use they individual key on a keypad to
access the system.
User Attribute Identification
Different user attribute identifications that can be used are fingerprint, eye retina etc. These
are unique for each user and are compared with the existing samples in the database. The user
can only access the system if there is a match.
One Time Password
These passwords provide a lot of security for authentication purposes. A one time password
can be generated exclusively for a login every time a user wants to enter the system. It cannot
be used more than once. The various ways a one time password can be implemented are −
Random Numbers
The system can ask for numbers that correspond to alphabets that are pre arranged. This
combination can be changed each time a login is required.
Secret Key
A hardware device can create a secret key related to the user id for login. This key can change
each time.

Computing Environments
Computing environments refer to the technology infrastructure and software platforms that
are used to develop, test, deploy, and run software applications. There are several types of
computing environments, including:
Mainframe: A large and powerful computer system used for critical applications and
largescale data processing.

12
ClientServer: A computing environment in which client devices access resources and services
from a central server.
Cloud Computing: A computing environment in which resources and services are provided
over the Internet and accessed through a web browser or client software.
Mobile Computing: A computing environment in which users access information and
applications using handheld devices such as smartphones and tablets.
Grid Computing: A computing environment in which resources and services are shared across
multiple computers to perform largescale computations.
Embedded Systems: A computing environment in which software is integrated into devices
and products, often with limited processing power and memory.
Each type of computing environment has its own advantages
Types of Computing Environments :

Personal Computing Environment: Individual computers used by single users for personal
tasks.
TimeSharing Computing Environment: Multiple users share system resources simultaneously,
typically through terminals.
ClientServer Computing Environment: Clients request services, and servers provide them,
often over a network.
Distributed Computing Environment: Multiple interconnected computers share processing
tasks.
Grid Computing Environment: Multiple computers work together to solve large tasks by
dividing workloads.

13
Cloud Computing Environment: Internetbased services providing ondemand resources and
storage.
Cluster Computing Environment: Group of linked computers that work together, acting as a
single system.

Open source operating system


An opensource operating system (OS) is software that manages computer hardware and
software resources, providing common services for computer programs. The defining
characteristic of opensource OSes is that their source code is freely available for anyone to
view, modify, and distribute. This openness fosters innovation, collaboration, and
transparency in software development.
Key Characteristics:
Accessible Source Code: The entire source code is available to the public.
Licensing: Governed by licenses (e.g., GPL, MIT) that define how the code can be used,
modified, and shared.
CommunityDriven Development: Contributions from a diverse group of developers
worldwide.
Transparency: Users can inspect the code for security, functionality, and compliance.
Popular OpenSource Operating Systems
1. Linux
Linux is arguably the most prominent opensource OS, known for its versatility and
robustness.
Distributions (Distros):
Ubuntu: Userfriendly, widely supported, ideal for desktops and servers.
Fedora: Cuttingedge features, sponsored by Red Hat, favored by developers.
Debian: Stabilityfocused, forms the base for many other distros.
Arch Linux: Highly customizable, rolling release model for the latest software.
Use Cases:
Servers: Hosting websites, databases, and applications.
Desktops: Personal computing with a variety of desktop environments (GNOME, KDE).
FreeBSD
FreeBSD is renowned for its performance, advanced networking, and security features.
Features:
ZFS Support: Advanced file system with high scalability and data integrity.
14
Jails: Lightweight virtualization for isolating applications.
Networking Stack: Robust and efficient, suitable for hightraffic servers.

ReactOS

ReactOS is an ambitious project aiming to create a free and compatible alternative to


Microsoft Windows.

Current Status:

• Development Stage: Alpha version, with ongoing efforts to improve stability and
compatibility.
• Compatibility: Focus on running Windows applications and drivers.

Operating System Services

Operating systems (OS) provide a range of essential services to ensure the proper functioning
of computer systems and the smooth execution of applications. These services act as
intermediaries between hardware and software, offering functionalities that simplify user and
application interaction with the system.

1. Process Management

This service handles the creation, scheduling, and termination of processes (running
programs). It ensures that multiple processes can run efficiently and that system resources
(CPU, memory) are properly allocated. Key components include:

Process Scheduling: Determines the order in which processes are executed by the CPU.

Process Synchronization: Coordinates access to shared resources between processes.

InterProcess Communication (IPC): Enables communication between processes (e.g., using


pipes or message queues).

2. Memory Management

Memory management involves handling the allocation and deallocation of memory space for
processes. It ensures efficient memory use and prevents issues like memory leaks or
segmentation faults. The OS uses techniques such as:

Virtual Memory: Extends physical memory by using disk space, allowing for larger programs
to run.

Paging/Segmentation: Breaks memory into fixedsize blocks (pages) or segments to improve


management and efficiency.

3. File System Management

15
This service provides a structured way to store, retrieve, and manage data on storage devices
(hard drives, SSDs). The OS organizes data into files and directories, ensuring secure and
efficient data access. Key aspects include:

File Access Control: Manages permissions to control who can read, write, or execute files.

File Organization: Stores files in directories (or folders) and provides a hierarchical structure
for easier navigation.

Storage Management: Manages how files are physically stored on disk and ensures efficient
use of storage space.

4. Device Management

Device management handles communication between the system and hardware peripherals
(e.g., printers, scanners, keyboards). It uses device drivers, which are specialized programs
that allow the OS to interact with hardware. Key tasks include:

I/O Operations: Manages input/output devices and operations.

Device Drivers: Provides abstraction for hardware to ensure compatibility and proper
functioning of devices.

Buffering and Spooling: Buffers data to smooth out differences in device speeds and spools
output (e.g., printing jobs) for efficient processing.

5. Security and Protection

The OS ensures the security of data, processes, and the system itself by protecting against
unauthorized access and ensuring data integrity. Key mechanisms include:

User Authentication: Verifies users via credentials (e.g., passwords, biometrics).

Access Control: Manages who can access specific files or resources.

Encryption: Protects data by encoding it, making it unreadable without the correct decryption
key.

Audit Logs: Tracks and records user activities to detect and investigate security incidents.

16
System Call
A system call is a programmatic way in which a computer program requests a service from
the kernel of the operating system it is executed on. A system call is a way for programs to
interact with the operating system. A computer program makes a system call when it requests
the operating system’s kernel.
System call provides the services of the operating system to the user programs via the
Application Program Interface(API). It provides an interface between a process and an
operating system to allow userlevel processes to request services of the operating system.
System calls are the only entry points into the kernel system. All programs needing resources
must use system calls.
Services Provided by System Calls
• Process Creation and Management
• Main Memory Management
• File Access, Directory, and File System Management
• Device Handling(I/O)
• Protection
• Networking, etc.

Process Control: end, abort, create, terminate, allocate, and free memory.
File Management: create, open, close, delete, read files, etc.
Device Management
Information Maintenance
Communication

Types of System Call

17
System calls are how programs request services from the operating system. They are grouped
into five main types:

1. Process Control
Manages process creation, execution, and termination.
fork(): Creates a new process by duplicating the calling process.
exec(): Replaces the current process image with a new process.
exit(): Terminates the current process and returns a status code.
wait(): Waits for a child process to finish execution.
kill(): Sends a signal to terminate or manage another process.
2. File Management
Handles file operations like opening, reading, writing, and closing files.
open(): Opens a file for reading, writing, or both.
read(): Reads data from a file into a buffer.
write(): Writes data from a buffer to a file.
close(): Closes an open file.
unlink(): Deletes a file.
3. Device Management
Manages communication with hardware devices.
ioctl(): Performs lowlevel devicespecific operations.
read(): Reads data from a device (same system call used for reading files).
write(): Writes data to a device (same system call used for writing to files).
4. Information Maintenance

18
Retrieves or sets system and process information.
getpid(): Retrieves the process ID of the current process.
getppid(): Retrieves the parent process ID.
getuid(): Retrieves the user ID of the calling process.
setuid(): Sets the user ID of the calling process.
times(): Returns the amount of CPU time used by the process.5. Communication
Communication
• Enables interprocess communication (IPC) between processes.
• pipe(): Creates a unidirectional data channel for IPC between related processes.
• shmget(): Allocates shared memory for processes.
• mmap(): Maps files or devices into memory for fast communication.
• msgsend(): Sends a message to a message queue.

System Programs
The system programs are responsible for the development and execution of a program and
they can be used by the help of system calls because system calls define different types of
system programs for different tasks.
File management − These programs create, delete, copy, rename, print, exit and generally
manipulate the files and directory.
Status information − It is the information regarding input, output process, storage and the
CPU utilization time how the process will be calculated in how much memory required to
perform a task is known by status information.
Programming language supports − compiler, assembler, interrupt are programming language
support used in the operating system for a particular purpose in the computer.
Programming Loading and execution − The needs to enter the program and after the loading
of a program it needs to execute the output of the programs and this task is also performed by
system calls by the help of system programs.
Communication − These services are provided by the user because by using this number of
devices communicates with each other by the help of device or wireless and communication
is necessary for the operating system.
Types of System programs
The types of system programs are as follows −
Utility program
It manages, maintains and controls various computer resources. Utility programs are
comparatively technical and are targeted for the users with solid technical knowledge.
Few examples of utility programs are: antivirus software, backup software and disk tools.
Device drivers

19
It controls the particular device connected to a computer system. Device drivers basically act
as a translator between the operating system and device connected to the system.
Example − printer driver, scanner driver, storage device driver etc.
Directory reporting tools
These tools are required in an operation system to have some software to facilitate the
navigation through the computer system.
Example − dir, ls, Windows Explorer etc.

System Boot
System boot is the process of starting a computer and loading the operating system (OS) into
memory so that it becomes operational. It involves several steps, beginning from when the
power is turned on to when the OS is fully loaded and ready for use.
Steps in the System Boot Process:
1. Power On and POST (PowerOn Self Test)
When the computer is powered on, the hardware performs a POST to check if all critical
hardware components (CPU, memory, keyboard, etc.) are functioning correctly. If a hardware
issue is detected, the boot process is halted, and an error message or beep code is provided.
2. Loading the Bootloader
After POST, the system's firmware (BIOS or UEFI) looks for the bootloader, a small program
stored on the disk's boot sector (Master Boot Record MBR or GPT). The bootloader’s job is
to load the operating system into memory.
BIOS (Basic Input/Output System): Older firmware used in many PCs.
UEFI (Unified Extensible Firmware Interface): A more modern alternative with better
features, like faster boot times and secure boot options.
3. Bootloader Execution
The bootloader (e.g., GRUB for Linux or Windows Boot Manager) is responsible for loading
the kernel of the OS into memory. It can also provide a boot menu to select different
operating systems if multiple OSes are installed.
4. Loading the OS Kernel
The bootloader locates and loads the OS kernel (the core part of the operating system) into
memory. The kernel takes over system management from the bootloader and initializes the
hardware components.
5. Hardware and Device Initialization

20
The kernel initializes the system’s hardware, including CPUs, memory, and input/output
devices (like keyboards, monitors, and storage drives). Device drivers are loaded to facilitate
communication with the hardware.
6. Starting System Services
The kernel starts essential system services and daemons (background processes), such as
networking services, security services, and other components required for the system to
function properly.
7. User Login and Shell
Once the system services are running, the system prompts the user to log in (via a login
screen or terminal). After logging in, the user interface (graphical or commandline) is
launched, allowing the user to interact with the operating system.

UNIT2
Process Concept
In an operating system (OS), a process is an instance of a program that is being executed. It is
a fundamental concept, as the OS manages multiple processes simultaneously, ensuring
efficient use of system resources like the CPU, memory, and I/O devices. Each process has its
own execution context, which includes its program code, data, and the state of the CPU
registers.
Process Scheduling
he process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such
operating systems allow more than one process to be loaded into the executable memory at a
time and the loaded process shares the CPU using time multiplexing.
Categories of Scheduling
Nonpreemptive: Here the resource can’t be taken from a process until the process completes
execution. The switching of resources occurs when the running process terminates and moves
to a waiting state.
Preemptive: Here the OS allocates the resources to a process for a fixed amount of time.
During resource allocation, the process switches from running state to ready state or from
waiting state to ready state.
Process Scheduling Queues
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main memory, ready and
waiting to execute. A new process is always put in this queue.

21
Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.

Operations on Processess

Creation: Starting a new process, typically using system calls like fork() or CreateProcess().
Scheduling: Deciding which process runs and for how long using algorithms like Round
Robin or FCFS.
Termination: Ending a process, either normally or due to errors.
Context Switching: Saving and loading process states when switching between processes.
Synchronization: Managing processes to prevent conflicts using tools like semaphores or
mutexes.
Communication: Exchanging data between processes via mechanisms like pipes or message
queues.
Waiting: Processes waiting for events, like I/O completion, and moving through different
states (e.g., ready, running).
Process Control Block (PCB): A data structure storing information about a process, including
its state and resources.
Process States: Various stages a process goes through, including New, Ready, Running,
Waiting, and Terminated.

Inter Process Communication (IPC)


Processes can coordinate and interact with one another using a method called interprocess
communication (IPC) . Through facilitating process collaboration, it significantly contributes
to improving the effectiveness, modularity, and ease of software systems.

22
Types of Process
• Independent process
• Cooperating process

Independent Process: A process that operates on its own without relying on or affecting other
processes. It does not share data or resources with other processes.
Cooperating Process: A process that interacts with other processes, sharing data and
resources. It may need synchronization to avoid conflicts and ensure smooth operation.
The communication between these processes can be seen as a method of cooperation between
them. Processes can communicate with each other through both:
Methods of IPC
• Shared Memory
• Message Passing

1. Shared Memory: Multiple processes access and modify a common memory space directly.
Requires synchronization to avoid conflicts.
2. Message Passing: Processes communicate by sending and receiving messages through a
communication channel, which avoids direct access to shared memory.

Client Server Communication


In an Operating System, Client Server Communication refers to the exchange of data and
Services among multiple machines or processes. In Client clientserver communication
System one process or machine acts as a client requesting a service or data, and Another
machine or process acts like a server for providing those Services or Data to the client
machine. This Communication model is widely used for exchanging data among various
computing environments like Distributed Systems, Internet Applications, and Networking
Application communication.
Different Ways of ClientServer Communication
• Sockets Mechanism
• Remote Procedure Call
• Message Passing
• Interprocess Communication
• Distributed File Systems

Sockets Mechanism: Uses network sockets to establish a connection between client and
server for data exchange.

23
Remote Procedure Call (RPC): Allows a client to invoke procedures or functions on a remote
server as if they were local.

Message Passing: Involves sending and receiving messages between client and server to
exchange data.

Interprocess Communication (IPC): Enables processes on the same machine to communicate


and synchronize their actions.

24
Distributed File Systems: Allows clients to access and manage files on a remote server as if
they were on their local system.

Thread in Operating System


A thread refers to a single sequential activity being executed. these activities are also known
as thread of execution or thread control. Now, any operating system process can execute a
thread. we can say, that a process can have multiple threads.
Components of Threads
These are the basic components of the Operating System.
• Stack Space
• Register Set
• Program Counter

MultiThreads
User Level User Level Thread is a type of thread that is not created using system calls. The
kernel has no work in the management of userlevel threads. Userlevel threads can be easily
implemented by the user.
Kernel Level Thread A kernel Level Thread is a type of thread that can recognize the
Operating system easily. Kernel Level Threads has its own thread table where it keeps track
of the system. The operating System Kernel helps in managing threads.
Hybrid Threads: Combines both userlevel and kernellevel threading. User threads are
managed by a userlevel library, but the kernel manages scheduling and resources for those
threads.
ManytoOne: Maps many userlevel threads to a single kernel thread. Thread management is
done in user space, and the kernel schedules one thread at a time.
OnetoOne: Maps each userlevel thread to a separate kernel thread. Allows multiple threads to
run in parallel, managed directly by the kernel.

25
ManytoMany: Maps many userlevel threads to many kernel threads. Provides flexibility and
allows multiple threads to run concurrently, with both user and kernel managing threads.

Scheduling Criteria
Scheduling criteria help determine the efficiency and performance of process scheduling in
operating systems. Key criteria include:
CPU Utilization: The percentage of time the CPU is active. Higher CPU utilization means the
CPU is used more efficiently.
Throughput: The number of processes completed per unit of time. Higher throughput
indicates better performance.
Turnaround Time: The total time taken from process submission to completion. Lower
turnaround time is preferable.
Waiting Time: The total time a process spends waiting in the ready queue. Lower waiting
time improves responsiveness.
Response Time: The time from process submission to the first response or output. Shorter
response times enhance interactivity.
Fairness: Ensures that all processes get a fair share of CPU time without indefinite
postponement.
Scheduling algorithms in os
• FirstCome, FirstServed (FCFS) Scheduling
• ShortestJobNext (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• MultipleLevel Queues Scheduling

These algorithms are either nonpreemptive or preemptive. Nonpreemptive algorithms are


designed so that once a process enters the running state, it cannot be preempted until it
completes its allotted time, whereas the preemptive scheduling is based on priority where a

26
scheduler may preempt a low priority running process anytime when a high priority process
enters into a ready state.

First Come First Serve (FCFS)

• Jobs are executed on first come, first serve basis.


• It is a nonpreemptive, preemptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

Wait time of each process is as follows −

Process Wait Time : Service Time Arrival Time

P0 0 0=0

P1 5 1=4

P2 8 2=6

P3 16 3 = 13

Average Wait Time: (0+4+6+13) / 4 = 5.75

Shortest Job First (SJF)

• This is also known as shortest job first, or SJF


• This is a nonpreemptive, preemptive scheduling algorithm.

27
• Best approach to minimize waiting time.
• Easy to implement in Batch systems where required CPU time is known in advance.
• Impossible to implement in interactive systems where required CPU time is not known.
• The processer should know in advance how much time process will take.

Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 14

P3 3 6 8

Waiting time of each process is as follows −

Process Waiting Time

P0 0 0=0

P1 5 1=4

P2 14 2 = 12

28
P3 8 3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25

Priority Based Scheduling

• Priority scheduling is a nonpreemptive algorithm and one of the most common


scheduling algorithms in batch systems.
• Each process is assigned a priority. Process with highest priority is to be executed first
and so on.
• Processes with same priority are executed on first come first served basis.
• Priority can be decided based on memory requirements, time requirements or any other
resource requirement.

Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering 1 is the lowest priority.

Process Arrival Time Execution Time Priority Service Time

P0 0 5 1 0

P1 1 3 2 11

P2 2 8 1 14

P3 3 6 3 5

29
Waiting time of each process is as follows −

Process Waiting Time

P0 0 0=0

P1 11 1 = 10

P2 14 2 = 12

P3 5 3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6

Shortest Remaining Time

• Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
• The processor is allocated to the job closest to completion but it can be preempted by a
newer ready job with shorter time to completion.
• Impossible to implement in interactive systems where required CPU time is not known.
• It is often used in batch environments where short jobs need to give preference.

Round Robin Scheduling

• Round Robin is the preemptive process scheduling algorithm.


• Each process is provided a fix time to execute, it is called a quantum.
• Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.
• Context switching is used to save states of preempted processes.

Wait time of each process is as follows −

30
Process Wait Time : Service Time Arrival Time

P0 (0 0) + (12 3) = 9

P1 (3 1) = 2

P2 (6 2) + (14 9) + (20 17) = 12

P3 (9 3) + (17 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5

MultipleLevel Queues Scheduling

Multiplelevel queues are not an independent scheduling algorithm. They make use of other
existing algorithms to group and schedule jobs with common characteristics.

• Multiple queues are maintained for processes with common characteristics.


• Each queue can have its own scheduling algorithms.
• Priorities are assigned to each queue.

For example, CPUbound jobs can be scheduled in one queue and all I/Obound jobs in another
queue. The Process Scheduler then alternately selects jobs from each queue and assigns them
to the CPU based on the algorithm assigned to the queue.

Process Synchronization
Process Synchronization is the coordination of execution of multiple processes in a
multiprocess system to ensure that they access shared resources in a controlled and
predictable manner. It aims to resolve the problem of race conditions and other
synchronization issues in a concurrent system.
Process synchronization ensures that multiple processes or threads operate smoothly and
without conflict when accessing shared resources. Key concepts include:
Critical Section: A segment of code where shared resources are accessed. Only one process
should be in the critical section at a time to prevent conflicts.
Mutual Exclusion: Ensures that only one process can access the critical section at any given
time.
Semaphore: A synchronization tool that controls access to shared resources. Semaphores can
be binary (0 or 1) or counting, allowing multiple units of a resource.

31
Mutex (Mutual Exclusion): A type of semaphore used to enforce mutual exclusion, ensuring
only one thread or process can access a critical section at a time.
Monitor: An abstraction that provides a highlevel synchronization mechanism. Monitors
combine mutual exclusion and condition synchronization.
Condition Variable: Used with monitors to allow processes to wait for certain conditions to be
met before proceeding.
Deadlock: A situation where two or more processes are waiting indefinitely for each other to
release resources. Deadlock prevention and avoidance strategies help mitigate this issue.
Starvation: A situation where a process is perpetually denied necessary resources due to other
processes continuously being favored. Fairness and proper scheduling help prevent
starvation.

Critical Section Problem


Critical Section is the part of a program which tries to access shared resources. That resource
may be any resource in a computer like a memory location, Data structure, CPU or any IO
device.
The critical section cannot be executed by more than one process at the same time; operating
system faces the difficulties in allowing and disallowing the processes from entering the
critical section.
Mutual Exclusion: Only one process or thread can be in its critical section (where it accesses
shared resources) at a time.
Progress: If no process is in the critical section and some processes want to enter it, then the
selection of the next process should not be postponed indefinitely. A process that requests
access to the critical section must eventually be allowed in.

32
Bounded Waiting: There must be a limit on how long a process will have to wait before it can
enter the critical section. This prevents starvation, where a process waits forever due to others
continually entering the critical section.
Solutions:
Peterson's Solution: A softwarebased solution using two flags and a turn variable to ensure
mutual exclusion and progress.
Semaphore: A synchronization primitive that can be used to control access to the critical
section. Binary semaphores (mutexes) are used for mutual exclusion.
Mutex (Mutual Exclusion): A lock that ensures only one thread or process can access the
critical section at a time.
Monitor: A highlevel abstraction that combines mutual exclusion and condition
synchronization, allowing processes to wait for conditions to be met.
TestandSet Lock: A hardwarebased solution that uses atomic operations to achieve mutual
exclusion.

Peterson's solution
Peterson's solution is a method for ensuring that two processes can safely access a shared
resource without interfering with each other. It ensures that only one process can access the
critical section (the part of the code where shared resources are accessed) at a time.
Components
Flags: Each process has a flag to show whether it wants to enter the critical section.
Process 0 has a flag to indicate if it wants to enter.
Process 1 has a flag to indicate if it wants to enter.
Turn: A shared variable that indicates whose turn it is to enter the critical section.

33
How It Works
Desire to Enter: Each process sets its flag to show that it wants to enter the critical section.
Priority: Each process sets the turn variable to indicate that it is giving priority to the other
process. For example, Process 0 sets the turn variable to indicate that it is Process 1's turn,
and vice versa.
Waiting: Before entering the critical section, each process checks two things:
Whether the other process has indicated that it wants to enter the critical section (its flag is
set).
Whether it is the other process's turn to enter (based on the turn variable).
If the other process wants to enter and it is that process's turn, the current process will wait.
Entering: If the other process does not want to enter or it is not that process’s turn, then the
current process will enter the critical section.
Leaving: When the process finishes its work in the critical section, it clears its flag, indicating
that it no longer needs to enter the critical section.

Key Points
Mutual Exclusion: Ensures that only one process can be in the critical section at any time.
This is managed through the turn variable and the flags.
Progress: Ensures that if no process is in the critical section and one or both processes want to
enter, the process with the turn (or if both want, whoever is allowed by the turn and flags)
will get access.
Bounded Waiting: Ensures that no process will wait forever to enter the critical section. Each
process will eventually get a chance based on the turn variable.

Synchronization Hardware
1.Atomic Instructions:
Atomic instructions are operations that execute as a single, indivisible unit. This means they
complete without being interrupted by other processes or threads, ensuring consistency when
modifying shared resources. Key atomic instructions include:
TestandSet: This instruction reads a value from a memory location and sets it to a new value
in one atomic operation. It's commonly used to implement spinlocks. For instance, if a
process wants to acquire a lock, it can test if the lock is free and set it to "locked" atomically.
CompareandSwap (CAS): This instruction compares the current value of a memory location
with a given value. If they match, it updates the memory location with a new value. This is

34
useful for implementing locks and other synchronization primitives, ensuring that updates are
made only if no other process has modified the value in the meantime.
FetchandAdd: This instruction atomically adds a given value to a memory location and
returns the old value. It’s useful for counting or managing resources where multiple processes
or threads might be incrementing or decrementing a shared counter.
2. Locks
Locks are synchronization mechanisms to ensure mutual exclusion and avoid concurrent
access issues. Hardwaresupported locks include:
Spinlocks: These are simple locks where a process continuously checks if the lock is
available. Spinlocks are efficient for short critical sections but can waste CPU cycles if the
lock is held for a long time.
Mutexes: Short for "mutual exclusion," mutexes are more sophisticated than spinlocks. They
can block a process if the lock is not available and wake it up when the lock becomes
available. Mutexes are implemented using atomic operations and can support complex
locking strategies, such as recursive or timed locks.
3. Memory Barriers (Fences)
Memory barriers or memory fences are special instructions that enforce an ordering of
memory operations. They prevent the CPU from reordering read and write operations in ways
that could lead to race conditions or inconsistent views of memory. Memory barriers ensure
that:
Writes to a memory location are visible to other threads or processes in the intended order.
Reads and writes occur in a specific sequence as defined by the barrier.
4. Interrupt Disabling
Interrupt disabling is a technique used to prevent context switches by disabling interrupts
during critical sections. This ensures that a process or thread can execute a critical section
without being interrupted by other processes or threads, maintaining consistency. While
effective, it can be inefficient as it may lead to poor responsiveness in systems that rely
heavily on interrupts.
5. Atomic Variables
Atomic variables are special types of variables that support atomic operations. These
variables are designed to be used in concurrent programming and can be manipulated safely
by multiple threads without requiring additional synchronization mechanisms. Examples
include:
Atomic Counters: Used to count events or manage resources safely across multiple threads.
Atomic Flags: Used to indicate status or control flow in a threadsafe manner.

35
Semaphores

Semaphores are synchronization tools used in operating systems to manage concurrent access
to shared resources and coordinate process or thread execution. They help solve issues related
to mutual exclusion and synchronization.
Types of Semaphores
Binary Semaphore: Also known as a mutex (short for mutual exclusion). It can only have two
values: 0 or 1. It's used to control access to a critical section, ensuring that only one process
or thread can access the shared resource at a time.
Counting Semaphore: Can have a range of integer values. It is used to manage a resource that
has a limited number of instances. The value represents the number of available resources.
For example, a counting semaphore initialized to 5 can allow up to 5 concurrent accesses
before blocking.
Basic Operations
Semaphores provide two primary operations:
Wait (P or Down Operation): This operation decrements the semaphore’s value. If the
semaphore’s value is greater than zero, it decrements the value and allows the process or
thread to proceed. If the value is zero, the process or thread is blocked until the semaphore’s
value is greater than zero.
Signal (V or Up Operation): This operation increments the semaphore’s value. If there are
any processes or threads waiting (blocked) on the semaphore, one of them is unblocked.
How Semaphores Work
Initialization: A semaphore is initialized with a value that represents the number of available
resources or permissions. For binary semaphores, the initial value is typically 1, indicating
that the critical section is available. For counting semaphores, the initial value is set based on
the number of available instances of the resource.
Wait Operation (P): When a process or thread wants to enter a critical section or access a
shared resource, it performs a wait operation on the semaphore:

36
If the semaphore’s value is greater than 0, the process or thread decrements the value and
proceeds.
If the value is 0, the process or thread is placed in a waiting queue until the semaphore’s
value is incremented by another process or thread.
Signal Operation (V): After a process or thread finishes using the resource, it performs a
signal operation on the semaphore:
If there are processes or threads waiting, one of them is unblocked and allowed to proceed.

Benefits
Mutual Exclusion: Ensures that only one process or thread can access the critical section or
shared resource at a time.
Synchronization: Coordinates processes or threads to ensure correct execution order and
resource usage.
Deadlock Avoidance: By properly managing semaphore operations, it is possible to design
systems that avoid deadlocks and ensure that all processes eventually get access to resources.

System model

1.Process Model

Processes: Executing programs with their own memory and resources.

Threads: Smaller execution units within a process.

Process States: New, ready, running, waiting, terminated.

Scheduling: Decides which process or thread to run.

2. Memory Model

Memory Hierarchy: Levels from fast registers to slow disk storage.

Virtual Memory: Allows more memory usage than physically available.


37
Memory Management: Allocates and deallocates memory using techniques like paging.

3. File System Model

Files and Directories: Organize and store data.

File Operations: Create, read, write, delete files.

Permissions: Control access to files and directories.

4. Input/Output (I/O) Model

Device Drivers: Interface between OS and hardware.

I/O Operations: Methods for managing data transfer to/from devices.

Device Management: Handles device interactions and resource allocation.

5. Concurrency Model

Synchronization: Ensures safe access to shared resources.

Communication: Methods for processes or threads to interact, such as message passing.

6. Security Model

Authentication and Authorization: Verifies and controls access to system resources.

Encryption: Protects data from unauthorized access.

7. System Calls

System Calls: Interface for programs to request services from the OS.

Deadlock Characterization

A deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.

A deadlock occurs if the four Coffman conditions hold true. But these conditions are not
mutually exclusive. They are given as follows −

Mutual Exclusion

There should be a resource that can only be held by one process at a time. In the diagram below,
there is a single instance of Resource 1 and it is held by Process 1 only.

38
Hold and Wait

A process can hold multiple resources and still request more resources from other processes
which are holding them. In the diagram given below, Process 2 holds Resource 2 and Resource
3 and is requesting the Resource 1 which is held by Process 1.

No Preemption

A resource cannot be preempted from a process by force. A process can only release a resource
voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will
only be released when Process 1 relinquishes it voluntarily after its execution is complete.

Circular Wait

A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a resource held
by the first process. This forms a circular chain.

39
Methods for handling deadlocks

1.Deadlock Prevention
Avoid Circular Wait: Ensure that processes acquire resources in a predefined order to prevent
circular dependencies.
Hold and Wait: Require processes to request all required resources at once, avoiding
situations where they hold some resources while waiting for others.
No Preemption: Allow resources to be preempted from processes if necessary to break the
deadlock.
Mutual Exclusion: Limit the use of resources that can be held exclusively, although this may
not always be feasible.
2. Deadlock Avoidance
Banker's Algorithm: Check resource allocation dynamically to ensure that the system remains
in a safe state. It calculates whether granting a resource request will lead to a safe or unsafe
state.
Resource Allocation Graph (RAG): Track the allocation and request of resources to ensure
that no circular wait conditions can occur.
3. Deadlock Detection
Resource Allocation Graph: Use a graph to detect cycles, which indicate deadlocks. The
system periodically checks for cycles in the graph.
Detection Algorithms: Implement algorithms that check for deadlocks by examining resource
allocation and process states, such as the WaitFor Graph.
4. Deadlock Recovery

40
Process Termination: Kill one or more processes involved in the deadlock to break the cycle.
This may be done selectively based on criteria like the least progress made.
Resource Preemption: Reclaim resources from processes to break the deadlock. This involves
forcibly taking resources away and reallocating them to other processes.
Rollback: Revert processes to a safe state before the deadlock occurred, often used with
checkpoints to restore processes.

Unit 3
Storage management
Storage management in an operating system (OS) is responsible for managing data storage on
a computer system, including how data is stored, accessed, and organized on physical and
virtual storage devices. It plays a critical role in ensuring that applications and processes can
efficiently read and write data while optimizing the use of available storage resources.

Key aspects of storage management include:

1. File Systems
A file system organizes and manages files on a storage device, such as a hard drive, SSD, or
USB stick. The OS uses file systems to control how data is stored and retrieved. Common file
systems include:
NTFS (New Technology File System) – used in Windows.
FAT32/exFAT – used for crossplatform compatibility.
EXT4 – used in Linux.
APFS – used in macOS.

The file system handles tasks such as:


Naming files.
Organizing directories (folders).
Defining file permissions and access controls.

2. Memory Management
Memory management refers to the process of controlling and coordinating computer
memory, including RAM and virtual memory. The OS ensures that each process has enough
memory to run efficiently by:

41
Allocating and deallocating memory space.
Managing virtual memory (which uses storage devices to simulate additional RAM).
Handling paging and swapping, where parts of memory are transferred to and from disk
storage.

3. Disk Scheduling
Disk scheduling algorithms determine the order in which read and write requests are
processed by the disk. Efficient disk scheduling improves performance by reducing the seek
time and rotational latency in hard drives. Some common algorithms include:
FCFS (FirstComeFirstServed): Processes requests in the order they arrive.
SSTF (Shortest Seek Time First): Processes requests closest to the current head position
first.
SCAN (Elevator Algorithm): Moves the disk arm across the disk in one direction,
processing requests along the way.

4. File Allocation Methods


Different methods are used to allocate space for files on a disk:
Contiguous Allocation: Files are stored in contiguous blocks of memory.
Linked Allocation: Each file is a linked list of disk blocks.
Indexed Allocation: A separate index block stores the pointers to all disk blocks of a file.

5. RAID (Redundant Array of Independent Disks)


RAID is a technology used to increase performance and reliability by distributing data
across multiple disks. Different RAID levels (e.g., RAID 0, RAID 1, RAID 5) offer tradeoffs
between speed, redundancy, and capacity.

6. Backup and Recovery


An important part of storage management is ensuring that data is regularly backed up and
can be recovered in case of hardware failure, corruption, or accidental deletion.

7. Cache Management
Caching helps speed up access to frequently used data by storing it in faster storage, such as
RAM or SSDs. The OS manages the cache to ensure that critical data is readily available
without excessive access to slower storage.

42
8. Security and Access Control
The OS manages access to storage resources by enforcing security policies, user
authentication, and permissions. This ensures that only authorized users can access or modify
certain files.

Background in storage management


Background of storage management in an operating system refers to the evolution and
importance of managing data storage efficiently as computer systems have advanced over
time. Storage management emerged from the need to organize and access large amounts of
data quickly and reliably while keeping storage space usage optimal.

Key Milestones in the Background of Storage Management:

1. Early Computers (1950s1960s):


In the early days of computing, storage management was minimal because computers had
limited memory (both main memory and secondary storage). Magnetic tapes and punch cards
were the primary storage media.
The need for file systems arose as computers began to use larger storage devices like
magnetic disks, requiring a structured way to store and retrieve files.

2. Development of File Systems (1960s1970s):


As diskbased storage became more prevalent, operating systems needed ways to manage
data on hard drives. The first file systems were simple, managing files in a linear fashion.
Operating systems like UNIX introduced more advanced file systems with hierarchical
directory structures and file permissions.

3. Virtual Memory and Paging (1960s1970s):


To address the limited physical memory (RAM), operating systems began to use virtual
memory, allowing computers to simulate more memory by using disk storage.
The concept of paging was developed, where memory was divided into small blocks
(pages) that could be moved between RAM and disk, optimizing memory usage for running
processes.

4. Introduction of Disk Scheduling (1970s1980s):

43
Disk scheduling algorithms became essential as hard drives got larger and more users
began to multitask on computers. These algorithms improved the speed and efficiency of
reading and writing data to disks.

5. Evolution of File Systems and Storage Devices (1980s2000s):


New file systems like FAT (File Allocation Table), NTFS (New Technology File System),
and EXT (Extended File System) were developed, each bringing improvements in storage
capacity, reliability, and performance.
With the rise of personal computers, the need for storage management became even more
critical, as users needed better ways to store, organize, and back up their growing data.
The introduction of solidstate drives (SSDs) revolutionized storage by providing faster data
access and improving overall system performance.

6. Modern Storage Challenges (2000sPresent):


Modern operating systems manage increasingly complex storage environments, including
cloud storage, networkattached storage (NAS), and largescale data centers.
With the advent of big data, the focus shifted to managing vast amounts of data efficiently,
and new technologies like RAID, distributed file systems, and data deduplication were
developed to handle data at scale.
Security and data privacy became more critical, with storage management now having to
integrate encryption, access controls, and backup/recovery mechanisms to safeguard data.

Importance of Storage Management:


Efficient storage management ensures:
Data accessibility: Files are organized in a way that they can be easily found and accessed by
users and applications.
Resource optimization: Storage space is used efficiently, and memory (RAM) is managed to
allow multiple applications to run smoothly.
System performance: Properly managed storage reduces disk I/O bottlenecks, enhancing the
overall speed of the system.
Data security and integrity: Through access control, encryption, and backup mechanisms,
storage management helps protect data from unauthorized access or loss.
Swapping

44
o Swapping in an operating system is a process that moves data or programs between
the computer’s main memory (RAM) and a secondary storage (usually a hard
disk or SSD).
o This helps manage the limited space in RAM and allows the system to run more
programs than it could otherwise handle simultaneously.
o It’s important to remember that swapping is only used when data isn’t available in
RAM. Although the swapping process degrades system performance, it allows larger
and multiple processes to run concurrently.
o Because of this, swapping is also known as memory compaction. The CPU scheduler
determines which processes are swapped in and which are swapped out.
o Consider a multiprogramming environment that employs a prioritybased scheduling
algorithm.
o When a highpriority process enters the input queue, a lowpriority process is swapped
out so the highpriority process can be loaded and executed.
o When this process terminates, the lowpriority process is swapped back into memory
to continue its execution.
The below figure shows the swapping process in the operating system:

Swapping has been subdivided into two concepts: swapin and swapout.
• Swapout is a technique for moving a process from RAM to the hard disc.
• Swapin is a method of transferring a program from a hard disc to main memory, or
RAM.
Process of Swapping

45
• When the RAM is full and a new program needs to run, the operating system selects a
program or data that is currently in RAM but not actively being used.
• The selected data is moved to the secondary storage, making space in RAM for the
new program.
• When the swappedout program is needed again, it can be swapped back into RAM,
replacing another inactive program or data if necessary.
Contiguous Memory Allocation

Contiguous Memory Allocation is a memory management technique used in operating


systems where each process is assigned a single continuous block of memory. This method
ensures that all parts of the process are stored in adjacent memory locations, simplifying
memory access but also having certain limitations.

Key Features of Contiguous Memory Allocation:


1. Single Block Allocation: Each process is given one continuous block of memory.
2. Base and Limit Registers: The operating system maintains two registers for each process:
Base register: Stores the starting address of the allocated memory.
Limit register: Stores the size of the allocated memory block.

3. Memory Allocation Types:


FixedPartition Allocation: The memory is divided into fixedsize partitions, and each
partition is allocated to a single process. This method can lead to internal fragmentation,
where the partition size is larger than the process size, leaving unused memory within the
partition.
VariablePartition Allocation: The memory is divided into partitions dynamically based on
process size. However, this can cause external fragmentation, where small free spaces
between allocated blocks become unusable.

46
Advantages of Contiguous Memory Allocation:
Simple and easy to implement: Since each process occupies a single block of memory, the
OS only needs to track the base and limit addresses.
Fast memory access: The CPU can quickly calculate memory addresses since all parts of the
process are stored contiguously.

Disadvantages of Contiguous Memory Allocation:


Fragmentation:
Internal Fragmentation: Occurs when fixedsize partitions are used, leading to wasted space
if the process doesn't fully use the allocated partition.
External Fragmentation: As processes are loaded and removed, free memory gets scattered
in small chunks that may not be sufficient for new processes.

Limited scalability: Contiguous allocation doesn't efficiently support dynamic processes


whose memory requirements grow or shrink, as expanding memory for a process requires
finding another contiguous block or relocating the entire process.

Solutions to Fragmentation:
Compaction: A technique used to reduce external fragmentation by moving allocated
memory blocks together and consolidating free space into one large block. However,
compaction is timeconsuming and resourceintensive.

Example:
Suppose a system has 100 MB of memory, and three processes need 20 MB, 30 MB, and 10
MB. The operating system would allocate a continuous block of memory for each process
based on their requirements, but this could lead to unused gaps if the processes terminate or
new processes cannot fit into the available memory.
Paging
Paging is a memory management technique used in operating systems to avoid the issues of
fragmentation (both internal and external) that arise with contiguous memory allocation. In
paging, the physical memory is divided into fixedsize blocks called frames, and the logical
memory (the address space used by a process) is divided into blocks of the same size, called
pages. This allows noncontiguous allocation of memory, meaning that a process can be
loaded into memory in a scattered manner.

47
Key Concepts in Paging:

1. Page and Frame:


Page: A fixedsize block of logical memory (the process's address space).
Frame: A fixedsize block of physical memory (RAM). Frames and pages are of the same
size, so any page can fit into any frame.

2. Page Table:
Each process has its own page table, which keeps track of where its pages are stored in
physical memory. The page table contains mappings from page numbers (logical addresses)
to frame numbers (physical addresses).
When a process references a memory address, the operating system uses the page table to
translate the logical page number to the corresponding physical frame.

3. Address Translation:
A logical address consists of two parts: the page number and the offset.
The page number identifies which page of the logical address space the address belongs to,
and the offset specifies the exact location within that page.
Using the page number, the system consults the page table to find the corresponding frame
number. The offset is then added to the starting address of the frame to determine the exact
physical memory address.

Address Translation Example:


If a process needs to access memory at logical address `1025`, and the page size is `1000`,
the system calculates the page number and offset as:
Page number = `1025 / 1000 = 1` (second page)
Offset = `1025 % 1000 = 25` (25th byte within the second page)

The system checks the page table to see which frame contains the second page and then
calculates the physical address by adding the offset.

Advantages of Paging:
1. Eliminates External Fragmentation: Since pages can be loaded into any available frame,
physical memory is used more efficiently, with no need for contiguous allocation.

48
2. Supports Larger Virtual Memory: Paging allows processes to use more memory than is
physically available by storing parts of the process in virtual memory (secondary storage).

3. Efficient Memory Utilization: Pages are loaded into memory as needed, which can
improve overall memory usage, especially when some parts of a program are not used
frequently.

Disadvantages of Paging:
1. Internal Fragmentation: Although paging eliminates external fragmentation, there may still
be some internal fragmentation if the last page of a process doesn’t fully use the allocated
frame, leaving some space wasted.

2. Overhead of Page Table Management: Maintaining page tables for each process requires
additional memory. For large processes, page tables can become very large, increasing
overhead.

3. Page Table Access Time: Every time a process accesses memory, the system must consult
the page table, which can slow down memory access. This can be mitigated using Translation
Lookaside Buffers (TLB), a type of cache that stores frequently used page table entries.

Types of Paging:
1. Demand Paging: Pages are only loaded into memory when they are needed. If a page is not
in memory when accessed, a page fault occurs, and the OS fetches the page from secondary
storage.
2. Multilevel Paging: For large address spaces, page tables themselves can become very
large. Multilevel paging breaks the page table into smaller parts, reducing memory usage.

Example of Paging:
Assume a process with a logical address space of 4000 bytes and a page size of 1000 bytes.
The process will be divided into 4 pages (Page 0, Page 1, Page 2, Page 3). These pages can be
loaded into any available physical memory frames, say Frame 5, Frame 2, Frame 8, and
Frame 4, respectively. The page table for the process will map each page to its corresponding
frame.

| Logical Page | Physical Frame |

49
|||
| Page 0 | Frame 5 |
| Page 1 | Frame 2 |
| Page 2 | Frame 8 |
| Page 3 | Frame 4 |

Page Fault:
A page fault occurs when a process tries to access a page that is not currently loaded in RAM.
When this happens:
The OS retrieves the page from secondary storage (usually a hard disk or SSD) and loads it
into a free frame.
The page table is updated to reflect the new pageframe mapping.

In summary, paging is a flexible memory management technique that solves fragmentation


problems by dividing both memory and processes into fixedsize units, allowing efficient
memory allocation and access.

Structure of the Page Table


The structure of a page table is crucial for translating a process's logical (virtual) addresses
into physical addresses in memory. The page table acts as a data structure that maps each
page number in the virtual address space to a corresponding frame number in physical
memory.

Basic Structure of a Page Table:

1. Page Number: The index of the page in the process’s logical address space. The page table
is indexed by page numbers to look up frame numbers.

50
2. Frame Number: The entry in the page table corresponding to each page number. This
points to the physical frame in which the page is currently stored in the physical memory
(RAM).

Each row in the page table corresponds to a single page in the process’s logical address space
and maps to a frame in the physical memory.

Page Table Structure Example:


| Page Number | Frame Number | Status (Valid/Invalid) |
||||
|0 |5 | Valid |
|1 |2 | Valid |
|2 | | Invalid (Not in memory) |
|3 |7 | Valid |

Page Number: Indicates the index of the page in the logical address space.
Frame Number: Points to the corresponding frame in physical memory. If the page is not
currently in memory (for example, due to swapping), the frame number may be empty or
invalid.
Status (Valid/Invalid): Indicates whether the page is currently loaded in memory. A valid
entry means the page is in physical memory, while an invalid entry indicates the page is not
loaded (page fault will occur if accessed).

Components of a Page Table Entry (PTE):


A Page Table Entry (PTE) contains several important pieces of information in addition to the
frame number. A PTE may look like this:

51
MultiLevel Page Tables:
For large address spaces, a singlelevel page table becomes inefficient, as the table itself could
be too large to fit into memory. To address this, multilevel page tables break down the page
table into smaller parts, reducing memory overhead.

# 1. TwoLevel Page Table:


The virtual address is divided into two parts: the page directory and the page table. The
page directory points to a set of page tables, and each page table points to frames.
Example structure:
Virtual address: [Directory | Page | Offset]
The directory points to a page table, which then provides the frame number.

# 2. ThreeLevel Page Table:


In systems with larger address spaces (e.g., 64bit systems), threelevel page tables may be
used to further reduce the size of individual page tables.

Translation Lookaside Buffer (TLB):


A Translation Lookaside Buffer (TLB) is a cache that stores a small subset of page table
entries to speed up address translation. Instead of consulting the page table on every memory
access, the TLB can provide the frame number directly for recently accessed pages, reducing
the overhead of address translation.

Summary:

52
Page Table is a key component in paging, mapping virtual page numbers to physical frame
numbers.
Each Page Table Entry (PTE) contains the frame number and additional status bits like the
valid/invalid bit, dirty bit, and access permissions.
For large address spaces, multilevel page tables are used to manage memory more efficiently.
TLB is a cache used to speed up memory access by storing frequently accessed page table
entries.

This structure allows efficient memory management and enables the OS to handle large
processes that may not fit entirely in physical memory.

Segmentation
Segmentation is a memory management technique in operating systems that divides a
process's memory into logically distinct units called segments. Unlike paging, where the
memory is divided into fixedsize pages, segmentation divides memory into variablesized
segments, each representing a specific type of data or functionality. Each segment
corresponds to a logical division of a program, such as code, data, stack, heap, etc., allowing
for more meaningful memory organization.
Key Concepts of Segmentation:
1. Segment:
o A segment is a variablesized portion of a process's memory that corresponds
to a logical unit. Each segment can be thought of as a different part of the
program, like the code segment, data segment, stack segment, or heap
segment.
o Unlike paging, where all pages are the same size, segments can have different
sizes depending on the program’s needs.
2. Segment Table:
o The operating system uses a segment table to keep track of each segment's
base address (starting location in physical memory) and its limit (size of the
segment).
o Each process has its own segment table, which maps segment numbers to
physical memory addresses.
Structure of the Segment Table:
A segment table entry typically contains two fields:
• Base Address: The starting address of the segment in physical memory.
• Limit: The length (size) of the segment.

53
Segment Number Base Address Limit (Size)

0 3000 1200

1 4200 800

2 5000 1500

3 7000 1000

Address Translation in Segmentation:


A logical address in segmentation consists of two parts:
• Segment Number: Identifies which segment the address belongs to.
• Offset: The specific location within the segment.
The operating system uses the segment number to look up the segment's base address in the
segment table, and then the offset is added to the base address to get the physical memory
address. However, before this, the OS checks if the offset is within the segment's limit to
prevent access beyond the segment's boundaries.
Example: Suppose the logical address is (2, 100), where 2 is the segment number and 100 is
the offset. If the base address of segment 2 is 5000 and its limit is 1500, the physical address
would be:
• Physical address = Base address + Offset = 5000 + 100 = 5100.
The system also checks whether the offset (100) is less than the segment's limit (1500) to
ensure the address is valid.
Advantages of Segmentation:
1. Logical Division: Segmentation mirrors the logical structure of programs. Code, data,
and stack segments are separated, allowing easier handling of different types of
memory.
2. Dynamic Memory Allocation: Segments can grow or shrink based on program
needs, which is more flexible compared to fixedsize pages.
3. Protection: Since segments can have different access permissions, the system can
enforce different levels of protection (e.g., readonly code segments).
4. Supports Sharing: Segments, especially code segments, can be shared between
processes without duplicating data, leading to memory savings.
Disadvantages of Segmentation:
1. External Fragmentation: Since segments are of variable size, free memory may
become fragmented as segments are allocated and deallocated, leading to inefficient
use of memory.
2. Complex Address Translation: Segmentation involves additional complexity in
address translation compared to paging, especially in managing segment tables.

54
3. Limited Scalability: Handling large numbers of segments can be challenging, and
maintaining segment tables can consume significant memory.
Segmentation vs Paging:
• Memory Division: In segmentation, memory is divided into logical units (segments),
while in paging, memory is divided into fixedsize pages.
• Fragmentation: Segmentation suffers from external fragmentation (because of
variable segment sizes), while paging suffers from internal fragmentation (because
the last page may not be fully used).
• Logical Meaning: Segmentation corresponds to logical units like functions, arrays,
and data, while paging is purely a physical memory management technique.
• Address Structure: Segmentation uses a segment number and offset for address
translation, while paging uses a page number and offset.
Example of Segmentation:
Consider a process that is divided into four segments: code, data, stack, and heap. Each
segment has a different size and is stored at different locations in memory.

Segment Type Segment Number Base Address Limit (Size)

Code 0 1000 4000

Data 1 5000 3000

Stack 2 8000 1000

Heap 3 9000 2000

In this example:
• The code segment is 4000 bytes, starting at physical address 1000.
• The data segment is 3000 bytes, starting at physical address 5000.
• The stack and heap segments are of different sizes and have different starting points
in memory.
Segmentation with Paging:
To overcome the limitations of pure segmentation (especially external fragmentation), some
systems combine segmentation and paging. In this scheme:
• Each segment is divided into fixedsize pages, and those pages are loaded into
memory.
• This combines the benefits of logical segmentation and the efficient memory use of
paging.

55
Virtual memory background
Virtual memory is a memory management technique used in operating systems to give the
illusion of a large, continuous block of memory to each process, even though the physical
memory (RAM) is limited. It allows the system to execute programs that may require more
memory than what is physically available by temporarily transferring data between the
physical memory and secondary storage (like a hard disk or SSD).

Background of Virtual Memory:


Purpose: Virtual memory was developed to allow systems to efficiently run large
applications and multitask by using disk storage to extend the system's usable memory space.
Concept: The key idea is that not all parts of a program need to be in physical memory at the
same time. Portions of a program that are not currently being used can be stored on disk (in a
swap file or paging file), and only the actively used parts are kept in RAM. When a process
needs to access data that isn't in RAM, the operating system loads it from disk into memory
through a process called paging.
Key Benefits:
Larger Address Space: Programs can use more memory than what is physically available,
making it possible to run large applications on systems with limited RAM.
Isolation: Virtual memory provides isolation between processes, meaning each process
operates in its own virtual address space, preventing one process from interfering with
another.
Efficient Memory Utilization: Only the necessary portions of programs are loaded into
memory, while the rest can reside on disk until needed.

Key Concepts of Virtual Memory:

1. Virtual Address Space:


Virtual memory creates a separate address space for each process. This virtual address
space may be larger than the available physical memory, allowing processes to operate as if
they have access to a large block of contiguous memory.

56
The virtual address space is mapped to physical memory by the operating system, using a
combination of paging and segmentation.

2. Paging:
Paging is a key mechanism used in virtual memory. In paging, both the virtual memory and
physical memory are divided into fixedsize blocks: pages (in virtual memory) and frames (in
physical memory). The operating system maintains a page table that maps virtual pages to
physical frames.
When a process accesses a page that is not in physical memory, a page fault occurs. The
operating system then loads the missing page from secondary storage (disk) into RAM.

3. Demand Paging:
In demand paging, pages are loaded into memory only when they are needed. This
minimizes the amount of physical memory used at any given time, as pages are fetched on
demand rather than all at once.

4. Page Replacement Algorithms:


When physical memory becomes full, the operating system needs to decide which pages to
swap out to make room for new ones. This is managed through page replacement algorithms,
such as:
Least Recently Used (LRU): Replaces the page that has not been used for the longest
time.
FirstInFirstOut (FIFO): Replaces the oldest loaded page.
Optimal Algorithm: Replaces the page that will not be used for the longest time in the
future (theoretical, as future usage is unpredictable).

5. Thrashing:
Thrashing occurs when a system spends more time swapping pages in and out of memory
than executing actual processes. This can happen when the system is overloaded and has
insufficient memory for the working set of all active processes. Thrashing severely degrades
performance.

Benefits of Virtual Memory:


1. Efficient Use of RAM: By loading only necessary parts of programs into memory, virtual
memory makes better use of the limited physical memory, enabling the system to run larger
programs or more programs concurrently.

57
2. Isolation and Security: Virtual memory provides each process with its own virtual address
space, protecting processes from each other. This isolation enhances both security and
stability, as one process cannot directly access another process’s memory.

3. Multitasking: Virtual memory enables multiple programs to run simultaneously, even if


their combined memory requirements exceed the system's physical RAM.

4. Memory Protection: Since each process operates in its own virtual address space, the
operating system can enforce access control, protecting processes from accessing or
modifying the memory of other processes or the kernel.

Disadvantages of Virtual Memory:


1. Disk I/O Overhead: Accessing data from disk is much slower than accessing it from RAM.
Frequent page swaps between RAM and disk (especially during thrashing) can significantly
degrade system performance.

2. Complexity: Implementing and managing virtual memory requires complex operating


system mechanisms, such as page tables, translation lookaside buffers (TLBs), and page
replacement algorithms.

3. Increased Resource Usage: The need for additional memory and CPU resources to
maintain page tables, manage page faults, and handle disk I/O can increase overall system
overhead.

Example of Virtual Memory Operation:


Consider a system with 4 GB of physical memory but processes that collectively require 10
GB of memory. Virtual memory allows the system to keep only the actively used portions of
the processes in RAM, while less frequently used parts are stored on disk. When a process
accesses a part of its memory that isn't currently in RAM, a page fault occurs, and the
operating system loads the required page from disk into memory.

Virtual Memory Mapping:


Virtual memory uses address translation to map virtual addresses to physical addresses. This
translation is done via:
1. Page Tables: Store the mappings between virtual pages and physical frames.

58
2. Translation Lookaside Buffer (TLB): A cache that stores frequently used page table entries
to speed up address translation.
3. MMU (Memory Management Unit): A hardware component responsible for performing the
address translation from virtual to physical addresses.

Demand paging
Demand Paging is a memory management technique used in virtual memory systems where
pages of a process are loaded into memory only when they are needed, rather than loading the
entire process into memory at once. This lazy loading approach helps to conserve memory by
loading only the required pages and keeping the others on disk until they are accessed.

Key Concepts of Demand Paging:

1. Lazy Loading:
In demand paging, when a process starts execution, no pages are loaded into memory
initially. Instead, the pages are brought into memory on demand, i.e., only when the process
tries to access them.
This approach contrasts with prepaging, where multiple pages may be loaded in
anticipation of future use.

2. Page Fault:
When a process tries to access a page that is not currently in physical memory (because it
hasn't been loaded yet), a page fault occurs.
A page fault is an interrupt that notifies the operating system to load the required page from
secondary storage (disk) into physical memory.

3. Page Replacement:
If physical memory is full and a new page needs to be loaded (due to a page fault), the
operating system must decide which page in memory to swap out. This is handled by page
replacement algorithms such as Least Recently Used (LRU), FIFO, or Clock algorithms.

4. Page Table:
Each process has a page table that keeps track of whether a page is in physical memory or
on disk. If the page is in memory, the page table entry has a valid bit set. If it's on disk, the
valid bit is cleared, and the page fault handler will load it into memory when accessed.

59
5. Secondary Storage (Swap Space):
Pages that are not currently in physical memory are stored in a special area of the disk
known as swap space. When a page fault occurs, the operating system retrieves the page from
this area and loads it into memory.

How Demand Paging Works:

1. Process Execution:
When a process is started, its pages are not immediately loaded into memory. The
operating system only loads a minimal portion of the process into memory to begin
execution, typically the first instruction.

2. Page Access:
As the process executes, it will try to access various pages in its virtual address space. If
the required page is already in memory, the CPU accesses it normally.

3. Page Fault Handling:


If the required page is not in memory (i.e., a page fault occurs):
1. The CPU triggers a page fault interrupt.
2. The operating system identifies the missing page.
3. The OS locates the page in secondary storage (swap space).
4. If memory is full, the OS may swap out another page to make space (page replacement).
5. The requested page is loaded into memory, and the page table is updated.
6. The process resumes execution from where the page fault occurred.

Example of Demand Paging:


Assume a process has 5 pages (P1, P2, P3, P4, P5), but physical memory can only hold 3
pages at a time. At the start, none of the pages are in memory. The execution proceeds as
follows:

1. The process tries to access P1, which triggers a page fault since P1 is not in memory.
2. The OS loads P1 from disk into memory.

60
3. The process now accesses P2, causing another page fault.
4. The OS loads P2 into memory, and so on.

If the process tries to access P4 when memory is full, the OS must decide which page to evict
(P1, P2, or P3) using a page replacement algorithm.

Advantages of Demand Paging:


1. Efficient Use of Memory: Only the pages that are actively being used by a process are
loaded into memory, reducing memory waste.
2. Faster Program Startup: Programs can start quickly since not all pages need to be loaded
initially.
3. Support for Large Programs: Demand paging allows programs that require more memory
than is physically available to run efficiently by loading pages on demand.
4. Multitasking: Demand paging allows multiple processes to run concurrently without using
excessive memory, as each process will only load the pages it needs.

Disadvantages of Demand Paging:


1. Page Fault Overhead: Every time a page fault occurs, the system experiences overhead as
it needs to load the required page from disk into memory. This can slow down program
execution.
2. Thrashing: If the system is overloaded with page faults (due to insufficient memory or too
many processes running), it may enter a state known as thrashing, where the system spends
most of its time swapping pages in and out of memory rather than executing processes.
3. Increased Complexity: Managing demand paging adds complexity to the operating system,
especially in handling page faults, page replacement, and maintaining page tables.

Page Replacement Algorithms in Demand Paging:


When physical memory is full and a new page needs to be loaded, the operating system must
choose which page to remove from memory. This decision is made using page replacement
algorithms:
FIFO (First In First Out): Removes the oldest page from memory.
LRU (Least Recently Used): Removes the page that has not been used for the longest time.
Optimal Page Replacement: Theoretical algorithm that replaces the page that will not be
used for the longest time in the future (impossible to implement perfectly in practice).

61
Clock Algorithm: A more efficient approximation of LRU that uses a circular buffer and a
reference bit.

Summary:
Demand paging is an efficient memory management technique that loads pages into memory
only when they are needed, reducing memory usage and allowing programs to run that are
larger than the physical memory available. It works through lazy loading and page faults,
with the operating system deciding which pages to swap in and out as processes execute.
While it offers several advantages, such as efficient memory usage and support for large
applications, it can lead to performance issues like page fault overhead and thrashing if not
managed properly.
CopyonWrite
CopyonWrite (COW) is an optimization technique used in memory management systems to
efficiently handle the duplication of data between processes. When two or more processes
share the same data (usually after a fork system call), instead of creating an immediate copy
of the data for each process, the operating system allows them to share the same memory
until one of the processes modifies the data. At that point, a copy of the shared data is made,
and the modification is applied to the new copy.

This technique is particularly useful in reducing the overhead of memory copying and
optimizing system performance, especially when working with processes that do not
immediately modify the shared data.

Key Concepts of CopyonWrite (COW):

1. Shared Memory Until Modification:


When a process (e.g., a parent process) is forked to create a child process, both the parent
and child processes share the same memory pages.
This shared memory continues until one of the processes attempts to modify the shared
data. At that point, the operating system makes a copy of the data for the modifying process.

2. Copy on Write Trigger:


The key idea is that memory copying is deferred until a write operation is performed by
one of the processes.
When the shared memory is modified, a page fault occurs, signaling the operating system
to copy the page, so the modifications can be applied without affecting the other process that
is still using the original data.

62
3. Page Table Marking:
Memory pages shared between processes are marked as readonly. This prevents any
process from modifying the data directly.
When a process attempts to write to a shared page, a write access violation occurs, which
triggers the CopyonWrite mechanism. The operating system then allocates a new page, copies
the data, and allows the process to make changes to the new copy.

4. Efficient Forking:
COW is commonly used during the fork system call in Unixlike operating systems.
Fork creates a new process (child) that is a duplicate of the calling process (parent).
Without COW, all memory pages of the parent would have to be copied to the child, even if
the child doesn’t modify them. With COW, the parent and child initially share all pages,
saving time and memory.
Only when the child or parent modifies a shared page is a copy made, reducing the
overhead of process creation.

Example of CopyonWrite:

Consider a process P that has 3 memory pages: A, B, and C.


When P forks a child process P', instead of copying pages A, B, and C immediately, both
processes share the same pages in memory.
If P or P' attempts to modify page B, the operating system detects this and copies page B for
the modifying process. Now P and P' have their own copies of page B, but they continue
sharing page A and page C until further modifications occur.

Steps in CopyonWrite:

1. Forking a Process:
A parent process forks a child process. The operating system marks all pages of the parent
process as readonly and maps the child process to the same pages.

2. Read Access:
Both processes can read from the shared memory pages without issue, since the data hasn’t
been modified.

63
3. Write Access (COW Trigger):
If one of the processes (say the child) tries to modify a page, a page fault occurs because
the memory is marked as readonly.
The operating system then creates a copy of the page for the child, marking the new page
as writable. The child can now modify its copy without affecting the parent’s data.

4. Process Separation:
Over time, as both processes make modifications, they start operating on their own copies
of the memory pages. Pages that remain unchanged continue to be shared.

Advantages of CopyonWrite:

1. Memory Efficiency:
COW optimizes memory usage by sharing memory pages between processes until one of
them modifies the data. This reduces the need for redundant memory copying.

2. Performance Improvement:
By deferring the memory copy operation, COW improves performance, especially in
scenarios where many processes are created (like in forking), but only a few modifications
are made to the shared memory.

3. Faster Process Creation:


Since forked processes initially share memory pages, the process creation time is reduced,
especially when the process creates a child that doesn’t immediately modify data (e.g.,
daemon processes or background services).

4. Lower Overhead:
COW minimizes the memory and CPU overhead by copying data only when necessary,
improving system performance in multitasking environments.

Disadvantages of CopyonWrite:

1. Page Fault Overhead:


64
While COW optimizes memory usage, the process still incurs page fault overhead when a
modification occurs, which can slow down the process if there are many writes.

2. Complexity:
Implementing COW requires additional complexity in managing memory and handling
page faults. The operating system needs to carefully manage readonly and writable pages
across processes.

Use Cases of CopyonWrite:

1. Process Forking:
In Unixbased systems, fork is a common system call used to create new processes. COW is
employed to minimize memory duplication when a process forks. For instance, after forking,
the child process may call `exec()` to replace its address space with a new program, making
the initial copy of the address space unnecessary.

2. Virtual Machines and Containers:


COW is used in virtual machine and container environments where virtual machines or
containers can share the same underlying operating system or application data. COW allows
efficient snapshots and cloning of virtual machines or containers.

3. File Systems:
Certain file systems, such as Btrfs and ZFS, use COW to handle file updates. Instead of
overwriting the original data when modifying a file, they create a new copy of the data. This
approach provides features like atomic updates, snapshots, and data integrity.

Summary:
CopyonWrite (COW) is a memory optimization technique where memory pages are shared
between processes until one process attempts to modify the page. At that point, a copy of the
page is made, allowing the modifying process to have its own copy without affecting the
other processes. COW is widely used in process creation, virtual machines, and file systems
to optimize memory usage and reduce overhead. However, it introduces complexity in
memory management and can incur page fault penalties during modifications.

Page replacement and algorithms

65
Page replacement is a key aspect of memory management in operating systems, especially in
systems using paging or demand paging. It occurs when the operating system needs to bring a
new page into memory, but all physical memory (RAM) frames are already occupied. The OS
must then decide which existing page in memory should be removed (or evicted) to make
room for the new page. This decision is made by a page replacement algorithm.

Key Concepts in Page Replacement:

1. Page Fault:
When a process attempts to access a page that is not currently in memory, a page fault
occurs.
The OS needs to load the requested page from secondary storage (disk) into RAM. If there
are no free frames available, it must replace an existing page.

2. Victim Page:
The victim page is the page selected to be evicted from memory to free up space for the
new page. This choice is critical to system performance, as choosing the wrong page can lead
to frequent page faults and slow down the system.

3. Page Replacement Algorithms:


The OS uses page replacement algorithms to select the victim page in an efficient way,
minimizing the number of page faults and maximizing system performance.

Page Replacement Algorithms:

1. Optimal Page Replacement (OPT):


Description: This theoretical algorithm selects the page that will not be used for the longest
time in the future. It guarantees the lowest number of page faults because it has perfect
knowledge of future page requests.
Advantages: Minimizes page faults; optimal in performance.
Disadvantages: Impossible to implement in practice because it requires knowledge of
future page accesses.
Use: Mostly used as a benchmark for comparing other algorithms.

2. Least Recently Used (LRU):

66
Description: This algorithm evicts the page that has not been used for the longest time. The
assumption is that pages used recently are more likely to be used again soon.
Advantages: Good approximation of the optimal algorithm, widely used.
Disadvantages: Can be costly to implement, as it requires tracking the order of page
accesses, typically via a linked list or stack, or through hardware support like timestamps.
Implementation:
Stackbased: A stack keeps track of the order in which pages are accessed, and the least
recently used page is at the bottom of the stack.
Counterbased: Each page has a counter that tracks when it was last accessed, and the page
with the oldest counter is evicted.

3. FirstIn, FirstOut (FIFO):


Description: This algorithm evicts the oldest page in memory, regardless of how frequently
it is used.
Advantages: Simple to implement, requiring only a queue to track the order in which pages
are loaded.
Disadvantages: Can lead to poor performance, as the oldest page might still be in use,
leading to frequent page faults. This is known as the Belady's anomaly, where increasing the
number of frames can actually result in more page faults.

4. SecondChance (Clock) Algorithm:


Description: A modified version of FIFO that gives pages a second chance before they are
evicted. Each page has a reference bit that is set when the page is accessed. If a page is
selected for eviction but has its reference bit set, it is given a second chance, and the
reference bit is cleared. The algorithm continues to the next page in a circular buffer (hence,
the name "clock").
Advantages: Improves upon FIFO by considering whether a page has been accessed
recently, reducing the likelihood of evicting active pages.
Disadvantages: Still not as efficient as LRU, and maintaining the circular buffer adds
overhead.

5. Least Frequently Used (LFU):


Description: This algorithm evicts the page that has been used the least frequently. It
assumes that pages with fewer accesses are less likely to be used again.
Advantages: Works well for workloads where some pages are used frequently, while others
are rarely accessed.

67
Disadvantages: Can suffer if pages are accessed heavily at the beginning but not used
again, as LFU might keep those pages in memory longer than necessary.
Implementation: Pages are tracked with a counter, which increments every time the page is
accessed. The page with the smallest counter is evicted.

6. Most Recently Used (MRU):


Description: This algorithm evicts the page that was used most recently, assuming that
pages used recently are less likely to be needed again soon.
Advantages: Useful in certain workloads, such as database operations, where the most
recently accessed pages may not be needed again for a while.
Disadvantages: Not commonly used, as it often performs worse than LRU in general
workloads.

7. Not Recently Used (NRU):


Description: Pages are divided into four categories based on whether they have been
referenced (R bit) or modified (M bit). Pages that haven’t been referenced recently (R = 0)
and haven’t been modified (M = 0) are prime candidates for replacement.
Advantages: Simple and efficient to implement using hardware support (R and M bits).
Disadvantages: Doesn't provide the same level of granularity as LRU.

How Page Replacement Algorithms Work:

1. Page Access:
When a process accesses a page, the OS checks whether the page is in memory. If the page
is not in memory, a page fault occurs.

2. Page Fault Handling:


Upon a page fault, the OS selects a page in memory to evict using the page replacement
algorithm. The evicted page is either discarded (if it hasn’t been modified) or written back to
disk (if it has been modified).

3. Page Replacement:
The OS loads the new page from disk into memory and updates the page table to reflect the
new page’s presence in memory.

68
4. Updating the Page Table:
The page table entry of the newly loaded page is updated to mark it as resident in memory,
and the page table entry of the evicted page is updated to reflect its new status (either on disk
or invalid).

Performance Considerations:

1. Page Fault Rate:


The goal of page replacement algorithms is to minimize the page fault rate—the frequency
with which page faults occur. A lower page fault rate means fewer interruptions to process
execution and better overall system performance.

2. Thrashing:
Thrashing occurs when the system spends more time handling page faults (swapping pages
in and out of memory) than executing actual processes. Thrashing often happens when the
working set (the set of pages a process needs to keep in memory to avoid frequent page
faults) is larger than the available physical memory.

3. Working Set Model:


The working set model helps mitigate thrashing by keeping track of the pages a process
has accessed recently (its working set) and ensuring that enough memory is allocated to hold
the entire working set.

69

You might also like