0% found this document useful (0 votes)
120 views329 pages

Overview of Operating Systems Functions

The document provides an overview of operating systems, detailing their role as intermediaries between users and hardware, and their goals of resource management, user convenience, and efficient hardware utilization. It discusses various types of operating systems, including single-user, multi-user, batch processing, multiprogramming, and real-time systems, along with their advantages and disadvantages. Additionally, it outlines key functions of operating systems such as memory management, process scheduling, device management, and security measures.

Uploaded by

jollyprachi01
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)
120 views329 pages

Overview of Operating Systems Functions

The document provides an overview of operating systems, detailing their role as intermediaries between users and hardware, and their goals of resource management, user convenience, and efficient hardware utilization. It discusses various types of operating systems, including single-user, multi-user, batch processing, multiprogramming, and real-time systems, along with their advantages and disadvantages. Additionally, it outlines key functions of operating systems such as memory management, process scheduling, device management, and security measures.

Uploaded by

jollyprachi01
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

Operating System (BCS-401)

Operating System
•An operating system acts as an intermediary between the
user of a computer and the computer hardware.
Operating system goals:
• Execute user programs and make solving user problems
easier:
1. The OS provides an environment to run user programs
smoothly by handling resource allocation, scheduling, and
execution.
2. It abstracts hardware complexities, allowing users to
interact with programs without needing to manage
low-level system operations.
Operating System
• Make the computer system convenient to use:
1. The OS offers a graphical user interface (GUI) or
command-line interface (CLI) for user-friendly interaction.
Use the computer hardware in an efficient manner:
1. The OS optimizes CPU, memory, and storage utilization by
managing processes and multitasking.
2. It ensures fair resource distribution among applications,
preventing bottlenecks and improving system performance.
Components of a Computer System
Components of a Computer System
● Computer system can be divided into four components:
● Hardware – provides basic computing resources

CPU, memory, I/O devices


● Operating system

Controls and coordinates use of hardware among


various applications and users
● Application programs – define the ways in which the
system resources are used to solve the computing
problems of the users
Word processors, web browsers, database systems,
video games
● Users

People, machines, other computers.


What Operating Systems Do
● The operating system controls the hardware and coordinates
its use among the various application programs for the various
users.
● We can also view a computer system as consisting of
hardware, software, and data.
How They Work Together:
▪ The hardware provides the physical resources and
processing power.
▪ The software provides instructions for how the hardware
should perform tasks.
▪ The data is what is being worked on, stored, or transmitted
by the system
● The operating system provides the means for proper use of
these resources in the operation of the computer system.
What Operating Systems Do
● An operating system is similar to a government. Like a
government, it performs no useful function by itself. It simply
provides an environment within which other programs can do
useful work.
● To understand more fully the operating system's role, we explore
operating systems from two viewpoints:
● The user

● The system
User View
Single-User Computers (e.g., PC, Workstations)
• Designed for a single user to fully utilize system resources.
• Focused on maximizing user tasks, whether for work or
entertainment.
• Operating system prioritizes ease of use and good
performance.

Multi-User Computers (e.g., Mainframes, Computing Servers)


• Multiple users share system resources and may exchange
information.
• The operating system ensures efficient resource utilization
(CPU, memory, and I/O).
• Prevents any single user from monopolizing system
resources.
User View
Handheld Computers (e.g., Smartphones and Tablets)
• These devices primarily use touchscreen displays as interface
for user interaction, allowing user friendly navigation and
input.
• Due to their compact size, they have limited processing
power and memory. They are optimized for usability and
energy efficiency to prolong battery life.
Embedded Computers (e.g., Home Devices and Automobiles)
• Interface may include numeric keypads and indicator lights
for status updates.
• Designed to function autonomously with minimal or no user
intervention.
System View
From a computer's perspective, the OS is the program that
directly interacts with the hardware. It can be viewed in two
different ways.
Resource Allocator
• Manages all system resources (CPU, memory, storage, etc.).
• Resolves conflicting resource requests to ensure fair and
efficient usage.
Control Program
• Oversees program execution to prevent errors.
• Ensures the system is used properly and prevents misuse.
Functions of an Operating System
The primary function of an operating system is to manage and
allocate system resources efficiently to ensure smooth execution
of processes and optimal system performance.
Memory Management:
The operating system manages the Primary Memory or Main
Memory. Main memory is made up of a large array of bytes or
words where each byte or word is assigned a certain address.
An OS performs the following activities for Memory Management:
• OS tracks memory usage by keeping record of allocated and
free memory addresses for each program.
• In multiprogramming, the OS decides the order in which
processes are granted memory access, and for how long.
• It allocates the memory to a process when the process requests
it and deallocates the memory when the process has
terminated.
Functions of an Operating System
Processor Management
The OS manages the order in which processes have access to
the processor, and how much processing time that each process
must stay in the multiprogramming environment. This is called
process scheduling.
• It tracks the status of processes using a program known as
the traffic controller.
• The OS allocates processor to the process and ensures each
process gets enough processing time.
• It de-allocates the processor when a process is no long
needed.
Functions of an Operating System
Device Management
The operating system (OS) manages device communication
through drivers and performs the following tasks:
• Keeping track of all devices connected to the system.
• The OS uses device drivers (software) to communicate with
I/O controllers (hardware), which manage data transfer
between the CPU and peripheral devices.
• It decides which process gets access to a device and for how
long.
• It allocates devices efficiently and deallocates them when
they are no longer required.
Functions of an Operating System
File System Management
A file system is structured into directories to facilitate efficient
navigation and organization. These directories may contain
subdirectories and files. The Operating System performs the
following file management tasks:
• Tracks file locations, access permissions, and attributes.
• Manages file operations such as creation, deletion, transfer,
copying, and storage in a systematic manner.
• Maintains data integrity and ensures the consistency of the
directory structure.
• Implements security measures to protect files from
unauthorized access.
Functions of an Operating System
Secondary Storage Management
Secondary storage management is a crucial OS function that handles
storage devices like HDDs, SSDs, USB drives, and other non-volatile
media.
• Allocates disk space efficiently for files and programs.
• Manages how files are stored, retrieved, and structured within
directories.
• Determines the order in which disk read/write requests are
processed to optimize performance.
• Keeps track of available storage space and reclaims it when files are
deleted.
• Helps in data protection by allowing backups and restoring lost data.
Functions of an Operating System
Security
• The operating system uses password protection to safeguard
user data.
• It prevents unauthorized access to programs and user data.
• For external security threats, additional malware protection
software is required to protect the system.
Job Accounting
• The operating system monitors and records the time and
resources utilized by various processes.
• This information is used to track resource consumption for
specific process.
Functions of an Operating System
Error Detecting Aids
• Operating systems constantly monitor the system operations
to identify and respond to errors, ensuring that the system
functions correctly and remains stable.
• This helps in preventing malfunctions and ensuring the
smooth operation of computer systems.
Coordination Between Other Software and Users
• Operating systems coordinate and manage interpreters,
compilers, assemblers, and other software.
• They assign these resources efficiently to different users of
the computer system.
Operating System - Services
An Operating System Service refers to the high-level
functionalities provided by the OS to users and applications,
enabling efficient interaction with system resources. These
services create a convenient execution environment for
programs and improve user accessibility.
Program execution
Operating System that manages how a program is going to be
executed.
• Loads a program into memory.
• Handles program's execution.
• Provides a mechanism for process synchronization.
• Provides a mechanism for process communication.
• Provides a mechanism for deadlock handling.
Operating System - Services
File system manipulation
A file system is normally organized into directories for easy
navigation and usage. These directories may contain files and
other directories. Major activities of an OS with respect to file
management −
• If a program needs to read a file or write a file. The OS gives the
permission to the program for operation on file.
• Permission varies from read-only, read-write, denied .
• OS provides an interface to the user to create/delete files.
• OS provides an interface to the user to create/delete
directories.
• OS provides an interface to create the backup of file system.
Operating System - Services
I/O Operation
An I/O subsystem comprises of I/O devices and their
corresponding driver software. Drivers hide the peculiarities of
specific hardware devices from the users. An OS manages the
communication between user and device drivers.
• I/O operation means read or write operation with any file or any
specific I/O device.
• Operating system provides the access to the required I/O device
when required.
Operating System - Services
Communication
• Processes may exchange information, that are executing on
the same computer or between processes that are executing
on different computers over a network.
• Data Communication is implemented using Shared Memory
or Message Passing methods, in which packets of
information are moved between processes by the OS.
Operating System - Services
Error handling
Errors can occur anytime and anywhere. An error may occur in
CPU, in I/O devices or in the memory hardware. Major activities of
an OS with respect to error handling −
• The OS constantly checks for possible errors.
• The OS takes an appropriate action to ensure correct and
consistent computing.
Operating System - Services
Resource Management
In a multi-user or multi-tasking environment, the OS allocates
resources like memory, CPU cycles, and file storage to each
processes. Major activities of an OS with respect to resource
management −
• The operating system manages all resources using schedulers.
• CPU scheduling algorithms optimize CPU utilization.

