0% found this document useful (0 votes)
3 views52 pages

HW806 6

An Operating System (OS) is system software that manages computer resources, serving as an interface between hardware and software while controlling program execution. It performs various functions including resource management, process management, memory management, and providing security, among others. The document also outlines the evolution of operating systems from serial processing to modern networked systems, and categorizes different types of operating systems such as batch, time-sharing, and real-time systems.

Uploaded by

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

HW806 6

An Operating System (OS) is system software that manages computer resources, serving as an interface between hardware and software while controlling program execution. It performs various functions including resource management, process management, memory management, and providing security, among others. The document also outlines the evolution of operating systems from serial processing to modern networked systems, and categorizes different types of operating systems such as batch, time-sharing, and real-time systems.

Uploaded by

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

Unit – 1

Operating System:
Definitions:
An Operating System is a System software that manages all the resources of the computing
deice.
 Acts as an interface between the software and different parts of the computer or
the computer hardware.
 Manages the overall resources and operations of the computer.
 Controls and monitors the execution of all other programs that reside in the
computer, which also includes application programs and other system software of
the computer.
 Examples of Operating Systems are Windows, Linux, macOS, Android, iOS, etc.

What is an Operating System Used for?


 As a platform for Application programs: It provides a platform, on top of which,
other programs, called application programs can run.
 Managing Input-Output unit: It also allows the computer to manage its own
resources such as memory, monitor, keyboard, printer, etc. Management of these
resources is required for effective and fair utilization.
 Multitasking: It manages memory and allows multiple programs to run in their
own space and even communicate with each other through shared memory.
 Manages memory and Files: It manages the computer’s main memory and second
storage. Additionally, it allows and deallocates memory to all tasks and
applications.
 Provides Security: It helps to maintain the system and applications safe through
the authorization process. Thus, the OS provides security to the system.
For more, refer to Need of Operating Systems.
Functions of the Operating System
 Resource Management: The operating system manages and allocates memory,
CPU time, and other hardware resources among the various programs and
processes running on the computer.
 Process Management: The operating system is responsible for starting, stopping,
and managing processes and programs. It also controls the scheduling of processes
and allocates resources to them.
 Memory Management: The operating system manages the computer’s primary
memory and provides mechanisms for optimizing memory usage.
 Security: The operating system provides a secure environment for the user,
applications, and data by implementing security policies and mechanisms such as
access controls and encryption.
 Job Accounting: It keeps track of time and resources used by various jobs or users.
 File Management: The operating system is responsible for organizing and
managing the file system, including the creation, deletion, and manipulation of
files and directories.
 Device Management: The operating system manages input/output devices such as
printers, keyboards, mice, and displays. It provides the necessary drivers and
interfaces to enable communication between the devices and the computer.
 Networking: The operating system provides networking capabilities such as
establishing and managing network connections, handling network protocols, and
sharing resources such as printers and files over a network.
 User Interface: The operating system provides a user interface that enables users
to interact with the computer system. This can be a Graphical User Interface (GUI),
a Command-Line Interface (CLI), or a combination of both.
 Backup and Recovery: The operating system provides mechanisms for backing up
data and recovering it in case of system failures, errors, or disasters.
 Virtualization: The operating system provides virtualization capabilities that allow
multiple operating systems or applications to run on a single physical machine.
This can enable efficient use of resources and flexibility in managing workloads.
 Performance Monitoring: The operating system provides tools for monitoring and
optimizing system performance, including identifying bottlenecks, optimizing
resource usage, and analyzing system logs and metrics.
 Time-Sharing: The operating system enables multiple users to share a computer
system and its resources simultaneously by providing time-sharing mechanisms
that allocate resources fairly and efficiently.
 System Calls: The operating system provides a set of system calls that enable
applications to interact with the operating system and access its resources. System
calls provide a standardized interface between applications and the operating
system, enabling portability and compatibility across different hardware and
software platforms.
 Error-detecting Aids: These contain methods that include the error messages, and
other debugging and error-detecting methods.

OS Components:

There are various components of an Operating System to perform well defined tasks. Though
most of the Operating Systems differ in structure but logically they have similar components.
Each component must be a well-defined portion of a system that appropriately describes the
functions, inputs, and outputs.

There are following 8-components of an Operating System:

1. Process Management
2. I/O Device Management
3. File Management
4. Network Management
5. Main Memory Management
6. Secondary Storage Management
7. Security Management
8. Command Interpreter System

Following section explains all the above components in more detail:

Process Management

A process is program or a fraction of a program that is loaded in main memory. A process needs
certain resources including CPU time, Memory, Files, and I/O devices to accomplish its task. The
process management component manages the multiple processes running simultaneously on
the Operating System.

The operating system is responsible for the following activities in connection with process
management:

 Create, load, execute, suspend, resume, and terminate processes.


 Switch system among multiple processes in main memory.
 Provides communication mechanisms so that processes can communicate with each
others
 Provides synchronization mechanisms to control concurrent access to shared data to
keep shared data consistent.
 Allocate/de-allocate resources properly to prevent or avoid deadlock situation.

I/O Device Management

One of the purposes of an operating system is to hide the peculiarities of specific hardware
devices from the user. I/O Device Management provides an abstract level of H/W devices and
keep the details from applications to ensure proper use of devices, to prevent errors, and to
provide users with convenient and efficient programming environment.

Following are the tasks of I/O Device Management component:

 Hide the details of H/W devices


 Manage main memory for the devices using cache, buffer, and spooling
 Maintain and provide custom drivers for each device.

File Management
File management is one of the most visible services of an operating system. Computers can
store information in several different physical forms; magnetic tape, disk, and drum are the
most common forms.

A file is defined as a set of correlated information and it is defined by the creator of the file.
Mostly files represent data, source and object forms, and programs. Data files can be of any
type like alphabetic, numeric, and alphanumeric.

The operating system implements the abstract concept of the file by managing mass storage
device, such as types and disks. Also files are normally organized into directories to ease their
use. These directories may contain files and other directories and so on.

The operating system is responsible for the following activities in connection with file
management:

 File creation and deletion


 Directory creation and deletion
 The support of primitives for manipulating files and directories
 Mapping files onto secondary storage
 File backup on stable (nonvolatile) storage media

Network Management

The definition of network management is often broad, as network management involves


several different components. Network management is the process of managing and
administering a computer network. A computer network is a collection of various types of
computers connected with each other.

Network management comprises fault analysis, maintaining the quality of service, provisioning
of networks, and performance management.

Following are the features of network management:

 Network administration
 Network maintenance
 Network operation
 Network provisioning
 Network security

Main Memory Management

Memory is a large array of words or bytes, each with its own address. It is a repository of
quickly accessible data shared by the CPU and I/O devices.
Main memory is a volatile storage device which means it loses its contents in the case of system
failure or as soon as system power goes down.

The operating system is responsible for the following activities in connections with memory
management:

 Keep track of which parts of memory are currently being used and by whom.
 Decide which processes to load when memory space becomes available.
 Allocate and deallocate memory space as needed.

Secondary Storage Management

The main purpose of a computer system is to execute programs. These programs, together with
the data they access, must be in main memory during execution. Since the main memory is too
small to permanently accommodate all data and program, the computer system must provide
secondary storage to backup main memory.

Most modern computer systems use disks as the principle on-line storage medium, for both
programs and data. Most programs, like compilers, assemblers, sort routines, editors,
formatters, and so on, are stored on the disk until loaded into memory, and then use the disk as
both the source and destination of their processing.

The operating system is responsible for the following activities in connection with disk
management:

 Free space management


 Storage allocation
Disk scheduling

Security Management

The operating system is primarily responsible for all task and activities happen in the computer
system. The various processes in an operating system must be protected from each other’s
activities. For that purpose, various mechanisms which can be used to ensure that the files,
memory segment, cpu and other resources can be operated on only by those processes that
have gained proper authorization from the operating system.

For example, memory addressing hardware ensure that a process can only execute within its
own address space. The timer ensure that no process can gain control of the CPU without
relinquishing it. Finally, no process is allowed to do it’s own I/O, to protect the integrity of the
various peripheral devices.

Command Interpreter System


One of the most important component of an operating system is its command interpreter. The
command interpreter is the primary interface between the user and the rest of the system.

Command Interpreter System executes a user command by calling one or more number of
underlying system programs or system calls.

Many commands are given to the operating system by control statements. A program which
reads and interprets control statements is automatically executed. This program is called the
shell and few examples are Windows DOS command window, Bash of Unix/Linux or C-Shell of
Unix/Linux.
Evolution of operating system:
Operating systems have progressed from slow and expensive systems to today's technology,
which has exponentially increased computing power at comparatively modest costs. So let's
have a detailed look at the evolution of operating systems.

The operating system can be classified into four generations, as follows:


 First Generation
 Second Generation
 Third Generation
 Fourth Generation
