Overview of Operating Systems Functions
Overview of Operating Systems Functions
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
● 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.
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
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
•
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
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
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-
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
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
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
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
p d f d
● 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.
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
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