Accounting –
To keep track of which users use how much and what kinds of
computer resources.
Operating System - Services
Protection and security
The owners of information stored in a multiuser or networked
computer system may want to control use of that information,
concurrent processes should not interfere with each other
• Protection means making sure that only authorized users or
programs can access certain data or resources in a computer
system. It ensures that different programs running at the same
time don’t interfere with each other.
• Security is about keeping the system safe from outside threats.
This includes requiring users to log in with a password
(authentication) and blocking unauthorized attempts to access
external devices like USB drives or printers.
Types of Operating System
Serial Processing :
• A method used in early computers (late 1940s – mid-1950s)
where programs were executed sequentially without an
operating system.
• There is no operating system; users interacted directly with
hardware.
• Programs were written in machine language and manually
loaded using punch cards, paper tape, or toggle switches.
• The entire machine was dedicated to a single program until
completion.
• No multitasking; only one program could run at a time.
Types of Operating System
Batch Processing Operating System
• The users of a batch processing operating system do not
interact with the computer directly.
• Each user prepares his “job” on an off-line device like punch
cards and submits it to the computer operator.
• Computer operator collects the jobs from different users and
sort the jobs into batches with similar needs and submit the
batches to the processor one by one.
• All the jobs of one batch executed together.
Types of Operating System
Batch Processing Operating System
Types of Operating System
Advantages of Batch Processing Operating System
• Multiple users can share the batch systems.
• The idle time for the batch system is very less.
• It efficiently manages repetitive large-scale tasks.
Disadvantages of Batch Processing Operating System
• CPU is not used efficiently. When the current process is doing
I/O, CPU is free and could be utilized by other processes
waiting.
• The other jobs will have to wait for an unknown time if any job
fails.
• In batch OS, average response time increases as all processes
are processed one by one.
Types of Operating System
Multiprogramming Operating System
• In this type of operating system, more than one program
reside into main memory.
• Each process needs two types of system time: CPU time and
IO time.
• In a multiprogramming environment, when a process does its
I/O, The CPU can start the execution of other processes.
• Thus, multiprogramming improves the efficiency of the
system.
Types of Operating System
Multiprogramming Operating System
Types of Operating System
Advantages of Multiprogramming OS
• Multiprogramming increases CPU utilization by ensuring that
the CPU always has a program to execute.
• Response time can also be reduced.
• Maximizes the utilization of resources like memory, I/O
devices, and processing power since more than one program
can be kept alive at any time.
• Several jobs are being processed in parallel, significantly more
tasks could be completed within a certain amount of time,
thus enhancing the overall throughput of the system.
• During times when a program is waiting for I/O operations,
the processor does not go idle since it jumps on to another
task to continue processing.
Types of Operating System
Disadvantages of Multiprogramming OS
• Multiple programs increase system complexity, as the
operating system needs to manage multiple processes, memory
management, and scheduling.
• It requires more memory compared to less sophisticated
operating systems because multiple programs run in memory
simultaneously.
• In a multiprogramming system, the OS switches the CPU
between multiple processes, which introduces scheduling
overhead and can reduce overall performance.
Types of Operating System
Multi-Tasking/Time-sharing Operating systems
• The multitasking OS refers to a logical extension of the
multiprogramming OS, which allows user to run many
programs simultaneously.
• It enables a user to complete multiple processes at the same
time.
• The switching of CPU between processes are so fast that it
gives the impression that all processes are executed
simultaneously.
Types of Operating System
Multi-Tasking/Time-sharing Operating systems
Types of Operating System
Advantages of Multitasking OS:
• Maximizes CPU usage by running multiple programs
simultaneously.
• Reduces response time as multiple applications run
concurrently.
• Users can perform multiple tasks at the same time, like editing
documents while downloading files.
• Optimizes memory and processing power allocation among
tasks.
• Enables seamless switching between applications without
delays.
Types of Operating System
Disadvantages of Multitasking OS:
• Frequent context switching adds overhead, reducing CPU
efficiency.
• Multiple active processes require more memory, leading to
higher resource consumption.
• Managing multiple tasks simultaneously makes scheduling
more complex.
• If too many tasks run at once, system performance can
degrade noticeably.
Types of Operating System
Multiprocessing OS
• A multiprocessing operating system uses more than one CPU
to execute multiple processes simultaneously, enhancing
system efficiency.
• It enables parallel processing, which significantly improves the
overall throughput of the system.
• The CPUs share common resources like memory and
input/output devices, promoting better utilization.
• Multiprocessing systems offer greater fault tolerance, as the
failure of one CPU does not halt the system entirely.
Types of Operating System
Multiprocessing OS
Types of Operating System
Interactive Operating System
• An interactive operating system allows real-time
communication between the user and the system.
• Users input commands or actions, and the system responds
immediately with corresponding output.
• It includes user-friendly interfaces, such as graphical user
interfaces (GUIs) or command-line interfaces (CLIs).
• These interfaces empower users to interact with their
computers intuitively and efficiently.
Types of Operating System
Advantage of Interactive OS
• Fast Responsiveness: Ensures minimal delay in processing
user requests.
• Dynamic Resource Allocation: Manages CPU time, memory,
and I/O devices efficiently.
• Example:
• General-purpose OS: Windows, macOS, Linux (supports
both GUI and CLI).
• CLI-Based Systems: Unix, DOS, and Linux terminal.
Types of Operating System
Real Time Operating System
• A Real-Time Operating System (RTOS) is an OS designed to
manage tasks with strict timing constraints.
• Unlike general-purpose OS that focus on overall performance,
an RTOS prioritizes tasks based on urgency, ensuring critical
tasks are completed within predefined deadlines.
• Its key characteristic is deterministic behavior, meaning tasks
execute within guaranteed time frames.
• This makes RTOS essential for time-sensitive applications
such as industrial automation, aerospace systems, medical
devices, and multimedia processing.
Types of Operating System
Soft Real-Time OS
• Prioritizes meeting timing constraints but allows occasional
missed deadlines.
• Used in applications where slight delays do not cause critical
failures.
• Commonly found in multimedia applications like video streaming
and online gaming.
• Enhances user experience but does not require strict timing
guarantees.
Hard Real-Time OS
• Guarantees that all tasks meet strict timing constraints.
• Missing a deadline can result in system failure.
• Essential for safety-critical applications like avionics, medical
devices, and automotive control.
• Ensures precise timing for reliable system operation.
Types of Operating System
Distributed Operating System
• A distributed operating system (DOS) is an OS manage a group
of autonomous computers connected by communication
network through a message passing mechanism which makes
them appear as a single coherent system to the user.
• When a program is executed on a distributed system, the OS
can split the work and assign different parts to different
nodes. The user doesn’t know or where exactly the program
runs — it’s abstracted.
• The fundamental design issues of a distributed operating
system are like those of a traditional operating system. These
include process synchronization, deadlock management,
scheduling, file systems, inter-process communication, and
memory management.
Types of Operating System
Distributed Operating System
Types of Operating System
Single User Operating System
• A single-user operating system allows only one user to
access the computer system at a time.
• It manages all system resources (CPU, memory, storage,
peripherals) exclusively for that user.
• It is commonly used in personal computers and
workstations.
• Examples include:
• Microsoft Windows – Versions like Windows 10 Home or
Windows 11 Home.
• macOS – Apple’s OS tailored for individual users.
• Linux distributions – Examples like Ubuntu, Fedora, or
Debian, configured for single-user environments.
Types of Operating System
Multiuser Operating System
• A multiuser operating system (OS) allows multiple users to
access and use a computer system simultaneously.
• It is designed to manage system resources (CPU, memory,
storage, etc.) efficiently so that each user gets fair access.
• These OSes support concurrent sessions, often over networks
or terminals.
• Example include:
• UNIX and UNIX-like systems: Includes Linux, BSD, and
macOS – all built with strong multiuser capabilities.
• Mainframe operating systems: Like IBM z/OS, designed to
handle thousands of users in enterprise environments.
BIOS Routine
• A BIOS routine refers to a set of low-level programs built into
the computer’s motherboard (BIOS chip) and responsible for
booting of computer.
• Booting is a start-up sequence that starts the operating
system of a computer when it is turned on.
• When the computer is switched on, the CPU has no software
in main memory.
• Following six steps are generally used to complete the booting
process.
Booting
STEP 1:
• BIOS is loaded from a ROM chip on the motherboard when the
computer is powered on.
• BIOS conducts a Power-On Self Test (POST) to ensure
hardware functionality.
• POST checks components such as the CPU, RAM, storage
devices, ports, and peripherals.
• If issues are found, error codes or messages are displayed.
• If all tests pass, BIOS initiates the boot process to load the
operating system.
Booting
STEP 2:
• After completing pre-boot tasks, BIOS reads the bootable
sequence from CMOS (Complementary Metal-Oxide
Semiconductor).
• It searches for the Master Boot Record (MBR) in the first
physical sector of the bootable disk.
• The search follows the boot order set in CMOS configuration.
• For instance, if the boot sequence is: Floppy Disk → Hard Disk
→ CD-ROM, BIOS checks these devices in that order.
• It attempts to boot from the first device containing a valid
MBR.
Booting
STEP 3:
• The boot sequence begins by searching for the Master Boot Record
(MBR) in a predefined order: floppy disk, hard disk, then CD-ROM
drive.
• If the MBR is not found on any device, the ROM displays a "No Boot
device found" message and halts the system.
• When the MBR is located on a bootable disk, the bootstrap loader
is loaded into memory from the boot sector.
STEP 4:
• Bootstrap loader initiates the loading process by first loading the
[Link] file.
• [Link] is a crucial file in the DOS operating system responsible for
handling basic input/output operations and initializing system
components.
Booting
• Following the loading of [Link], the bootstrap loader proceeds
to load the [Link] file.
• [Link] serves as the core file of the DOS operating
system, containing essential system configurations and
functionalities.
• Once both [Link] and [Link] are loaded into memory,
the DOS operating system becomes fully initialized and ready
for user interaction.
STEP 5:
• After loading [Link], the DOS operating system searches
for the Command Interpreter specified in the [Link] file.
• If a Command Interpreter is specified in [Link], it is
loaded into memory.
• The Command Interpreter is responsible for interpreting and
executing user commands in the DOS environment.
Booting
• If no Command Interpreter is specified, DOS loads the default
Command Interpreter, [Link], into memory.
• [Link] provides a command-line interface to the
user, allowing interaction with the operating system.
STEP 6:
• After the Command Interpreter is loaded, the DOS operating
system proceeds to execute the [Link] file.
• [Link] is a batch file containing a series of DOS
commands that run automatically during system startup.
• The commands in [Link] can include setting
environment variables, executing programs, or performing
other system configurations.
Types of Booting
Cold Booting
• Cold booting occurs when the computer starts for the first
time after being powered off or in a shut-down state.
• When the power button is pressed, the system begins by
running instructions from the BIOS (Basic Input/Output
System), which is stored on a ROM chip on the motherboard.
• The BIOS runs a Power-On Self Test (POST) to check if the
computer’s hardware is working correctly.
• POST checks important components like the CPU, RAM,
storage devices, ports, and peripherals.
• If everything passes the test, the BIOS then loads the
operating system from a bootable storage device.
Types of Booting
Warm Booting
• Warm or hot booting occurs when a computer is restarted
while it is already powered on and running.
• This process is often referred to as rebooting and can be
initiated for several reasons, such as system
unresponsiveness, installing new software or hardware,
making configuration changes, or resolving abnormal system
behavior.
• A warm boot is typically done to apply updates or changes
that require the system to restart.
• In some cases, if the system becomes completely
unresponsive, a force restart may be necessary. This can be
triggered using key combinations like Ctrl + Alt + Del or by
pressing a dedicated reset button, if available on the system.
Program VS. Process
Definition
• Program: A program is a set of instructions written in a
programming language that performs a specific task when
executed.
• Process: A process is an instance of a program that is currently
being executed by the operating system.
Execution
• Program: A program is a static entity stored on disk until it is
loaded into memory and executed.
• Process: A process is dynamic and actively running in memory,
executing the instructions defined in the program.
Program VS. Process
Resource Allocation:
• Program: A program does not directly consume system
resources. It only occupies storage space on disk.
• Process: A process consumes system resources such as CPU
time, memory, I/O devices, and network resources while it is
executing.
State:
• Program: A program is typically in a passive or inactive state
until it is executed.
• Process: A process can be in various states such as new,
running, ready, waiting , or terminated, depending on its
execution stage and interactions with the operating system.
Program VS. Process
Inter-process Communication (IPC)
• Program: Programs typically do not communicate directly with
each other.
• Process: Processes can communicate with each other through
inter-process communication mechanisms provided by the
operating system, such as pipes, sockets, shared memory, or
message queues.
Process Address Space
• In operating systems, a process address space refers to the
range of memory addresses that a process can use during its
execution.
• The memory layout of a process is typically divided into
multiple sections as shown in Figure.
Process Address Space
• Text section—It contains executable code.
• Data section—It stores global variables.
• Heap section—Memory, that is dynamically allocated during
program run
• Stack section—Temporary data storage, when invoking
functions (such as function parameters, return addresses, and
local variables)
In operating systems, the two main kinds of address spaces are:
• Physical address space is the set of actual memory locations
in RAM where a program's instructions and data are
ultimately stored and accessed after address translation.
Process Address Space
• Logical address is the address generated by the CPU during
program execution, which is then mapped to a physical
address by the memory management unit (MMU).
Process Identification Information
• Set of attributes used by the operating system to uniquely
identify, manage, and control each running process.
• It includes the Process ID (PID) to uniquely identify each
process in the system.
• It stores the Parent Process ID (PPID) to maintain process
hierarchy, which links a process to the one that created it.
• It records User ID (UID) and Group ID (GID) to manage
ownership and permissions.
• It helps the operating system in tracking, scheduling, and
controlling active processes.
Process State
Process State
When a process executes, it changes it’s state and the possible
states are:
• New − When a new process is created, It enter into the new
state.
• Ready − In this state, the process is waiting to be assigned a
processer.
• Running - Instructions are being executed.
• Waiting – The process is waiting for some event to occurs
such as input/output completion.
• Terminated- In this state, the process has finished its
execution.
Process Control Block (PCB)
In OS each process is represented by a process control block
(PCB) or a task control block. It contains many pieces of
information associated with a specific process that includes
the following.
Process Control Block (PCB)
• Process ID(PID): A distinct Process ID (PID) on the PCB serves as
the process's identifier within the operating system.
• Process State: The state of the process, such as running, waiting,
ready, or terminated, is indicated.
• Program Counter(PC): The program counter value, indicates the
address of the next instruction to be executed in the process.
• CPU Registers: Contains the values of all process-specific CPU
registers such as the program counter (PC), stack pointer (SP),
general-purpose registers, and program status word (PSW) —
essential for resuming process execution accurately.
• CPU Scheduling Information: It contains the information
regarding scheduling being used such as FCFS, SJF, Priority etc.
Process Control Block (PCB)
• Memory Management Information: Contains details about
the memory assigned to a process.
• Accounting Information: It stores important details like how
much CPU the process has used, how long it can run (time
limit) , and other information about the resources it is using.
• I/O status Information: Information regarding the process's
interaction with I/O devices, including pending I/O requests,
and status flags.
• List of Open files: Information related to the open file for the
process.
Context Switching
Context Switching
• Switching of CPU from one process to another process
requires saving the state of old process and loading the saved
state for the new process. This is known as context switching.
• The context of a process is typically represented by various
pieces of information stored in a data structure called a
"process control block" (PCB). This includes things like the
process's program counter, register values, memory
allocation, and other essential information needed to resume
execution of the process from where it left off.
• The switching time between two process is pure overhead,
because the system does no useful work, while switching.
Process Scheduling
• The 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 multiprogramming
operating systems, which allow multiple processes to reside
in memory simultaneously and share the CPU through time
multiplexing.
Categories of Scheduling
[Link] Scheduling:
• In preemptive scheduling, the CPU can be taken away from a
process even if it hasn’t completed its execution.
• The scheduler forcibly suspends the current running process to
assign the CPU to another process (usually of higher priority or
due to time quantum expiry).
• Example: Round Robin (RR), Shortest Remaining Time First
(SRTF)
2. Non-preemptive Scheduling:
• In non-preemptive scheduling, once a process starts executing,
it runs till completion or until it voluntarily releases the CPU
(e.g., waits for I/O).
• The CPU is not taken away forcibly.
• Example : First Come First Serve (FCFS), Shortest Job Next (SJN)
Process Scheduling Queues
The Operating System maintains the following important process
scheduling queues −
• Job Queue: Holds all processes (jobs) in the system, it resides
in secondary memory.
• Ready Queue: Holds processes that are in main memory and
ready to run,
• Device Queue: Holds processes that are blocked and waiting
for an I/O device to become available, it resides in main
memory.
Process Scheduling Queues
Types of Process Scheduler
Long Term Scheduler
• Long term scheduler is also known as job scheduler. It chooses
the Jobs from the pool (secondary memory) and keeps them
in the ready queue maintained in the primary memory.
• Long Term scheduler mainly controls the degree of
Multiprogramming.
• It chooses a good balance between I/O-bound processes and
CPU-bound processes. This balance helps the computer work
efficiently and stay fast.
Types of Process Scheduler
Short term scheduler
• Short term scheduler, also known as CPU scheduler, selects
jobs from the ready queue for execution on the CPU.
• The short-term scheduler operates by continuously keeping
track of the status of all the system’s processes.
• It uses a scheduling algorithm to decide which process should
run next.
• The short-term scheduler can help to reduce process waiting
times and maximize CPU use with the appropriate scheduling
algorithm.
Types of Process Scheduler
Types of Process Scheduler
Medium Term Scheduler
• Medium term scheduler is responsible for the swapping of
processes between main memory and disk storage.
• When a running process requests a resource that is currently
unavailable, it may become suspended.
• To free up memory for other processes and allow them to
execute, the suspended process is moved from main memory
to secondary storage, such as a hard disk drive.
• Swapping is a dynamic process that occurs based on the
system's memory management policies and the current state
of processes in memory.
Various Times related to the Process
Criteria of CPU Scheduling
CPU Utilization
• The main objective of any CPU scheduling algorithm is to keep the
CPU as busy as possible.
• CPU utilization can range from 0 to 100. but in a real-time system, it
varies from 40 to 90 percent depending on the load upon the system.
Throughput
• Number of processes being executed on CPU per unit of time is called
throughput.
Turnaround Time
• The total time taken from the submission (arrival time) of a process to
its completion.
• Turn-around time is the sum of times spent waiting to get into
memory, waiting in the ready queue, executing in CPU, and waiting for
I/O
Turnaround Time=Completion Time−Submission Time
Criteria of CPU Scheduling
Waiting Time
• Waiting time is the amount of time spent by a process waiting
in the ready queue for getting the CPU.
Waiting Time = Turnaround Time – Burst Time
Response Time
• Response Time is the time from a process's arrival to its first
CPU execution.
Response Time=First CPU Allocation Time−Arrival Time
Optimization criteria in scheduling
• Maximum CPU utilization: Ensuring that the CPU is
consistently occupied with executing processes to optimize
system performance.
• Maximum throughput: Maximizing the number of processes
completed per unit of time to enhance system productivity.
• Minimum turnaround time: Reducing the total time taken for
a process to complete execution from arrival to completion.
• Minimum waiting time: Minimizing the duration processes
spend in the ready queue waiting for CPU allocation.
• Minimum response time: Decreasing the time elapsed
between submitting a request and receiving the first response,
crucial for interactive systems.
Scheduling Algorithm- FCFS
• Processes are executed in the order they arrive in the ready
queue; the process that arrives first is executed first.
• It is non-preemptive in nature.
• Implementation is based on a First-In First-Out (FIFO) queue.
• It has poor performance because short processes have to wait
a long time if a long process comes before them.
• Convoy effect can happen, where short processes get stuck
waiting behind long ones, making the system slower and less
responsive.
Scheduling Algorithm- FCFS
Example 1: Consider the following 3 process with their bust time
and arrival time. Draw Gantt chart and compute the turnaround
time and waiting time for each process.