First Generation (1945-1955)
Serial Processing
The evolution of operating systems began with serial processing. It marks the start of the
development of electronic computing systems as alternatives to mechanical computers.
Because of the flaws in mechanical computing devices, humans' calculation speed is limited,
and they are prone to making mistakes. Because there is no operating system in this
generation, the computer system is given instructions that must be carried out immediately.
Programmers were incorporated into hardware components without using an operating
system by the 1940s and 1950s. The challenges here are scheduling and setup time. The user
logs in for machine time by wasting computational time. Setup time is required when loading
the compiler, saving the compiled program, the source program, linking, and buffering. The
process is restarted if an intermediate error occurs.
Example: Windows 95 and 98 are examples of serial processing operating systems.
Second Generation (1955-1965)
Batch System
The batched systems marked the second generation in the evolution of operating systems. In
the second generation, the batch processing system was implemented, which allows a job or
task to be done in a series and then completed sequentially. The computer system in this
generation does not have an operating system, although various operating system
functionalities are available, such as FMS and IBSYS. It is used to improve computer utilization
and application. On cards and tapes, jobs were scheduled and submitted. Then, using Job
Control Language, they were successively executed on the monitors. The first computers
employed in the batch operation method created a computer batch of jobs that never paused
or stopped. The software is written on punch cards and then transferred to the tape's
processing unit. When the computer finishes one job, it immediately moves on to the next item
on the tape. Despite the fact that it is inconvenient for the users, it is designed to keep the
pricey computer as busy as possible by running a leveraged stream of operations. Memory
protection prevents the memory space that makes up the monitor from being changed, and the
timer prevents the job from monopolizing the system. When the input and output devices are
in use, the processor remains idle due to poor CPU utilization.
Example: MVS Operating System of IBM is an example of a batch processing operating system.
Third Generation (1965-1980)
Multi-Programmed Batched System
The evolution of operating systems embarks the third generation with multi-programmed
batched systems. In the third generation, the operating system was designed to serve
numerous users simultaneously. Interactive users can communicate with a computer via an
online terminal, making the operating system multi-user and multiprogramming. It is used to
execute several jobs that should be kept in the main memory. The processor determines which
program to run through job scheduling algorithms.
Example: Windows and IOS are examples of multi-programmed batched operating systems.
Fourth Generation (1980-Now)
The operating system is employed in this age for computer networks where users are aware of
the existence of computers connected to one another.
The era of networked computing has already begun, and users are comforted by a Graphical
User Interface (GUI), which is an incredibly comfortable graphical computer interface. In
the fourth generation, the time-sharing operating system and the Macintosh operating system
came into existence.
1. Time-Sharing Operating System

The Time-sharing of operating systems had a great impact on the evolution of


operating systems. Multiple users can access the system via terminals at the same
time, and the processor's time is divided among them. Printing ports were required
for programs having a command-line user interface, which required written
responses to prompts or written commands. The interaction is scrolled down like a
roll of paper. It was previously used to develop batch replacement systems. The
user interfaces directly with the computer via printing ports, much like an electric
teletype. Few users shared the computer immediately, and each activity was
completed in a fraction of a second before moving on to the next. By establishing
iterations when they are receiving full attention, the fast server may act on a large
number of users' processes at once. Multiple programs use time-sharing systems to
apply to the computer system by sharing the system interactively.

Example: Unix Operating System is an example of a time-sharing OS.


2. Macintosh Operating System

It was based on decades of research into graphical operating systems and


applications for personal computers. The photo depicts a Sutherland pioneer
program sketchpad that was developed in 1960, employing many of the
characteristics of today's graphical user interface, but the hardware components
cost millions of dollars and took up a room. The initiative on massive computers
and hardware improvements made the Macintosh commercially and economically
viable after many research gaps. Many research laboratories are still working on
research prototypes like sketchpads. It served as the foundation for anticipated
products.

Example: Mac OS X 10.6.8 snow leopard and OS X 10.7.5 Lion are some examples
of macintosh OS.

Types of Operating Systems:


 Batch Operating System: A Batch Operating System is a type of operating system
that does not interact with the computer directly. There is an operator who takes
similar jobs having the same requirements and groups them into batches.
 Time-sharing Operating System: Time-sharing Operating System is a type of
operating system that allows many users to share computer resources (maximum
utilization of the resources).
 Distributed Operating System: Distributed Operating System is a type of operating
system that manages a group of different computers and makes appear to be a
single computer. These operating systems are designed to operate on a network of
computers. They allow multiple users to access shared resources and communicate
with each other over the network. Examples include Microsoft Windows Server
and various distributions of Linux designed for servers.
 Network Operating System: Network Operating System is a type of operating
system that runs on a server and provides the capability to manage data, users,
groups, security, applications, and other networking functions.
 Real-time Operating System: Real-time Operating System is a type of operating
system that serves a real-time system and the time interval required to process
and respond to inputs is very small. These operating systems are designed to
respond to events in real time. They are used in applications that require quick and
deterministic responses, such as embedded systems, industrial control systems,
and robotics.
 Multiprocessing Operating System: Multiprocessor Operating Systems are used in
operating systems to boost the performance of multiple CPUs within a single
computer system. Multiple CPUs are linked together so that a job can be divided
and executed more quickly.
 Single-User Operating Systems: Single-User Operating Systems are designed to
support a single user at a time. Examples include Microsoft Windows for personal
computers and Apple macOS.
 Multi-User Operating Systems: Multi-User Operating Systems are designed to
support multiple users simultaneously. Examples include Linux and Unix.
 Embedded Operating Systems: Embedded Operating Systems are designed to run
on devices with limited resources, such as smartphones, wearable devices, and
household appliances. Examples include Google’s Android and Apple’s iOS.
 Cluster Operating Systems: Cluster Operating Systems are designed to run on a
group of computers, or a cluster, to work together as a single system. They are
used for high-performance computing and for applications that require high
availability and reliability. Examples include Rocks Cluster Distribution and
OpenMPI.

Multiprogramming:
Multiprogramming means more than one program can be active at the same time. Before the
operating system concept, only one program was to be loaded at a time and run. These
systems were not efficient as the CPU was not used efficiently. For example, in a single-
tasking system, the CPU is not used if the current program waits for some input/output to
finish. The idea of multiprogramming is to assign CPUs to other processes while the current
process might not be finished. This has the below advantages.
1) The user gets the feeling that he/she can run multiple applications on a single CPU even if
the CPU is running one process at a time.
2) CPU is utilized better
All modern operating systems like MS Windows, Linux, etc are multiprogramming operating
systems.
Features of Multiprogramming
 Need Single CPU for implementation.
 Context switch between process.
 Switching happens when current process undergoes waiting state.
 CPU idle time is reduced.
 High resource utilization.
 High Performance.

Multitasking:
Multi tasking operating systems allow multiple users to perform multiple tasks at the same
time. The allocation of system resources such as input/output
devices, CPU and memory among processes can be easily managed by multi-tasking operating
system. There are various states through which a processor passes to complete particular or
multiple executions. Multitasking is the ability of an OS to execute more than
one task simultaneously on a CPU machine as shown in the below diagram.
Features of Multi-Tasking Operating System
 Time Sharing – In this, many processes are allocated with resources of computer in
respective time slots, processors time is shared with multiple processes.
 Context Switching – context switching is a process of saving the context of one
process and loading the context of another process. In simpler terms it is loading
another process when the prior process has finished its execution.
 Multi-Threading – Multithreading is the ability of a program or an operating
system to enable more than one user at a time without requiring multiple copies
of the program running on the computer.
 Hardware Interrupt – When a process or an event requires urgent
attention, hardware or software will signal with an interrupt. It informs the
processor that a high-priority task has arisen that necessitates interrupting the
running process.
Types of Multi-Tasking Operating System
Two types of Multi-Tasking Operating System available as shown below:
 Pre-emptive Multi-Tasking Operating System: In pre-emptive multitasking, the
operating system can initiate a context switching from the running process to
another process. In other words, the operating system allows stopping the
execution of the currently running process and allocating the CPU to some other
process. The OS uses some criteria to decide for how long a process should
execute before allowing another process to use the operating system. The
mechanism of taking control of the operating system from one process and giving
it to another process is called pre-emption. Here are some Examples UNIX,
Windows 95, Windows NT operating system.
 Non-pre-emptive Multi-Tasking Operating System: Non-pre-emptive Multi-
Tasking Operating System is also known as cooperative multitasking, this operating
system never initiates context switching from the running process to another
process. A context switch occurs only when the processes voluntarily yield control
periodically or when idle or logically blocked to allow multiple applications to
execute simultaneously. Also, in this multitasking, all the processes cooperate for
the scheduling scheme to work. Example – Macintosh OS version 8.0-9.2.2 and
Windows 3.x operating system.

Client-Server:
The Client-server model is a distributed application structure that partitions tasks or
workloads between the providers of a resource or service, called servers, and service
requesters called clients. In the client-server architecture, when the client computer sends a
request for data to the server through the internet, the server accepts the requested process
and delivers the data packets requested back to the client. Clients do not share any of their
resources. Examples of the Client-Server Model are Email, World Wide Web, etc.
How Does the Client-Server Model Work?
In this article, we are going to take a dive into the Client-Server model and have a look at how
the Internet works via, web browsers. This article will help us have a solid WEB foundation
and help us easily work with WEB technologies.
 Client: When we say the word Client, it means to talk of a person or an
organization using a particular service. Similarly in the digital world, a Client is a
computer (Host) i.e. capable of receiving information or using a particular service
from the service providers (Servers).
 Servers: Similarly, when we talk about the word Servers, It means a person or
medium that serves something. Similarly in this digital world, a Server is a remote
computer that provides information (data) or access to particular services.

Peer-to-peer:
A peer-to-peer (P2P) operating system is a network architecture that allows devices to share
resources without a central server. In a P2P network, each device can act as both a server and a
client.
Features
 Decentralized: Each device stores a portion of the shared data locally.
 Equal participants: All devices have the same capabilities and can initiate
communication sessions.
 Distributed storage: Eliminates reliance on a single point of failure.
 Anonymized routing: Can provide anonymized routing of network traffic.

Operating System services:


An Operating System is a complex Software System. Apart from the above mentioned
components and responsibilities, there are many other activities performed by the Operating
System. Few of them are listed below:

 Security − By means of password and similar other techniques, it prevents unauthorized


access to programs and data.
 Control over system performance − Recording delays between request for a service and
response from the system.
 Job accounting − Keeping track of time and resources used by various jobs and users.
 Error detecting aids − Production of dumps, traces, error messages, and other
debugging and error detecting aids.
 Coordination between other softwares and users − Coordination and assignment of
compilers, interpreters, assemblers and other software to the various users of the
computer systems.

