0% found this document useful (0 votes)
35 views63 pages

OS Notes

Uploaded by

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

OS Notes

Uploaded by

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

Comprehensive Operating Systems Notes

Adhishthatri Singh

August 29, 2025


OS Notes by Adhishthatri Singh August 29, 2025

Contents
1 An Introduction to Operating Systems 5
1.1 Software Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 What is an Operating System? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Why is an OS Necessary? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 What if there were no OS? . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.2 What is an OS made of? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Key Functions of an Operating System . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Layered View of a Computer System . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 Types of Operating Systems 7


2.1 Scheduling Goals of Modern Operating Systems . . . . . . . . . . . . . . . . . . . 7
2.2 1. Single-Process OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 2. Batch Processing OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 3. Multiprogramming OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 4. Multitasking (Time-Sharing) OS . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 5. Multi-processing OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7 6. Distributed OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 7. Real-Time OS (RTOS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Multitasking and Multithreading 10


3.1 Fundamental Concepts: Program, Process, and Thread . . . . . . . . . . . . . . . 10
3.2 Comparison: Multitasking vs. Multithreading . . . . . . . . . . . . . . . . . . . . 10
3.3 Context Switching: Process vs. Thread . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Thread Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

4 Components of the Operating System 12


4.1 Core Components: Kernel and User Space . . . . . . . . . . . . . . . . . . . . . . 12
4.2 Communication Between User Mode and Kernel Mode . . . . . . . . . . . . . . . 12
4.3 Functions of the Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.4 Types of Kernel Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

5 System Calls and Inter-Process Communication 15


5.1 Communication Between User Mode and Kernel Mode . . . . . . . . . . . . . . . 15
5.2 Inter-Process Communication (IPC) . . . . . . . . . . . . . . . . . . . . . . . . . 15

6 System Calls 16
6.1 What are System Calls? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.2 The System Call Transition (User to Kernel) . . . . . . . . . . . . . . . . . . . . 16
6.3 Types of System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.4 Examples: Windows vs. Unix System Calls . . . . . . . . . . . . . . . . . . . . . 17

7 What Happens When You Turn On Your Computer? 18


7.1 The Boot Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

8 32-Bit vs 64-Bit OS 20
8.1 Core Architectural Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8.2 Advantages of 64-Bit over 32-Bit OS . . . . . . . . . . . . . . . . . . . . . . . . . 20

9 Storage Devices Basics 21


9.1 The Memory Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
9.2 Levels of the Hierarchy Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2
OS Notes by Adhishthatri Singh August 29, 2025

10 Comparison of Memory Types 23

11 Introduction to Process 24
11.1 Program vs. Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
11.2 How the OS Creates a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
11.3 Architecture of a Process (Memory Layout) . . . . . . . . . . . . . . . . . . . . . 24
11.4 Attributes of a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
11.5 The Process Control Block (PCB) Structure . . . . . . . . . . . . . . . . . . . . . 25

12 Process States & Process Queues 26


12.1 Process States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
12.2 Process Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
12.3 Schedulers and System Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

LEC-11: Swapping | Context-Switching | Orphan process | Zombie process 28

13 Introduction to Process Scheduling 29


13.1 Core Concepts of Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
13.2 Goals and Metrics of CPU Scheduling . . . . . . . . . . . . . . . . . . . . . . . . 29
13.3 First-Come, First-Serve (FCFS) Scheduling . . . . . . . . . . . . . . . . . . . . . 30

14 CPU Scheduling Algorithms: SJF, Priority, & Round Robin 31


14.1 Shortest Job First (SJF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
14.1.1 SJF (Non-Preemptive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
14.1.2 SJF (Preemptive) - Also known as Shortest Remaining Time First (SRTF) 31
14.2 Priority Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
14.2.1 Priority Scheduling (Non-Preemptive) . . . . . . . . . . . . . . . . . . . . 31
14.2.2 Priority Scheduling (Preemptive) . . . . . . . . . . . . . . . . . . . . . . . 32
14.3 Round Robin (RR) Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

15 Multi-level Queue Scheduling: MLQ & MLFQ 33


15.1 Multi-level Queue Scheduling (MLQ) . . . . . . . . . . . . . . . . . . . . . . . . . 33
15.2 Multi-level Feedback Queue Scheduling (MLFQ) . . . . . . . . . . . . . . . . . . 33
15.3 Comparison of Scheduling Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 34

16 Introduction to Concurrency 35

17 The Critical Section Problem and How to Address It 36

18 Conditional Variable and Semaphores for Threads Synchronization 37

19 The Producer-Consumer Problem 38


19.1 Problem Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
19.2 Requirements for a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
19.3 Solution using Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

20 The Readers-Writers Problem 41


20.1 Problem Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
20.2 Problem Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
20.3 Solution for Readers’ Priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3
OS Notes by Adhishthatri Singh August 29, 2025

21 The Dining Philosophers Problem 44


21.1 The Problem Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
21.2 The Deadlock Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
21.3 Solutions to Avoid Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
21.4 Code Solution (Atomic Fork Pickup) . . . . . . . . . . . . . . . . . . . . . . . . . 44

22 Introduction to Deadlock 47
22.1 What is a Deadlock? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
22.2 Deadlock Necessary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
22.3 Methods for Handling Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

23 Memory Management Techniques | Contiguous Memory Allocation 50


23.1 Memory Mapping and Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
23.2 Contiguous Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
23.3 Limitations of Fixed Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
23.3.1 Dynamic Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

24 Free Space Management 54


24.1 Defragmentation / Compaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
24.2 Managing Free Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
24.3 Algorithms for Allocating Free Holes . . . . . . . . . . . . . . . . . . . . . . . . . 54

25 Paging: Non-Contiguous Memory Allocation 55


25.1 The Idea Behind Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
25.2 Paging Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
25.3 Performance Issues with Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
25.4 Translation Look-aside Buffer (TLB) . . . . . . . . . . . . . . . . . . . . . . . . . 56

26 Segmentation: Non-Contiguous Memory Allocation 57


26.1 Segmentation vs. Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
26.2 Segmentation Hardware and Address Translation . . . . . . . . . . . . . . . . . . 57
26.3 Advantages and Disadvantages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

27 Virtual Memory, Demand Paging, & Page Faults 59


27.1 What is Virtual Memory? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
27.2 Demand Paging: The Lazy Swapper . . . . . . . . . . . . . . . . . . . . . . . . . 59
27.3 The Page Fault: Handling Missing Pages . . . . . . . . . . . . . . . . . . . . . . . 60
27.4 Advantages and Disadvantages of Virtual Memory . . . . . . . . . . . . . . . . . 60

28 Page Replacement Algorithms 61


28.1 Why Page Replacement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
28.2 Algorithm Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

29 Thrashing 63
29.1 Techniques to Handle Thrashing . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

4
OS Notes by Adhishthatri Singh August 29, 2025

1 An Introduction to Operating Systems


1.1 Software Categories
Computer software can be broadly divided into two main categories:

Application Software Performs specific tasks directly for the end-user. Its primary purpose
is to accomplish a user-centric goal.

• Examples: Web browsers (Chrome), word processors (MS Word), video games,
database software.

System Software Operates and controls the computer’s hardware and provides a foundational
platform for application software to run. The user does not typically interact with system
software directly.

• The Operating System (OS) is the most important example of system software.
Other examples include device drivers, compilers, and utility software.

1.2 What is an Operating System?


An Operating System (OS) is a crucial piece of system software that manages all the hard-
ware and software resources of a computer system. It acts as an intermediary between the
user/application and the computer hardware.
The primary goals of an OS are to:

1. Provide an environment for users to execute programs in a convenient and efficient


manner.

2. Act as a resource manager, allocating and deallocating resources fairly and efficiently
among competing programs.

1.3 Why is an OS Necessary?


The importance of an OS becomes clear when we consider a system without one.

1.3.1 What if there were no OS?


1. Bulky and Complex Applications: Every application developer would need to write
low-level code to interact directly with hardware (e.g., keyboard, disk drives, screen). This
hardware interaction code would need to be included in every single application, making
them enormous and incredibly complex to develop and maintain.

2. Uncontrolled Resource Exploitation: A single application could monopolize a re-


source, like the CPU, preventing any other program from running. A buggy or malicious
program could halt the entire system.

3. No Memory Protection: Without an OS, every program would have access to all of
physical memory. One application could easily read, corrupt, or completely overwrite
the memory space of another application (or even the non-existent OS itself), leading to
constant crashes and major security vulnerabilities.

1.3.2 What is an OS made of?


At its core, an OS is a collection of system software, including the kernel, device drivers, system
utilities, and a shell (user interface), all working together to manage the system.

5
OS Notes by Adhishthatri Singh August 29, 2025

1.4 Key Functions of an Operating System


The OS performs two fundamental roles: acting as an Extended Machine (Abstraction) and as
a Resource Manager (Arbitration).

• Abstraction (Hiding Complexity): The OS hides the messy, low-level details of the
hardware from application programs and users. It presents a clean, simple, and consistent
interface. For example, you save a ’file’ rather than managing individual disk sectors. This
is the OS acting as an extended machine.

• Resource Management (Arbitration): The OS manages and allocates all system re-
sources. This involves deciding which program gets to use the CPU, how memory is shared,
how file access is controlled, and managing access to I/O devices. This is a process of ar-
bitration—settling disputes between competing processes for resources. Key resources
managed include:

– Memory Management
– Process Management & Scheduling
– File System Management
– Device Management
– Security and Protection

• Provides an Interface: It provides an interface (Command Line Interface - CLI, or


Graphical User Interface - GUI) for the user to interact with the computer.

• Facilitates Execution: It creates a protected environment that facilitates the execu-


tion of application programs by providing services, isolation, and protection from one
another.

1.5 Layered View of a Computer System


A computer system can be viewed as a set of layers, with each layer using the services of the layer
directly below it. The OS sits in a crucial position between the hardware and the applications.

User
Application Programs
(e.g., Compiler, Browser, Games)
Operating System
Computer Hardware
(CPU, Memory, I/O Devices)

The operating system provides the means for the proper and efficient use of the resources in
the operation of the computer system.

6
OS Notes by Adhishthatri Singh August 29, 2025

2 Types of Operating Systems


2.1 Scheduling Goals of Modern Operating Systems
Before classifying operating systems, it’s important to understand their core performance goals,
which influence their design:

• Maximize CPU Utilization: Keep the CPU as busy as possible, minimizing idle time.

• Minimize Process Starvation: Ensure that every process gets a fair share of the CPU
over time and no process is indefinitely postponed (starved) for resources.

• Prioritize Job Execution: Allow higher-priority jobs to be executed before lower-


priority ones.

• Maximize Throughput: Complete the maximum number of jobs per unit of time.

• Minimize Turnaround Time: Minimize the time elapsed from a job’s submission to its
completion.

• Provide Responsiveness: Minimize the response time for interactive users.

2.2 1. Single-Process OS
This is the simplest and oldest type of operating system. It allows only one user process to run
at a time. If another process needs to be executed, the first one must complete its execution.

• Characteristics: Only one process from the ready queue is loaded into memory and
executed.

• Limitations: Leads to very poor CPU utilization, as the CPU sits idle whenever the
single process performs an I/O operation.

• Example: MS-DOS (Microsoft Disk Operating System) from 1981.

2.3 2. Batch Processing OS


In a batch system, jobs with similar needs are grouped together into batches. The computer
executes these batches sequentially, one after another, without any direct user interaction.

Workflow of a Batch System


1. Users prepare their jobs offline on a device like a punch card.

2. Users submit their jobs to a central computer operator.

3. The operator collects jobs and sorts them into batches based on similar requirements (e.g.,
all FORTRAN compilation jobs in one batch).

4. The operator submits the batches to the processor one by one.

5. All jobs within one batch are executed together. The output for each job is sent back to
the respective user.

7
OS Notes by Adhishthatri Singh August 29, 2025

Advantages and Disadvantages


• Advantage: Simple for managing a large number of similar, non-interactive jobs.

• Disadvantage - No Interactivity: Users cannot interact with their jobs during execu-
tion.

• Disadvantage - No Prioritization: Priorities cannot be easily set. A high-priority job


might have to wait for a long, low-priority batch to finish.

• Disadvantage - CPU Idle Time: The CPU often becomes idle during I/O operations,
as there is no other job in memory to switch to.

• Disadvantage - Starvation: A job might have to wait for a very long time if it is part
of a batch that is repeatedly postponed.

Example: ATLAS at Manchester University (late 1950s).

2.4 3. Multiprogramming OS
Multiprogramming was developed to address the problem of CPU idle time. It increases CPU
utilization by organizing jobs so that the CPU always has one to execute.

• Core Idea: Keep multiple jobs (code and data) in main memory at the same time.

• Execution: When the currently running process needs to perform an I/O operation (and
would otherwise make the CPU idle), the OS performs a context switch and gives the
CPU to another ready job in memory.

• Hardware: Works on a system with a single CPU.

• Outcome: Significantly reduces CPU idle time and increases throughput.

Example: THE system by Dijkstra (early 1960s).

2.5 4. Multitasking (Time-Sharing) OS


Multitasking is a logical extension of multiprogramming that allows for user interaction. It
switches between jobs so frequently that users can interact with each program while it is running.

• Core Idea: Share CPU time among multiple processes using a concept called time-
slicing. Each process gets a small quantum of CPU time, and the OS rapidly switches
between them.

• Concurrency: This rapid switching creates the illusion that multiple tasks are running
simultaneously (concurrently) on a single CPU.

• Responsiveness: This approach dramatically increases system responsiveness, making


it suitable for interactive applications.

• Hardware: Works on a system with a single CPU.

Example: CTSS (Compatible Time-Sharing System) at MIT (early 1960s).

8
OS Notes by Adhishthatri Singh August 29, 2025

2.6 5. Multi-processing OS
A multi-processing system contains more than one CPU in a single computer, with all CPUs
sharing the same memory and peripherals. This is also known as a parallel or tightly-coupled
system.

• Parallelism: This is the only system type that provides true parallelism, where multiple
processes can be executed genuinely simultaneously on different CPUs.

• Increased Throughput: By executing multiple processes at once, the system gets more
work done in less time.

• Increased Reliability (Fault Tolerance): If one CPU fails, the other CPUs can pick
up the workload, allowing the system to continue functioning, albeit at a slower pace. This
is called graceful degradation.

• Lesser Process Starvation: With multiple CPUs, there are more resources available,
reducing the chance that a ready process has to wait for a CPU.

Example: Modern operating systems like Windows, macOS, Linux, and Android are all multi-
processing systems.

2.7 6. Distributed OS
A distributed system is a collection of independent, networked, communicating, and physically
separate computer nodes that work together to act as a single, coherent system.

• Architecture: It is a loosely-coupled system where each node has its own CPU, mem-
ory, and resources. The OS manages the resources across the entire network.

• Transparency: The key goal is to hide the fact that resources are physically distributed
across multiple nodes. To the user, it appears as a single, powerful computer.

• Advantages: High scalability, fault tolerance (if one node fails, the system continues to
operate), and resource sharing across the network.

Example: LOCUS, Amoeba.

2.8 7. Real-Time OS (RTOS)


An RTOS is a system where the correctness of a computation depends not only on the logical
result but also on the time at which the result is produced.

• Core Idea: Computations must be completed within tight time boundaries (dead-
lines).

• Predictability: These systems are designed to be predictable and deterministic, not


necessarily fast.

• Types of RTOS:

– Hard Real-Time: Missing a deadline is a catastrophic system failure. Examples:


Air traffic control systems, anti-lock brakes, medical pacemakers.
– Soft Real-Time: Missing a deadline degrades performance but does not cause sys-
tem failure. A late result is still useful. Examples: Live video streaming, robotics,
scientific data acquisition.

9
OS Notes by Adhishthatri Singh August 29, 2025

3 Multitasking and Multithreading


3.1 Fundamental Concepts: Program, Process, and Thread
To understand multitasking and multithreading, we must first define the core components they
manage.

Program A program is a passive entity. It is an executable file containing a set of instructions


(compiled code) stored on a disk, ready to be executed.

Process A process is a program in execution. It is an active entity that resides in the


computer’s primary memory (RAM). A process is more than just the program code; it is
a complete execution environment that includes:

• The code segment (the program text).


• The current activity, represented by the value of the Program Counter and pro-
cessor registers.
• The process stack, which contains temporary data like function parameters, return
addresses, and local variables.
• A data section, which contains global variables.
• The heap, which is memory that is dynamically allocated during runtime.

Thread A thread is the basic unit of CPU utilization. It is often described as a light-weight
process because it is an independent path of execution within a single process. A process
can have multiple threads running concurrently.

• Shared Resources: All threads of a process share the process’s code section, data
section, and other OS resources like open files.
• Independent Resources: Each thread has its own program counter, a register
set, and a stack.
• Purpose: Threads are used to achieve parallelism and responsiveness by dividing a
process’s tasks into smaller, independent sub-tasks.
• Example: In a web browser, one thread can handle rendering the page (UI), another
can download an image, and a third can play a video. In a text editor, one thread
processes user input, while others handle spell-checking, auto-saving, and formatting
concurrently.

3.2 Comparison: Multitasking vs. Multithreading


While related, these two concepts achieve concurrency at different levels.

3.3 Context Switching: Process vs. Thread


A context switch is the process of storing the state of a process or thread so that it can be
restored and resume execution at a later point. The overhead differs significantly.

10
OS Notes by Adhishthatri Singh August 29, 2025

Table 1: Key Differences between Multitasking and Multithreading


Multitasking Multithreading
The concurrent execution of more than one The concurrent execution of more than one
process is called multitasking. The CPU thread within the same process is called
rapidly switches between processes to give multithreading. It divides a single process
the illusion of simultaneous execution. into multiple sub-tasks.
The OS performs context switching be- The OS (or application runtime) per-
tween independent processes. forms context switching between related
threads.
Primarily designed for single-CPU systems Can run on a single CPU to provide respon-
to provide concurrency and responsiveness. siveness, but its true power is unlocked on
It can also run on multi-CPU systems. multi-CPU systems where threads can run
in true parallel.
Strong Isolation exists. The OS pro- No Isolation between threads. All
vides memory protection by allocating sep- threads of a process share the same memory
arate memory address spaces and resources and resources. This allows for easy commu-
to each process. One process cannot di- nication but means one faulty thread can
rectly access another’s memory. crash the entire process.

Table 2: Difference between Thread and Process Context Switching


Thread Context Switching Process Context Switching
The OS saves the current state of a thread The OS saves the entire state of a process
(registers, program counter, stack pointer) (its Process Control Block) and switches to
and switches to another thread within the another process, restoring its state.
same process.
Does not involve switching the memory Includes switching the memory address
address space. The memory map remains space. The system must load the memory
the same since the threads belong to the map (e.g., page tables) of the new process.
same process.
Fast Switching. The overhead is very Slow Switching. The overhead is high
low because only the thread-specific con- due to the need to save the full PCB and
text needs to be saved/restored. manage memory space changes.
CPU Cache state is preserved. Since CPU Cache state is flushed. Because
threads share the same data and address the new process has a different address
space, the data in the CPU’s cache (includ- space, most of the cached data is now in-
ing the TLB) often remains valid and can valid for the new process and must be
be reused, leading to better performance. cleared, leading to performance degrada-
tion (cache misses).

3.4 Thread Scheduling


The operating system is responsible for scheduling threads for execution.

• Threads are scheduled for execution based on factors like their priority and the scheduling
algorithm being used.

• Even though threads execute within a process, the OS scheduler assigns processor time
slices to individual threads, not just the parent process. This allows for fine-grained
control over the execution flow within an application.

11
OS Notes by Adhishthatri Singh August 29, 2025

4 Components of the Operating System


An operating system is not a single entity but a collection of components that work together.
The most fundamental division is between the Kernel and the User Space.

4.1 Core Components: Kernel and User Space


The Kernel The kernel is the central, core component of the OS. It can be considered the heart
of the OS, as it interacts directly with the hardware and performs the most crucial system
tasks.

• It is the very first part of the operating system to be loaded into memory on start-up.
• It runs in a privileged mode (called kernel mode), where it has unrestricted access
to all hardware and system resources.

User Space This is the region of memory where application software and user processes run.

• Applications in user space run in a non-privileged mode (called user mode).


• They do not have direct access to hardware and must request services from the kernel
to perform privileged operations (like I/O or memory allocation).

The Shell A shell, also known as a command interpreter, is a special user program that
provides an interface for users to interact with the OS. It receives commands from the user
(via keyboard or script) and gets the kernel to execute them.

• CLI (Command-Line Interface): A text-based shell where users type commands


(e.g., Bash on Linux, Command Prompt on Windows).
• GUI (Graphical User Interface): A visual, icon-based interface where users in-
teract using a mouse and windows (e.g., Windows Desktop, GNOME on Linux).

4.2 Communication Between User Mode and Kernel Mode


A process in user mode needs a way to request services from the kernel. This is done through
System Calls. When a system call is made, the hardware switches from user mode to kernel
mode, allowing the OS to execute the privileged task.
The question of communication can also refer to Inter-Process Communication (IPC),
which is a mechanism provided by the kernel to allow different processes to communicate with
each other.

• Problem: Processes have independent, protected memory spaces and cannot access each
other’s data directly. However, they often need to cooperate and share data.

• Solution (IPC): The kernel facilitates this communication using two primary methods:

1. Shared Memory: A region of memory is designated as "shared" and is mapped into


the address space of multiple processes. This is very fast as no data is copied.
2. Message Passing: Processes communicate by sending and receiving messages through
the kernel. This is slower but easier to implement and provides better synchroniza-
tion.

12
OS Notes by Adhishthatri Singh August 29, 2025

4.3 Functions of the Kernel


The kernel is responsible for managing all major system activities.

1. Process Management:

• Scheduling processes and threads on the CPUs.


• Creating and deleting both user and system processes.
• Suspending and resuming processes.
• Providing mechanisms for process synchronization and communication (IPC).

2. Memory Management:

• Allocating and deallocating memory space to processes as needed.


• Keeping track of which parts of memory are currently being used and by which
process.

3. File Management:

• Creating and deleting files and directories.


• Mapping files onto secondary storage (like a hard drive).
• Providing backup support onto stable (non-volatile) storage media.

4. I/O Management: Manages and controls all I/O operations and devices. This includes
using techniques like:

• Spooling (Simultaneous Peripheral Operations On-Line): Putting jobs in a


buffer (a special area in memory or on disk) so that a device can access them when
it is ready. Used to manage jobs with differing speeds. Example: A print spooler
collects all documents to be printed and sends them to the printer one by one.
• Buffering: Storing data temporarily while it is being transferred between two devices
or between a device and an application. Used to handle speed mismatches within one
job. Example: YouTube video buffering loads a portion of the video into a buffer
before playing it to ensure smooth playback.
• Caching: Storing parts of data from a slow device (like a hard disk) into a faster
one (like RAM) to speed up subsequent accesses to that same data. Example: Web
caching, memory caching.

4.4 Types of Kernel Architectures


The way kernel functions are structured defines the kernel’s architecture.

1. Monolithic Kernel
All OS services (process management, memory management, file systems, device drivers) run in
kernel space, as a single, large block of code.

+ High Performance: Communication between components is very fast (simple function


calls) as there are no overheads from switching between user and kernel mode.

- Bulky and Large: The kernel executable is very large.

- Less Reliable: A failure in one module (e.g., a buggy device driver) can crash the entire
system.

13
OS Notes by Adhishthatri Singh August 29, 2025

2. Microkernel

Only the most fundamental functions (memory management, process scheduling, IPC) are
in the kernel. Other services (file systems, device drivers) are run as user-space processes.

+ More Stable & Secure: If a user-space service like a device driver fails, it does not
crash the entire kernel. It can often be restarted independently.
+ Smaller in Size: The kernel itself is minimal.
- Slower Performance: Services must communicate via IPC, which requires switch-
ing between user and kernel mode, creating significant overhead.

3. Hybrid Kernel

This is a combined approach that takes the advantages of both worlds. It keeps more
services in kernel space than a microkernel (for performance) but structures them in
a modular, client-server way, like a microkernel (for stability).
+ Good Performance: Faster than a microkernel.
+ Better Stability: More stable and modular than a monolithic kernel.
- Complex Design: Inherits complexities from both designs.

4. Other Architectures
· Nanokernel / Exokernel: These are even more minimal than microker-
nels, delegating almost all services and hardware management directly to
applications for maximum flexibility, often seen in research systems.

14
OS Notes by Adhishthatri Singh August 29, 2025

5 System Calls and Inter-Process Communication


5.1 Communication Between User Mode and Kernel Mode
For a secure and stable system, processes run in a restricted user mode, while
the OS kernel runs in a privileged kernel mode with direct access to hardware.
The primary mechanism for a user process to request a service from the kernel
is a System Call.

The System Call Mechanism


1. Request: A user application needs to perform a privileged action (e.g., read
a file, create a new process). It cannot do this directly.
2. Trap: The application invokes a system call, which triggers a software inter-
rupt or trap.
3. Mode Switch: This trap causes the CPU to switch from user mode to kernel
mode.
4. Kernel Execution: The kernel’s system call handler executes, performs the
requested service (e.g., accessing the disk), and prepares the result.
5. Return & Mode Switch: The kernel finishes, switches the CPU back to
user mode, and returns control (and any data) to the calling application.

5.2 Inter-Process Communication (IPC)


The information from your note, Inter-Process Communication (IPC), is a
crucial service managed by the kernel, but its primary purpose is to allow different
user-space processes to communicate with each other.
· The Need: The OS enforces memory protection, giving each process its
own independent memory space. While essential for stability, this prevents
processes from easily sharing information.
· The Solution: The kernel provides IPC mechanisms to bridge this gap in
a controlled manner. The two main types are:
Shared Memory The kernel allocates a region of memory and maps it into
the address space of multiple processes. This allows these processes to read
and write to the same memory location.
+ Very Fast: No data is copied by the kernel; processes access the memory
directly.
- Synchronization: Processes must coordinate to avoid overwriting each
other’s data (e.g., using semaphores).
Message Passing Processes communicate by sending formatted messages
to each other through the kernel. One process sends a message, which the
kernel temporarily stores and then delivers to the destination process.
+ Simpler & Safer: The kernel handles synchronization, making it easier
to implement without data corruption.
- Slower: Involves at least two data copies (process-to-kernel and kernel-to-
process), which adds overhead.

15
OS Notes by Adhishthatri Singh August 29, 2025

6 System Calls
6.1 What are System Calls?
A system call is the primary mechanism through which a user program can
request a service from the kernel. Since applications run in a restricted user
mode, they lack the permission to perform privileged operations like accessing
I/O devices or managing memory directly. System calls provide a secure and
controlled interface to these kernel services.
· The Gateway to the Kernel: System calls are the only way for a user-
space process to transition into the privileged kernel mode.
· Implementation: They are typically implemented as functions in C and
C++.
· Wrapper Functions: User applications don’t usually invoke system calls
directly. Instead, they use standard library functions (APIs) that act as
wrappers. These wrappers set up the necessary parameters and then trigger
the actual system call.

Example: The mkdir command


When you run a command like mkdir new_folder in the terminal:
1. The mkdir program (running in user space) calls a library function to create
a directory.
2. This library function is a wrapper that invokes the actual kernel system
call for creating a directory (e.g., mkdir() in Unix).
3. The kernel’s file management module receives the request, creates the di-
rectory, and returns the result.

6.2 The System Call Transition (User to Kernel)


The transition from user space (US) to kernel space (KS) is managed by the
hardware using a software interrupt or trap.
Step 1 A process executing in User Space needs a kernel service.
Step 2 It invokes a system call (e.g., the exec() call to run a new program).
Step 3 A software interrupt is triggered, causing the CPU to switch to Kernel
Space.
Step 4 The kernel executes the requested service.
Step 5 The kernel finishes and returns control to the process, switching the CPU
back to User Space.

6.3 Types of System Calls


System calls can be grouped into several major categories based on their function:
1. Process Control
· Create or terminate a process (create process, terminate process).
· Load and execute a program (load, execute).
· Get or set process attributes.
· Wait for events or time (wait for time, wait event, signal event).
· End or abort execution (end, abort).

16
OS Notes by Adhishthatri Singh August 29, 2025

· Allocate and free memory.


2. File Management
· Create, open, close, and delete files.
· Read from, write to, or reposition within a file.
· Get or set file attributes (e.g., permissions).
3. Device Management
· Request and release devices.
· Read from, write to, or reposition a device.
· Get or set device attributes.
· Logically attach or detach devices.
4. Information Maintenance
· Get or set the time and date.
· Get or set system data.
· Get attributes of processes, files, or devices.
5. Communication Management
· Create and delete communication connections (e.g., network sockets).
· Send and receive messages.
· Transfer status information.
· Attach or detach remote devices.

6.4 Examples: Windows vs. Unix System Calls


Different operating systems provide similar functionality but often with different
names for their system calls.

Table 3: Comparison of Windows and Unix System Call APIs


Category Windows Unix / Linux
Process Control CreateProcess() fork()
ExitProcess() exit()
WaitForSingleObject() wait()
File Management CreateFile() open()
ReadFile() read()
WriteFile() write()
CloseHandle() close()
Device Management SetConsoleMode() ioctl()
ReadConsole() read()
WriteConsole() write()
Information GetCurrentProcessId() getpid()
Management SetTimer() alarm()
Sleep() sleep()
Communication CreatePipe() pipe()
CreateFileMapping() shmget() (shared memory)
MapViewOfFile() mmap() (memory mapping)

17
OS Notes by Adhishthatri Singh August 29, 2025

7 What Happens When You Turn On Your Com-


puter?
The process of starting a computer, from the moment power is supplied until
the operating system is ready to use, is known as booting or bootstrapping.
It involves a precise sequence of steps managed by the system’s firmware and a
special program called a bootloader.

7.1 The Boot Sequence


1. Power On & CPU Initialization
· When the power button is pressed, the Power Supply Unit (PSU) sends
a signal to the motherboard and the CPU.
· The CPU initializes itself and immediately looks for firmware to execute.
2. Firmware Loading (BIOS/UEFI)
· The CPU loads the system’s firmware, which is stored on a ROM chip
on the motherboard.
· BIOS (Basic Input-Output System): The traditional firmware that
allows for basic system configuration and hardware initialization.
· UEFI (Unified Extensible Firmware Interface): The modern re-
placement for BIOS. It is more flexible, has better security features, and
is essentially a tiny, powerful operating system in itself.
3. Power-On Self-Test (POST)
· The firmware (BIOS or UEFI) runs the POST, which tests and initial-
izes all critical system hardware components (e.g., CPU, RAM, storage
devices).
· If any component is missing or fails the test (like no RAM detected),
an error is thrown, and the boot process is halted.
4. Locating the Bootloader
· After a successful POST, the firmware’s next job is to find the operating
system’s bootloader. It examines storage devices (like your SSD or hard
drive) according to a pre-configured boot order.
· Legacy BIOS Method: The BIOS looks for a Master Boot Record
(MBR) at the very beginning of the storage disk. The MBR is a special
boot sector containing the executable code of the bootloader.
· Modern UEFI Method: UEFI looks for a specific EFI System
Partition on the disk, which contains the bootloader file (e.g., a .efi
file).
· The firmware executes this bootloader code and hands off all responsi-
bility for the rest of the boot process.
5. Operating System Loading
· The bootloader is a program with the critical task of loading the rest
of the operating system.
· It loads the OS kernel from the disk into RAM.
· Once the kernel is loaded, the bootloader passes control to it. The
kernel then proceeds to load system daemons, drivers, and the user-
space environment, completing the boot process.
· Common Bootloaders: Windows uses the Windows Boot Man-
ager (bootmgr.exe), most Linux systems use GRUB (GRand Unified
Bootloader), and macOS uses a bootloader called boot.efi.

18
OS Notes by Adhishthatri Singh August 29, 2025

A Note on Modern Firmware


UEFI firmware is far more advanced than just initializing hardware. For example,
many Intel CPUs include the Intel Management Engine, a tiny, independent
computer system built into the processor. It provides powerful features for remote
management of business PCs, a capability that is initialized and managed at the
firmware level.

19
OS Notes by Adhishthatri Singh August 29, 2025

8 32-Bit vs 64-Bit OS
The terms 32-bit and 64-bit refer to the CPU’s architecture, specifically the width
of its registers and the amount of data it can process in a single instruction
cycle. This fundamentally impacts the operating system’s capabilities, especially
regarding memory addressing.

8.1 Core Architectural Differences


32-Bit Architecture · Has 32-bit registers and can process 32 bits of data
at once.
· Can access a maximum of 232 unique memory addresses.
· This translates to a physical memory (RAM) limit of 4 Gigabytes
(GB).
64-Bit Architecture · Has 64-bit registers and can process 64 bits of data
at once.
· Can access a theoretical maximum of 264 unique memory addresses.
· This translates to a massive 16 Exabytes (EB) of physical memory
(17, 179, 869, 184 GB), though practical limits in current OS are lower.

8.2 Advantages of 64-Bit over 32-Bit OS


1. Vastly Increased Addressable Memory
· This is the most significant advantage. While a 32-bit CPU is limited to
addressing 4 GB of RAM, a 64-bit CPU can address a nearly limitless
amount.
· A 32-bit OS can only utilize a maximum of 4 GB of RAM. Installing
more RAM provides no performance benefit.
· A 64-bit OS is required to effectively use more than 4 GB of RAM.
2. Improved Performance
· All calculations take place in the CPU’s registers. Having larger, 64-bit
registers allows the processor to perform larger calculations at the same
time.
· In a single instruction cycle, a 32-bit processor can execute on 4 bytes
of data, whereas a 64-bit processor can execute on 8 bytes of data.
This doubles the data throughput for certain operations, leading to
significant performance gains in demanding applications.
3. Backward Compatibility
· A 64-bit CPU running a 64-bit OS can execute both 64-bit and 32-bit
applications.
· A 32-bit CPU can only run a 32-bit OS and 32-bit applications.
4. Better Graphics Performance
· The ability to handle 8-byte calculations in a single step makes graphics-
intensive applications (like video editing, 3D rendering, and high-end
gaming) run much faster and more smoothly.

20
OS Notes by Adhishthatri Singh August 29, 2025

9 Storage Devices Basics


The computer system uses a variety of memory types, organized in a hierarchy
based on speed, cost, and capacity. This structure allows the system to balance
performance with affordability.

9.1 The Memory Hierarchy


The different memory present in the computer system can be visualized as a
pyramid, with the fastest, smallest, and most expensive memory at the top, and
the slowest, largest, and cheapest at the bottom.

CPU
Registers
↓ Cache Memory ← Primary Memory (Volatile)
Main Memory (RAM)

Electronic Disk (SSD)
Magnetic Disk (HDD)
← Secondary Memory (Non-Volatile)
Optical Disk (CD/DVD)
Magnetic Tapes

9.2 Levels of the Hierarchy Explained


1. Register
· Registers are the smallest and fastest unit of storage. They are not
just near the CPU; they are an integral part of the CPU itself.
· A register may hold an instruction, a storage address, or any data (such
as a bit sequence or individual characters).
· As a type of computer memory, their purpose is to quickly accept, store,
and transfer data and instructions that are being used immediately
by the CPU.
2. Cache
· Cache is an additional, smaller, and faster memory system that tem-
porarily stores frequently used instructions and data.
· Its goal is to provide quicker processing by the CPU, as fetching data
from the cache is significantly faster than fetching it from the main
memory.
3. Main Memory
· This is commonly known as RAM (Random-Access Memory).
· It is the primary workspace of the computer, holding the operating
system, applications, and data currently in use. It is volatile, meaning
its contents are lost when the power is turned off.
4. Secondary Memory
· These are the storage media on which a computer can permanently
store data and programs.
· This type of memory is non-volatile, meaning it retains its data even
without power.

21
OS Notes by Adhishthatri Singh August 29, 2025

· It includes devices like Solid-State Drives (SSDs), Hard Disk Drives


(HDDs), CDs/DVDs, and magnetic tapes.

22
OS Notes by Adhishthatri Singh August 29, 2025

10 Comparison of Memory Types


The trade-offs between different levels of the memory hierarchy can be summa-
rized by four key characteristics: cost, access speed, storage size, and volatility.

Table 4: Memory Hierarchy Comparison


Characteristic Primary Memory Secondary Memory
(Registers, Cache, RAM) (SSD, HDD)
Cost Costly. Registers are the most expen- Cheaper than primary mem-
sive due to the high cost of semicon- ory.
ductors & labor.
Access Speed Higher access speed. Slower access speed.
The speed order is:
Registers > Cache > Main Memory.
Storage Size Smaller capacity. Larger capacity (more
space).
Volatility Volatile: Data is lost when power is Non-volatile: Data is re-
off. tained without power.

23
OS Notes by Adhishthatri Singh August 29, 2025

11 Introduction to Process
11.1 Program vs. Process
What is a program? A program is a passive entity. It is a file containing
compiled code and instructions, stored on a disk, that is ready to be exe-
cuted.
What is a process? A process is a program under execution. It is an ac-
tive entity with its own memory space and system resources.

11.2 How the OS Creates a Process


The operating system converts a program into a process by performing the fol-
lowing steps:
1. Load Program & Data: Load the program’s executable code (text seg-
ment) and any static data from the disk into memory.
2. Allocate Runtime Stack: Allocate a memory region for the stack, which
will be used for local variables, function arguments, and return values.
3. Allocate Heap: Allocate a memory region for the heap, which will be used
for dynamic memory allocation during runtime.
4. I/O Initialization: Perform necessary I/O setup tasks.
5. Transfer Control: The OS hands off control to the program’s entry point
(e.g., the main() function), and execution begins.

11.3 Architecture of a Process (Memory Layout)


When a process is loaded into memory, it is organized into four main segments:

Table 5: Process Memory Layout


Segment Description
Stack Stores temporary data such as local variables, function arguments,
and return values. It grows and shrinks dynamically.
Heap Used for dynamically allocated variables during program execution.
Managed by the programmer (e.g., using malloc or new).
Data Contains global and static variables that are initialized.
Text The code segment. Contains the compiled, executable instructions
loaded from the disk.

11.4 Attributes of a Process


The OS needs to track every running process. It does this using a special data
structure and assigns unique attributes to each process.
Process Table The OS maintains a data structure, often a table, that tracks
all active processes in the system.
Process Control Block (PCB) Each entry in the process table is a PCB.
The PCB is a data structure that stores all the essential information and
attributes of a single process. It is the unique identifier and operational
container for a process from the OS’s perspective.

24
OS Notes by Adhishthatri Singh August 29, 2025

11.5 The Process Control Block (PCB) Structure


The PCB stores all the information the OS needs to manage a process, including:

Table 6: Key Fields in a Process Control Block


Field Description
Process ID (PID) A unique identifier for the process.
Program Counter (PC) The memory address of the next instruction to be executed.
Process State The current state of the process (e.g., new, ready, running, waiting).
Priority A priority value used by the scheduler to decide which process gets CPU time n
Registers A snapshot of the CPU register values for the process.
List of Open Files A list of files that the process currently has open.
List of Open Devices A list of I/O devices allocated to the process.

The Role of Registers in the PCB


The registers field in the PCB is a crucial data structure for context switching.
· When a running process’s time slice expires, the OS saves the current values
of all process-specific CPU registers into the registers field of its PCB. The
process is then swapped out.
· When the process is scheduled to run again, the OS reads the register values
from the PCB and loads them back into the CPU registers.
· This process of saving and restoring state ensures that the process can re-
sume its execution exactly where it left off, without any loss of data.

25
OS Notes by Adhishthatri Singh August 29, 2025

12 Process States & Process Queues


As a process executes, it changes its state. Each state defines the current activity
of the process. The operating system manages processes by moving them between
different states and organizing them into queues.

12.1 Process States


Each process may be in one of the following five states:
New The process is being created. The OS is about to pick up the program
from the disk and convert it into a process.
Ready The process is loaded into main memory and is waiting to be assigned
to a processor for execution.
Running The process’s instructions are being executed by the CPU; the CPU
has been allocated to it.
Waiting The process is waiting for some event to occur, such as the completion
of an I/O operation. It cannot execute even if the CPU is free.
Terminated The process has finished its execution. Its PCB entry is removed
from the process table, and its resources are deallocated.
A diagram of these state transitions is essential for understanding the process
life cycle. When a process moves from one state to another, it is called a context
switch.

12.2 Process Queues


The OS organizes processes in the same state into queues.
Job Queue · Contains: All processes in the system that are in the New
state.
· Location: These processes reside in secondary memory (the disk).
· Scheduler: The Job Scheduler, also known as the Long-Term
Scheduler (LTS), selects processes from this queue and loads them
into main memory for execution.
Ready Queue · Contains: All processes that are in the Ready state.
· Location: These processes reside in main memory.
· Scheduler: The CPU Scheduler, also known as the Short-Term
Scheduler (STS), selects a process from this queue and dispatches it
to the CPU for execution.
Waiting Queue (or Device Queue) · Contains: All processes that are in
the Waiting state.
· Location: These processes are typically in main memory, waiting for
an I/O device. Each device has its own waiting queue.

12.3 Schedulers and System Control


Degree of Multi-programming This term refers to the total number of pro-
cesses currently residing in the main memory (i.e., the number of processes in
the ready and waiting states). The Long-Term Scheduler (LTS) directly
controls the degree of multi-programming by deciding how many processes
to admit from the job queue into the ready queue.

26
OS Notes by Adhishthatri Singh August 29, 2025

Dispatcher The dispatcher is a module of the OS that gives control of the


CPU to the process selected by the Short-Term Scheduler (STS). Its
functions include switching context, switching to user mode, and jumping
to the proper location in the user program to restart that program.

27
OS Notes by Adhishthatri Singh August 29, 2025

LEC-11: Swapping | Context-Switching | Orphan pro-


cess | Zombie process
1. Swapping
a. A time-sharing system may have medium term schedular (MTS).
b. Remove processes from memory to reduce degree of multi-programming.
c. These removed processes can be reintroduced into memory, and its ex-
ecution can be continued where it left off. This is called Swapping.
d. Swap-out and swap-in is done by MTS.
e. Swapping is necessary to improve process mix or because a change in
memory requirements has overcommitted available memory, requiring
memory to be freed up.
f. Swapping is a mechanism in which a process can be swapped tem-
porarily out of main memory (or move) to secondary storage (disk) and
make that memory available to other processes. At some later time,
the system swaps back the process from the secondary storage to main
memory.
2. Context-Switching
a. Switching the CPU to another process requires performing a state save
of the current process and a state restore of a different process.
b. When this occurs, the kernel saves the context of the old process in its
PCB and loads the saved context of the new process scheduled to run.
c. It is pure overhead, because the system does no useful work while switch-
ing.
d. Speed varies from machine to machine, depending on the memory speed,
the number of registers that must be copied etc.
3. Orphan process
a. The process whose parent process has been terminated and it is still
running.
b. Orphan processes are adopted by init process.
c. Init is the first process of OS.
4. Zombie process / Defunct process
a. A zombie process is a process whose execution is completed but it still
has an entry in the process table.
b. Zombie processes usually occur for child processes, as the parent process
still needs to read its child’s exit status. Once this is done using the
wait system call, the zombie process is eliminated from the process
table. This is known as reaping the zombie process.
c. It is because parent process may call wait () on child process for a longer
time duration and child process got terminated much earlier.
d. As entry in the process table can only be removed, after the parent
process reads the exit status of child process. Hence, the child process
remains a zombie till it is removed from the process table.

28
OS Notes by Adhishthatri Singh August 29, 2025

13 Introduction to Process Scheduling


13.1 Core Concepts of Scheduling
Process Scheduling This is the basis of a multi-programming OS.
· By switching the CPU among processes, the OS can make the computer
more productive.
· Many processes are kept in memory at a time. When a process must
wait or its time quantum expires, the OS takes the CPU away from
that process and gives the CPU to another process, and this pattern
continues.
CPU Scheduler (Short-Term Scheduler) · Whenever the CPU becomes
idle, the OS must select one process from the ready queue to be exe-
cuted.
· This selection is done by the Short-Term Scheduler (STS).
Non-Preemptive Scheduling · Once the CPU has been allocated to a pro-
cess, the process keeps the CPU until it releases it, either by terminating
or by switching to the wait-state.
· Disadvantages: Can lead to starvation, as a process with a long burst
time may starve processes with less burst time. It can also cause low
CPU utilization if the running process enters a long wait state.
Preemptive Scheduling · The CPU can be taken away from a process after
its time quantum expires, in addition to when it terminates or switches
to a wait-state.
· Advantages: Results in less starvation and generally leads to high
CPU utilization.

13.2 Goals and Metrics of CPU Scheduling


Goals of CPU Scheduling
The primary objectives of any CPU scheduling algorithm are to optimize the
following:
· Maximum CPU utilization
· Minimum Turnaround time (TAT)
· Minimum Wait time (WT)
· Minimum Response time
· Maximum Throughput of the system

Key Performance Metrics


Throughput The number of processes completed per unit of time.
Arrival Time (AT) The time at which a process arrives at the ready queue.
Burst Time (BT) The total time required by the process for its execution on
the CPU.
Completion Time (CT) The time at which a process finishes its execution
and gets terminated.
Turnaround Time (TAT) The total time taken from when a process first en-
ters the ready state until it terminates.

29
OS Notes by Adhishthatri Singh August 29, 2025

TAT = CT − AT
Wait Time (WT) The total time a process spends in the ready queue waiting
for the CPU.
WT = TAT − BT
Response Time The time duration between a process getting into the ready
queue and getting the CPU for the first time.

13.3 First-Come, First-Serve (FCFS) Scheduling


FCFS is the simplest non-preemptive scheduling algorithm.
· Logic: Whichever process comes first in the ready queue will be given the
CPU first.
· Drawback: If one process has a longer Burst Time (BT), it will have a
major negative effect on the average Wait Time (WT) of all other processes.
This is known as the Convoy Effect.

The Convoy Effect


The Convoy Effect is a situation where many processes, which need to use a
resource for only a short time, are blocked by one process holding that resource
for a long time.
· This is a major cause of poor resource management and performance degra-
dation in FCFS.

30
OS Notes by Adhishthatri Singh August 29, 2025

14 CPU Scheduling Algorithms: SJF, Priority, & Round


Robin
14.1 Shortest Job First (SJF)
The SJF algorithm associates with each process the length of its next CPU burst.
When the CPU is available, it is assigned to the process that has the smallest
next CPU burst.

14.1.1 SJF (Non-Preemptive)


· Logic: The process with the least Burst Time (BT) will be dispatched to
the CPU first.
· Criteria: Arrival Time (AT) and Burst Time (BT).
· Mechanism: The scheduler must estimate the BT for each process in the
ready queue beforehand. It runs the lowest time process for its entire dura-
tion, then chooses the next job with the lowest BT at that instance.
· Challenges:
· Correct estimation of BT is an impossible task (ideally).
· This will suffer from the convoy effect if the very first process that arrived
is having a large BT.
· Process starvation might happen for longer jobs.

14.1.2 SJF (Preemptive) - Also known as Shortest Remaining Time


First (SRTF)
· This is the preemptive version of SJF. If a new process arrives with a CPU
burst length less than the remaining time of the current executing process,
the current process is preempted.

· Advantages:

· Less starvation compared to the non-preemptive version.

· No convoy effect.

· It gives the optimal (minimum) average waiting time for a given set of
processes. Scheduling a short job before a long one decreases the WT of
the short job more than it increases the WT of the long process.

14.2 Priority Scheduling


A priority is associated with each process, and the CPU is allocated to the process
with the highest priority.

14.2.1 Priority Scheduling (Non-Preemptive)


· Logic: A priority is assigned to a process when it is created. The scheduler
selects the process with the highest priority from the ready queue.
· SJF is a special case of general priority scheduling, with priority being in-
versely proportional to the predicted Burst Time (BT).

31
OS Notes by Adhishthatri Singh August 29, 2025

14.2.2 Priority Scheduling (Preemptive)


· Logic: A running job will be preempted if a new job arrives with a higher
priority.
· Challenge (Starvation): Both preemptive and non-preemptive versions
can cause indefinite waiting (starvation) for lower priority jobs, with the
possibility that they never get executed.
· Solution (Aging): Aging is the solution to starvation. This technique
involves gradually increasing the priority of processes that wait in the system
for a long time. For example, increase a process’s priority every 15 minutes
it remains in the ready queue.

14.3 Round Robin (RR) Scheduling


Round Robin is one of the most popular scheduling algorithms, designed specif-
ically for time-sharing systems.
· Nature: It is like FCFS but is preemptive.
· Criteria: Arrival Time (AT) and a fixed Time Quantum (TQ). The
scheduling decision does not depend on Burst Time (BT).
· Mechanism: Each process gets a small unit of CPU time (a time quan-
tum), usually 10-100 milliseconds. After this time has elapsed, the process
is preempted and added to the end of the ready queue.
· Low Starvation: No process is going to wait forever, hence it has very low
starvation and no convoy effect.
· Implementation: It is easy to implement.
· Overhead: If the Time Quantum (TQ) is too small, there will be a large
number of context switches, which increases the system overhead.

32
OS Notes by Adhishthatri Singh August 29, 2025

15 Multi-level Queue Scheduling: MLQ & MLFQ


15.1 Multi-level Queue Scheduling (MLQ)
In MLQ, the ready queue is divided into multiple separate queues, often based
on process priority or type.
· Process Assignment: A process is permanently assigned to one queue
when it enters the system, based on some property like memory size, process
priority, or process type. This makes the design inflexible.
· Independent Scheduling: Each queue has its own scheduling algorithm.
For example, a high-priority System Process queue might use FCFS, an
Interactive Process queue might use Round Robin, and a low-priority Batch
Process queue might use FCFS.
· Inter-queue Scheduling: Scheduling among the different sub-queues is
typically implemented as fixed priority preemptive scheduling. Higher-
priority queues have absolute priority over lower-priority ones.
· Example: If an interactive process arrives while a batch process is currently
executing, the batch process will be preempted.
· Problem (Starvation): Only after all processes from the top-level ready
queues are completed will the further level ready queues be scheduled. This
can cause starvation for lower priority processes. A convoy effect is also
present.

Example Process Types


System Process Created by the OS. It has the highest priority.
Interactive Process (Foreground) Needs user input and I/O.
Batch Process (Background) Runs silently in the background; no user input
is required.

15.2 Multi-level Feedback Queue Scheduling (MLFQ)


MLFQ is an enhancement of MLQ that allows processes to move between queues.
· Flexibility: Unlike MLQ, a process in MLFQ is not permanently assigned
to a queue. This makes the algorithm more flexible and adaptive.
· Mechanism: The idea is to separate processes according to the character-
istics of their CPU bursts.
· If a process uses too much CPU time, it will be moved to a lower-priority
queue. This scheme leaves I/O-bound and interactive processes in the
higher-priority queues.
· If a process waits for too long in a lower-priority queue, it may be moved to
a higher-priority queue. This form of aging prevents starvation.
· Advantages: It results in less starvation than MLQ and is highly config-
urable to match specific system design requirements.

33
OS Notes by Adhishthatri Singh August 29, 2025

15.3 Comparison of Scheduling Algorithms

Table 7: Scheduling Algorithm Characteristics


FCFS SJF PSJF Priority P-Priority RR MLQ MLFQ
Design Simple Complex Complex Complex Complex Simple Complex Complex
Preemption No No Yes No Yes Yes Yes Yes
Convoy Effect Yes Yes No Yes Yes No Yes Yes
Overhead No No Yes No Yes Yes Yes Yes

34
OS Notes by Adhishthatri Singh August 29, 2025

16 Introduction to Concurrency
1. Concurrency is the execution of the multiple instruction sequences at the
same time. It happens in the operating system when there are several process
threads running in parallel.
2. Thread:
· Single sequence stream within a process.
· An independent path of execution in a process.
· Light-weight process.
· Used to achieve parallelism by dividing a process’s tasks which are in-
dependent paths of execution.
· E.g., Multiple tabs in a browser, text editor (When you are typing in an
editor, spell checking, formatting of text and saving the text are done
concurrently by multiple threads.)
3. Thread Scheduling: Threads are scheduled for execution based on their
priority. Even though threads are executing within the runtime, all threads
are assigned processor time slices by the operating system.
4. Threads context switching:
· OS saves current state of thread & switches to another thread of same
process.
· Doesn’t includes switching of memory address space. (But Program
counter, registers & stack are included.)
· Fast switching as compared to process switching.
· CPU’s cache state is preserved.
5. How each thread get access to the CPU?
· Each thread has its own program counter.
· Depending upon the thread scheduling algorithm, OS schedules these
threads.
· OS will fetch instructions corresponding to PC of that thread and exe-
cute instruction.
6. I/O or TQ based context switching is done here as well
· We have TCB (Thread control block) like PCB for state storage man-
agement while performing context switching.
7. Will single CPU system would gain by multi-threading technique?
· Never.
· As two threads have to context switch for that single CPU.
· This won’t give any gain.
8. Benefits of Multi-threading.
· Responsiveness
· Resource sharing: Efficient resource sharing.
· Economy: It is more economical to create and context switch threads.
(a) Also, allocating memory and resources for process creation is costly,
so better to divide tasks into threads of same process.
· Threads allow utilization of multiprocessor architectures to a greater
scale and efficiency.

35
OS Notes by Adhishthatri Singh August 29, 2025

17 The Critical Section Problem and How to Address


It
1. Process synchronization techniques play a key role in maintaining the
consistency of shared data.
2. Critical Section (C.S)
a. The critical section refers to the segment of code where processes/threads
access shared resources, such as common variables and files, and perform
write operations on them.
b. Since processes/threads execute concurrently, any process can be inter-
rupted mid-execution.
3. Major Thread scheduling issue
a. Race Condition
i. A race condition occurs when two or more threads can access shared
data and they try to change it at the same time.
ii. Because the thread scheduling algorithm can swap between threads
at any time, you don’t know the order in which the threads will
attempt to access the shared data.
iii. Therefore, the result of the change in data is dependent on the
thread scheduling algorithm, i.e., both threads are "racing" to ac-
cess/change the data.
4. Solution to Race Condition
a. Atomic operations: Make Critical code section an atomic operation,
i.e., Executed in one CPU cycle.
b. Mutual Exclusion using locks.
c. Semaphores
5. Can we use a simple flag variable to solve the problem of race
condition?
· No.
6. Peterson’s solution can be used to avoid race condition but holds good
for only 2 process/threads.
7. Mutex/Locks
a. Locks can be used to implement mutual exclusion and avoid race condi-
tion by allowing only one thread/process to access critical section.
b. Disadvantages:
i. Contention: one thread has acquired the lock, other threads will
be busy waiting, what if thread that had acquired the lock dies, then
all other threads will be in infinite waiting.
ii. Deadlocks
iii. Debugging
iv. Starvation of high priority threads.

36
OS Notes by Adhishthatri Singh August 29, 2025

18 Conditional Variable and Semaphores for Threads


Synchronization
1. Conditional variable
a. The condition variable is a synchronization primitive that lets the thread
wait until a certain condition occurs.
b. Works with a lock.
c. A thread can enter a wait state only when it has acquired a lock. When
a thread enters the wait state, it will release the lock and wait until
another thread notifies that the event has occurred. Once the waiting
thread enters the running state, it again acquires the lock immediately
and starts executing.
d. Why to use conditional variable?
i. To avoid busy waiting.
ii. Contention is not here.
2. Semaphores
a. Synchronization method.
b. An integer that is equal to number of resources.
c. Multiple threads can go and execute C.S concurrently.
d. Allows multiple program threads to access the finite instance of resources
whereas mutex allows multiple threads to access a single shared resource
one at a time.
e. Binary semaphore: value can be 0 or 1.
i. Aka, mutex locks.
f. Counting semaphore
i. Can range over an unrestricted domain.
ii. Can be used to control access to a given resource consisting of a
finite number of instances.
g. To overcome the need for busy waiting, we can modify the definition
of the wait() and signal() semaphore operations. When a process
executes the wait() operation and finds that the semaphore value is not
positive, it must wait. However, rather than engaging in busy waiting,
the process can block itself. The block operation places a process into
a waiting queue associated with the semaphore, and the state of the
process is switched to the Waiting state. Then control is transferred to
the CPU scheduler, which selects another process to execute.
h. A process that is blocked, waiting on a semaphore S, should be restarted
when some other process executes a signal() operation. The process is
restarted by a wakeup () operation, which changes the process from the
waiting state to the ready state. The process is then placed in the ready
queue. t’enumerate

37
OS Notes by Adhishthatri Singh August 29, 2025

19 The Producer-Consumer Problem


The Producer-Consumer problem (also known as the bounded-buffer problem)
is a classic example of a multi-process synchronization problem. It models a
scenario where one or more "producer" threads generate data and place it into a
shared buffer, while one or more "consumer" threads take data out of the buffer
and process it.

19.1 Problem Definition


· The Producer: Generates data items and puts them into the shared buffer.
· The Consumer: Consumes data items by taking them from the shared
buffer.
· The Shared Buffer: A fixed-size, shared queue where producers deposit
data and consumers retrieve it.
The challenge is to design a synchronization mechanism that works correctly
even when the producer and consumer threads run concurrently and at different
speeds.

19.2 Requirements for a Solution


A correct solution must satisfy three conditions:
1. Mutual Exclusion: Only one thread (producer or consumer) can access and
modify the shared buffer at any given time. This prevents race conditions.
2. Producer Wait: The producer must wait if the buffer is full. It cannot add
data to a full buffer.
3. Consumer Wait: The consumer must wait if the buffer is empty. It cannot
remove data from an empty buffer.

19.3 Solution using Semaphores


A common and elegant solution uses three semaphores:
mutex A binary semaphore used to enforce mutual exclusion when accessing
the buffer. Initialized to 1.
full A counting semaphore that tracks the number of full slots in the buffer.
The consumer waits on this. Initialized to 0.
empty A counting semaphore that tracks the number of empty slots in the
buffer. The producer waits on this. Initialized to N (the buffer size).

C Code Example (using pthreads and semaphores)


This code demonstrates a complete solution in C.
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < pthread .h >
4 # include < semaphore .h >
5 # include < unistd .h >
6
7 # define BUFFER_SIZE 5
8 # define NUM_PRODUCERS 1
9 # define NUM_CONSUMERS 1
10
11 // Shared buffer and buffer index
12 int buffer [ BUFFER_SIZE ];

38
OS Notes by Adhishthatri Singh August 29, 2025

13 int in = 0;
14 int out = 0;
15
16 // Semaphores
17 sem_t mutex ; // For mutual exclusion
18 sem_t full ; // Counts full slots
19 sem_t empty ; // Counts empty slots
20
21 // Producer thread function
22 void * producer ( void * arg ) {
23 int item ;
24 for ( int i = 0; i < 10; i ++) {
25 item = rand () % 100; // Produce a random item
26
27 sem_wait (& empty ) ; // Wait for an empty slot
28 sem_wait (& mutex ) ; // Enter critical section
29
30 // Add item to the buffer
31 buffer [ in ] = item ;
32 printf ( " Producer produced : % d \ n " , item ) ;
33 in = ( in + 1) % BUFFER_SIZE ;
34
35 sem_post (& mutex ) ; // Leave critical section
36 sem_post (& full ) ; // Signal that a slot is now full
37
38 sleep ( rand () % 2) ; // Simulate work
39 }
40 pthread_exit ( NULL ) ;
41 }
42
43 // Consumer thread function
44 void * consumer ( void * arg ) {
45 int item ;
46 for ( int i = 0; i < 10; i ++) {
47 sem_wait (& full ) ; // Wait for a full slot
48 sem_wait (& mutex ) ; // Enter critical section
49
50 // Remove item from the buffer
51 item = buffer [ out ];
52 printf ( " Consumer consumed : % d \ n " , item ) ;
53 out = ( out + 1) % BUFFER_SIZE ;
54
55 sem_post (& mutex ) ; // Leave critical section
56 sem_post (& empty ) ; // Signal that a slot is now empty
57
58 sleep ( rand () % 3) ; // Simulate work
59 }
60 pthread_exit ( NULL ) ;
61 }
62
63 int main () {
64 pthread_t prod_threads [ NUM_PRODUCERS ];
65 pthread_t cons_threads [ NUM_CONSUMERS ];
66
67 // Initialize semaphores
68 sem_init (& mutex , 0 , 1) ;
69 sem_init (& full , 0 , 0) ;
70 sem_init (& empty , 0 , BUFFER_SIZE ) ;
71
72 // Create producer and consumer threads
73 for ( int i = 0; i < NUM_PRODUCERS ; i ++) {
74 pthread_create (& prod_threads [ i ] , NULL , producer , NULL ) ;
75 }

39
OS Notes by Adhishthatri Singh August 29, 2025

76 for ( int i = 0; i < NUM_CONSUMERS ; i ++) {


77 pthread_create (& cons_threads [ i ] , NULL , consumer , NULL ) ;
78 }
79
80 // Wait for threads to finish
81 for ( int i = 0; i < NUM_PRODUCERS ; i ++) {
82 pthread_join ( prod_threads [ i ] , NULL ) ;
83 }
84 for ( int i = 0; i < NUM_CONSUMERS ; i ++) {
85 pthread_join ( cons_threads [ i ] , NULL ) ;
86 }
87
88 // Destroy semaphores
89 sem_destroy (& mutex ) ;
90 sem_destroy (& full ) ;
91 sem_destroy (& empty ) ;
92
93 return 0;
94 }
Listing 1: Producer-Consumer Solution in C

How the Semaphores Work


· The producer first calls sem_wait(&empty). If the buffer is full (empty is
0), it blocks. Otherwise, it decrements empty and proceeds.
· The consumer first calls sem_wait(&full). If the buffer is empty (full is
0), it blocks. Otherwise, it decrements full and proceeds.
· Both use sem_wait(&mutex) and sem_post(&mutex) to ensure only one of
them is modifying the buffer at a time.
· After producing, the producer calls sem_post(&full) to signal that a slot
is now full, potentially waking up a waiting consumer.
· After consuming, the consumer calls sem_post(&empty) to signal that a slot
is now empty, potentially waking up a waiting producer.

40
OS Notes by Adhishthatri Singh August 29, 2025

20 The Readers-Writers Problem


The Readers-Writers problem is another classic synchronization problem. It mod-
els a situation where a shared data resource (like a file or a record in a database)
is accessed by multiple concurrent threads. Some of these threads only read the
data (Readers), while others modify it (Writers).

20.1 Problem Constraints


The goal is to design a synchronization scheme that allows for maximum concur-
rency while maintaining data consistency. The constraints are:
1. Multiple readers are allowed to read the shared data simultaneously.
2. Only one writer is allowed to access the shared data at any given time.
3. If a writer is modifying the data, no reader should be allowed to read it.
This prevents readers from accessing inconsistent or partially updated data.

20.2 Problem Variations


There are two common variations of the problem, based on how to handle priority
when both readers and writers are waiting:
Readers’ Priority In this version, no reader should be kept waiting unless a
writer has already obtained permission to use the shared object. This means
if there is a continuous stream of readers, a writer might be starved (kept
waiting forever).
Writers’ Priority In this version, if a writer is waiting to access the object, no
new reader is allowed to start reading. This ensures that waiting writers get
preference, but it can lead to starvation for readers.

20.3 Solution for Readers’ Priority


A common solution for the readers’ priority problem uses one binary semaphore
for the writers, one binary semaphore as a mutex for a shared counter, and a
shared integer to count the number of active readers.
wrt A binary semaphore that is shared by both readers and writers. It is used by
the first reader who enters and the last reader who exits the critical section.
It is also used by every writer. Initialized to 1.
mutex A binary semaphore used to ensure mutual exclusion when updating the
read_count variable. Initialized to 1.
read_count An integer variable that keeps track of how many readers are cur-
rently in their critical section. Initialized to 0.

C Code Example (Readers’ Priority Solution)


This code demonstrates the logic for the readers’ priority solution.
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < pthread .h >
4 # include < semaphore .h >
5 # include < unistd .h >
6
7 // Semaphores and shared variable
8 sem_t wrt ;
9 sem_t mutex ;

41
OS Notes by Adhishthatri Singh August 29, 2025

10 int read_count = 0;
11
12 // Writer thread function
13 void * writer ( void * arg ) {
14 long id = ( long ) arg ;
15 while (1) {
16 // Entry section for writer
17 sem_wait (& wrt ) ;
18
19 // --- CRITICAL SECTION ---
20 printf ( " Writer % ld is writing ...\ n " , id ) ;
21 sleep (2) ; // Simulate writing
22 // --- END OF CRITICAL SECTION ---
23
24 // Exit section for writer
25 sem_post (& wrt ) ;
26
27 printf ( " Writer % ld is done writing .\ n " , id ) ;
28 sleep (3) ; // Simulate other work
29 }
30 pthread_exit ( NULL ) ;
31 }
32
33 // Reader thread function
34 void * reader ( void * arg ) {
35 long id = ( long ) arg ;
36 while (1) {
37 // Entry section for reader
38 sem_wait (& mutex ) ;
39 read_count ++;
40 if ( read_count == 1) {
41 sem_wait (& wrt ) ; // First reader locks the writer
42 }
43 sem_post (& mutex ) ;
44
45 // --- CRITICAL SECTION ---
46 printf ( " Reader % ld is reading ... ( Active readers : % d ) \ n " ,
id , read_count ) ;
47 sleep (1) ; // Simulate reading
48 // --- END OF CRITICAL SECTION ---
49
50 // Exit section for reader
51 sem_wait (& mutex ) ;
52 read_count - -;
53 if ( read_count == 0) {
54 sem_post (& wrt ) ; // Last reader unlocks the writer
55 }
56 sem_post (& mutex ) ;
57
58 printf ( " Reader % ld is done reading .\ n " , id ) ;
59 sleep (4) ; // Simulate other work
60 }
61 pthread_exit ( NULL ) ;
62 }
63
64 int main () {
65 pthread_t readers [5] , writers [2];
66
67 // Initialize semaphores
68 sem_init (& wrt , 0 , 1) ;
69 sem_init (& mutex , 0 , 1) ;
70
71 // Create threads

42
OS Notes by Adhishthatri Singh August 29, 2025

72 for ( long i = 0; i < 2; i ++) {


73 pthread_create (& writers [ i ] , NULL , writer , ( void *) i ) ;
74 }
75 for ( long i = 0; i < 5; i ++) {
76 pthread_create (& readers [ i ] , NULL , reader , ( void *) i ) ;
77 }
78
79 // Join threads ( this program will run indefinitely )
80 for ( int i = 0; i < 2; i ++) {
81 pthread_join ( writers [ i ] , NULL ) ;
82 }
83 for ( int i = 0; i < 5; i ++) {
84 pthread_join ( readers [ i ] , NULL ) ;
85 }
86
87 return 0;
88 }
Listing 2: Readers-Writers Solution in C (Readers’ Priority)

How the Solution Works


· Writer’s Logic: A writer simply waits on the wrt semaphore, enters the
critical section, and then signals wrt when it leaves. This ensures only one
writer can be active at a time and that no readers can enter while a writer
is active.
· Reader’s Logic (Entry): A reader first locks the mutex to update read_count.
If it is the first reader (read_count == 1), it is responsible for locking out
any writers by waiting on the wrt semaphore. It then unlocks the mutex to
allow other readers to enter.
· Reader’s Logic (Exit): A reader locks the mutex again to decrement
read_count. If it is the last reader to leave (read_count == 0), it is re-
sponsible for signaling the wrt semaphore, which allows a waiting writer (if
any) to proceed. It then unlocks the mutex.

43
OS Notes by Adhishthatri Singh August 29, 2025

21 The Dining Philosophers Problem


This is a classic synchronization problem that illustrates the challenges of allocat-
ing limited resources among competing processes in a way that avoids deadlock.

21.1 The Problem Setup


· There are 5 philosophers sitting at a circular table.
· In the center of the table is a bowl of noodles, and the table is laid with 5
single forks.
· The philosophers spend their lives alternating between two states: Thinking
and Eating.
· Thinking state: When a philosopher thinks, they do not interact with
others.
· Eating state: When a philosopher gets hungry, they try to pick up the two
forks adjacent to them (left and right). They can only pick up one fork at
a time. To eat, a philosopher must have both forks at the same time. After
eating, they release both forks.

21.2 The Deadlock Problem


A simple solution might be to represent each fork as a binary semaphore and
have each philosopher perform a wait() operation on their left and then their
right fork.
Semaphore fork[5] = {1, 1, 1, 1, 1};
· Although this semaphore solution makes sure that no two neighbors are
eating simultaneously, it can still create a deadlock.
· Deadlock Scenario: Suppose that all 5 philosophers become hungry at the
same time and each picks up their left fork. Now, all forks are held by a
philosopher, and all fork semaphores would be 0. When each philosopher
tries to grab their right fork, they will be waiting forever. This is a classic
deadlock.

21.3 Solutions to Avoid Deadlock


Since simple semaphores are not enough to solve this problem on their own, we
must add some enhancement to create a deadlock-free solution.
1. Limit the number of philosophers: Allow at most 4 philosophers to be
sitting simultaneously at the table.
2. Atomic Fork Pickup: Allow a philosopher to pick up their forks only if
both forks are available. To do this, the picking-up process must be executed
within a critical section.
3. Odd-Even Rule: An odd-numbered philosopher picks up their first left
fork and then their right fork, whereas an even-numbered philosopher picks
up their right fork then their left fork.

21.4 Code Solution (Atomic Fork Pickup)


This C code implements the atomic fork pickup strategy using a mutex and
condition variables. A philosopher only picks up forks if both are available,
otherwise they wait.

44
OS Notes by Adhishthatri Singh August 29, 2025

1 # include < stdio .h >


2 # include < stdlib .h >
3 # include < pthread .h >
4 # include < unistd .h >
5
6 # define N 5 // Number of philosophers
7
8 // States a philosopher can be in
9 enum { THINKING , HUNGRY , EATING } state [ N ];
10
11 // Mutex for critical sections
12 pthread_mutex_t mutex ;
13 // Condition variables , one for each philosopher
14 pthread_cond_t cond [ N ];
15
16 // Function declarations
17 void * philosopher ( void * num ) ;
18 void take_forks ( int phil_num ) ;
19 void put_forks ( int phil_num ) ;
20 void test ( int phil_num ) ;
21
22 int main () {
23 pthread_t thread_id [ N ];
24
25 // Initialize mutex and condition variables
26 pthread_mutex_init (& mutex , NULL ) ;
27 for ( int i = 0; i < N ; i ++) {
28 pthread_cond_init (& cond [ i ] , NULL ) ;
29 state [ i ] = THINKING ;
30 }
31
32 // Create philosopher threads
33 for ( long i = 0; i < N ; i ++) {
34 pthread_create (& thread_id [ i ] , NULL , philosopher , ( void *) i )
;
35 }
36
37 // Wait for threads to finish ( they won ’t in this infinite loop
)
38 for ( int i = 0; i < N ; i ++) {
39 pthread_join ( thread_id [ i ] , NULL ) ;
40 }
41 return 0;
42 }
43
44 void * philosopher ( void * num ) {
45 long i = ( long ) num ;
46 while (1) {
47 printf ( " Philosopher % ld is thinking .\ n " , i ) ;
48 sleep ( rand () % 3 + 1) ; // Think for a while
49
50 take_forks ( i ) ; // Get hungry and try to pick up forks
51
52 printf ( " Philosopher % ld is eating .\ n " , i ) ;
53 sleep ( rand () % 2 + 1) ; // Eat for a while
54
55 put_forks ( i ) ; // Done eating , put forks back
56 }
57 }
58
59 // Tries to pick up both forks
60 void take_forks ( int phil_num ) {
61 pthread_mutex_lock (& mutex ) ; // Enter critical section

45
OS Notes by Adhishthatri Singh August 29, 2025

62
63 state [ phil_num ] = HUNGRY ;
64 printf ( " Philosopher % d is hungry .\ n " , phil_num ) ;
65
66 // Check if forks can be taken
67 test ( phil_num ) ;
68
69 // If couldn ’t get forks , wait to be signaled
70 if ( state [ phil_num ] != EATING ) {
71 pthread_cond_wait (& cond [ phil_num ] , & mutex ) ;
72 }
73
74 pthread_mutex_unlock (& mutex ) ; // Exit critical section
75 }
76
77 // Puts down both forks
78 void put_forks ( int phil_num ) {
79 pthread_mutex_lock (& mutex ) ; // Enter critical section
80
81 state [ phil_num ] = THINKING ;
82
83 // Check if neighbors can now eat
84 int left = ( phil_num + N - 1) % N ;
85 int right = ( phil_num + 1) % N ;
86 test ( left ) ;
87 test ( right ) ;
88
89 pthread_mutex_unlock (& mutex ) ; // Exit critical section
90 }
91
92 // Test if a philosopher can start eating
93 void test ( int phil_num ) {
94 int left = ( phil_num + N - 1) % N ;
95 int right = ( phil_num + 1) % N ;
96
97 // Condition : I am hungry AND my neighbors are not eating
98 if ( state [ phil_num ] == HUNGRY &&
99 state [ left ] != EATING &&
100 state [ right ] != EATING ) {
101
102 state [ phil_num ] = EATING ;
103 // Signal the waiting philosopher that they can now eat
104 pthread_cond_signal (& cond [ phil_num ]) ;
105 }
106 }
Listing 3: Deadlock-Free Dining Philosophers Solution

46
OS Notes by Adhishthatri Singh August 29, 2025

22 Introduction to Deadlock
22.1 What is a Deadlock?
1. In a Multi-programming environment, we have several processes competing
for a finite number of resources.
2. A process requests a resource (R). If R is not available (taken by another
process), the process enters a waiting state.
3. Sometimes that waiting process is never able to change its state because the
resource it has requested is busy (forever). This is called DEADLOCK
(DL).
4. Two or more processes are waiting on some resource’s availability, which will
never be available as it is also busy with some other process. The processes
are said to be in Deadlock.
5. A Deadlock is a bug present in the process/thread synchronization method.
6. In a Deadlock, processes never finish executing, and the system resources are
tied up, preventing other jobs from starting.
7. Example of resources: Memory space, CPU cycles, files, locks, sockets,
IO devices etc.
8. A single resource can have multiple instances. E.g., CPU is a resource, and
a system can have 2 CPUs.

How a process/thread utilizes a resource?


a. Request: Request the R. If R is free Lock it, else wait till it is available.
b. Use: Use the resource.
c. Release: Release the resource instance and make it available for other pro-
cesses.

22.2 Deadlock Necessary Conditions


For a deadlock to occur, 4 conditions must hold simultaneously in a system:
1. Mutual Exclusion
· Only 1 process at a time can use the resource. If another process requests
that resource, the requesting process must wait until the resource has
been released.
2. Hold & Wait
· A process must be holding at least one resource and waiting to acquire
additional resources that are currently being held by other processes.
3. No-preemption
· A resource must be voluntarily released by the process after completion
of execution. (No resource preemption).
4. Circular wait
· A set {P 0, P 1, . . . , P n} of waiting processes must exist such that P0 is
waiting for a resource held by P1, P1 is waiting for a resource held by
P2, and so on.

22.3 Methods for Handling Deadlocks


There are three primary methods for dealing with deadlocks:

47
OS Notes by Adhishthatri Singh August 29, 2025

a. Use a protocol to prevent or avoid deadlocks, ensuring that the system will
never enter a deadlocked state.
b. Allow the system to enter a deadlocked state, detect it, and recover.
c. Ignore the problem altogether and pretend that deadlocks never occur in the
system. (Also known as the Ostrich algorithm or Deadlock Ignorance).

Deadlock Prevention
To ensure that deadlocks never occur, the system can use a deadlock prevention
scheme. This is done by ensuring that at least one of the necessary conditions
cannot hold.
Mutual Exclusion · Use locks (mutual exclusion) only for non-sharable re-
sources.
· Sharable resources like Read-Only files can be accessed by multiple pro-
cesses/threads.
· However, we can’t prevent DLs by denying the mutual-exclusion condi-
tion, because some resources are intrinsically non-sharable.
Hold & Wait · To ensure H&W condition never occurs in the system, we
must guarantee that whenever a process requests a resource, it doesn’t
hold any other resource.
· Protocol (A): Each process has to request and be allocated all its
resources before its execution.
· Protocol (B): Allow a process to request resources only when it has
none. It can request any additional resources after it must have released
all the resources that it is currently allocated.
No Preemption · If a process is holding some resources and requests an-
other resource that cannot be immediately allocated to it, then all the
resources the process is currently holding are preempted. The process
will restart only when it can regain its old resources, as well as the new
one that it is requesting. (Live Lock may occur).
· If a process requests some resources, we first check whether they are
available. If yes, we allocate them. If not, we check whether they are
allocated to some other process that is waiting for additional resources.
If so, preempt the desired resource from the waiting process and allocate
them to the requesting process.
Circular Wait · To ensure that this condition never holds is to impose a
proper ordering of resource allocation.
· P1 and P2 both require R1 and R2. Locking on these resources should
be like, both try to lock R1 then R2. By this way whichever process first
locks R1 will get R2.

48
OS Notes by Adhishthatri Singh August 29, 2025

1. Deadlock Avoidance: The idea is, the kernel is given in advance informa-
tion concerning which resources a process will use in its lifetime.
a. By this, the system can decide for each request whether the process
should wait.
b. To decide whether the current request can be satisfied or delayed, the
system must consider the resources currently available, resources cur-
rently allocated to each process in the system and the future requests
and releases of each process.
c. Schedule process and its resources allocation in such a way that the DL
never occur.
d. Safe state: A state is safe if the system can allocate resources to each
process (up to its max) in some order and still avoid DL. A system is in
a safe state only if there exists a safe sequence.
e. In an Unsafe state, the operating system cannot prevent processes from
requesting resources in such a way that any deadlock occurs. It is not
necessary that all unsafe states are deadlocks; an unsafe state may lead
to a deadlock.
f. The main key of the deadlock avoidance method is whenever the request
is made for resources then the request must only be approved only in
the case if the resulting state is a safe state.
g. In a case, if the system is unable to fulfill the request of all processes,
then the state of the system is called unsafe.
h. Scheduling algorithm using which DL can be avoided by finding a safe
state. (Banker Algorithm)
2. Banker Algorithm
a. When a process requests a set of resources, the system must determine
whether allocating these resources will leave the system in a safe state.
If yes, then the resources may be allocated to the process. If not, then
the process must wait till other processes release enough resources.
3. Deadlock Detection: Systems haven’t implemented deadlock-prevention
or a deadlock avoidance technique, then they may employ DL detection,
then, recovery technique.
a. Single Instance of Each resource type (wait-for graph method)
i. A deadlock exists in the system if and only if there is a cycle in the
wait-for graph.
ii. In order to detect the deadlock, the system needs to maintain the
wait-for graph and periodically system invokes an algorithm that
searches for the cycle in the wait-for graph.
b. Multiple instances for each resource type
i. Banker Algorithm
4. Recovery from Deadlock
a. Process termination
i. Abort all DL processes.
ii. Abort one process at a time until DL cycle is eliminated.
b. Resource preemption
i. To eliminate DL, we successively preempt some resources from pro-
cesses and give these resources to other processes until DL cycle is
broken.

49
OS Notes by Adhishthatri Singh August 29, 2025

23 Memory Management Techniques | Contiguous Mem-


ory Allocation
1. In a Multi-programming environment, we have multiple processes in the main
memory (Ready Queue) to keep the CPU utilization high and to make the
computer responsive to the users.
2. To realize this increase in performance, however, we must keep several pro-
cesses in the memory; that is, we must share the main memory. As a result,
we must manage main memory for all the different processes.
3. Logical versus Physical Address Space
a. Logical Address
i. An address generated by the CPU.
ii. The logical address is basically the address of an instruction or data
used by a process.
iii. User can access logical address of the process.
iv. User has indirect access to the physical address through logical ad-
dress.
v. Logical address does not exist physically. Hence, aka, Virtual ad-
dress.
vi. The set of all logical addresses that are generated by any program
is referred to as Logical Address Space.
vii. Range: 0 to max.
b. Physical Address
i. An address loaded into the memory-address register of the physical
memory.
ii. User can never access the physical address of the Program.
iii. The physical address is in the memory unit. It’s a location in the
main memory physically.
iv. A physical address can be accessed by a user indirectly but not
directly.
v. The set of all physical addresses corresponding to the Logical ad-
dresses is commonly known as Physical Address Space.
vi. It is computed by the Memory Management Unit (MMU).
vii. Range: (R + 0) to (R + max), for a base value R.
c. The runtime mapping from virtual to physical address is done by a
hardware device called the memory-management unit (MMU).
d. The user’s program mainly generates the logical address, and the user
thinks that the program is running in this logical address, but the pro-
gram mainly needs physical memory in order to complete its execution.

50
OS Notes by Adhishthatri Singh August 29, 2025

23.1 Memory Mapping and Protection


To manage the isolation and protection of processes, the OS uses the following
concepts and hardware support:
a. OS provides the Virtual Address Space (VAS) concept.
b. To separate memory space, we need the ability to determine the range of
legal addresses that the process may access and to ensure that the process
can access only these legal addresses.
c. The relocation register contains the value of the smallest physical address
(Base address [R]); the limit register contains the range of logical addresses
(e.g., relocation = 100040 & limit = 74600).
d. Each logical address must be less than the limit register.
e. The MMU maps the logical address dynamically by adding the value in the
relocation register.
f. When the CPU scheduler selects a process for execution, the dispatcher loads
the relocation and limit registers with the correct values as part of the con-
text switch. Since every address generated by the CPU (Logical address) is
checked against these registers, we can protect both the OS and other users’
programs and data from being modified by a running process.
g. Any attempt by a program executing in user mode to access the OS memory
or other uses’ memory results in a trap in the OS, which treats the attempt
as a fatal error.
h. Address Translation: The process is as follows:
· The CPU generates a logical address.
· This address is compared with the limit register.
· If the logical address is greater than or equal to the limit, a trap (ad-
dressing error) is generated.
· If the logical address is less than the limit, it is added to the relocation
register.
· The result is the final physical address, which is used to access the
Main Memory.

51
OS Notes by Adhishthatri Singh August 29, 2025

23.2 Contiguous Memory Allocation


Physical memory can be allocated to processes in two primary ways:
· Contiguous Allocation: The entire process is stored in one single, unbro-
ken block of memory.
· Non-contiguous Allocation: The process is broken into pieces, which are
stored in different parts of memory.
This section focuses on Fixed Partitioning, a basic form of contiguous allo-
cation. In this scheme, main memory is pre-divided into partitions of equal or
different sizes before processes are loaded.

23.3 Limitations of Fixed Partitioning


While simple, this method suffers from several major drawbacks that limit its
efficiency.

Table 8: Key Drawbacks of Fixed Partitioning


Limitation Description
Internal Fragmentation Occurs when a process is smaller than the partition it is al-
located to. The unused space within the partition is wasted
because it cannot be assigned to another process.
External Fragmentation Occurs when there is enough total free space to fit a new pro-
cess, but that space is not in a single contiguous block. The
memory is fragmented into small, unusable holes between par-
titions.
Process Size Limit A process cannot be larger than the largest partition in the
system. If a 5MB process needs to run but the largest partition
is 4MB, it cannot be loaded.
Low Degree of Multipro- The number of processes that can run concurrently is strictly
gramming limited by the number of predefined partitions. This is ineffi-
cient if many small processes need to run, as the system cannot
create more partitions on the fly.

52
OS Notes by Adhishthatri Singh August 29, 2025

23.3.1 Dynamic Partitioning


In this technique, the partition size is not declared initially. It is declared at the
time of process loading, meaning the partition size is equal to the process size.

Advantages over fixed partitioning:


1. No internal fragmentation.
2. No limit on the size of a process (besides total memory).
3. Better degree of multi-programming.

Limitation:
1. External fragmentation.

Visualizing Dynamic Partitioning

Table 9: Allocation in Dynamic Partitioning


Incoming Processes Memory State After Allocation
Operating System
Process P1 (5 MB)
Process P2 (2 MB)
· Process P1 (5 MB) Process P3 (3 MB)
· Process P2 (2 MB) Process P4 (4 MB)
· Process P3 (3 MB)
· Process P4 (4 MB)

Visualizing External Fragmentation


External fragmentation occurs when processes complete and leave holes in mem-
ory.

Table 10: External Fragmentation in Dynamic Partitioning


Memory State (After P1 & P3 Com- Allocation Attempt for New Process
plete)
Operating System
5 MB Free
Process P2 (2 MB) New Process P5 requires 8 MB.
3 MB Free
Process P4 (4 MB)
Total Free Space = 8 MB Allocation Fails!

P5 can’t be loaded into memory even


though there is 8 MB of space available,
because the free space is not contiguous.

53
OS Notes by Adhishthatri Singh August 29, 2025

24 Free Space Management


24.1 Defragmentation / Compaction
This is a technique used to solve the problem of external fragmentation.
· Dynamic partitioning suffers from external fragmentation.
· Compaction is used to minimize the probability of external fragmentation.
· By applying this technique, all the free partitions are made contiguous, and
all the loaded partitions are brought together.
· We can then store the bigger processes in the memory. The free partitions are
merged which can now be allocated according to the needs of new processes.
This technique is also called defragmentation.
· The efficiency of the system is decreased in the case of compaction since all
the free spaces will be transferred from several places to a single place.

24.2 Managing Free Space


· How free space is stored/represented in OS? Free holes in the memory
are represented by a free list (often a Linked-List data structure).
· How to satisfy a request of a of n size from a list of free holes?
Various algorithms are implemented by the Operating System in order to
find out the holes in the linked list and allocate them to the processes.

24.3 Algorithms for Allocating Free Holes


First Fit · Logic: Allocate the first hole that is big enough.
+ Simple and easy to implement.
+ Fast/Less time complexity.
Next Fit · Logic: An enhancement on First Fit, but it starts the search
from the location of the last allocated hole.
+ Same advantages of First Fit.
Best Fit · Logic: Allocate the smallest hole that is big enough.
+ Results in lesser internal fragmentation (if applicable).
- May create many small, unusable holes and cause major external frag-
mentation.
- Slow, as it is required to iterate through the whole free holes list.
Worst Fit · Logic: Allocate the largest hole that is big enough.
- Slow, as it is required to iterate through the whole free holes list.
+ Leaves larger leftover holes that may be able to accommodate other
processes.

54
OS Notes by Adhishthatri Singh August 29, 2025

25 Paging: Non-Contiguous Memory Allocation


25.1 The Idea Behind Paging
1. The main disadvantage of Dynamic partitioning is External Fragmenta-
tion. This can be removed by Compaction, but with overhead.
2. We need a more dynamic/flexible/optimal mechanism to load processes in
the partitions.
a. If we have only two small non-contiguous free holes in the memory, say
1KB each.
b. If the OS wants to allocate RAM to a process of 2KB, in contiguous
allocation, it is not possible, as we must have contiguous memory space
available of 2KB. (External Fragmentation)
c. What if we divide the process into 1KB-1KB blocks?

25.2 Paging Explained


3. Paging
a. Paging is a memory-management scheme that permits the physical ad-
dress space of a process to be non-contiguous.
b. It avoids external fragmentation and the need for compaction.
c. The idea is to divide the physical memory into fixed-sized blocks called
Frames, along with dividing logical memory into blocks of the same size
called Pages. (# Page size = Frame size)
d. Page size is usually determined by the processor architecture. Tradition-
ally, pages in a system had a uniform size, such as 4,096 bytes. However,
processor designs often allow two or more, sometimes simultaneous, page
sizes due to its benefits.
e. Page Table
i. A Data structure that stores which page is mapped to which frame.
ii. The page table contains the base address of each page in the
physical memory.
f. Every address generated by the CPU (logical address) is divided into two
parts: a page number (p) and a page offset (d). The p is used as
an index into a page table to get the base address of the corresponding
frame in physical memory.
g. Paging model of logical and physical memory: A process’s logical
address space (its pages) is mapped to physical memory (frames) via a
page table. The frames do not need to be contiguous. For example:

Table 11: Example Page Table Mapping for Process P1


Page Number Frame Number
0 1
1 4
2 3
3 7

h. The Page table is stored in main memory at the time of process creation
and its base address is stored in the process control block (PCB).

55
OS Notes by Adhishthatri Singh August 29, 2025

i. A page table base register (PTBR) is present in the system that


points to the current page table.
j. Changing page tables requires only this one register, at the time of
context-switching.

25.3 Performance Issues with Paging


4. How Paging avoids external fragmentation?
a. Non-contiguous allocation of the pages of the process is allowed in the
random free frames of the physical memory.
5. Why paging is slow and how do we make it fast?
a. There are too many memory references to access the desired location in
physical memory.

25.4 Translation Look-aside Buffer (TLB)


6. Translation Look-aside buffer (TLB)
a. A Hardware support to speed-up paging process.
b. It’s a hardware cache, high speed memory.
c. TBL has key and value.
d. The Page table is stored in main memory & because of this when the
memory reference is made the translation is slow.
e. When we are retrieving a physical address using the page table, after
getting the frame address corresponding to the page number, we put
an entry of the into the TLB. So that next time, we can get the values
from the TLB directly without referencing the actual page table. Hence,
making the paging process faster.

The TLB Process: Hit vs. Miss


The TLB is checked before the page table.
· TLB hit: The TLB contains the mapping for the requested logical address.
The frame number is retrieved directly from the TLB, which is very fast.
· TLB miss: The page number is not found in the TLB. The system must
then access the page table in main memory to find the frame number. The
new page-to-frame mapping is then added to the TLB for future accesses.

Address Space Protection with ASIDs


· Address space identifiers (ASIDs) are stored in each entry of the TLB.
· An ASID uniquely identifies each process and is used to provide address
space protection and allow the TLB to contain entries for several different
processes simultaneously.
· When the TLB attempts to resolve virtual page numbers, it ensures that the
ASID for the currently executing process matches the ASID associated with
the virtual page in the TLB entry.
· If it doesn’t match, the attempt is treated as a TLB miss.

56
OS Notes by Adhishthatri Singh August 29, 2025

26 Segmentation: Non-Contiguous Memory Alloca-


tion
[colback=blue!5!white, colframe=blue!75!black, title=Core Concepts]
1. An important aspect of memory management that became unavoidable with
paging is the separation of the user’s view of memory from the actual
physical memory.
2. Segmentation is a memory management technique that supports this user
view of memory.
3. A logical address space is a collection of segments. These segments are based
on the user view of logical memory.
4. Each segment has a segment number and an offset, defining a logical
address as: <segment-number, offset>.

26.1 Segmentation vs. Paging


Segmentation is closer to the Operating System rather than the User. It di-
vides all the processes into the form of pages although a process can have
some relative parts of functions which need to be loaded in the same page.
Paging doesn’t care about the User’s view of the process. It may divide the
same function into different pages; and those pages may or may not be loaded
at the same time into the memory. It decreases the efficiency of the system.
It is better to have segmentation which divides the process into segments.
Each segment contains the same type of functions such as the main function
can be included in one segment and the library functions can be included in
the other segment.

26.2 Segmentation Hardware and Address Translation


The hardware support for segmentation involves a Segment Table. Each entry
in the segment table has a base (the starting physical address of the segment)
and a limit (the length of the segment).
The translation process is as follows:
1. The CPU generates a logical address containing a segment number (s)
and an offset (d).
2. The segment number (s) is used as an index into the segment table.
3. The hardware checks if the offset (d) is less than the limit specified in the
segment table entry.
4. If Yes (d < limit): The offset is valid. The final physical address is
calculated by adding the base value from the segment table to the offset
(d).
5. If No (d >= limit): The access is outside the segment’s boundary. A trap
is generated, causing an addressing error.

57
OS Notes by Adhishthatri Singh August 29, 2025

26.3 Advantages and Disadvantages


[colback=green!5!white, colframe=green!50!black, title=Advantages]
1. No internal fragmentation.
2. One segment has a contiguous allocation, hence efficient working within
that segment.
3. The size of the segment table is generally less than the size of the page
table.
4. It results in a more efficient system because the compiler keeps the same
type of functions in one segment.
[colback=red!5!white, colframe=red!75!black, title=Disadvantages]
1. External fragmentation.
2. The different size of segments is not good for the time of swapping.

Modern Approach
Modern System architecture provides both segmentation and paging, imple-
mented in some hybrid approach.

58
OS Notes by Adhishthatri Singh August 29, 2025

27 Virtual Memory, Demand Paging, & Page Faults


27.1 What is Virtual Memory?
[colback=blue!5!white, colframe=blue!75!black, title=Virtual Memory]
1. Virtual memory is a technique that allows the execution of processes that
are not completely in the memory. It provides the user an illusion of having
a very big main memory. This is done by treating a part of secondary
memory as the main memory. (Swap-space)
2. Advantage of this is, programs can be larger than physical memory.
3. It is required that instructions must be in physical memory to be executed.
But it limits the size of a program to the size of physical memory. In fact, in
many cases, the entire program is not needed at the same time. So, we want
an ability to execute a program that is only partially in memory would give
many benefits:
a. A program would no longer be constrained by the amount of physical
memory that is available.
b. Because each user program could take less physical memory, more pro-
grams could be run at the same time, with a corresponding increase in
CPU utilization and throughput.
4. Running a program that is not entirely in memory would benefit both the
system and the user.
5. The programmer is provided with a very large virtual memory when only a
smaller physical memory is available.

27.2 Demand Paging: The Lazy Swapper


[colback=purple!5!white, colframe=purple!75!black, title=Demand Paging]
6. Demand Paging is a popular method of virtual memory management.
7. In demand paging, the pages of a process which are least used, get stored in
the secondary memory.
8. A page is copied to the main memory when its demand is made, or a page
fault occurs. There are various page replacement algorithms which are used
to determine the pages which will be replaced.
9. Rather than swapping the entire process into memory, we use a Lazy Swap-
per. A lazy swapper never swaps a page into memory unless that page will
be needed.
10. We are viewing a process as a sequence of pages, rather than one large
contiguous address space, using the term Swapper is technically incorrect.
A swapper manipulates entire processes, whereas a Pager is concerned with
individual pages of a process.
11. How Demand Paging works?
a. When a process is to be swapped-in, the pager guesses which pages will
be used.
b. Instead of swapping in a whole process, the pager brings only those pages
into memory. This, it avoids reading into memory pages that will not
be used anyway.
c. Above way, OS decreases the swap time and the amount of physical
memory needed.
d. The valid-invalid bit scheme in the page table is used to distinguish
between pages that are in memory and that are on the disk.

59
OS Notes by Adhishthatri Singh August 29, 2025

i. Valid-invalid bit 1 means, the associated page is both legal and


in memory.
ii. Valid-invalid bit 0 means, the page either is not valid (not in the
LAS of the process) or is valid but is currently on the disk.
12. Pure Demand Paging
i. In an extreme case, we can start executing a process with no pages in
memory. When the OS sets the instruction pointer to the first instruction
of the process, which is not in the memory. The process immediately
faults for the page and page is brought into the memory.
ii. Never bring a page into memory until it is required.
13. We use locality of reference to bring out reasonable performance from
demand paging.

27.3 The Page Fault: Handling Missing Pages


· If a process never attempts to access some invalid bit page, the process will
be executed successfully without even the needed pages present in the swap
space.
· What happens if the process tries to access a page that was not brought
into memory? Access to a page marked invalid causes a page fault. Paging
hardware noticing the invalid bit for a demanded page will cause a trap to
the OS.
[colback=orange!5!white, colframe=orange!85!black, title=Procedure to han-
dle the page fault:]
i. Check an internal table (in PCB of the process) to determine whether the
reference was a valid or an invalid memory access.
ii. If the reference was invalid, the process throws an exception.
iii. If the reference is valid, the pager will swap-in the page.
iv. We find a free frame (from the free-frame list).
v. Schedule a disk operation to read the desired page into the newly allocated
frame.
vi. When the disk read is complete, we modify the page table that, the page is
now in memory.
vii. Restart the instruction that was interrupted by the trap. The process can
now access the page as though it had always been in memory.

27.4 Advantages and Disadvantages of Virtual Memory


[colback=green!5!white, colframe=green!50!black, title=Advantages of Virtual
Memory]
1. The degree of multi-programming will be increased.
2. Users can run large apps with less real physical memory.
[colback=red!5!white, colframe=red!75!black, title=Disadvantages of Virtual
Memory]
1. The system can become slower as swapping takes time.
2. Thrashing may occur.

60
OS Notes by Adhishthatri Singh August 29, 2025

28 Page Replacement Algorithms


28.1 Why Page Replacement?
1. Whenever a Page Fault occurs, that is, a process tries to access a page
which is not currently present in a frame and OS must bring the page from
swap-space to a frame.
2. OS must do page replacement to accommodate a new page into a free frame,
but there might be a possibility the system is working in high utilization
and all the frames are busy, in that case OS must replace one of the pages
allocated into some frame with the new page.
3. The page replacement algorithm decides which memory page is to be
replaced. Some allocated page is swapped out from the frame and a new
page is swapped into the freed frame.
4. Types of Page Replacement Algorithm: (AIM is to have minimum
page faults)

28.2 Algorithm Types


[colback=blue!5!white, colframe=blue!75!black, title=a. First-In, First-Out
(FIFO)]
i. Allocate frame to the page as it comes into the memory by replacing the
oldest page.
ii. Easy to implement.
iii. Performance is not always good.
(a) The page replaced may be an initialization module that was used long
time ago (Good replacement candidate).
(b) The page may contain a heavily used variable that was initialized early
and is in content use. (Will again cause a page fault).
[colback=red!10!white, colframe=red!75!black, title=iv. Belady’s Anomaly]
A strange behavior shown by the FIFO algorithm.
(a) In the case of LRU and optimal page replacement algorithms, it is seen
that the number of page faults will be reduced if we increase the number
of frames.
(b) However, Belady found that, in the FIFO page replacement algorithm,
the number of page faults will get increased with the increment in the
number of frames. This is a strange behavior shown by FIFO algorithm
in some of the cases.
[colback=green!5!white, colframe=green!60!black, title=b. Optimal Page Re-
placement]
i. Find if a page that is never referenced in future. If such a page exists,
replace this page with the new page. If no such page exists, find a page that
is referenced farthest in future. Replace this page with the new page.
ii. Lowest page fault rate among any algorithm.
iii. Difficult to implement as OS requires future knowledge of a reference
string which is kind of impossible. (Similar to SJF scheduling).
[colback=purple!5!white, colframe=purple!75!black, title=c. Least-Recently
Used (LRU)]
i. We can use the recent past as an approximation of the near future then we
can replace the page that has not been used for the longest period.

61
OS Notes by Adhishthatri Singh August 29, 2025

ii. Can be implemented by two ways:


(a) Counters

a. Associate a time field with each page table entry.

b. Replace the page with the smallest time value.

(b) Stack

a. Keep a stack of page numbers.

b. Whenever a page is referenced, it is removed from the stack & put


on the top.

c. By this, the most recently used is always on the top, & least re-
cently used is always on the bottom.

d. As entries might be removed from the middle of the stack, so a


Doubly linked list can be used.

[colback=orange!5!white, colframe=orange!85!black, title=d. Counting-Based


Page Replacement] Keep a counter of the number of references that have been
made to each page. (Reference counting)
i. Least Frequently Used (LFU)
(a) Actively used pages should have a large reference count.
(b) Replace page with the smallest count.
ii. Most Frequently Used (MFU)
(a) Based on the argument that the page with the smallest count was prob-
ably just brought in and has yet to be used.
iii. Neither MFU nor LFU replacement is common.

62
OS Notes by Adhishthatri Singh August 29, 2025

29 Thrashing
[colback=red!5!white, colframe=red!75!black, title=1. Thrashing]
a. If the process doesn’t have the number of frames it needs to support pages
in active use, it will quickly page-fault. At this point, it must replace some
page. However, since all its pages are in active use, it must replace a page
that will be needed again right away.
b. Consequently, it quickly faults again, and again, and again, replacing pages
that it must bring back in immediately.
c. This high paging activity is called Thrashing.
d. A system is Thrashing when it spends more time servicing the page
faults than executing processes.

Visualizing Thrashing
The diagram in the notes shows the relationship between CPU utilization and
the degree of multiprogramming.
· Initially, as the degree of multiprogramming increases, CPU utilization also
increases.
· After reaching a maximum point, if the degree of multiprogramming increases
further, the system starts thrashing.
· During thrashing, the high paging activity causes CPU utilization to drop
sharply because the system is constantly busy swapping pages in and out of
memory instead of performing useful work.

29.1 Techniques to Handle Thrashing


[colback=blue!5!white, colframe=blue!75!black, title=d. Technique to Handle
Thrashing]
i. Working set model [colback=gray!10, sharp corners]
(a) This model is based on the concept of the Locality Model.
(b) The basic principle states that if we allocate enough frames to a process
to accommodate its current locality, it will only fault whenever it moves
to some new locality. But if the allocated frames are lesser than the
size of the current locality, the process is bound to thrash.
ii. Page Fault frequency [colback=gray!10, sharp corners]
(a) Thrashing has a high page-fault rate.
(b) We want to control the page-fault rate.
(c) When it is too high, the process needs more frames. Conversely, if the
page-fault rate is too low, then the process may have too many frames.
(d) We establish upper and lower bounds on the desired page fault rate.
(e) If the pf-rate exceeds the upper limit, allocate the process another frame;
if the pf-rate falls below the lower limit, remove a frame from the process.
(f) By controlling the pf-rate, thrashing can be prevented.

63

You might also like