Solution:
Scheduling Algorithm- FCFS
Arrival time
P1: arrives at 0, P2: arrives at 2, P3: arrives at 4,
--------
Completion time
P1: Completes at 6 , P2: Completes at 9 P3: Completes at 16
--------
Now, we can calculate the waiting time and turnaround time for
each process:
For P1:
• Turnaround Time = Completion Time - Arrival Time = 6 - 0 = 6
• Waiting Time = Turnaround Time - Burst Time = 6 - 6 = 0
Scheduling Algorithm- FCFS
For P2:
• Turnaround Time = Completion Time - Arrival Time = 9 - 2 = 7
• Waiting Time = Turnaround Time - Burst Time = 7 - 3 = 4
For P3:
• Turnaround Time = Completion Time - Arrival Time = 16 - 4 = 12
• Waiting Time = Turnaround Time - Burst Time = 12 - 7 = 5
Average Turnaround Time:
(6 + 7 + 12) / 3 = 25 / 3 = 8.33
Average Waiting Time:
(0 + 4 + 5) / 3 = 9 / 3 = 3
Throughput = Number of Processes / Total Execution Time
Throughput = 3 / 16 ≈ 0.1875 processes per unit of time
Scheduling Algorithm- FCFS
Example 2: Consider the following 5 processes with their bust
time and arrival time. Draw Gantt chart and compute the
turnaround time and waiting time for each process.

Solution:
Scheduling Algorithm- FCFS
Now, we can calculate the waiting time and turnaround time for
each process:
For P1:
• Turnaround Time = Completion Time - Arrival Time = 5 - 0 = 5
• Waiting Time = Turnaround Time - Burst Time = 5 - 5 = 0
For P2:
• Turnaround Time = Completion Time - Arrival Time = 9 - 2 = 7
• Waiting Time = Turnaround Time - Burst Time = 7 - 4 = 3
For P3:
• Turnaround Time = Completion Time - Arrival Time = 15 - 4 = 11
• Waiting Time = Turnaround Time - Burst Time = 11 - 6 = 5
Scheduling Algorithm- FCFS
For P4:
• Turnaround Time: Completion Time - Arrival Time = 18 - 6 = 12
• Waiting Time: Turnaround Time - Burst Time = 12 - 3 = 9
For P5:
• Turnaround Time: Completion Time - Arrival Time = 26 - 8 = 18
• Waiting Time: Turnaround Time - Burst Time = 18 - 8 = 10
So, the turnaround time and waiting time for each process are as
follows:
Scheduling Algorithm- FCFS
Let's calculate:
• Total Turnaround Time = (5 + 7 + 11 + 12 + 18) = 53
• Average Turnaround Time = Total Turnaround Time / Number of
Processes = 53 / 5 = 10.6
• Total Waiting Time = (0 + 3 + 5 + 9 + 10) = 27
• Average Waiting Time = Total Waiting Time / Number of
Processes = 27 / 5 = 5.4
• So, the average turnaround time is 10.6 and the average waiting
time is 5.4.
• Throughput = Number of Processes / Total Execution Time
Throughput = 5 / 26 ≈ 0.1923 processes per unit time
Scheduling Algorithm- FCFS
Example 3: Consider the following 6 processes with their bust
time and arrival time. Draw Gantt chart and compute the
turnaround time, waiting time for each process and also find
throughput using FCFS Scheduling.

Solution:
Scheduling Algorithm- FCFS
Process Completion Waiting
Turnaround Time
ID Time Time
P1 5 5 0
P2 7 5 3
P3 10 7 4
P4 12 7 5
P5 14 7 5
P6 23 14 5

Throughput = 6 / 23 ≈ 0.2608 processes per unit time


Scheduling Algorithm- FCFS
Example 4 :Consider the set of 3 processes whose arrival time
and burst time are given below-

Process Id Arrival Time Burst Time

P1 0 2
P2 3 1
P3 5 6
If the CPU scheduling policy is FCFS, calculate the average
waiting time and average turn around time.
Scheduling Algorithm- FCFS

Turn
Waiting
Process Id Arrival Burst Exit time Around
time
Time Time time
P1 0 2 2 2-0=2 2-2=0
P2 3 1 4 4-3=1 1-1=0
P3 5 6 11 11-5=6 6-6=0

Hence,
Average Turn Around time = (2 + 1 + 6) / 3 = 9 / 3 = 3 unit
Average waiting time = (0 + 0 + 0) / 3 = 0 / 3 = 0 unit
Scheduling Algorithm- FCFS

Process Id Arrival Time Burst Time


P1 0 3
P2 1 2
P3 2 1
P4 3 4
P5 4 5
P6 5 2
Scheduling Algorithm- FCFS

Dispatcher
Dispatcher module gives control of the CPU to the process selected
by the short-term scheduler; this involves :
• Context Switching: When the dispatcher selects a new process
to run on the CPU, it needs to switch the CPU's context from the
currently running process to the newly selected one.
• Switching to User Mode: Modern operating systems typically
operate in two modes: kernel mode (system mode) and user
mode. Before transferring control to a user process, the
dispatcher ensures that the CPU switches from kernel mode to
user mode
• Jumping to the Proper Location in the User Program: Once the
CPU is in user mode, the dispatcher jumps to the appropriate
location in the user program to resume its execution. This
location is typically the point where the process was interrupted
or preempted previously.
Dispatcher
Dispatch Latency: It is the time taken by dispatcher to stop one
process and start another process running.
Scheduling Algorithm- SJF
• In SJF scheduling, the CPU is allocated to the process with the
shortest CPU burst time.
• It is considered an optimal scheduling algorithm because it
provides the minimum average waiting time for a set of
processes.
• If two processes have the same CPU burst time, the First Come
First Serve (FCFS) policy is used as a tiebreaker.
• There are two types of SJF scheduling:
• Non-Preemptive SJF: Once the CPU is allocated to a process, it
cannot be taken away until the process completes.
• Preemptive SJF, also known as Shortest Remaining Time First
(SRTF), allows the CPU can be taken away from the current
running process if a new process arrives with a shorter
remaining burst time.
Scheduling Algorithm- SJF
Example 6 : Calculate the average waiting time and turn around
time in (i) Preemptive SJF scheduling (ii) Non-preemptive
scheduling.
Scheduling Algorithm- SJF
Preemptive SJF scheduling

Turnaround Time (TAT)= Completion Time - Arrival Time


Waiting Time: Turnaround Time - Burst Time
Scheduling Algorithm- SJF
Non-Preemptive SJF scheduling
Scheduling Algorithm- SJF
Example 7 : Calculate the average waiting time and turn around
time using SRTF algorithm.
Scheduling Algorithm- SJF
Shortest remaining time first (SRTF)
Scheduling Algorithm- SJF
Example 8 : Calculate the average waiting time and turn around
time using SRTF algorithm.

Note Point: In such question adjust process according to arrival


time.
Scheduling Algorithm- SJF
Scheduling Algorithm- SJF
Example 9 : Calculate the average waiting time and turn around
time using (i) Non preemptive SJF scheduling algorithm (ii) (i)
Preemptive SJF scheduling algorithm
Scheduling Algorithm- SJF
Non preemptive SJF
Scheduling Algorithm- SJF
Example 10: Calculate the average waiting time and turn around
time using Non preemptive SJF algorithm.
Scheduling Algorithm- SJF

So, the turnaround time and waiting time for each process are as
follows:
Scheduling Algorithm- SJF
Example 11 : Calculate the average waiting time and turn around
time using Non preemptive SJF algorithm.
Scheduling Algorithm- SJF
Scheduling Algorithm- Priority Scheduling
• Priority scheduling assigns a priority number to each process.
• Priority numbers may follow different conventions: lower
numbers indicating higher priority or higher numbers
indicating higher priority.
• The CPU is assigned to the process with the highest priority
among the available processes.
• Two types of priority scheduling algorithms exist: Preemptive
and Non-Preemptive.
• In Preemptive priority scheduling, the CPU can be taken away
from a running process if a higher priority process arrives.
• In Non-Preemptive priority scheduling, a process continues
execution until it completes or voluntarily yields the CPU.
Scheduling Algorithm- Priority Scheduling
Handling Equal Priorities in Priority Scheduling
• Tie-Breaking by CPU Burst Time:
– When two processes share the same priority:
• Their CPU burst times are compared.
• The process with the shorter burst time gains
precedence.
• Fallback to First-Come-First-Serve (FCFS):
– In scenarios where processes possess identical burst times:
• Execution order follows the FCFS principle.
• Priority is given to the process that arrived earlier.
Scheduling Algorithm- Priority Scheduling
• Starvation in Priority Scheduling
• Starvation happens when low-priority processes are
continuously delayed because higher-priority processes
keep arriving.
• Aging (Solution to Starvation)
• Aging prevents starvation by gradually increasing the
priority of waiting processes, ensuring they eventually get
executed.
Scheduling Algorithm- Priority Scheduling
Example 12 : Calculate the average waiting time and turn around
time using preemptive priority scheduling and non
preemptive scheduling
Scheduling Algorithm- Priority Scheduling
Preemptive
Scheduling Algorithm- Priority Scheduling
Non Preemptive
Scheduling Algorithm- Priority Scheduling
Example 13 : Calculate the average waiting time and turn around
time using preemptive priority scheduling and non
preemptive scheduling
Scheduling Algorithm- Priority Scheduling
Preemptive
Scheduling Algorithm- Priority Scheduling
Non Preemptive
Scheduling Algorithm- Priority Scheduling
Example 14 : Calculate the average waiting time and turn around
time using priority scheduling.
Scheduling Algorithm- Priority Scheduling
Scheduling Algorithm- Priority Scheduling
Example 15 : Calculate the average waiting time and turn around
time using non preemptive priority scheduling.
Scheduling Algorithm- Priority Scheduling