System calls:
System calls provide an interface to the services made by an operating system. The user
interacts with the operating system programs through System calls. These calls are normally
made available as library functions in high-level languages such as C, Java, Python etc. It
provides a level of abstraction as the user is not aware of the implementation or execution of
the call made. Details of the operating system is hidden from the user. Different hardware
and software services can be availed through system calls.
System calls are available for the following operations:
 Process Management
 Memory Management
 File Operations
 Input / Output Operations

Protection of I/O,memory and CPU:


To protect I/O, memory, and the CPU, operating systems employ mechanisms like privileged I/O
instructions, memory partitioning, and hardware features like IOMMU to prevent unauthorized
access and ensure system stability.
I/O Protection:
 Privileged Instructions:
I/O instructions are treated as privileged, meaning user programs cannot directly execute
them. Instead, they must make system calls, allowing the operating system to handle I/O
operations in kernel mode.
 Memory-Mapped I/O:
In systems with memory-mapped I/O, the operating system manages access to specific
memory regions used for I/O, preventing user programs from directly accessing these
areas.
 IOMMU (Input-Output Memory Management Unit):
Modern servers use IOMMU to protect memory during I/O operations, ensuring that
devices like network cards don't cause issues by accessing memory incorrectly.
 DMA (Direct Memory Access):
IOMMU allows the operating system to assign virtual addresses (IOVAs) to devices, which
are then translated to physical addresses by the IOMMU, preventing unauthorized access.
Memory Protection:
 Memory Partitioning:
The operating system divides memory into segments or pages, assigning different access
permissions (read, write, execute) to each segment or page.
 Protection Rings:
Some systems use protection rings, where each segment has a protection ring, and a
process can only access segments with a ring number equal to or higher than its own.
 Memory Management Unit (MMU):
The MMU translates virtual addresses used by processes into physical addresses, allowing
the operating system to control memory access and prevent unauthorized access.
CPU Protection:
 Timer Interrupts:
The operating system uses timer interrupts to periodically switch between processes,
preventing any single process from monopolizing the CPU.
 Privileged Mode:
The operating system runs in a privileged mode (kernel mode) while user programs run in
a less privileged mode (user mode), preventing user programs from executing privileged
instructions or accessing protected resources.
 Interrupts:
Interrupts allow the operating system to regain control of the CPU when necessary, such
as when an I/O operation is complete or an error occurs.
Unit – 2
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 a Multiprogramming operating systems. Such


operating systems allow more than one process to be loaded into the executable memory at a
time and the loaded process shares the CPU using time multiplexing.

Categories of Scheduling

There are two categories of scheduling:

1. Non-preemptive: Here the resource can’t be taken from a process until the process
completes execution. The switching of resources occurs when the running process
terminates and moves to a waiting state.
2. Preemptive: Here the OS allocates the resources to a process for a fixed amount of
time. During resource allocation, the process switches from running state to ready state
or from waiting state to ready state. This switching occurs as the CPU may give priority
to other processes and replace the process with higher priority with the running
process.

Concept of a process:
A process is a program in execution. For example, when we write a program in C or C++ and
compile it, the compiler creates binary code. The original code and binary code are both
programs. When we actually run the binary code, it becomes a process.
 A process is an 'active' entity instead of a program, which is considered a 'passive'
entity.
 A single program can create many processes when run multiple times; for example,
when we open a .exe or binary file multiple times, multiple instances begin
(multiple processes are created). .
How Does a Process Look Like in Memory?
A process in memory is divided into several distinct sections, each serving a different purpose.
Here's how a process typically looks in memory:
 Text Section: A text or code segment contains executable instructions. It is
typically a read only section
 Stack: The stack contains temporary data, such as function parameters, returns
addresses, and local variables.
 Data Section: Contains the global variable.
 Heap Section: Dynamically memory allocated to process during its run time.
Attributes of a Process
A process has several important attributes that help the operating system manage and
control it. These attributes are stored in a structure called the Process Control Block
(PCB) (sometimes called a task control block). The PCB keeps all the key information about
the process, including:
1. Process ID (PID): A unique number assigned to each process so the operating
system can identify it.
2. Process State: This shows the current status of the process, like whether it is
running, waiting, or ready to execute.
3. Priority and other CPU Scheduling Information: Data that helps the operating
system decide which process should run next, like priority levels and pointers to
scheduling queues.
4. I/O Information: Information about input/output devices the process is using.
5. File Descriptors: Information about open files files and network connections.
6. Accounting Information: Tracks how long the process has run, the amount of CPU
time used, and other resource usage data.
7. Memory Management Information: Details about the memory space allocated to
the process, including where it is loaded in memory and the structure of its
memory layout (stack, heap, etc.).
These attributes in the PCB help the operating system control, schedule, and manage each
process effectively.

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

PCB: A Process Control Block, commonly referred to as PCB in OS, is a crucial data structure that
encapsulates the specifics about every process present within a computing system. When a
process is birthed, the operating system instantaneously establishes a unique PCB in OS for it.
This is done to ensure that the system can actively monitor the state of the process, efficiently
manage the resources it might require, and oversee its execution in a streamlined manner.

Essentially, the PCB in OS acts as the fingerprint of a process, providing the OS with all the
pertinent details required to manage and control the process optimally. Without the PCB in OS,
the operating system would be blind to the processes it needs to govern, making the PCB
indispensable for smooth OS functionality.

Structure of Process Control Block

The process control block contains many attributes such as process ID, process state, process
priority, accounting information, program counter, CPU registers`, etc for each process.

Now we will discuss each field of PCB in detail.


1. Process ID:

When a new process is created by the user, the operating system assigns a unique ID i.e.
a process-ID to that process. This ID helps the process to be distinguished from other processes
existing in the system.

The operating system has a limit on the maximum number of processes it is capable of dealing
with, let's say the OS can handle most N processes at a time.

So, process-ID will get the values from 0 to N-1.

 The first process will be given ID0.


 The second process will be given ID 1.
 It continues till N-1.

Now if a new process is created after process(N-1) then the Operating System will allot ID 0 to
this new process considering the older process at id 0 is already terminated. This is one of the
schemes for assigning the process IDs.

There is another scheme of assignment where the process IDs are not allocated in ascending
order.

Let's say a single PCB requires X bytes of memory and in total there can be N processes allowed
at a time. Then, the operating system will reserve N*X bytes for all the PCBs.

These PCBs are numbered from 0 to N-1. Please note here we are giving IDs to PCBs and not to
the processes.

Now whenever a process is triggered by the user a free PCB slot is allotted to that process and
the process ID of that process will be the same as the PCB slot number. So, the operating
system maintains a chain of free PCB slots. If the chain is empty no new process can be created.

2. Process State:

A process, from its creation to completion goes through different states. Generally, a process
may be present in one of the 5 states during its execution:

 New: This state contains the processes that are ready to be loaded by the operating
system into the main memory.
 Ready: This state contains the process that is both ready to be executed and is currently
in the main memory of the system. The operating system brings the processes from
secondary memory(hard disk) to main memory(RAM). As these processes are present in
the main memory and are waiting to be assigned to the CPU, the state of these
processes is known as the Ready state.
 Running: This state contains the processes that are currently executed by the CPU in our
system. If there is a total x CPU in our system, then the maximum number of running
processes for a particular time is also x.
 Block or wait: A process from its running state may transition to a block or wait-for state
depending on the scheduling algorithm or because of the internal behavior of the
process (the process explicitly wants to wait).
 Termination: A process that completes its execution comes to its termination state. All
the contents of that process(Process control block) will also be deleted by the operating
system.

3. Process Priority:

Process priority is a numeric value that represents the priority of each process. The lesser the
value, the greater the priority of that process. This priority is assigned at the time of the
creation of the PCB and may depend on many factors like the age of that process, the resources
consumed, and so on. The user can also externally assign a priority to the process.

4. Process Accounting Information:

This attribute gives information on the resources used by that process in its lifetime. For
Example: CPU time connection time, etc.

5. Program Counter:

The program counter is a pointer that points to the next instruction in the program to be
executed. This attribute of PCB contains the address of the next instruction to be executed in
the process.

6. CPU registers:

A CPU register is a quickly accessible small-sized location available to the CPU. These registers
are stored in virtual memory (RAM)`.

6. Context Switching:

Context switching is a process that involves switching the CPU from one process or task to
another. It is the process of storing the state of a process so that it can be restored and resume
execution at a later point. This allows multiple processes to share a single CPU and is an
essential feature of a multitasking operating system.

So, whenever context switching occurs in the code execution the current state of that process is
stored temporarily in CPU registers. This helps in the fast execution of the process by not
wasting time-saving and retrieving state information from the secondary memory(hard disk).
8. PCB pointer:

This field contains the address of the next PCB, which is in ready state. This helps the operating
system to hierarchically maintain an easy control flow between parent processes and child
processes.

9. List of open files:

As the name suggests, It contains information on all the files that are used by that process. This
field is important as it helps the operating system to close all the opened files at the
termination state of the process.

10. Process I/O information:

In this field, the list of all the input/output devices that are required by that process during its
execution is mentioned.

Process lifecycle:
When you run a program (which becomes a process), it goes through different phases before
it completion. These phases, or states, can vary depending on the operating system, but the
most common process lifecycle includes two, five, or seven states. Here’s a simple
explanation of these states:
The Two-State Model
The simplest way to think about a process’s lifecycle is with just two states:
1. Running: This means the process is actively using the CPU to do its work.
2. Not Running: This means the process is not currently using the CPU. It could be
waiting for something, like user input or data, or it might just be paused.

Two State Process Model

When a new process is created, it starts in the not running state. Initially, this process is kept
in a program called the dispatcher.
Here’s what happens step by step:
1. Not Running State: When the process is first created, it is not using the CPU.
2. Dispatcher Role: The dispatcher checks if the CPU is free (available for use).
3. Moving to Running State: If the CPU is free, the dispatcher lets the process use the
CPU, and it moves into the running state.
4. CPU Scheduler Role: When the CPU is available, the CPU scheduler decides which
process gets to run next. It picks the process based on a set of rules called
the scheduling scheme, which varies from one operating system to another.
The Five-State Model
The five-state process lifecycle is an expanded version of the two-state model. The two-state
model works well when all processes in the not running state are ready to run. However, in
some operating systems, a process may not be able to run because it is waiting for something,
like input or data from an external device. To handle this situation better, the not running
state is divided into two separate states:

Five state Process Model

Here’s a simple explanation of the five-state process model:


 New: This state represents a newly created process that hasn’t started running yet.
It has not been loaded into the main memory, but its process control block (PCB)
has been created, which holds important information about the process.
 Ready: A process in this state is ready to run as soon as the CPU becomes
available. It is waiting for the operating system to give it a chance to execute.
 Running: This state means the process is currently being executed by the CPU.
Since we’re assuming there is only one CPU, at any time, only one process can be
in this state.
 Blocked/Waiting: This state means the process cannot continue executing right
now. It is waiting for some event to happen, like the completion of an input/output
operation (for example, reading data from a disk).
 Exit/Terminate: A process in this state has finished its execution or has been
stopped by the user for some reason. At this point, it is released by the operating
system and removed from memory.

Operations on processes:
The states of a process are as follows:
 New State: In this step, the process is about to be created but not yet created. It is
the program that is present in secondary memory that will be picked up by the OS
to create the process.
 Ready State: New -> Ready to run. After the creation of a process, the process
enters the ready state i.e. the process is loaded into the main memory. The
process here is ready to run and is waiting to get the CPU time for its execution.
Processes that are ready for execution by the CPU are maintained in a queue called
a ready queue for ready processes.
 Run State: The process is chosen from the ready queue by the OS for execution
and the instructions within the process are executed by any one of the available
processors.
 Blocked or Wait State: Whenever the process requests access to I/O needs input
from the user or needs access to a critical region(the lock for which is already
acquired) it enters the blocked or waits state. The process continues to wait in the
main memory and does not require CPU. Once the I/O operation is completed the
process goes to the ready state.
 Terminated or Completed State: Process is killed as well as PCB is deleted. The
resources allocated to the process will be released or deallocated.
 Suspend Ready: Process that was initially in the ready state but was swapped out
of main memory(refer to Virtual Memory topic) and placed onto external storage
by the scheduler is said to be in suspend ready state. The process will transition
back to a ready state whenever the process is again brought onto the main
memory.
 Suspend Wait or Suspend Blocked: Similar to suspend ready but uses the process
which was performing I/O operation and lack of main memory caused them to
move to secondary memory. When work is finished it may go to suspend ready.
Operation on a Process
The execution of a process is a complex activity. It involves various operations. Following are
the operations that are performed while execution of a process:

1. Creation
This is the initial step of the process execution activity. Process creation means the
construction of a new process for execution. This might be performed by the system, the
user, or the old process itself. There are several events that lead to the process creation.
Some of the such events are the following:
 When we start the computer, the system creates several background processes.
 A user may request to create a new process.
 A process can create a new process itself while executing.
 The batch system takes initiation of a batch job.
2. Scheduling/Dispatching
The event or activity in which the state of the process is changed from ready to run. It means
the operating system puts the process from the ready state into the running state.
Dispatching is done by the operating system when the resources are free or the process has
higher priority than the ongoing process. There are various other cases in which the process
in the running state is preempted and the process in the ready state is dispatched by
the operating system.
3. Blocking
When a process invokes an input-output system call that blocks the process, and operating
system is put in block mode. Block mode is basically a mode where the process waits for
input-output. Hence on the demand of the process itself, the operating system blocks the
process and dispatches another process to the processor. Hence, in process-blocking
operations, the operating system puts the process in a ‘waiting’ state.
4. Preemption
When a timeout occurs that means the process hadn’t been terminated in the allotted time
interval and the next process is ready to execute, then the operating system preempts the
process. This operation is only valid where CPU scheduling supports preemption. Basically,
this happens in priority scheduling where on the incoming of high priority process the
ongoing process is preempted. Hence, in process preemption operation, the operating system
puts the process in a ‘ready’ state.
5. Process Termination
Process termination is the activity of ending the process. In other words, process termination
is the relaxation of computer resources taken by the process for the execution. Like creation,
in termination also there may be several events that may lead to the process of termination.
Some of them are:
 The process completes its execution fully and it indicates to the OS that it has
finished.
 The operating system itself terminates the process due to service errors.
 There may be a problem in hardware that terminates the process.

Context switch:

A context switching is the mechanism to store and restore the state or context of a CPU in
Process Control block so that a process execution can be resumed from the same point at a
later time. Using this technique, a context switcher enables multiple processes to share a single
CPU. Context switching is an essential part of a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block. After this, the state
for the process to run next is loaded from its own PCB and used to set the PC, registers, etc. At
that point, the second process can start executing.

Context switches are computationally intensive since register and memory state must be saved
and restored. To avoid the amount of context switching time, some hardware systems employ
two or more sets of processor registers. When the process is switched, the following
information is stored for later use.

 Program Counter
 Scheduling information
 Base and limit register value
 Currently used register
 Changed State
 I/O State information
 Accounting information

Types of schedulers:

Schedulers

Schedulers are special system software which handle process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to
run. Schedulers are of three types −

 Long-Term Scheduler
 Short-Term Scheduler
 Medium-Term Scheduler

Long Term Scheduler

It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing. It selects processes from the queue and loads them into
memory for execution. Process loads into the memory for CPU scheduling.

The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the
average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems have no long term scheduler. When a process changes the state from new to
ready, then there is use of long-term scheduler.

Short Term Scheduler

It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of the
process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.

Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next. Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler

Medium-term scheduling is a part of swapping. It removes the processes from the memory. It
reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling
the swapped out-processes.

A running process may become suspended if it makes an I/O request. A suspended processes
cannot make any progress towards completion. In this condition, to remove the process from
memory and make space for other processes, the suspended process is moved to the secondary
storage. This process is called swapping, and the process is said to be swapped out or rolled
out. Swapping may be necessary to improve the process mix.

CPU burst-I/O burst cycles:

In operating systems, a process execution consists of alternating CPU bursts and I/O bursts:

1. CPU Burst: The period during which a process is executing instructions on the CPU
without waiting for input/output (I/O) operations. This involves computations, memory
access, and other processing tasks.
2. I/O Burst: The period during which a process is waiting for or performing input/output
operations, such as reading from a disk, writing to a file, or receiving user input. During
this time, the CPU is idle for that process, and the system may schedule another process
for execution.

A process typically experiences multiple CPU and I/O bursts throughout its execution, leading to
an alternating CPU-I/O burst cycle until completion.
Dispatcher:
Dispatcher is a special type of program whose work starts after the scheduler. When the
scheduler completes its task of selecting a process, it is the dispatcher that moves the process
to the queue it needs to go to.
The dispatcher is the module that hands over control of the CPU to the process that has been
selected by the short-term scheduler.
The following things happen in this function:
1. switching context: Cores the current process and restores the state of the process
to be run next.
2. Switching to User Mode: Makes sure that it runs in the user mode and not kernel
mode, which is for security and privilege break-
3. Jumps to the correct location in the user program from where the program can be
restarted.
Advantages of a Dispatcher
 Fast Process Switching: Evaluates circumstances where a procedure shifts from the
ready queue to the execution phase in a way that causes less delay.
 Efficient CPU Time Allocation: Is important in making sure that processes receive
CPU time, hence giving the necessary support for multitasking to occur.
Disadvantages of a Dispatcher
 Dispatch Latency: Although the time taken is considerably small, this lateness in
dispatching the requests can slow down the system.
 Dependent on Scheduler: The dispatcher hence cannot work on her own since she
is reliant on the decisions made by the scheduler.

Scheduling criteria:
There are several criteria that the CPU scheduler considers when selecting the next process to
be executed. These criteria help in evaluating the performance of different scheduling
algorithms & determining which one is best suited for a particular situation. The main criteria of
CPU scheduling are:
1. CPU Utilization: It refers to the percentage of time the CPU is busy executing processes. The
goal is to maximize CPU utilization & minimize idle time.

2. Throughput: It is the number of processes completed per unit time. A higher throughput
indicates that the system is able to execute more processes in a given time period.

3. Turnaround Time: It is the total time taken by a process from its submission to its
completion, including waiting time & execution time. The aim is to minimize the average
turnaround time.
4. Waiting Time: It is the amount of time a process spends waiting in the ready queue before it
gets executed. The objective is to minimize the average waiting time.

5. Response Time: It is the time between the submission of a request & the first response from
the system. The goal is to minimize the average response time.

Scheduling algorithms:
CPU scheduling is a process used by the operating system to decide which task or process
gets to use the CPU at a particular time. This is important because a CPU can only handle one
task at a time, but there are usually many tasks that need to be processed. The following are
different purposes of a CPU scheduling time.
 Maximize the CPU utilization
 Minimize the response and waiting time of the process.

FCFS, SJF, STRN, Round Robin:


the common CPU scheduling algorithms: First-Come, First-Served (FCFS), Shortest Job First
(SJF), Shortest Remaining Time First (SRTF), and Round Robin, along with their key
characteristics:
1. First-Come, First-Served (FCFS)
 Concept: Processes are executed in the order they arrive in the ready queue.
 Characteristics:
o Simple to implement.
o Non-preemptive (once a process starts, it runs to completion).
o Can lead to long wait times for processes arriving later if there are long
processes ahead in the queue.
 Example: Imagine a line at a store; the first person in line gets served first, regardless of