WT = TAT -
Process AT BT CT TAT = CT - AT
BT
P1 0 3 3 3 0
P2 2 5 8 6 1
P3 1 4 37 36 32
P4 4 2 29 25 23
P5 6 9 27 21 12
P6 5 4 33 28 24
P7 7 10 18 11 1
Scheduling Algorithm-Round Robin
• Round Robin (RR) scheduling is a preemptive CPU scheduling
algorithm in which each process is assigned a fixed time slice
known as a time quantum, during which it can execute.
• When a process begins execution, it runs either for the
duration of the time quantum or until it completes its CPU
burst—whichever occurs first.
• If the process does not finish within the time quantum, it is
preempted and placed at the end of the ready queue.
• The CPU scheduler then selects the next process in the queue
to execute, and this cycle continues until all processes are
completed.
• If there are n processes in the ready queue and the time
quantum is q, then each process effectively receives 1/n of
the CPU time in chunks of q time units.
Scheduling Algorithm-Round Robin
• Each process will wait no longer than (n−1)qtime units before
getting the CPU again. This scheduling ensures fairness and
prevents any single process from monopolizing the CPU for
too long.
• If the time quantum is set too large, Round Robin behaves
similarly to First-Come First-Served (FCFS) scheduling.
• Conversely, if the time quantum is set too small, it leads to
frequent context switches, which can degrade system
performance due to the overhead involved in switching.
Scheduling Algorithm-Round Robin
Example 16 : Calculate the average waiting time and turn around
time using round robin scheduling with time quantum q=4
units.
Scheduling Algorithm-Round Robin
Scheduling Algorithm-Round Robin
Example 17 : Calculate the average waiting time and turn around
time using round robin scheduling with time quantum q=2
units.
Scheduling Algorithm-Round Robin
Scheduling Algorithm- Multilevel Queue
• Multilevel Queue Scheduling (MQS) divides the ready queue
into multiple separate queues based on predefined criteria such
as process priority, memory requirements, or process type
(e.g., system vs. user processes).
• Each process is permanently assigned to one specific queue
according to its characteristics and does not move between
queues.
• Each queue has its own scheduling algorithm, optimized for the
nature of the processes it handles. For example, foreground
processes may be scheduled using Round Robin, while
background processes might use First-Come, First-Served
(FCFS).
• There exists scheduling among the queues themselves, typically
implemented as fixed priority preemptive scheduling.
Scheduling Algorithm- Multilevel Queue
• Priorities among queues determine the order in which they are
serviced, with higher-priority queues preempting lower-priority
ones.
• For instance, the foreground queue may have absolute priority
over the background queue, meaning background processes are
only executed when the foreground queue is empty.
• Let us consider an example of a Multilevel Queue Scheduling
algorithm with five distinct queues, ordered by priority (from
highest to lowest):
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
Scheduling Algorithm- Multilevel Queue
Scheduling Algorithm- Multilevel Feedback Queue
• Multilevel Feedback Queue Scheduling (MFQS) partitions the
ready queue into separate queues based on CPU burst
characteristics. Processes are not permanently allocated to one
queue; instead, they are allowed to move between the queues
dynamically.
• The queues are typically classified into higher priority and lower
priority queues. If a process takes longer time to execute, it may
be demoted to a lower priority queue to prevent them from
monopolizing the CPU.
• The algorithm tends to prioritize I/O-bound and interactive
processes in the higher priority queue, ensuring that they receive
more immediate attention from the CPU scheduler. This
approach helps in maintaining system responsiveness and
ensuring that interactive tasks are serviced promptly.
Scheduling Algorithm- Multilevel Feedback Queue
• In Multilevel Feedback Queue Scheduling (MFQS), each queue
typically has its own time quantum or time slice.
• Higher-priority queues usually have shorter time quanta to
provide faster response times for critical or interactive
processes.
• Lower-priority queues may have longer time quanta, allowing
CPU-bound processes more uninterrupted execution time.
• MFQS often employs multiple feedback mechanisms to adjust
process priorities. These mechanisms may include aging, where
processes in lower priority queues are periodically moved up in
higher priority queue to prevent starvation.
• MFQS can dynamically adjust the scheduling priorities to ensure
optimal performance and resource utilization.
Scheduling Algorithm- Multilevel Feedback Queue

• Three queues:
– Q0 – RR with time quantum 8
milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS

• Scheduling
– A new job enters queue Q0 which is
served FCFS
• When it gains CPU, job receives
8 milliseconds
• If it does not finish in 8
milliseconds, job is moved to
queue Q1
– At Q1 job is again served FCFS and
receives 16 additional milliseconds
• If it still does not complete, it is
preempted and moved to
queue Q2
Scheduling Algorithm- Multiprocessor Scheduling
• Multiprocessor scheduling is a technique used to distribute tasks
across multiple processors in a computer system.
• By executing tasks concurrently, it significantly reduces the total
processing time and improves overall system efficiency.
• There are two primary approaches to multiprocessor scheduling:
1. Symmetric Multiprocessor Scheduling (SMP):
• In this approach, all processors are treated equally, and each
processor can schedule and execute tasks independently. The
operating system typically maintains a common queue of tasks
accessible by all processors.
2. Asymmetric Multiprocessor Scheduling (AMP):
• In this method, one processor is designated as the master and is
responsible for task scheduling. The remaining processors execute
tasks assigned to them by the master processor, reducing scheduling
overhead on slave processors. break into bullet point
Deadlock
• A deadlock situation in operating systems occurs when two or
more processes are unable to complete their execution because
each process is holding at least one resource that is being
requested by another process, resulting in a circular waiting
condition.
System Model
• A system consists of a finite number of resources that must be
shared among multiple competing processes.
• These resources can be categorized into different types, with each
type containing a certain number of identical instances.
• For example: If a system has four processors, then the resource
type "Processor" includes four instances, which can be allocated to
processes as needed.
• Under normal operation, a process utilizes a resource following this
three-step sequence:
• Request: The process requests a resource. If the requested
resource is not available, the process must wait until it becomes
available.
• Use : Once granted, the process uses the resource to perform its
operations.
• Release: The process releases the resource after its use.
Deadlock Characterization
• Deadlock characterization in operating systems refers to the
identification and analysis of the conditions that can lead to
deadlock situations.
• It includes – (i) Necessary conditions for deadlock and (ii)
Resource allocation graph.
Necessary Conditions for Deadlock
• A deadlock occurs if the following four conditions hold
simultaneously in a system.
1. Mutual exclusion:
• At least one resource must be held in a non-sharable mode.
• A mutually exclusive resource can only be used by one process at
a time.
• If another process requests that resource, the requesting process
must be delayed (or wait) until the resource has been released.
Necessary Conditions for Deadlock
2. Hold and wait: A process must be holding at least one resource
and waiting to acquire additional resources that are currently
being held by other processes.
Necessary Conditions for Deadlock
[Link] preemption: Resources cannot be preempted; that is, a resource
can only be released by the process holding it when the process
completes the execution.
• In the diagram below, Process 2 cannot preempt the Resource 1 from
Process 1.
• Resource 1 will only be released by Process 1 when its completes the
execution.
Necessary Conditions for Deadlock
[Link] Wait : A set {P1, P2 ..., Pn} of waiting processes must exist
such that P1 is waiting for a resource held by P2, P2 is waiting for
a resource held by P3, ..., Pn-1 is waiting for a resource held by Pn,
and Pn is waiting for a resource held by P1.
Resource-Allocation Graph
• Deadlocks can be described more precisely in terms of a directed
graph called a system resource-allocation graph.
• This graph consists of a set of vertices V and a set of edges E.
• V is partitioned into two types:
– P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system.
– R = {R1, R2, …, Rm}, the set consisting of all resource types in
the system.
• request edge – directed edge Pi → Rj
• assignment edge – directed edge Rj → Pi
• If graph contains no cycles, then no process in the system is
deadlocked. If the graph does contain a cycle, then a deadlock
may exist.
Resource-Allocation Graph
Example of a Resource Allocation Graph
The sets P, R, and E:
• P = {P1, P2, P3 }
• R = {R1, R2, R3, R4}
• E = {P1 → R1, P2 → R3, R1 → P2,
R 2 → P 1 , R 2 → P 2, R 3 → P 3}
Graph contains no cycles, then no process
in the system is deadlocked.
Resource-Allocation Graph
Example of a Resource Allocation Graph
The sets P, R, and E:
• P = {P1, P2, P3 }
• R = {R1, R2, R3, R4}
• E = {P1 → R1, P2 → R3, P3 → R2 , R1 → P2,
R 2 → P 1 , R 2 → P 2, R 3 → P 3}
Cycle
• P1 → R 1 → P 2 → R 3 → P 3 → R 2 → P 1
• P2 → R 3 → P 3 → R 2 → P 2

Processes P1, P2, and P3 are deadlocked


Resource-Allocation Graph
Example of a Resource Allocation Graph
• Cycle :
P1 → R 1 → P 3 → R 2 → P 1
There is no deadlock.
Deadlock Prevention
By ensuring that at least one of the necessary conditions (mutual
exclusion, hold and wait, no preemption, circular wait) cannot occur
simultaneously, we can prevent the occurrence of a deadlock.
Mutual Exclusion:
• The mutual exclusion condition must hold for non-sharable
resources.
• For example a printer can't be simultaneously shared by several
processes.
• Thus mutual exclusion condition can’t be disallowed.
Hold and wait:
• Allocate all required resources to the process before the start of
its execution, this way hold and wait condition is eliminated but
it will lead to low resource utilization.
Deadlock Prevention
• For example, if a process requires a printer at a later time and we
have allocated a printer before the start of its execution printer
will remain blocked till it has completed its execution.
No Preemption
• If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released.
• Preempted resources are added to the list of resources for which
the process is waiting.
• Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
Circular Wait
• In order to ensure that circular wait condition never holds we
may impose a total ordering of all resource types, and to require
that each process requests resources in an increasing order of
enumeration.
Deadlock Prevention
• In this method resources are numbered. Suppose that we have
three resources: tape drive (1), disk drive (2) and printer (3).
• If a process needs both the disk drive and the printer, it must
first request the disk drive (2) and then the printer (3).
Deadlock Avoidance
• In deadlock avoidance, the request for any resource will be granted
only if the resulting state of the system doesn't lead to deadlock.
• The system continuously checks whether it is in a safe or unsafe
state:
• A safe state means there exists a sequence in which all
processes can complete execution without leading to deadlock.
• An unsafe state does not necessarily mean deadlock has
occurred, but it may eventually lead to one if further requests
are made.
To enable deadlock avoidance:
• The simplest and most useful approach states that the process
should declare the maximum number of resources of each type it
may ever need.
• The Deadlock avoidance algorithm examines the resource
allocation so that there can never be a circular wait condition.
Safe State
• A state is safe, if the system can allocate resources to each
process in some order and avoid deadlock.
• A sequence of processes <P₁, P₂, ..., P > is considered a safe
sequence for the current allocation state if, for each Pᵢ, the
resource requests that Pᵢ can make can be satisfied by the
currently available resources plus the resources held by all P ,
where j < i.
• In this situation, if the resources that Pᵢ needs are not
immediately available, then Pᵢ can wait until all P have finished.
• When Pj have finished, Pi can obtain all of its needed resources,
complete its execution, release its allocated resources, and
terminate.
• If a system is in safe state ⇒ no deadlocks.
• If a system is in unsafe state ⇒ possibility of deadlock.
Safe State
Safe State
• Example 1: Consider a system with twelve resources and three
processes.

Need : P1 = 5, P2 = 2 ,P3=7
Safe Sequence <P2, P1,P3>
Deadlock Avoidance Algorithms
• Single instance of a resource type
– Use a resource-allocation graph

• Multiple instances of a resource type


– Use the banker’s algorithm
Resource Allocation Graph Algorithm
• In this scheme each resource have single instances.
• In addition to the request and assignment edges a new type of
edge, called claim edge Pi → Rj is used to represent that process
Pi may request resource Rj in future; represented by a dashed
line.
• Claim edge converts to request edge when a process requests a
resource.
• Request edge converted to an assignment edge when the
resource is allocated to the process.
• When a resource is released by a process, assignment edge
reconverts to a claim edge.
• Resources must be claimed a priori in the system.
• Suppose that Pi requests resource Rj. The request can be
granted iff on converting the request edge Pi → Rj to an
allocation edge Rj → Pi doesn’t result in the formation of cycle.
Resource Allocation Graph Algorithm

Suppose P2 requests R2. Although R2 is currently free we can’t


allocate to P2. Since this action will create a cycle in the graph.
Banker’s Algorithm
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types
• Available: Vector of length m. If available [j] = k, there are k
instances of resource type Rj available.
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at
most k instances of resource type Rj.
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently
allocated k instances of Rj.
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more
instances of Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j]
Banker’s Algorithm
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1

2. Find an i such that both:


(a) Finish [i] = false
(b) Needi ≤ Work
If no such i exists, go to step 4

3. Work = Work + Allocationi


Finish[i] = true
go to step 2