how long their purchase will take.
2. Shortest Job First (SJF)
 Concept: The process with the shortest CPU burst time is selected for execution.
 Characteristics:
o Minimizes average waiting time.
o Non-preemptive (once a process starts, it runs to completion).
o Can lead to starvation for long processes if short processes continuously arrive.
 Example: In a project, the tasks with the shortest estimated time are completed first.
3. Shortest Remaining Time First (SRTF)
 Concept:
The preemptive version of SJF, where the process with the shortest remaining time until
completion is selected for execution.
 Characteristics:
 Preemptive (can interrupt a running process if a new process with shorter
remaining time arrives).
 Minimizes average waiting time.
 Can lead to context switching overhead.
 Example:
In a project, if a short task arrives while a longer task is in progress, the short task is
executed first.
4. Round Robin
 Concept:
Each process is given a fixed time slice (quantum) to execute, and then it's moved to the
end of the queue.
 Characteristics:
 Preemptive (processes are interrupted after their time slice).
 Ensures fair CPU allocation to all processes.
 Can lead to context switching overhead.
 Example:
Imagine a group of people taking turns to speak in a meeting; each person has a fixed
amount of time to speak, and then the next person takes their turn.

Priority scheduling:
Priority scheduling is one of the most common scheduling algorithms used by the operating
system to schedule processes based on their priority. Each process is assigned a priority. The
process with the highest priority is to be executed first and so on.
Processes with the same priority are executed on a first-come first served basis. Priority can
be decided based on memory requirements, time requirements or any other resource
requirement. Also priority can be decided on the ratio of average I/O to average CPU burst
time.
Priority Scheduling can be implemented in two ways:
 Non-Preemptive Priority Scheduling
 Preemptive Priority Scheduling
Non-Preemptive Priority Scheduling
In Non-Preemptive Priority Scheduling, the CPU is not taken away from the running process.
Even if a higher-priority process arrives, the currently running process will complete first.
Ex: A high-priority process must wait until the currently running process finishes.
In Preemptive Priority Scheduling, the CPU can be taken away from the currently running
process if a new process with a higher priority arrives.
Ex: A low-priority process is running, and a high-priority process arrives; the CPU immediately
switches to the high-priority process.

multilevel queue scheduling:


Multilevel Queue Scheduling is a CPU scheduling mechanism where the process is divided into
several hierarchy queues and each queue possesses a different priority, and process type. The
scheduling algorithm can be different for each Queue and these processes are mapped in a
permanent manner to a particular Queue following some criteria, for example in relation to
priority or resources.
Features of Multilevel Queue (MLQ) CPU Scheduling
 Multiple Queues: In MLQ scheduling, processes are divided into multiple queues
based on their priority, with each queue having a different priority level. Higher-
priority processes are placed in queues with higher priority levels, while lower-
priority processes are placed in queues with lower priority levels.
 Priorities Assigned: Priorities are assigned to processes based on their type,
characteristics, and importance. For example, interactive processes like user
input/output may have a higher priority than batch processes like file backups.
 Preemption: Preemption is allowed in MLQ scheduling, which means a higher
priority process can preempt a lower priority process, and the CPU is allocated to
the higher priority process. This helps ensure that high-priority processes are
executed in a timely manner.
 Scheduling Algorithm: Different scheduling algorithms can be used for each
queue, depending on the requirements of the processes in that queue. For
example, Round Robin scheduling may be used for interactive processes, while
First Come First Serve scheduling may be used for batch processes.
 Feedback Mechanism: A feedback mechanism can be implemented to adjust the
priority of a process based on its behavior over time. For example, if an interactive
process has been waiting in a lower-priority queue for a long time, its priority may
be increased to ensure it is executed in a timely manner.
 Efficient Allocation of CPU Time: MLQ scheduling ensures that processes with
higher priority levels are executed in a timely manner, while still allowing lower
priority processes to execute when the CPU is idle.
 Fairness: MLQ scheduling provides a fair allocation of CPU time to different types
of processes, based on their priority and requirements.
 Customizable: MLQ scheduling can be customized to meet the specific
requirements of different types of processes.

Performance evaluation of algorithms through deterministic modeling:


The first thing we need to decide is how we will evaluate the algorithms. To do this we need to
decide on the relative importance of the factors we listed above (Fairness, Efficiency, Response
Times, Turnaround and Throughput). Only once we have decided on our evaluation method can
we carry out the evaluation.

Deterministic Modeling
This evaluation method takes a predetermined workload and evaluates each algorithm using
that workload.
Assume we are presented with the following processes, which all arrive at time zero.

Process Burst Time

P1 9

P2 33

P3 2

P4 5

P5 14

Which of the following algorithms will perform best on this workload?


First Come First Served (FCFS), Non Preemptive Shortest Job First (SJF) and Round Robin (RR).
Assume a quantum of 8 milliseconds.
Before looking at the answers, try to calculate the figures for each algorithm.
The advantages of deterministic modeling is that it is exact and fast to compute. The
disadvantage is that it is only applicable to the workload that you use to test. As an example,
use the above workload but assume P1 only has a burst time of 8 milliseconds. What does this
do to the average waiting time?
Of course, the workload might be typical and scale up but generally deterministic modeling is
too specific and requires too much knowledge about the workload.

Queuing Models
Another method of evaluating scheduling algorithms is to use queuing theory. Using data from
real processes we can arrive at a probability distribution for the length of a burst time and the
I/O times for a process. We can now generate these times with a certain distribution.
We can also generate arrival times for processes (arrival time distribution).
If we define a queue for the CPU and a queue for each I/O device we can test the various
scheduling algorithms using queuing theory.
Knowing the arrival rates and the service rates we can calculate various figures such as average
queue length, average wait time, CPU utilization etc.
One useful formula is Little's Formula.
n = λw Where
n is the average queue length
λ is the average arrival rate for new processes (e.g. five a second)
w is the average waiting time in the queue
Knowing two of these values we can, obviously, calculate the third. For example, if we know that
eight processes arrive every second and there are normally sixteen processes in the queue we
can compute that the average waiting time per process is two seconds.
The main disadvantage of using queuing models is that it is not always easy to define realistic
distribution times and we have to make assumptions. This results in the model only being an
approximation of what actually happens.

Simulations
Rather than using queuing models we simulate a computer. A Variable, representing a clock is
incremented. At each increment the state of the simulation is updated.
Statistics are gathered at each clock tick so that the system performance can be analysed.
The data to drive the simulation can be generated in the same way as the queuing model,
although this leads to similar problems.
Alternatively, we can use trace data. This is data collected from real processes on real machines
and is fed into the simulation. This can often provide good results and good comparisons over a
range of scheduling algorithms.
However, simulations can take a long time to run, can take a long time to implement and the
trace data may be difficult to collect and require large amounts of storage.

Implementation
The best way to compare algorithms is to implement them on real machines. This will give the
best results but does have a number of disadvantages.

· It is expensive as the algorithm has to be written and then implemented on real hardware.
· If typical workloads are to be monitored, the scheduling algorithm must be used in a live
situation. Users may not be happy with an environment that is constantly changing.
· If we find a scheduling algorithm that performs well there is no guarantee that this state will
continue if the workload or environment changes.
Unit – 3
Memory management:
The main memory is central to the operation of a Modern Computer. Main Memory is a large
array of words or bytes, ranging in size from hundreds of thousands to billions. Main memory
is a repository of rapidly available information shared by the CPU and I/O devices. Main
memory is the place where programs and information are kept when the processor is
effectively utilizing them. Main memory is associated with the processor, so moving
instructions and information into and out of the processor is extremely fast. Main memory is
also known as RAM (Random Access Memory). This memory is volatile. RAM loses its data
when a power interruption occurs.

Main Memory

What is Memory Management?


Memory management mostly involves management of main memory. In a multiprogramming
computer, the Operating System resides in a part of the main memory, and the rest is used by
multiple processes. The task of subdividing the memory among different processes is called
Memory Management. Memory management is a method in the operating system to manage
operations between main memory and disk during process execution. The main aim of
memory management is to achieve efficient utilization of memory.
Why Memory Management is Required?
 Allocate and de-allocate memory before and after process execution.
 To keep track of used memory space by processes.
 To minimize fragmentation issues.
 To proper utilization of main memory.
 To maintain data integrity while executing of process.
read more about – Requirements of Memory Management System
Now we are discussing the concept of Logical Address Space and Physical Address Space
Logical and Physical Address Space
 Logical Address Space: An address generated by the CPU is known as a “Logical
Address”. It is also known as a Virtual address. Logical address space can be
defined as the size of the process. A logical address can be changed.
 Physical Address Space: An address seen by the memory unit (i.e. the one loaded
into the memory address register of the memory) is commonly known as a
“Physical Address”. A Physical address is also known as a Real address. The set of
all physical addresses corresponding to these logical addresses is known as
Physical address space. A physical address is computed by MMU. The run-time
mapping from virtual to physical addresses is done by a hardware device Memory
Management Unit(MMU). The physical address always remains constant.
Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different types of
loading :
 Static Loading: Static Loading is basically loading the entire program into a fixed
address. It requires more memory space.
 Dynamic Loading: The entire program and all data of a process must be in physical
memory for the process to execute. So, the size of a process is limited to the size
of physical memory. To gain proper memory utilization, dynamic loading is used.
In dynamic loading, a routine is not loaded until it is called. All routines are residing
on disk in a relocatable load format. One of the advantages of dynamic loading is
that the unused routine is never loaded. This loading is useful when a large
amount of code is needed to handle it efficiently.
Static and Dynamic Linking
To perform a linking task a linker is used. A linker is a program that takes one or more object
files generated by a compiler and combines them into a single executable file.
 Static Linking: In static linking, the linker combines all necessary program modules