4. If Finish [i] == true for all i, then the system is in a safe state
Banker’s Algorithm
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait,
since resources are not available
3. Pretend to allocate requested resources to Pi by modifying
the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
● If safe ⇒ the resources are allocated to Pi
● If unsafe ⇒ Pi must wait, and the old resource-allocation
state is restored
Banker’s Algorithm
Example : Consider a system with five processes P0 through P4
and three resources types A,B,C. Resource type A has 10
instances, B has 5 instances and type C has 7 instances. Suppose
at time t0 following snapshot.
Banker’s Algorithm
(i)What will be the content of need matrix?
(ii) Is the system in safe state? If yes, then what is safe
sequence?
(iii) What will happen if process P1 requests one additional
instances of resource type A and two instances of
resource type C.
(iv)If a request (3,3,0) by process P4 arrives in state
defined by (iii) can be granted immediately?
(v)If a request (0,2,0) by process P0 arrives then check
whether it is granted or not?
Banker’s Algorithm
(i) As we know that
Need [i,j] = Max[i,j] – Allocation [i,j]
Banker’s Algorithm
(ii)Work = Available = [3,3,2] and Finish = [F F F F F]
For Pi
Finish [i] = F
if Needi ≤ Work
Then Pi is in safe sequence
Work = Work + Allocationi
Finish [i] = T
For Po :
Finish [0] = F
Need0= 7,4,3 ; Work= 3,3,2
Need0 ≤ Work is false ; So Po must wait.
Finish = [F F F F F]
Banker’s Algorithm
For P1:
Finish [1] = F
Need1 = 1,2,2 ; Work = 3,3,2
Need1 ≤ Work is true So , P1 will be kept in safe sequence
Now Work will be updated as:
Work = 3,3,2 +2,0,0 =5,3,2
Finish = [F T F F F]
---------
For P2 :
Finish [2] = F
Need2= 6,0,0; Work = 5,3,2
Need2 ≤ Work is false; So P2 must wait.
Finish = [F T F F F]
Banker’s Algorithm
For P3:
Finish [3] = F
Need3 = 0,1,1 ; Work= 5,3,2
Need3 ≤ Work is true So , P3 will be kept in safe sequence
Now Work will be updated as:
Work = 5,3,2 +2,1,1 =7,4,3
Finish = [F T F T F]
--------------
For P4:
Finish [4] = F
Need4 = 4,3,1 ; Work= 7,4,3
Need4 ≤ Work is true So , P4 will be kept in safe sequence
Now Work will be updated as:
Work =7, 4, 3+ 0,0,2 = 7,4,5
Finish = [F T F T T]
Banker’s Algorithm
For P0:
Finish [0] = F
Need0 = 7,4,3 ; Work= 7,4,5
Need0 ≤ Work is true So , P0 will be kept in safe sequence
Now Work will be updated as:
Work =7, 4, 5+ 0,1,0 = 7,5,5
Finish = [T T F T T]
-------------
For P2:
Finish [2] = F
Need0 = 6,0,0; Work= 7,5,5
Need2 ≤ Work is true So , P2 will be kept in safe sequence
Now Work will be updated as:
Work =7,5,5 + 3,0,2 = 10,5,7
Finish = [T T T T T]
Thus safe sequence is < P1, P3, P4, P0, P2>
Banker’s Algorithm
(iii) If process P1 requests one additional instances of
resource type A and two instances of resource type C.
Request1 = 1,0,2 ; Need1 = 1,2,2 ; Available = 3,3,2
Step 1 : Request1≤ Need1 is true
Step 2 : Request1≤ Available is true
Step 3: Available = Available – Request1;
Allocation1= Allocation1+ Request1;
Need1 = Need1 – Request1;
Banker’s Algorithm
Banker’s Algorithm
Work = Available = [2,3,0] and Finish = [F F F F F]
For Pi
Finish [i] = F
if Needi ≤ Work
Then Pi is in safe sequence
Work = Work + Allocationi
---------------------------------------------
For Po :
Finish [0] = F
Need0= 7,4,3 ; Work= 2,3,0
Need0 ≤ Available is false ; So Po must wait.
Finish = [F F F F F]
Banker’s Algorithm
For P1:
Finish [1] = F
Need1 = 0,2,0 ; Work = 2,3,0
Need1 ≤ Work is true So , P1 will be kept in safe sequence
Now Work will be updated as:
Work = 2,3,0 +3,0,2 =5,3,2
Finish = [F T F F F]
--------------------------
For P2 :
Finish [2] = F
Need2= 6,0,0; Work = 5,3,2
Need2 ≤ Work is false; So P2 must wait.
Finish = [F T F F F]
Banker’s Algorithm
For P3:
Finish [3] = F
Need3 = 0,1,1 ; Work= 5,3,2
Need3 ≤ Work is true So , P3 will be kept in safe sequence
Now Work will be updated as:
Work = 5,3,2 +2,1,1 =7,4,3
Finish = [F T F T F]
--------------------------
-For P4:
Finish [4] = F
Need4 = 4,3,1 ; Work= 7,4,3
Need4 ≤ Work is true So , P4 will be kept in safe sequence
Now Work will be updated as:
Work =7, 4, 3+ 0,0,2 = 7,4,5
Finish = [F T F T T]
Banker’s Algorithm
For P0:
Finish [0] = F
Need0 = 7,4,3 ; Work= 7,4,5
Need0 ≤ Work is true So , P0 will be kept in safe sequence
Now Work will be updated as:
Work =7, 4, 5+ 0,1,0 = 7,5,5
Finish = [T T F T T]
-------------------------
For P2:
Finish [2] = F
Need0 = 6,0,0; Work= 7,5,5
Need2 ≤ Work is true So , P2 will be kept in safe sequence
Now Work will be updated as:
Work =7,5,5 + 3,0,2 = 10,5,7
Finish = [T T T T T]
Thus safe sequence is < P1, P3, P4, P0, P2>
Banker’s Algorithm
(iv) If a request (3,3,0) by process P4 arrives in state defined by (iii)
can be granted immediately?
Banker’s Algorithm
Request4 = 3,3,0 ; Need4 = 4,3,1 ; Available = 2,3,0
Step 1 : Request4≤ Need4 is true
Step 2 : Request4≤ Available is not true
Since the no of available resource is less than the no of resource
requested
Thus request cannot be granted; So P4 must wait
Banker’s Algorithm
(v) If a request (0,2,0) by process P0 arrives then check whether it is
granted or not?
Request0 = 0,2,0 ; Need4 = 7,4,3 ; Available = 2,3,0
Step 1 : Request0≤ Need0 is true
Step 2 : Request0≤ Available is true
Step 3: Available = Available – Request0;
Allocation0= Allocation0+ Request0;
Need0 = Need0 – Request0;
Banker’s Algorithm
Applying safety algorithm on new state-

Work = Available = [2,1,0] and Finish = [F F F F F]


For any Pi , Needi ≤ Work is not satisfied.
So request for P0 is not granted through the available resources;
because system is in unsafe state.
Deadlock Detection
• Deadlock detection algorithm allows a system to enter into
deadlock state.
• In order to get rid of deadlocks, The OS periodically checks the
system for any deadlock. In case, it finds any of the deadlock
then the OS will recover the system using some recovery
techniques.
• We may apply the detection algorithm in 2 possible scenario.
1. Single Instance of Each Resource Type
2. Multiple Instances of a Resource Type
Single Instance of Each Resource Type
• If all the resources has single instance; then deadlock detection
algorithm uses a variant of Resource allocation graph (RAG); and it is
called as a Wait for graph.
• We can get the Wait for graph from resource allocation graph by
removing the resource nodes and collapsing the appropriate
edges.
• If wait for graph has any cycle then there is a deadlock;
• An edge from Pi to Pj in a wait-for graph implies that process Pi is
waiting for process Pj to release a resource.
• An edge Pi → Pj exists in a wait-for graph if and only if the
corresponding resource-allocation graph contains two edges P i →
Rq and Rq → Pj for some resource Rq .
Single Instance of Each Resource Type

Resource-Allocation Corresponding wait-for


Graph graph

Cycle
• P1 → P 2 → P 3 → P 4 → P 1
• P1 → P 2 → P 4 → P 1
Thus Process P1,P2,P3 and P4 are deadlock.
Multiple Instances of a Resource Type
In this case we are using another deadlock detection algorithm
which is similar to Banker’s algorithm.
Data structure used:
• Available: A vector of length m indicates the number of
available resources of each type
• Allocation: An n x m matrix defines the number of resources of
each type currently allocated to each process
• Request: An n x m matrix indicates the current request of each
process. If Request [i][j] = k, then process Pi is requesting k more
instances of resource type Rj.
Multiple Instances of a Resource Type
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi ≠ 0, then
Finish[i] = false; otherwise, Finish[i] = true

2. Find an index i such that both:


(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1 ≤ i ≤ n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is
deadlocked
Multiple Instances of a Resource Type
Consider a system with five processes P0 through P4 and three
resources types A,B,C. Resource type A has 7 instances, B has 2
instances and type C has 6 instances. Suppose at time T following
snapshot.

(i) Is the given system in deadlock state?


(ii) Suppose P2 makes an additional request (0,0,1) what will be
the effect of this request to the system?
Multiple Instances of a Resource Type
(i) Work = Available = [0,0,0] and Finish = [F F F F F]
For Pi
Finish [i] =F
if Requesti ≤ Work
Then Pi is in safe sequence
Work = Work + Allocationi
Finish[i]= T
-----------------------
For Po :
Finish [0] =F
Request0= 0,0,0 ; Work= 0,0,0
Request0 ≤ Work is true ; So Po must Kept in safe sequence.
Now Work will be updated as:
Work = 0,0,0 +0,1,0 = 0,1,0
Finish = [T F F F F]
Multiple Instances of a Resource Type
For P1:
Finish [1] =F
Request1 = 2,0,2 ; Work = 0,1,0
Request1 ≤ Work is false So , P1 must wait.
Finish = [T F F F F]
--------------------------
For P2:
Finish[2] = F
Request2 = 0,0,0 ; Work = 0,1,0
Request2 ≤ Work is true So , P2 must Kept in safe sequence
Now Work will be updated as:
Work = 0,1,0 +3,0,3 = 3,1,3
Finish = [T F T F F]
Multiple Instances of a Resource Type
For P3:
Finish[3] = F
Request3 = 1,0,0 ; Work = 3,13
Request3 ≤ Work is true So , P3 must Kept in safe sequence
Now Work will be updated as:
Work = 3,1,3 +2,1,1 = 5,2,4
Finish = [T F T T F]
-----------------
For P4:
Finish[4] = F
Request4 = 0,0,2 ; Work = 5,2,4
Request4 ≤ Work is true So , P4 must Kept in safe sequence
Now Work will be updated as:
Work = 5,2,4 +0,0,2 = 5,2,6
Finish = [T F T T T]
Multiple Instances of a Resource Type
For P1:
Finish[1] = F
Request1 = 2,0,2 ; Work = 5,2,6
Request1 ≤ Work is true So , P1 must Kept in safe sequence
Now Work will be updated as:
Work = 5,2,6 +2,0,0 = 7,2,6
Finish = [T T T T T]
Multiple Instances of a Resource Type
(ii) Suppose P2 makes an additional request (0,0,1) what will be the
effect of this request to the system?
Multiple Instances of a Resource Type
Work = Available = [0,0,0] and Finish = [F F F F F]
For Pi
Finish [i] =F
if Requesti ≤ Work
Then Pi is in safe sequence
Work = Work + Allocationi
Finish[i]= T
--------------
For Po :
Finish [0] =F
Request0= 0,0,0 ; Work= 0,0,0
Request0 ≤ Work is true ; So Po must Kept in safe sequence.
Now Work will be updated as:
Work = 0,0,0 +0,1,0 = 0,1,0
Finish = [T F F F F]
Multiple Instances of a Resource Type
For P1:
Finish [1] =F
Request1 = 2,0,2 ; Work = 0,1,0
Request1 ≤ Work is false So , P1 must wait.
Finish = [T F F F F]
-----------------------------
-For P2:
Finish [2] =F
Request2 = 0,0,1 ; Work = 0,1,0
Request2 ≤ Work is false So , P2 must wait.
Finish = [T F F F F]
Multiple Instances of a Resource Type
-For P3:
Finish [3] =F
Request3 = 1,0,0 ; Work = 0,1,0
Request3 ≤ Work is false So , P3 must wait.
Finish = [T F F F F]
-----------------------
-For P4:
Finish [4] =F
Request4 = 0,0,2 ; Work = 0,1,0
Request4 ≤ Work is false So , P4 must wait.
Finish = [T F F F F]
Now the system is in deadlock state consisting of processes P1, P2, P3
and P4
Recovery from Deadlock
• When a detection algorithm determines that a deadlock exists,
several alternatives are available.
Process Termination
1. Abort all deadlocked processes
• This method clearly will break the deadlock cycle, but at great
expense.
• The deadlocked processes may have computed for a long time,
and the results of these partial computations must be discarded
and probably will have to be recomputed later.
2. Abort one process at a time until the deadlock cycle is
eliminated
• This method incurs considerable overhead, since after each
process is aborted, a deadlock-detection algorithm must be
invoked to determine whether any processes are still
deadlocked.
Recovery from Deadlock
Many factors may affect which process is chosen, including
1. What the priority of the process is
2. How long the process has computed and how much longer the
process will compute before completing its designated task.
3. How many and what types of resources the process has used.
4. How many more resources the process needs in order to
complete.
5. How many processes will need to be terminated.
Recovery from Deadlock
Resource Preemption
• To eliminate deadlocks using resource preemption, we
successively preempt some resources from processes and give
these resources to other processes until the deadlock cycle is
broken.
• If preemption is required to deal with deadlocks, then three
issues need to be addressed:
• 1. Selecting a victim: Which resources and which processes are
to be preempted.
• As in process termination, we must determine the order of
preemption to minimize cost.
• Cost factors may include such parameters as the number of
resources a deadlocked process is holding and the amount of
time the process has consumed.
Recovery from Deadlock
Rollback
• If we preempt a resource from a process, then it cannot continue its
normal execution as it is missing some needed resource. So we must
rollback the process to some safe state and restart it from that state.
• Generally, it is very difficult to determine the safe state. So a simple
solution is total rollback i.e., abort the process and then restart it.
Starvation
• In a system where victim selection is based primarily on cost factors, it
may happen that the same process is always picked as a victim.
• Thus this starvation situation must address.
• Thus a process can be picked as a victim only a (small) finite number
of times.
• The most common solution is to include the number of rollbacks in
the cost factor.
Scheduling Algorithm- Multiprocessor Scheduling
• Multiprocessor scheduling is a technique used to distribute tasks
across multiple processors in a computer system.
• By executing tasks concurrently, it significantly reduces the total
processing time and improves overall system efficiency.
• There are two primary approaches to multiprocessor scheduling:
1. Symmetric Multiprocessor Scheduling (SMP):
• In this approach, all processors are treated equally, and each
processor can schedule and execute tasks independently. The
operating system typically maintains a common queue of tasks
accessible by all processors.
2. Asymmetric Multiprocessor Scheduling (AMP):
• In this method, one processor is designated as the master and is
responsible for task scheduling. The remaining processors
execute tasks assigned to them by the master processor,
reducing scheduling overhead on slave processors.
System Component of OS
• A component of an operating system is a basic part that helps to
control and manage how the computer's hardware and software
work together. Each component has a specific job, like handling
memory, running programs, or managing files.
Kernel
• The core of the OS, managing communication between hardware
and software.
• Handles process management, memory management, device
management, and system calls.
Process Management
• Manages processes in the system (creation, scheduling, and
termination).
• Ensures CPU time is distributed efficiently.
Memory Management
• Manages the system’s RAM and virtual memory.
• Allocates and deallocates memory space as needed by programs
System Component of OS
File System
• Manages files and directories on storage devices.
• Handles file naming, storage, retrieval, and access permissions.
Device Management
• Controls hardware devices through drivers.
• Facilitates input/output operations between hardware and
software.
User Interface (UI)
• Provides interaction between the user and the computer.
• Can be Command-Line Interface (CLI) or Graphical User Interface
(GUI).
Security and Access Control
• Protects system data and resources from unauthorized access.
• Manages user authentication and permissions.
System Component of OS
Networking
• Manages data transmission between devices over networks.
• Supports protocols and resource sharing.
System Utilities and Services
• Includes system tools for performance monitoring, backup, and
configuration.
Modes of Operation
• The operating system uses two modes to ensure safe and
correct execution: user mode and kernel mode.
• A mode bit (0 for kernel, 1 for user) indicates the current mode.
• In user mode, user applications run with limited access to
system resources.
• When a user program makes a system call, the operating system
switches to kernel mode to provide the requested service or
resource.
• This separation protects the system from accidental or
malicious actions by user programs.
Modes of Operation
Modes of Operation
• A system call is a way for programs (user-level applications) to
request services or resources from the operating system kernel.
Types of System Calls:
• Process Control : Used to create, terminate, and manage
processes.
fork() – Create a new process
exec() – Execute a new program
exit() – Terminate a process
wait() – Wait for a process to finish
• File Management : Used to handle files and directories.
open() – Open a file
read() – Read from a file
write() – Write to a file
close() – Close a file
delete() – Delete a file
Modes of Operation
• Device Management: Used to request and release access to hardware
devices..
request_device() – Request access to a device
release_device() – Release device access
read_device() – Read from a device
write_device() – Write to a device
• Information Maintenance: Used to get or set system-related
information.
get_time() – Get system time
set_time() – Set system time
get_pid() – Get process ID
get_user_id() – Get user ID
Modes of Operation
• Communication (Interprocess Communication) : Used for data
exchange between processes.
pipe() – Create a communication pipe.
shmget() – Allocate shared memory.
send() / recv() – Send or receive messages.

• Example : A process executes the following code:

The total number of child processes created is


Answer : Let's trace it step by step, assuming the initial process has
process ID (PID) 100, and let's say n is 3:
Modes of Operation
Modes of Operation
For each iteration of the loop, the number of running processes
doubles. If the loop runs n times, the initial one process will have
spawned 2n−1 child processes, resulting in a total of 2n processes
running concurrently (including the original parent).
Memory Management
What is Memory Management?
• In a multiprogramming computer system, the OS occupies a
portion of the memory, while the remaining memory is allocated
to multiple processes.
• Memory management is the process of allocating and managing
this memory space among different processes efficiently.
Memory Management Goals
• Allocate and deallocate memory before and after process
execution.
• Track memory usage by processes.
• Minimize fragmentation issues.
• Ensure optimal utilization of main memory.
• Maintain data integrity during process execution.
Bare Machine
• In a bare machine setup, there's no operating system to handle
memory allocation, de-allocation, and protection.
• In this, memory allocation is often done manually by the
programmer.
• These memory management scheme is used in user dedicated
system.
• In a general sense, a microprocessor is often considered a bare
machine because it represents the essential processing unit of a
computer system without any additional software.
Resident Monitor
• In this scheme memory is divided into two contiguous areas.
• One of them is allocated to the resident monitor ( monitor /OS).
• The other is allocated to the transient processes , where one
process is loaded and executed in response to the user
command.
• When a transient process is completed, the monitor may load
another one for execution.
• “Fence address" refer to a memory address boundary that
separates different areas of memory .
Memory Protection
• To safely run multiple processes at the same time, each process
must have its own memory space. This prevents processes from
interfering with each other’s data.
• We can ensure this protection using two registers: base and
limit.
Memory Protection
• For example, if the base register holds 300040 and the limit register
is 120900, then the program can legally access all addresses from
300040 through 420939.
Hardware Address Protection
Hardware Address Protection
• CPU generates an address during execution.
• This address is checked against two conditions to ensure it's valid
and safe:
• First, is the address ≥ base?
• If no, it's an illegal address → trap to OS.
• Then, is the address < base + limit?
• If no, it's beyond allowed range → trap to OS.
• If both conditions pass, the address is considered valid → the
memory access proceeds.
Purpose:
• This mechanism ensures that a process cannot access memory
outside its allocated region, thus providing memory protection and
isolating processes from each other.
Address Binding
• Address binding is the process of mapping logical addresses
(used by programs) to physical addresses (actual memory
locations).
• Compile Time: If the memory location is known at compile time,
absolute addresses can be generated by the compiler.
• However, this approach is inflexible because the program must
be loaded at the same memory location each time it runs.
• Load Time: If the memory location is not known at compile time,
the compiler generates relocatable address.
• The final binding is delayed until the program is loaded into
memory. The loader then adjusts the addresses based on where
the program is loaded.
Address Binding
• Execution Time: This is the most flexible method, where binding
occurs dynamically during program execution.
• Logical addresses are bound to physical addresses at runtime,
allowing a program to be loaded at any available memory
location.
• This is often managed by the operating system with the help of
hardware mechanisms like the Memory Management Unit
(MMU).
Address Binding-Multistep Processing of
a User Program
Logical Versus Physical Address Space
• An address generated by the CPU is commonly referred to as a
logical address.
• whereas an address seen by the memory unit is commonly referred
to as a physical address.
• The run-time mapping from virtual to physical addresses is
done by a hardware device called the memory-management
unit (MMU)
Logical Versus Physical Address Space
• The set of all logical addresses generated by a program is a
logical address space.
• The set of all physical addresses corresponding to these
logical addresses is a physical address pace.
Dynamic relocation using a relocation
register
• CPU will generate logical address.
• The value in the relocation register is added to every address
generated by a user process at the time the address is sent to
memory
Dynamic Loading and Dynamic Linking
Dynamic loading
• Dynamic loading is a technique of loading modules of a program
into memory only when they are required during its execution.
• This allows for more efficient use of memory and potentially
faster startup times for applications.
• A common example of dynamic loading is when a web browser
loads a plugin or extension only when a webpage requiring it.
Dynamic Linking
• Dynamic linking is a technique to link executable code to
libraries (collections of precompiled routines) during runtime
rather than at compile time.
• This allows multiple programs to share a single copy of a
library, reducing memory usage.
Overlays
• It refers to a memory management technique used to run
programs larger than the available memory by dividing them
into logical segments.
• Overlays selectively load into memory only those instructions
and data (logical segments) that are needed at a particular
moment during program execution.
• This allows programs larger than the available memory to run
efficiently by swapping segments in and out of memory as
required.
Overlays
Swapping
Swapping
• A process can be swapped temporarily out of memory to a
backing store and then brought back into memory for continued
execution.
• Backing Store: Fast disk storage large enough to hold the
contents of processes; provides direct access for quick swapping.
• Roll out, roll in – swapping variant used for priority-based
scheduling algorithms; lower-priority process is swapped out so
higher-priority process can be loaded and executed
Memory Management Scheme
• Contiguous Memory allocation: It is a memory management
technique in which a process or program is allocated a single
contiguous block of memory.
• Resident monitor, Multiprogramming with fixed partition and
multiprogramming with variable partitions is categorized as
contiguous memory allocation.
• Noncontiguous Memory Allocation: It is a memory
management technique where memory is allocated to a
process or program in scattered manner, rather than in a
single contiguous block.
• Paging (System view) and Segmentation ( User view) are two
mechanisms that are used to manage non-contiguous memory
allocation.
Memory Management Scheme
• Hardware Support for Relocation and Limit Registers
Multiprogramming with Fixed Partition
/Static Partitioning
• The main memory is divided into several fixed-sized partitions
in this scheme.
• These partitions can be of the same size or different sizes.
• Each partition can hold a single process.
• The number of partitions determines the degree of
multiprogramming, i.e., the maximum number of processes can
be in memory.
• These partitions are made at the time of system generation and
remain fixed after that.
Multiprogramming with Fixed Partition
/Static Partitioning
Partition Description Table (PDT)

Partition Partition Partition Partition


No Base Size Status

0 0K 100K Allocated
1 100K 300K Free
2 400K 100K Allocated
3 500K 250K Allocated
4 750K 150K Allocated
5 900K 100K Free
Multiprogramming with Fixed Partition
/Static Partitioning
How to select a specific partition for a given process ?
• First Fit: This approach allocates the first free partition large
enough to accommodate the process.
• Best Fit: This approach allocates the smallest free partition that
meets the requirements of the process under consideration.
• Worst Fit: This approach allocates the partition to the process
which is the largest sufficient among the freely available partitions
available in the main memory.
Multiprogramming with Fixed Partition
/Static Partitioning
• Given memory partition of size 100K, 500K, 200K ,300 K & 600K,
how would the first fit, best fit and worst fit algorithm place the
processes of 212K, 417K, 112K and 426K. Which algorithm invokes
the efficient use of memory?
• Solution : Step1: Let's suppose memory is used by: P1 = 212 K,
P2 = 417K, P3 = 112 K and P4 = 426 K
Step2: Memory
Multiprogramming with Fixed Partition
/Static Partitioning
First Fit: This approach allocates the first free partition large enough
to accommodate the process. Thus,
• P1 is allocated to Partition No. 1, which created fragment
size: 500K – 212K = 288 K
• P2 is allocated to Partition No. 4, which created fragment size:
600K – 417K = 183K
• P3 is allocated to Partition No. 2, which created fragment size:
200K – 112 K = 88K.
• P4 is not allocated to any Partition, Because any Partition size
is not sufficient for P4.
Multiprogramming with Fixed Partition
/Static Partitioning
Multiprogramming with Fixed Partition
/Static Partitioning
Best Fit: This approach allocates the smallest free partition that
meets the requirements of the process under consideration.
• P1 is allocated to Partition No. 3, which created fragment size:
300 K – 212 K = 88 K
• P2 is allocated to Partition No. 1, which created fragment size:
500K – 417 K= 83 K
• P3 is allocated to Partition No. 2, which created fragment size:
200K – 112K = 88 K
• P4 is allocated to Partition No. 4, which created fragment
size: 600 K – 426 K = 174 K
Multiprogramming with Fixed Partition
/Static Partitioning
Multiprogramming with Fixed Partition
/Static Partitioning
Worst Fit: This approach allocates the partition to the process
which is the largest sufficient among the freely available
partitions available in the main memory.
• P1 is allocated to Partition No. 4, which created fragment size:
600 K – 212 K = 388 K
• P2 is allocated to Partition No. 1, which created fragment size:
500K – 417 K= 83 K
• P3 is allocated to Partition No. 3, which created fragment size:
300K – 112K = 188 K
• P4 is not allocated to any Partition, Because any Partition size
is not sufficient for P4.
Multiprogramming with Fixed Partition
/Static Partitioning
Multiprogramming with Fixed Partition
/Static Partitioning
What is fragmentation?
• Memory fragmentation in an operating system occurs when
memory is divided into small, non-contiguous blocks that cannot be
allocated to processes.
• This leads to wasted memory space and can impact system
performance negatively.
Internal fragmentation
• Internal fragmentation occurs when the memory is
distributed into fixed-sized blocks.
• If the memory allocated to the process is slightly larger than
the memory demanded, then the difference between
allocated and demanded memory is known as internal
fragmentation.
Multiprogramming with Variable/Dynamic
Partitioning
• Dynamic partitioning tries to overcome the problems caused by
fixed partitioning.
• In this technique, the partition size is not declared initially.
• When a process is brought into memory, It is allocated exactly to
that amount of memory that it needs not more than that it
requires.
Multiprogramming with Variable/Dynamic
Partitioning
• Let's Total Memory is 1024K. Lets Suppose processes are loaded
starting where OS ends.
• Suppose P1 needs 320K, P2 needs 224K P3 needs 228K and P4 needs
128K.
Multiprogramming with Variable/Dynamic
Partitioning
• Now there is not sufficient memory for process P4.
• Let suppose Process P2 is swapped out which leaves a sufficient
space to load the process P4.
Multiprogramming with Variable/Dynamic
Partitioning
• Let’s suppose after some P1 finished its execution and there is no
process in the ready queue.
• P2 wants to be executed, Then OS swap in Process P2.
Multiprogramming with Variable/Dynamic
Partitioning
• As shown in example, the method begins effectively but over
time, it generates numerous small holes.
• A "hole" indeed refers to an unallocated block of memory
between allocated processes.
External fragmentation
• External fragmentation arises when the total memory is
adequate for a request, but the available memory isn't
contiguous.
• This leads to scattered, unusable memory across the system,
hampering efficient allocation and utilization.
Multiprogramming with Variable/Dynamic
Partitioning
• One technique to overcome
external fragmentation is
compaction.
• Compaction involves moving
processes in memory so that
scattered pieces of unused
memory (holes) can be
consolidated together.
• For example if compaction is
involves in previous one, A
block of 316K memory is
created, which may be
occupied by other process
Noncontiguous Memory Allocation
• This technique allows a process to acquire multiple memory blocks
located in different parts of memory based on its requirements.
• In non-contiguous memory allocation, the process is divided into
blocks (pages or segments), and these blocks are then assigned to
various memory locations based on the available memory.
• Let a process with three segments, let's say P1, P2, and P3
Noncontiguous Memory Allocation
• Paging (System view) and Segmentation ( User view) are two
mechanisms that are used to manage non-contiguous memory
allocation.
• Paged Segmentation is another technique that combines the
concepts of paging and segmentation to allow for
non-contiguous allocation of memory.
Paging
• Physical address space of a process can be
non-contiguous.
• Process is divided into fixed-size blocks,
each of which may reside in a different part
of physical memory.
● Divide physical memory into fixed-sized
blocks called frames
● Divide logical memory into blocks of same
size as frames called pages
• When a process/ program to be executed,
its pages are loaded into any available
memory frames from the backing store.
• The backing store is also divided into fixed
sized blocks that are same size of frames
i.e. the size of frame is same as size of a
page for a particular hardware.
Paging
• Keep track of all free frames
• To run a program of size N pages, need to find N free frames
and load program from backing store.
• Set up a page table to translate logical to physical addresses
• Page table is kept in memory.
– Page-table base register (PTBR) points to the page table
– Page-table length register (PTLR) indicates size of the
page table
• Still have Internal fragmentation
Paging
• Keep track of all free frames
• To run a program of size N pages, need to find N free frames
and load program from backing store.
• Set up a page table to translate logical to physical addresses
• Page table is kept in memory.
– Page-table base register (PTBR) points to the page table
– Page-table length register (PTLR) indicates size of the
page table
• Still have Internal fragmentation
Paging
Address Translation Scheme
● Assume the logical address space is 2m. (How is m bit determined?)
● Assume page size is 2n
● Address generated by CPU is divided into:
● Page number (p) – used as an index into a page table which
contains base address of each page in physical memory. Size of
p is “m – n”
● Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit. Size
of d is “n”.
Paging
Paging Hardware
Paging
Paging Model of Logical and Physical Memory
Storage Units

Paging
Logical to physical address mapping
● The technique of transforming logical address to physical address
with the help of page table is known as paging .
● Paging is implemented on Hardware level, not in software level
● Logical Address Space(LAS) will be divided into equal size of pages

• Page size is equal to frame size :


• Physical Address Space (PAS) will be divided into equal frames.
Paging
● Question 1:
Consider a system which has logical address is 29 bits and physical address is 23
bits. Page size = 4 KB, Memory is byte addressable. The calculate

A) Number of frames

B) Number of pages
Paging
Logical Address = Physical Address =
29bits 23bits

p d f d
Paging Physical Address =
Logical Address =
29bits 23bits

p d f d

17 bits 12 bits 11 bits 12 bits


Paging
● Question 2
Consider a system which has logical address space is 128MB and
physical address is 24 bits. The physical address is divided into 8 K
frames. Calculate

A) Size of a page

B) Number of pages
Paging
● Question 2
Consider a system which has logical address space is 128MB and
physical address is 24 bits. The physical address is divided into 8 K
frames. Calculate

A) Size of a page

B) Number of pages
Paging
Logical Address = Physical Address =
27bits 24 bits

p d f d
Problems on Paging

Logical Address = Physical Address =


27bits 24bits

p d f d

16 bits 11 bits 13 bits 11 bits


Paging
● Question 3
Consider a system which has logical address space of 256 MB,
Physical address is 27 bit and physical address space is divided into
4 KB frame.
(i)Calculate the number of pages in LAS.
(ii) i)Calculate the number of frames in PAS
Paging
TLB -- Associative Memory
• The hardware implementation of page table we can use translation
look-aside buffers (TLBs), a special, small, fast look up hardware
cache.
• The TLB is associative and high-speed memory – parallel search.

● Each entry in TLB consists of two parts: a tag and a value.


● When this memory is used, then an item is compared with all
tags simultaneously. If the item is found, then corresponding
value is returned.
● Address translation (p, d)
● If p is in associative register, get frame # out
● Otherwise get frame # from page table in memory
Paging Hardware With TLB
Effective Access Time
● Associative Lookup = ε(Epsilon) time unit
Can be < 10% of memory access time
● Hit ratio = α (Alpha)
● Hit ratio – percentage of times that a page number is found
in the associative registers; ratio related to number of
associative registers
● Miss ratio = 1- α (Alpha)
● Effective Access Time (EAT)
● TLB_hit_time := TLB_search_time + memory_access_time

● TLB_miss_time := TLB_search_time + memory_access_time +


memory_access_time

● EAT = hit_ratio * (TLB_hit_time) + (1- hit_ratio) *


(TLB_miss_time)
Problems on Paging Hardware With TLB
● Problem 1:
● TLB lookup takes 5 nano sec.

● Memory access time is 100 nano sec.

● Hit ratio (probability to find page number in TLB) is ε

▪ Suppose ε = 80% (for example, TLB size = 16)

▪ Suppose ε = 98% (for example, TLB size = 64)


Problems on Paging Hardware With TLB

● Effective Access Time = (5+100)* ε +( 5+100+100)*(1- ε)

Suppose ε = 80% (for example, TLB size = 16)


● EAT = 105*.8 + 205*.2 = 125 nano sec.

Suppose ε = 98% (for example, TLB size = 64)


● EAT = 105*.98 + 205*.02 = 107 nano sec.
Problems on Paging Hardware With TLB
Problem 2:
● A paging scheme uses a Translation Look-aside Buffer (TLB). A
TLB-access takes 10 ns and a main memory access takes 50 ns.
What is the effective access time(in ns) if the TLB hit ratio is 90%
and there is no page fault.
Problems on Paging Hardware With TLB
Effective access time = hit ratio * time during hit + miss ratio * time
during miss
TLB time = 10ns, Memory time = 50ns, Hit Ratio= 90%
E.A.T. = (0.90)*(60)+0.10*110 =65 ns
Problems on Paging Hardware With TLB
Problem 3:
Consider a paging hardware with a TLB. Assume that the entire page
table and all the pages are in the physical memory. It takes 10
milliseconds to search the TLB and 80 milliseconds to access the
physical memory. If the TLB hit ratio is 0.6, the effective memory
access time (in milliseconds)
Problems on Paging Hardware With TLB
● First Calculate TLB hit time and TLB miss time
● TLB_hit_time := TLB_search_time + memory_access_time

● TLB_miss_time := TLB_search_time +
memory_access_time + memory_access_time
● As both page table and page are in physical memory
● EAT = hit_ratio * (TLB_hit_time) + (1- hit_ratio) * (TLB_miss_time)

= 0.6*(10+80) + (1-0.6)*(10+2*80)
= 0.6 * (90) + 0.4 * (170)
= 122
Valid (v) or Invalid (i) Bit In A Page
Table
Valid (v) or Invalid (i) Bit In A Page
Table
• Valid-invalid bit attached to each entry in the page table:
– “valid” indicates that the associated page is in the process’
logical address space, and is thus is a legal page
– “invalid” indicates that the page is not in the process’ logical
address space
Page Table Entries
• Page table has page table entries where each page table entry
stores a frame number and optional status (like protection) bits.
Many of status bits used in the virtual memory system. The
most important thing in PTE is frame Number.
• Page table entry has the following information –
Page Table Entries
1. Frame Number – It gives the frame number in which the current
page you are looking for is present. The number of bits required
depends on the number of frames.
Number of frame = Size of physical memory/frame size
Number of bits for frame number=log2(Number of frames )
2. Present/Absent bit – Present or absent bit says whether a particular
page you are looking for is present or absent. Sometimes this bit is
also known as valid/invalid bits.
3. Protection bit – Protection bit says that what kind of
protection you want on that page. So, these bit for the
protection of the page frame (read, write etc).
Page Table Entries
4. Referenced bit – Referenced bit will say whether this page has
been referred in the last clock cycle or not. It is set to 1 by
hardware when the page is accessed.
5. Caching enabled/disabled – Sometimes there is a need of
fresh data (i.e. input by the user) for execution . this
bit enables or disable caching of the page.
6. Modified bit – A modified bit is a flag (i.e 0 or 1) that indicates
whether an attribute needs to be updated. Sometimes this
modified bit is also called as the Dirty bit.
Segmentation
• Segmentation is memory management technique in which memory
is divided into variable size parts.
• Each part is known as segment which can be allocated to processes.
• The details about each segment are stored in a table called a
segment table.
• Logical address space is collection of segments. Each segment has a
name (or number) and length. Thus each logical address must
specify segment name (or number) and offset
<segment-number, offset>
• Thus program is divided into segments “that contains a logical unit
of a program”. A logical unit such as main programs, procedures,
functions, common blocks etc.
• It supports the user view of memory.
User’s View of a Program
Logical and Physical Memory
Segmentation
• Need to map a two-dimensional logical addresses to a
one-dimensional physical address. This is done with the
though Segment table. Thus, each entry of segment table
consist of
● base – contains the starting physical address where the
segments reside in memory
● limit – specifies the length of the segment.

● Segment table is kept in memory

● Segment-table base register (STBR) points to the segment


table’s location in memory
● Segment-table length register (STLR) indicates number of
segments used by a program;
segment number s is legal if s < STLR
Segmentation
Segmentation Hardware
Problem on Segmentation
Problem 1:
Consider the following segment table:
Segment Base
Bound Size
Number Address
0 219 600
1 2300 14
2 90 100
3 1327 580
4 1952 96
Calculate the Physical address for the following logical address
(segment number, offset):
a) 0, 430 b) 1, 10 c) 2, 500
d) 3,400 e) 4, 12
Problem on Segmentation
Problem on Segmentation
Problem on Segmentation