into a single executable program. So there is no runtime dependency. Some
operating systems support only static linking, in which system language libraries
are treated like any other object module.
 Dynamic Linking: The basic concept of dynamic linking is similar to dynamic
loading. In dynamic linking, “Stub” is included for each appropriate library routine
reference. A stub is a small piece of code. When the stub is executed, it checks
whether the needed routine is already in memory or not. If not available then the
program loads the routine into memory.
Swapping
When a process is executed it must have resided in memory. Swapping is a process of
swapping a process temporarily into a secondary memory from the main memory, which is
fast compared to secondary memory. A swapping allows more processes to be run and can be
fit into memory at one time. The main part of swapping is transferred time and the total time
is directly proportional to the amount of memory swapped. Swapping is also known as roll-
out, or roll because if a higher priority process arrives and wants service, the memory
manager can swap out the lower priority process and then load and execute the higher
priority process. After finishing higher priority work, the lower priority process swapped back
in memory and continued to the execution process.

swapping in memory management

Memory Management Techniques


Memory management techniques are methods used by an operating system to efficiently
allocate, utilize, and manage memory resources for processes. These techniques ensure
smooth execution of programs and optimal use of system memory
Different Memory Management techniques are:

Address binding:
The Address Binding refers to the mapping of computer instructions and data to physical
memory locations. Both logical and physical addresses are used in computer memory. It assigns
a physical memory region to a logical pointer by mapping a physical address to a logical address
known as a virtual address. It is also a component of computer memory management that the
OS performs on behalf of applications that require memory access.

Types of Address Binding in Operating System

There are mainly three types of an address binding in the OS. These are as follows:

1. Compile Time Address Binding


2. Load Time Address Binding
3. Execution Time or Dynamic Address Binding
Compile Time Address Binding
It is the first type of address binding. It occurs when the compiler is responsible for performing
address binding, and the compiler interacts with the operating system to perform the address
binding. In other words, when a program is executed, it allocates memory to the system code of
the computer. The address binding assigns a logical address to the beginning of the memory
segment to store the object code. Memory allocation is a long-term process and may only be
modified by recompiling the program.

Load Time Address Binding


It is another type of address binding. It is done after loading the program in the memory, and it
would be done by the operating system memory manager, i.e., loader. If memory allocation is
specified when the program is assigned, no program in its compiled state may ever be
transferred from one computer to another. Memory allocations in the executable code may
already be in use by another program on the new system. In this case, the logical addresses of
the program are not connected to physical addresses until it is applied and loaded into
memory.

Execution Time or Dynamic Address Binding


Execution time address binding is the most popular type of binding for scripts that aren't
compiled because it only applies to variables in the program. When a variable in a program is
encountered during the processing of instructions in a script, the program seeks memory space
for that variable. The memory would assign the space to that variable until the program
sequence finished or unless a specific instruction within the script released the memory address
connected to a variable.

Contiguous memory allocation:

Contiguous memory allocation refers to a memory management technique in which whenever


there occurs a request by a user process for the memory, one of the sections of the contiguous
memory block would be given to that process, in accordance with its requirement.
As you can see in the illustration shown above, three files are there in the directory. The
starting block, along with the length of each file, is mentioned in the table. Thus, we can see in
this table that all the contiguous blocks get assigned to every file as per their need.

Types of Partitions

Contiguous memory allocation can be achieved when we divide the memory into the following
types of partitions:

1. Fixed-Sized Partitions

Another name for this is static partitioning. In this case, the system gets divided into multiple
fixed-sized partitions. In this type of scheme, every partition may consist of exactly one process.
This very process limits the extent at which multiprogramming would occur, since the total
number of partitions decides the total number of processes. Read more on fixed-sized
partitions here.

2. Variable-Sized Partitions

Dynamic partitioning is another name for this. The scheme allocation in this type of partition is
done dynamically. Here, the size of every partition isn’t declared initially. Only once we know
the process size, will we know the size of the partitions. But in this case, the size of the process
and the partition is equal; thus, it helps in preventing internal fragmentation.

On the other hand, when a process is smaller than its partition, some size of the partition gets
wasted (internal fragmentation). It occurs in static partitioning, and dynamic partitioning solves
this issue. Read more on dynamic partitions here.

Pros of Contiguous Memory Allocation

1. It supports a user’s random access to files.

2. The user gets excellent read performance.

3. It is fairly simple to implement.

Cons of Contiguous Memory Allocation

1. Having a file grow might be somewhat difficult.


2. The disk may become fragmented.

static and dynamic partitioned memory:

Memory management is a fundamental aspect of an operating system (OS), ensuring efficient


allocation of memory to processes. Two primary techniques for partitioning memory are static
partitioning and dynamic partitioning.

1. Static Partitioned Memory

Definition

Static partitioning divides the memory into fixed-sized partitions at system startup. Each
partition can hold exactly one process, and its size remains constant throughout execution.

Characteristics

 Memory is pre-divided before execution.


 The number and size of partitions are fixed.
 Processes are assigned to a partition based on their size.
 Unused space in partitions leads to internal fragmentation.

Advantages

✅ Simple Implementation – Easy to manage as partitions are predefined.


✅ Less Overhead – No need for complex memory allocation strategies.

Disadvantages

❌ Internal Fragmentation – If a process uses less memory than the partition size, the remaining
space is wasted.
❌ Fixed Number of Processes – Cannot exceed the predefined partition limit, even if memory is
available.
❌ Inefficient Utilization – Large processes may not fit in any partition, leading to underutilized
memory.

2. Dynamic Partitioned Memory


Definition

Dynamic partitioning allows memory to be allocated dynamically based on the process's size,
rather than using fixed partitions.

Characteristics

 Partitions are created at runtime when a process requests memory.


 Memory is allocated exactly as needed, reducing internal fragmentation.
 Unused memory remains available for new processes.
 Requires complex management for tracking free memory.

Advantages

✅ Efficient Utilization – Only the required memory is allocated, minimizing waste.


✅ No Fixed Partition Limit – Any number of processes can be accommodated as long as
memory is available.
✅ Better Memory Usage – More flexibility in handling different process sizes.

Disadvantages

❌ External Fragmentation – Small free memory blocks form over time, making it difficult to
allocate large processes.
❌ Memory Compaction Overhead – OS may need to rearrange memory to merge free spaces,
increasing CPU load.
❌ Complex Implementation – Requires additional mechanisms to track memory allocation.

Fragmentation:

Fragmentation refers to a process of information storage where the memory space of the
system is used inadequately, thus reducing the overall efficiency or ability or both (sometimes).
The implications of the process of fragmentation depend entirely on the specific allocation of
storage space schemes in the operation along with the particular fragmentation types. In some
instances, fragmentation leads to some unused storage capacity. This concept is also applicable
to the generated unused space in this very situation.

The memory used for the preservation of the data set (like file formats) is very similar to the
other systems (like the FAT file system), irrespective of the amount of fragmentation (it
happens from null to the extreme).
Types of Fragmentation

Fragmentation is of three types:

 External Fragmentation
 Internal Fragmentation
 Data Fragmentation (which exists beside or a combination)
Fragmentation can often be acknowledged when preferring enhancements, usability, or
inefficiency. Similar things might also happen for the other tools, like processors. Let us discuss
what’s the major difference between internal and external fragmentation.

Internal Fragmentation

Whenever a memory block gets allocated with a process, and in case the process happens to be
smaller than the total amount of requested memory, a free space is ultimately created in this
memory block. And due to this, the memory block’s free space is unused. This is what causes
internal fragmentation. Read more on Internal Fragmentation here.

External Fragmentation

External fragmentation occurs whenever a method of dynamic memory allocation happens to


allocate some memory and leave a small amount of unusable memory. The total quantity of the
memory available is reduced substantially in case there’s too much external fragmentation. So,
there’s enough memory space in order to complete a request, and it is not contiguous. Thus, it
is known as external fragmentation. Read more on External Fragmentation here.

Causes of Fragmentation

The user processes are unloaded and loaded from the main memory. Also, all the processes are
kept in the memory blocks in a system’s main memory. Various spaces are left after the loading
and swapping of processes that other processes can’t load because of their sizes. The main
memory is available, but the space isn’t sufficient in order to load other processes since the
allocation of the main memory processes is dynamic.

Pros and Cons of Fragmentation

Fragmentation comes with various pros and cons. Let us look into some of the prominent ones
now.
Pros

Storage optimization

The fragmented systems might make potentially better use of the storage devices by using
every storage block available to us.

Fewer failures

If a system has insufficient sequential space, then it won’t support fragmentation and would
ultimately fail. No issues would arise.

Fast data writes

The data written in a system supporting data fragmentation can be faster than the reorganizing
data storage. It enables data writes in a contiguous manner.

Cons

Need for regular defragmentation

The storage device with more fragmentation would ultimately degrade over time. Thus, there
would be a necessary requirement for time-consuming operations of defragmentation.

Slower read times

The amount of time taken to read a non-sequential file may increase when a storage device
happens to become more fragmented.

Memory management keeps track of the status of each memory location, whether it is
allocated or free. It allocates the memory dynamically to the programs at their request and
frees it for reuse when it is no longer needed. Memory management meant to satisfy some
requirements that we should keep in mind.
These Requirements of memory management are:
1. Relocation – The available memory is generally shared among a number of
processes in a multiprogramming system, so it is not possible to know in advance
which other programs will be resident in main memory at the time of execution of
this program. Swapping the active processes in and out of the main memory
enables the operating system to have a larger pool of ready-to-execute process.
When a program gets swapped out to a disk memory, then it is not always possible
that when it is swapped back into main memory then it occupies the previous
memory location, since the location may still be occupied by another process. We
may need to relocate the process to a different area of memory. Thus there is a
possibility that program may be moved in main memory due to swapping.

The figure depicts a process image. The process image is occupying a continuous
region of main memory. The operating system will need to know many things
including the location of process control information, the execution stack, and the
code entry. Within a program, there are memory references in various instructions
and these are called logical addresses.

After loading of the program into main memory, the processor and the operating
system must be able to translate logical addresses into physical addresses. Branch
instructions contain the address of the next instruction to be executed. Data
reference instructions contain the address of byte or word of data referenced.
2. Protection – There is always a danger when we have multiple programs at the
same time as one program may write to the address space of another program. So
every process must be protected against unwanted interference when other
process tries to write in a process whether accidental or incidental. Between
relocation and protection requirement a trade-off occurs as the satisfaction of
relocation requirement increases the difficulty of satisfying the protection
requirement.

Prediction of the location of a program in main memory is not possible, that’s why
it is impossible to check the absolute address at compile time to assure protection.
Most of the programming language allows the dynamic calculation of address at
run time. The memory protection requirement must be satisfied by the processor
rather than the operating system because the operating system can hardly control
a process when it occupies the processor. Thus it is possible to check the validity of
memory references.
3. Sharing – A protection mechanism must have to allow several processes to access
the same portion of main memory. Allowing each processes access to the same
copy of the program rather than have their own separate copy has an advantage.

For example, multiple processes may use the same system file and it is natural to
load one copy of the file in main memory and let it shared by those processes. It is
the task of Memory management to allow controlled access to the shared areas of
memory without compromising the protection. Mechanisms are used to support
relocation supported sharing capabilities.
4. Logical organization – Main memory is organized as linear or it can be a one-
dimensional address space which consists of a sequence of bytes or words. Most of
the programs can be organized into modules, some of those are unmodifiable
(read-only, execute only) and some of those contain data that can be modified. To
effectively deal with a user program, the operating system and computer hardware
must support a basic module to provide the required protection and sharing. It has
the following advantages:

 Modules are written and compiled independently and all the references