Ans:
d) and e) are self practice
Paged segmentation
• Paged segmentation is a scheme that combine the concept of
segmentation and paging.
• In this the logical address space of a process is divided into
segments. Each segment is further divided into pages of fixed
size.
• A page table exists for each segment that keep track of the
frames storing the pages of that segment.
• Number of entries in the page table of a segment = Number of
pages that segment
• Each entry in the segment table points to the page table of that
segment.
• Main memory in paged segmentation is partitioned into fixed-size
frames, each capable of holding a single page from any segment
of a process.
Translation of logical address to physical
address
In paged segmentation, the CPU generates a logical address
consisting of a segment number and a segment offset. The
segment offset is further divided into a page number and a
page offset.
• The segment number is used to access the segment table,
which gives the base address of the page table and the
segment limit.
• If the segment offset is within the limit, the page number is
used to index into the page table to get the frame number.
• The frame number and page offset are combined to form
the physical address in main memory.
Translation of logical address to physical
address
Virtual Memory
• Virtual memory is a technique that allows the execution of
processes that are not completely in memory.
• One major advantage of this scheme is that programs can be
larger than physical memory.
• It allows secondary memory to be addressed as if it is part of the
main memory.
• A memory hierarchy, consisting of a computer system’s memory
and a disk, that enables a process to operate with only some
portions of its address space in memory.
• A virtual memory is what its name indicates- it is an illusion of a
memory that is larger than the real memory.
• The software responsible for managing this functionality is
referred to as the virtual memory manager.
Virtual Memory That is Larger Than
Physical Memory
Virtual Memory That is Larger Than
Physical Memory
• The operating system divides virtual memory into pages, each
containing a fixed number of addresses.
• Each page is stored on a disk until it is needed.
• When a page is needed, the operating system copies it from
disk to main memory, translating virtual addresses into real
addresses.
• The process of translating virtual addresses into real
addresses is called mapping.
• Virtual memory is commonly implemented using two primary
techniques: demand paging and demand segmentation.
Demand Paging
• In demand paging, a page is brought into memory for its
execution when it is needed; otherwise, it remains in backing
storage (disk).
• In real-world analogy, it is similar to baby-demanded feeding:
a policy where mothers feed the baby when he cries for it.
• This method combines paging and swapping techniques.
• The main requirement of this method is that the complete
program should be present in backing storage (disk) in the
form of pages.
• Swapping is used to transfer the contents (pages) of a
program from disk to main memory when required.
• This method employs a “lazy swapper,” which swaps the
pages only when they are needed.
Demand Paging
Demand Paging- Advantages
• Reduced Memory Requirement: Virtual memory extends
physical memory by utilizing disk space, thereby reducing the
need for a large amount of RAM.
• Increased Degree of Multiprogramming: Allows more processes
to run concurrently with separate address spaces.
• Efficient Memory Utilization: Only the required portions of a
program are loaded into memory, optimizing memory usage and
improving overall performance.
• Flexibility and Scalability: Virtual memory dynamically allocates
memory resources, enabling the system to adapt to varying
workloads and application sizes.
• Improved System Responsiveness: Frequently accessed data is
kept in RAM through techniques like paging and swapping, which
reduces access time and enhances system responsiveness.
Demand Paging
• Page Table When Some Pages are Not in Main Memory
Page Fault
• In virtual memory systems, when the required page is not found
in RAM, the operating system pauses the execution of the
program and triggers a page fault.
• The operating system then loads the missing page from
secondary storage (such as a hard disk or SSD) into RAM. Once
the page is available in memory, the program resumes execution.
Steps in Handling a Page Fault
Page Fault
1. Check Memory Access
• The program attempts to access memory (e.g., load M). The
memory reference is checked using the page table to see if the
page is currently loaded in physical memory.
2. Trap to Operating System
• If the page is not in memory, a page fault trap is generated, and
control is transferred to the operating system.
3. Locate the Page on Backing Store
• The operating system determines that the page is on the
backing store (e.g., disk).
Page Fault
4. Bring in the Missing Page
• The operating system finds a free frame in physical memory
and reads the missing page from the backing store into that
frame.
[Link] Page Table
• The page table is updated to reflect that the page is now
present in memory and the frame number is recorded.
[Link] the Instruction
• The instruction that caused the page fault is restarted, now
with the required page in memory, and the program
continues execution normally.
Page Replacement
Page Replacement Algorithm
• Objective: Minimize page faults for both first access and
re-access.
• Evaluate algorithm by running it on a particular string of
memory references (reference string) and computing the
number of page faults on that string.
• Reference String: Sequence of page numbers
representing memory references.
• Repeated access to the same page does not cause a page
fault.
• Results depend on number of frames available.
• In all our examples, the reference string of referenced
page numbers is
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
Graph of Page Faults Versus The
Number of Frames
First-In-First-Out (FIFO) Algorithm
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)
• When a page fault occurs, the first in first out (FIFO) page
replacement algorithm replaces the oldest page in memory.
First-In-First-Out (FIFO) Algorithm
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)
Calculating Miss ratio-
Total number of page misses or page faults = 15
Thus, Miss ratio
= Total number of page misses / Total number of references
= 15 / 20
= 0.75 or 75%
Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.25
= 0.75 or 75%
Number of Frames vs. Page Faults
• One would expect that the more frames are allocated
to a process the fewer page faults
• Consider the reference string:
1,2,3,4,1,2,5,1,2,3,4,5
• How many page faults if we have 3 frames?
Page Reference String 1 2 3 4 1 2 5 1 2 3 4 5 Page Fault

3 1 1 1 4 4 4 5 5 5
Fra 2 2 2 1 1 1 3 3 9
me
3 3 3 2 2 2 4

• How many page faults if we have 4 frames?


Page Reference String 1 2 3 4 1 2 5 1 2 3 4 5 Page Fault

1 1 1 1 5 5 5 5 4 4
4 2 2 2 2 1 1 1 1 5
Fra 10
me 3 3 3 3 2 2 2 2
4 4 4 4 3 3 3
Belady’s Anomaly
• Belady’s Anomaly is the phenomenon where increasing the
number of page frames results in an increase in the number of
page faults for a given memory access pattern.
• FIFO Illustrating Belady’s Anomaly
Optimal Algorithm
• Replace page that will not be used for longest period of
time.
– With 3 frame, 9 is optimal for the example reference.

• How do you know which page will not be used for longest
period of time
- Can’t read the future
• The optimal algorithm is used mainly for comparison studies
Least Recently Used (LRU) Algorithm
• Use past knowledge rather than future
• Replace page that has not been used for the longest period
of time.
• Associate time of last use with each page

• 12 faults – better than FIFO but worse than OPTIMAL


Performance of Demand Paging-
Effective Access time (EAT)
• A page fault occurs when the referenced page is not found in
the main memory.
– Page fault handling routine is executed on the occurrence
of page fault.
– The time taken to service the page fault is called as page
fault service time.
• Effective access time with page fault
= Page fault rate x { Effective Access time without page fault
+ Page fault service time } + (1 – Page fault rate ) x
{Effective Access time without page fault }
Practice Problems Based on Page
Faults
Problem-01:
• Let the page fault service time be 10 ms in a computer with
average memory access time being 20 ns. If one page fault is
generated for every 106 memory accesses, what is the
effective access time for the memory?
a) 21 ns
b) 30 ns
c) 23 ns
d) 35 ns
Practice Problems Based on Page
Faults
Effective Access Time With Page Fault-
• It is given that effective memory access time without page fault
= 20 ns.
• Now, substituting values in the above formula, we get-
• Effective access time with page fault
= Page fault rate x { Effective Access time without page fault +
Page fault service time } + (1 – Page fault rate ) x {Effective
Access time without page fault }
= 10-6 x { 20 ns + 10 ms } + ( 1 – 10-6 ) x { 20 ns }
= 10-6 x 10 ms + 20 ns
= 10-5 ms + 20 ns
= 10 ns + 20 ns
= 30 ns
• Thus, Option (B) is correct.
Practice Problems Based on Page
Faults
Problem-02:
• Suppose the time to service a page fault is on the average 10
milliseconds, while a memory access takes 1 microsecond.
Then, a 99.99% hit ratio results in average memory access
time of-
a) 1.9999 milliseconds
b) 1 millisecond
c) 9.999 microseconds
d) 1.9999 microseconds
e) None of these
Practice Problems Based on Page
Faults
• Solution-
• Given-
– Page fault service time = 10 msec
– Average memory access time = 1 μsec
– Hit ratio = 99.99% = 0.9999
• Page Fault Rate-
– Page fault rate
= 1 – Hit ratio
= 1 – 0.9999
= 0.0001
Practice Problems Based on Page
Faults
• Effective Access Time With Page Fault-
– It is given that effective memory access time without page
fault = 1 μsec.
– Substituting values in the above formula, we get-
– Effective access time with page fault
= Page fault rate x { Effective Access time without page fault + Page
fault service time } + (1 – Page fault rate ) x {Effective Access time
without page fault }
= 0.0001 x { 1 μsec + 10 msec } + 0.99999 x 1 μsec
= 0.0001 μsec + 0.001 msec + 0.9999 μsec
= 1 μsec + 0.001 msec
= 1 μsec + 1 μsec
= 2 μsec or 0.002 msec
• Thus, Option (E) is correct.
Practice Problems Based on Page
Faults
Problem-03:
• If an instruction takes i microseconds and a page fault takes
an additional j microseconds, the effective instruction time if
on the average a page fault occurs every k instruction is-
a) i + j / k
b) i + j x k
c) (i + j) / k
d) (i + j) x k
Practice Problems Based on Page
Faults
• Solution-
• Given-
– Page fault service time = j μsec
– Average memory access time = i μsec
– One page fault occurs every k instruction
• Page Fault Rate-

• It is given that one page fault occurs every k instruction.


Thus, Page fault rate = 1 / k
Practice Problems Based on Page
Faults
• Effective Access Time With Page Fault-
– It is given that effective memory access time without
page fault = i μsec
– Now, substituting values in the above formula, we get-
– Effective access time with page fault
= Page fault rate x { Effective Access time without page fault +
Page fault service time } + (1 – Page fault rate ) x {Effective
Access time without page fault }
= (1 / k) x { i μsec + j μsec } + ( 1 – 1 / k) x { i μsec }
= j / k μsec + i μsec
= i + j / k μsec
• Thus, Option (A) is correct.
Frame Allocation Algorithms
• A frame allocation algorithm is a method used by an operating
system to determine how to allocate available physical
memory frames to the pages of processes.
1. Equal Allocation
• Description: Divides the total number of frames equally among
all processes.
• Example: If there are 100 frames and 5 processes, each process
gets 20 frames.
2. Proportional Allocation
• Description: Allocates frames to processes based on their size
relative to the total size of all processes.
Frame Allocation Algorithms
Thrashing
• Thrashing occurs when page faults and swapping happen
very frequently.
• Results in the operating system spending excessive time
swapping pages.
• This state in the operating system is known as thrashing.
• This significantly reduces CPU utilization or can make the
system nearly unusable due to constant swapping.
Factors Contributing to Thrashing
• High degree of multiprogramming: Too many processes
compete for limited memory, causing frequent page faults.
• Lack of frames: Insufficient memory frames per process
leads to constant swapping of pages.
• Page replacement policy: Inefficient policies may replace
frequently used pages, increasing page faults and leading to
thrashing.
Techniques to Prevent Thrashing
1. Working-Set Model
• This model is based on the assumption of locality.
• A locality is a set of pages that are actively used together.
• It uses Δ as the working set window.
• Example with Δ = 10
Techniques to Prevent Thrashing
• Working set is defined as the set of pages in the most
recent page references.
• Allocate enough frames to a process to accommodate its
current locality.
• If D is the total demand for frames and WSSi is the working set
size for process i,
D = ⅀ WSSi
• Now, if 'm' is the number of frames available in the memory,
there are two possibilities:
• D>m, i.e., total demand exceeds the number of frames, then
thrashing will occur as some processes would not get enough
frames.
• D<=m, then there would be no thrashing.
Techniques to Prevent Thrashing

• If there are enough extra frames, then some more processes


can be loaded into the memory.
• On the other hand, if the summation of working set sizes
exceeds the frames' availability, some of the processes have
to be suspended (swapped out of memory).
• This technique prevents thrashing along with ensuring the
highest degree of multiprogramming possible. Thus, it
optimizes CPU utilization.
Techniques to Prevent Thrashing
2. Page Fault Frequency
• The problem associated with thrashing is the high page fault
rate, thus, the fundamental principle is to control the page fault
rate.
• If the page fault rate is too high, it indicates that the process
has too few frames allocated to it.
• A low page fault rate indicates that the process has too many
frames.
• Upper and lower limits can be established on the desired
page fault rate, as shown in the diagram.
• If the page fault rate falls below the lower limit, frames can be
removed from the process. Similarly, if the page faults rate
exceeds the upper limit, more frames can be allocated to the
process.
Techniques to Prevent Thrashing
• In other words, the graphical state of the system should be kept
limited to the rectangular region formed in the given diagram.
Cache Memory Organization
• Cache stores frequently used data or contents from main
memory.
• It allows the processor to access data quickly.
• CPU Memory Access:
Step 1: CPU checks the cache memory.
– If data is found, it is accessed directly.
Step 2: If data is not found in cache memory:
– CPU accesses the data from the main memory.
Cache Memory Organization
• Hit Ratio(H) = hit / (hit + miss) = no. of hits/total accesses
Miss Ratio = miss / (hit + miss) = no. of miss/total accesses = 1 - hit
ratio(H)
• Average Memory Access Time = Hit ratio * Cache Memory
Access Time + (1 – Hit ratio) * (Cache Memory Access Time +
Main Memory Access Time)
• Example: A computer system has a cache memory with an
access time of 10 ns and a main memory access time of 100
ns. The system has a cache hit ratio of 0.8. Calculate the
average memory access time for this system.
• Average memory access time =0.8×10 ns+(1−0.8)×
(10 ns+100 ns) = 30 ns
Locality of reference
• Locality of reference refers to a phenomenon in which a
computer program tends to access same set of memory
locations for a particular time period.
• It is essential for optimizing memory usage and performance.
Types of Locality
1. Temporal Locality
• Refers to the principle where recently accessed data or
instructions are likely to be accessed again in the near future.
• To optimize memory access times by storing recently accessed
data in cache memory.
• Improves efficiency by minimizing the time spent searching for
and fetching data from main memory.
Locality of reference
2. Spatial Locality
• Spatial locality means instruction or data near to the current
memory location that is being fetched, may be needed soon
in the near future.

You might also like