from one module to another module are resolved by `the system at run
time.
 Different modules are provided with different degrees of protection.
 There are mechanisms by which modules can be shared among
processes. Sharing can be provided on a module level that lets the user
specify the sharing that is desired.
5. Physical organization – The structure of computer memory has two levels referred
to as main memory and secondary memory. Main memory is relatively very fast
and costly as compared to the secondary memory. Main memory is volatile. Thus
secondary memory is provided for storage of data on a long-term basis while the
main memory holds currently used programs. The major system concern between
main memory and secondary memory is the flow of information and it is
impractical for programmers to understand this for two reasons:

 The programmer may engage in a practice known as overlaying when


the main memory available for a program and its data may be
insufficient. It allows different modules to be assigned to the same
region of memory. One disadvantage is that it is time-consuming for the
programmer.
 In a multiprogramming environment, the programmer does not know
how much space will be available at the time of coding and where that
space will be located inside the memory.
Compaction:
Compaction is a technique to collect all the free memory present in the form of fragments
into one large chunk of free memory, which can be used to run other processes.
It does that by moving all the processes towards one end of the memory and all the available
free space towards the other end of the memory so that it becomes contiguous.
It is not always easy to do compaction. Compaction can be done only when the relocation is
dynamic and done at execution time. Compaction can not be done when relocation is static
and is performed at load time or assembly time.

Non-Contiguous memory allocation:


Non-contiguous allocation, also known as dynamic or linked allocation, is a memory allocation
technique used in operating systems to allocate memory to processes that do not require a
contiguous block of memory. In this technique, each process is allocated a series of non-
contiguous blocks of memory that can be located anywhere in the physical memory.
Non-contiguous allocation involves the use of pointers to link the non-contiguous memory
blocks allocated to a process. These pointers are used by the operating system to keep track
of the memory blocks allocated to the process and to locate them during the execution of the
process.
Fundamental Approaches of Implementing Non-Contiguous Memory Allocation
There are two fundamental approaches to implementing non-contiguous memory
allocation. Paging and Segmentation are the two ways that allow a process’s physical address
space to be non-contiguous. It has the advantage of reducing memory wastage but it
increases the overheads due to address translation. It slows the execution of the memory
because time is consumed in address translation.
 Paging
 Segmentation
What is Paging?
In paging, each process consists of fixed-size components called pages. The size of a page is
defined by the hardware of a computer, and the demarcation of pages is implicit in it. The
memory can accommodate an integral number of pages. It is partitioned into memory areas
that have the same size as a page, and each of these memory areas is considered separately
for allocation to a page. This way, any free memory area is exactly the same size as a page,
so external fragmentation does not arise in the system. Internal fragmentation can arise
because the last page of a process is allocated a page-size memory area even if it is smaller
than a page in size.
Why Paging is Important?
In paging, the Operating system needs to maintain the table which is called the Page Table for
each process which contains the base address of each block that is acquired by the process in
memory space. In non-contiguous memory allocation, different parts of a process are
allocated to different places in Main Memory. Spanning is allowed which is not possible in
other techniques like Dynamic or Static Contiguous memory allocation. That’s why paging is
needed to ensure effective memory allocation. Paging is done to remove External
Fragmentation.
What is Segmentation?
In segmentation, a programmer identifies components called segments in a process. A
segment is a logical entity in a program, e.g., a set of functions, data structures, or objects.
Segmentation facilitates the sharing of code, data, and program modules processes. However,
segments have different sizes, so the kernel has to use memory reuse techniques such as
first-fit or best-fit allocation. Consequently, external fragmentation can arise.

Virtual Memory:
Virtual memory is a memory management technique used by operating systems to give the
appearance of a large, continuous block of memory to applications, even if the physical
memory (RAM) is limited. It allows larger applications to run on systems with less RAM.
 The main objective of virtual memory is to support multiprogramming, The main
advantage that virtual memory provides is, a running process does not need to be
entirely in memory.
 Programs can be larger than the available physical memory. Virtual Memory
provides an abstraction of main memory, eliminating concerns about storage
limitations.
 A memory hierarchy, consisting of a computer system’s memory and a disk,
enables a process to operate with only some portions of its address space in RAM
to allow more processes to be in memory.
A virtual memory is what its name indicates- it is an illusion of a memory that is larger than
the real memory. We refer to the software component of virtual memory as a virtual memory
manager. The basis of virtual memory is the noncontiguous memory allocation model. The
virtual memory manager removes some components from memory to make room for other
components.
Demand paging:
Demand paging is a memory management scheme used in operating systems to improve
memory usage and system performance. Let’s understand demand paging with real life
example Imagine you are reading a very thick book, but you don’t want to carry the entire
book around because it’s too heavy. Instead, you decide to only bring the pages you need as
you read through the book. When you finish with one page, you can put it away and grab the
next page you need.
In a computer system, the book represents the entire program, and the pages are parts of the
program called “pages” of memory. Demand paging works similarly: instead of loading the
whole program into the computer’s memory at once (which can be very large and take up a
lot of space), the operating system only loads the necessary parts (pages) of the program
when they are needed.
This concept says that we should not load any pages into the main memory until we need
them, or keep all pages in secondary memory until we need them.
What is Demand Paging?
Demand paging is a technique used in virtual memory systems where pages enter main
memory only when requested or needed by the CPU. In demand paging, the operating system
loads only the necessary pages of a program into memory at runtime, instead of loading the
entire program into memory at the start. A page fault occurred when the program needed to
access a page that is not currently in memory.
The operating system then loads the required pages from the disk into memory and updates
the page tables accordingly. This process is transparent to the running program and it
continues to run as if the page had always been in memory.
What is Page Fault?
The term “page miss” or “page fault” refers to a situation where a referenced page is not
found in the main memory.
When a program tries to access a page, or fixed-size block of memory, that isn’t currently
loaded in physical memory (RAM), an exception known as a page fault happens. Before
enabling the program to access a page that is required, the operating system must bring it
into memory from secondary storage (such a hard drive) in order to handle a page fault.
In modern operating systems, page faults are a common component of virtual memory
management. By enabling programs to operate with more data than can fit in physical
memory at once, they enable the efficient use of physical memory. The operating system is
responsible for coordinating the transfer of data between physical memory and secondary
storage as needed.
What is Thrashing?
Thrashing is the term used to describe a state in which excessive paging activity takes place in
computer systems, especially in operating systems that use virtual memory, severely
impairing system performance. Thrashing occurs when a system’s high memory demand and
low physical memory capacity cause it to spend a large amount of time rotating pages
between main memory (RAM) and secondary storage, which is typically a hard disc.
It is caused due to insufficient physical memory, overloading and poor memory management.
The operating system may use a variety of techniques to lessen thrashing, including lowering
the number of running processes, adjusting paging parameters, and improving memory
allocation algorithms. Increasing the system’s physical memory (RAM) capacity can also
lessen thrashing by lowering the frequency of page swaps between RAM and the disc.
Pure Demand Paging
Pure demand paging is a specific implementation of demand paging. The operating system
only loads pages into memory when the program needs them. In on-demand paging only, no
pages are initially loaded into memory when the program starts, and all pages are initially
marked as being on disk.
Operating systems that use pure demand paging as a memory management strategy do so
without preloading any pages into physical memory prior to the commencement of a task.
Demand paging loads a process’s whole address space into memory one step at a time,
bringing just the parts of the process that are actively being used into memory from disc as
needed.
It is useful for executing huge programs that might not fit totally in memory or for computers
with limited physical memory. If the program accesses a lot of pages that are not in memory
right now, it could also result in a rise in page faults and possible performance overhead.
Operating systems frequently use caching techniques and improve page replacement
algorithms to lessen the negative effects of page faults on system performance as a whole.
Working Process of Demand Paging
Let us understand this with the help of an example. Suppose we want to run a process P
which have four pages P0, P1, P2, and P3. Currently, in the page table, we have pages P1 and
P3.

The operating system‘s demand paging mechanism follows a few steps in its operation.
 Program Execution: Upon launching a program, the operating system allocates a
certain amount of memory to the program and establishes a process for it.
 Creating Page Tables: To keep track of which program pages are currently in
memory and which are on disk, the operating system makes page tables for each
process.
 Handling Page Fault: When a program tries to access a page that isn’t in memory
at the moment, a page fault happens. In order to determine whether the
necessary page is on disk, the operating system pauses the application and
consults the page tables.
 Page Fetch: The operating system loads the necessary page into memory by
retrieving it from the disk if it is there.
 The page’s new location in memory is then reflected in the page table.
 Resuming The Program: The operating system picks up where it left off when the
necessary pages are loaded into memory.
 Page Replacement: If there is not enough free memory to hold all the pages a
program needs, the operating system may need to replace one or more pages
currently in memory with pages currently in memory. on the disk. The page
replacement algorithm used by the operating system determines which pages are
selected for replacement.
 Page Cleanup: When a process terminates, the operating system frees the memory
allocated to the process and cleans up the corresponding entries in the page
tables.
How Demand Paging in OS Affects System Performance?
Demand paging can improve system performance by reducing the memory needed for
programs and allowing multiple programs to run simultaneously. However, if not
implemented properly, it can cause performance issues. When a program needs a part that
isn’t in the main memory, the operating system must fetch it from the hard disk, which takes
time and pauses the program. This can cause delays, and if the system runs out of memory, it
will need to frequently swap pages in and out, increasing delays and reducing performance.
Common Algorithms Used for Demand Paging in OS
Demand paging is a memory management technique that loads parts of a program into
memory only when needed. If a program needs a page that isn’t currently in memory, the
system fetches it from the hard disk. Several algorithms manage this process:
 FIFO (First-In-First-Out): Replaces the oldest page in memory with a new one. It’s
simple but can cause issues if pages are frequently swapped in and out, leading to
thrashing.
 LRU (Least Recently Used): Replaces the page that hasn’t been used for the
longest time. It reduces thrashing more effectively than FIFO but is more complex
to implement.
 LFU (Least Frequently Used): Replaces the page used the least number of times. It
helps reduce thrashing but requires extra tracking of how often each page is used.
 MRU (Most Recently Used): Replaces the page that was most recently used. It’s
simpler than LRU but not as effective in reducing thrashing.
 Random: Randomly selects a page to replace. It’s easy to implement but
unpredictable in performance.

What is the Impact of Demand Paging in Virtual Memory Management?


With demand paging, the operating system swaps memory pages between the main memory
and secondary storage based on need. When a program needs a page not currently in
memory, the operating system retrieves it from secondary storage, a process called a page
fault.
Demand paging significantly impacts virtual memory management by allowing the operating
system to use virtual memory efficiently, improving overall system performance. Its main
advantage is reducing the physical memory required, enabling more applications to run at
once and allowing larger programs to run.
However, demand paging has some drawbacks. The page fault mechanism can delay program
execution because the operating system must retrieve pages from secondary storage. This
delay can be minimized by optimizing the page replacement algorithm.

What is Thrashing?
At any given time, only a few pages of any process are in the main memory, and therefore
more processes can be maintained in memory. Furthermore, time is saved because unused
pages are not swapped in and out of memory. However, the OS must be clever about how it
manages this scheme. In the steady state practically, all of the main memory will be occupied
with process pages, so that the processor and OS have direct access to as many processes as
possible. Thus when the OS brings one page in, it must throw another out. If it throws out a
page just before it is used, then it will just have to get that page again almost immediately.
Too much of this leads to a condition called Thrashing. The system spends most of its time
swapping pages rather than executing instructions. So a good page replacement algorithm is
required.
In the given diagram, the initial degree of multiprogramming up to some extent of
point(lambda), the CPU utilization is very high and the system resources are utilized 100%.
But if we further increase the degree of multiprogramming the CPU utilization will drastically
fall down and the system will spend more time only on the page replacement and the time
taken to complete the execution of the process will increase. This situation in the system is
called thrashing.

Causes of Thrashing
Thrashing occurs in a computer system when the CPU spends more time swapping pages in
and out of memory than executing actual processes. This happens when there is insufficient
physical memory, causing frequent page faults and excessive paging activity. Thrashing
reduces system performance and makes processes run very slowly. There are many cause of
thrashing as discussed below.
1. High Degree of Multiprogramming
If the number of processes keeps on increasing in the memory then the number of frames
allocated to each process will be decreased. So, fewer frames will be available for each
process. Due to this, a page fault will occur more frequently and more CPU time will be
wasted in just swapping in and out of pages and the utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of processes = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per
process are decreased. Hence CPU time will be consumed just by swapping pages.
2. Lacks of Frames
If a process has fewer frames then fewer pages of that process will be able to reside in
memory and hence more frequent swapping in and out will be required. This may lead to
thrashing. Hence a sufficient amount of frames must be allocated to each process in order to
prevent thrashing.
Recovery of Thrashing
 Do not allow the system to go into thrashing by instructing the long-term scheduler
not to bring the processes into memory after the threshold.
 If the system is already thrashing then instruct the mid-term scheduler to suspend
some of the processes so that we can recover the system from thrashing.

Page fault frequency:


What is Page Fault in Operating System?
When the page accessed by the CPU is not found in the main memory, the situation is referred
to as Page Fault. If the required page is not loaded into the memory, then a page fault trap
arises.
To recover from this, the required page has to be fetched from the secondary memory(hard
disk) into the main memory.
The page fault primarily causes an exception, which informs the operating system that it will
need to retrieve "pages" from virtual memory in order to continue the execution. The software
resumes normal operation once all of the data has been placed into physical memory. The Page
Fault procedure happens in the background, so the user isn't aware of it. The computer's
hardware is connected to the kernel, and the program counter (PC) is usually saved on the
stack. The information about the current status of instruction is stored in CPU registers. An
assembly program is begun, which preserves the general registers as well as other volatile data
to prevent the OS from destroying it.
When a page fault occurs in an operating system, it means that the requested memory page
isn't currently in physical RAM but rather resides in secondary storage, like a hard drive or SSD.
Here are the actions taken by the OS:
 Page Fault Trap: The CPU detects a page fault while trying to access a memory page
that isn't currently in RAM. This triggers a page fault exception, which transfers
control to the operating system.
 Page Table Lookup: The operating system looks up the page table to determine the
location of the required page in secondary storage, such as a disk.
 Fetch Page from Secondary Storage: The OS initiates a process to fetch the
required page from secondary storage into an available page frame in RAM. This
involves reading the page data from disk into a free page frame.
 Update Page Table: Once the page is successfully brought into RAM, the operating
system updates the page table to reflect the new location of the page in physical
memory. This includes updating the page table entry with the physical address of
the page frame where the page now resides.
 Resume Process Execution: Finally, the operating system restarts the interrupted
memory access instruction that caused the page fault. With the required page now
available in RAM, the process can proceed as expected, accessing the data it needs
from memory.
Page Fault Handling
The below image explains how Page Fault Handling will be done by the operating system.

A Page Fault occurs when you access a page that has been flagged as invalid. Then, while
translating the address across the page table, the paging hardware will notice that the incorrect
bit is set, triggering a trap in the operating system.
This trap is mostly caused by the Operating System's failure to put the desired page into
memory.
Let's look at the technique for dealing with a page fault, as depicted in the diagram above:
 To begin, an internal table (typically the process control block) was created for this
process to identify whether the reference was valid or invalid memory access.
 If the reference is invalid, the process will be terminated. If the reference is correct,
but we haven't yet purchased that page, we'll just page it in.
 Then, in order to find the free frame, we locate the free frame list.
 In order to read the required page into the newly allocated frame, a disc operation
is now scheduled.
 When the disc is entirely read, the internal table that is kept with the process is
amended, and the page table, which primarily shows that the page is now in
memory, is updated.
 We'll now restart the instruction that was interrupted due to the trap. The page can
now be accessed as if it had always been in memory by the process.

Types of page faults


There are a total of 3 types of page faults as follows.
Minor Page Fault
A small page fault or soft page fault happens when a page is loaded into memory when a fault
occurs, but the page is not registered as being loaded into memory in the memory
management unit.
Major Page Fault
The operating system uses a significant page fault to boost available program memory on
demand. Until the program tries to use it and creates a page fault, the operating system will
postpone loading some program parts from the disc.
Invalid Page Fault
An invalid page fault happens when a page fault references an address that does not belong to
the virtual address space, implying no equivalent page in memory.

Page Fault Terminology


 Page Hit: It occurs when a program references a memory page that is already
present in the main physical memory (RAM). This leads to quick retrieval of the
required data without the need for additional disk access

 Page Miss: It happens when a program accesses a memory page that is not
currently residing in the main memory. This triggers a page fault and requires the
OS to bring the missing page into RAM from secondary storage

 Page Fault Time: It refers to the total time taken to handle a page fault. It includes
the time to find the required page on disk, swap it into RAM, update data
structures, and restart the interrupted program

 Page Fault Delay: This term denotes the time interval between the moment a page
fault occurs and the time when the corresponding page is fully loaded into the main
memory, allowing the program to continue its execution

 Hard Page Fault: It takes place when the missing page is not present in any
available storage, including both RAM and secondary storage. This situation often
requires complex memory management techniques, such as swapping pages to disk

 Soft Page Fault: It occurs when the required page is found elsewhere in memory or
storage but hasn't been assigned to the correct address space of the requesting
process. It's generally easier to resolve than a hard page fault
 Minor Page Fault: It signifies that the page being accessed is already in memory,
but it is marked as not yet loaded. It typically happens when a program first
accesses a memory page, causing the OS to load it into the main memory for future
use.

You might also like