0% found this document useful (0 votes)
16 views115 pages

End Sem OS

Uploaded by

ld17021997
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)
16 views115 pages

End Sem OS

Uploaded by

ld17021997
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/ 115

Operating System

Unit 1
Introduction
Simple Batch Systems:
Multiprogrammed Batch Systems:
Time Sharing Systems:
Personal-Computer Systems:
Parallel Systems:
Distributed Systems:
Real-Time Systems:
Operating Systems as Resource Managers:
Processes
Introduction to Processes:
Process States:
Process Management:
Interrupts
Interprocess Communication (IPC):
Threads: Introduction and Thread States
Thread Operation:
Threading Models:
Processor Scheduling:
Scheduling Levels:
Preemptive Scheduling:
Non-Preemptive Scheduling:
Priorities in Scheduling:
Scheduling Objectives:
Scheduling Criteria:
Scheduling algorithms
Demand Scheduling:
Real-Time Scheduling:
Unit 2
Process Synchronization: Mutual Exclusion
Software Solutions to the Mutual Exclusion Problem:
Hardware Solutions to the Mutual Exclusion Problem:

Operating System 1
Semaphores
Critical Section Problems
Case Study: The Dining Philosophers Problem
Case Study: The Barber Shop Problem
Memory Organization
Memory Hierarchy
Memory Management Strategies
Contiguous Memory Allocation vs. Non-Contiguous Memory Allocation
Partition Management Techniques
Logical Address Space vs. Physical Address Space
Swapping
Paging
Segmentation
Segmentation with Paging Virtual Memory: Demand Paging
Page Replacement and Page-Replacement Algorithms
Performance of Demand Paging
Thrashing
Demand Segmentation and Overlay Concepts
Unit 3
Deadlocks
Deadlock Solution
Deadlock Prevention:
Deadlock Avoidance with Banker's Algorithm:
Banker's Algorithm Steps:
Deadlock Detection:
Resource Allocation Graph (RAG):
Deadlock Recovery:
Recovery Methods:
Resource concepts
Hardware Resources:
Software Resources:
Resource Allocation and Management:
Device Management
Device Management Components:
Disk Scheduling Strategies:
Common Disk Scheduling Algorithms:
Rotational Optimization:

Operating System 2
Techniques for Rotational Optimization:
System Consideration:
Key System Considerations:
Caching and Buffering:
Significance and Benefits:
Unit 4
File System Introduction:
Components of a File System:
File Organization:
Common File Organization Techniques:
Logical File System:
Characteristics and Functions:
Physical File System:
Key Aspects and Functions:
Relationship between Logical and Physical File Systems:
File allocation strategies
Common File Allocation Strategies:
Factors influencing File Allocation Strategies:
Free Space Management
Common Free Space Management Techniques:
File Access Control
Data Access Techniques:
Considerations in File Access Control and Data Access:
Data Integrity Protection
Techniques and Measures for Data Integrity Protection:
Importance and Benefits:
Challenges:
File systems
FAT32 (File Allocation Table 32):
NTFS (New Technology File System):
Ext2/Ext3 (Second Extended File System/Third Extended File System):
APFS (Apple File System):
ReFS (Resilient File System):

Unit 1

Operating System 3
Introduction
An Operating System (OS) is a fundamental component of a computer system that
acts as an intermediary between the hardware and the user or application software.
It serves several crucial functions:

1. Resource Management: The OS manages hardware resources like the central


processing unit (CPU), memory, storage devices, and input/output devices. It
allocates these resources to various programs and ensures their efficient use.

2. Process Management: It allows the execution of multiple processes


concurrently. A process is a program in execution. The OS schedules
processes, provides mechanisms for inter-process communication, and ensures
they run without interfering with each other.

3. Memory Management: The OS manages system memory, ensuring that each


program or process gets the necessary memory space. It uses techniques like
virtual memory to provide an illusion of larger memory than physically available.

4. File System Management: The OS handles file and directory management,


allowing users to store, retrieve, and organize data efficiently. It manages file
permissions and access control.

5. Device Management: It controls the interaction between software and


hardware devices. This includes device drivers that enable software to
communicate with various hardware components.

6. User Interface: The OS provides a user-friendly interface for users to interact


with the system. This interface can be command-line (text-based) or graphical
(GUI), depending on the OS.

7. Security and Access Control: It enforces security policies, user


authentication, and access controls to protect the system from unauthorized
access and data breaches.

8. Error Handling: The OS provides mechanisms for handling errors and


exceptions, preventing system crashes due to software bugs.

9. Networking: In modern operating systems, networking capabilities are


integrated to allow communication over networks, which is vital for internet

Operating System 4
connectivity and networked applications.

10. Task Scheduling: The OS employs scheduling algorithms to determine the


order in which processes are executed, ensuring fair allocation of CPU time and
system responsiveness.

Examples of Operating Systems:

1. Windows: Microsoft Windows is a widely used OS known for its graphical user
interface and compatibility with a variety of software applications.

2. Linux: Linux is an open-source operating system known for its stability,


security, and flexibility. It comes in various distributions, such as Ubuntu,
CentOS, and Debian.

3. macOS: macOS is the OS developed by Apple for their Macintosh computers,


known for its user-friendly interface and integration with Apple hardware.

4. Unix: Unix is an older, robust OS that has influenced many other operating
systems, including Linux.

5. Android: Android is a popular mobile operating system used in smartphones


and tablets.

6. iOS: iOS is Apple's mobile operating system used in iPhones and iPads.

Simple Batch Systems:


A Simple Batch System is an early type of operating system that manages batch
processing. Batch processing involves the execution of multiple jobs without user
interaction. Jobs are submitted to the system in the form of batch jobs, and the OS
processes them one after the other. Here are the key characteristics and
components of simple batch systems:

Batch Jobs: In a simple batch system, users submit their jobs to the system as
batch jobs. A batch job typically consists of one or more programs or tasks that
need to be executed sequentially.

Operating System 5
Job Scheduling: The OS's primary responsibility is to schedule and manage
the execution of batch jobs. It maintains a job queue and selects the next job to
run based on criteria like job priority.

Job Control Language (JCL): Users provide job control language statements
in their batch job submissions. JCL specifies details like the input and output
files, resource requirements, and other job-specific information.

Job Spooling: Jobs are often spooled (spooling stands for Simultaneous
Peripheral Operations On-line) before execution. This means they are placed in
a queue and stored on secondary storage, making it easier for the system to
retrieve and execute them.

No Interactivity: Unlike interactive systems where users can provide input


during program execution, simple batch systems have no user interaction
during job execution. They are suited for long-running and computationally
intensive tasks.

Resource Allocation: The OS allocates resources, such as CPU time,


memory, and I/O devices, to each job in the queue as it is scheduled.

Job Termination: Once a job is completed, the OS releases the allocated


resources, manages output files, and may notify the user of job completion.

Advantages of Simple Batch Systems:

Efficiency: Simple batch systems are efficient for processing large volumes of
similar tasks without the overhead of user interaction.

Resource Utilization: They make efficient use of system resources by allowing


continuous execution of jobs without idle times.

Error Recovery: Batch systems can be designed to restart a job in case of


system failures, improving error recovery.

Disadvantages of Simple Batch Systems:

Lack of Interactivity: They are not suitable for tasks that require user
interaction, making them unsuitable for real-time or interactive applications.

Operating System 6
Limited Flexibility: Users need to submit jobs in advance, which may lead to
delays if a high-priority task suddenly arises.

Resource Contentions: Resource allocation can be a challenge in busy batch


systems, leading to contention for resources.

Debugging: Debugging batch jobs can be more challenging since there is no


immediate feedback from the system.

Job Prioritization: Determining job priorities and scheduling can be complex in


busy batch environments. The main idea behind
multiprogramming is to allow
multiple jobs to reside in memory
simultaneously, with the operating
Multiprogrammed Batch Systems: system switching between them to
maximize CPU usage.
A Multiprogrammed Batch System is an extension of simple batch systems that
aims to improve the overall efficiency of the system by allowing multiple jobs to be
in memory simultaneously. This approach addresses some of the limitations of
simple batch systems. Here are the key aspects of multiprogrammed batch
systems:

Job Pool: In a multiprogrammed batch system, there is a job pool that contains
a collection of batch jobs. These jobs are ready to run and are loaded into
memory as space becomes available.

Job Scheduling: The operating system employs job scheduling algorithms to


select the next job from the job pool and load it into memory. This helps in
reducing idle time of the CPU and improves system throughput.

Memory Management: Multiprogrammed batch systems manage memory


efficiently by allocating and de-allocating memory space for each job. Jobs may
need to be swapped in and out of memory to make the best use of available
resources.

I/O Overlap: These systems aim to overlap I/O operations with CPU
processing. While one job is waiting for I/O, another job can utilize the CPU,
enhancing overall system performance.

Job Prioritization: Jobs are prioritized based on their characteristics and


requirements. High-priority jobs may be selected for execution before lower-

Operating System 7
priority ones.

Batch Job Execution: Each job is executed as a separate program, similar to


simple batch systems. It runs until it completes or is blocked by I/O, at which
point the CPU scheduler selects the next job for execution.

Advantages of Multiprogrammed Batch Systems:

Improved Throughput: The system can execute multiple jobs concurrently,


reducing CPU idle time and increasing the overall throughput of the system.

Resource Utilization: Resources are used efficiently as they are not wasted on
idle time. This leads to better CPU and I/O device utilization.

Enhanced Job Scheduling: Job scheduling algorithms play a crucial role in


selecting the next job for execution, optimizing the use of system resources.

Reduced Waiting Time: By overlapping I/O operations with CPU processing,


waiting times for I/O-bound jobs are reduced.

Disadvantages of Multiprogrammed Batch Systems:

Complexity: Managing multiple jobs in memory requires complex memory


management and job scheduling algorithms.

Increased Overhead: The need to load and swap jobs in and out of memory
introduces some overhead in the system.

Contention for Resources: With multiple jobs running concurrently, contention


for resources like memory and I/O devices can arise.

Priority Inversion: Job prioritization can sometimes lead to priority inversion


issues where lower-priority jobs block resources needed by higher-priority ones.

Multiprogrammed batch systems are a significant improvement over simple batch


systems as they allow for better resource utilization and system throughput. They
were a crucial step in the evolution of operating systems, laying the foundation for
more advanced OS designs.

Operating System 8
Time Sharing Systems:
A Time-Sharing System, also known as a multi-user operating system, allows
multiple users to interact with the computer simultaneously. Here are the key
aspects of time-sharing systems:

User Interaction: Time-sharing systems provide a user-friendly interface that


allows multiple users to log in and work on the computer concurrently.

Time Slicing: The CPU's time is divided into small time slices, and each user or
process is allocated a time slice to execute their tasks. This provides the illusion
of concurrent execution for multiple users.

Resource Sharing: Resources like CPU, memory, and I/O devices are shared
among users or processes. The system ensures fair access to resources.

Multi-Tasking: Time-sharing systems support true multi-tasking, where multiple


processes can run concurrently, and the OS manages the context switching.

Response Time: They are designed for fast response times to ensure that
users can interact with the system in real-time.

Example: Unix is an example of a time-sharing operating system, providing a


command-line interface for multiple users to log in and work simultaneously.

Personal-Computer Systems:
Personal Computer (PC) Systems are designed for individual users and small-scale
computing needs. Here are the key characteristics:

Single User: PC systems are typically single-user systems, designed for use
by a single individual.

User-Friendly GUI: They often have a graphical user interface (GUI) that
makes it easy for users to interact with the system.

Limited Resource Sharing: PC systems are not designed for heavy multi-user
interaction or resource sharing. They focus on providing resources to a single
user's tasks.

Operating System 9
Broad Application: PC systems are used for a wide range of applications, from
word processing and web browsing to gaming and multimedia.

Operating Systems: Common PC operating systems include Microsoft


Windows, macOS, and various Linux distributions.

Parallel Systems:
Parallel Systems are designed to execute tasks concurrently by using multiple
processors or cores. Here are the key aspects of parallel systems:

Multiple Processors: Parallel systems have multiple processors, which can be


on a single chip or distributed across multiple machines.

Parallel Processing: They leverage parallelism to divide tasks into smaller


subtasks that can be executed simultaneously.

High Performance: Parallel systems offer high computing power and are used
for scientific computing, simulations, and tasks that can be divided into parallel
threads.

Challenges: Developing parallel software can be complex due to issues like


data synchronization and load balancing.

Examples: High-performance computing clusters, supercomputers, and multi-


core processors in modern PCs are examples of parallel systems.

Each of these types of systems serves different purposes and has its unique
characteristics, catering to specific user needs and computing requirements.

Distributed Systems:
Distributed Systems are a collection of interconnected computers that work together
as a single, unified system. Here are the key aspects of distributed systems:

Multiple Machines: Distributed systems consist of multiple independent


machines or nodes that communicate and collaborate to perform tasks.

Resource Sharing: Resources like processing power, memory, and data can
be shared across the network, allowing for more efficient use of resources.

Operating System 10
Scalability: Distributed systems can be easily scaled by adding more machines
to the network.

Fault Tolerance: They are designed to handle failures gracefully, ensuring that
the system continues to function even if some nodes fail.

Examples: The internet is a massive distributed system, and cloud computing


platforms like AWS, Google Cloud, and Azure are examples of distributed
systems used for various applications.

Real-Time Systems:
Real-Time Systems are designed to respond to events or input within a predefined
time constraint. They are used in applications where timing and predictability are
critical. Here are the key characteristics of real-time systems:

Timing Constraints: Real-time systems have strict timing constraints, and


tasks must be completed within specific time limits.

Deterministic Behavior: These systems aim for deterministic behavior,


ensuring that the system's response is predictable and consistent.

Hard and Soft Real-Time: Real-time systems can be classified as hard real-
time (where missing a deadline is catastrophic) or soft real-time (where
occasional missed deadlines are acceptable).

Applications: Real-time systems are used in areas like aviation (flight control
systems), automotive (engine control units), and industrial automation
(robotics).

Challenges: Developing real-time systems is challenging due to the need for


precise timing, and they often require specialized hardware and software.

Both distributed systems and real-time systems are specialized types of computer
systems, each with its unique requirements and applications. Distributed systems
focus on resource sharing and scalability across multiple machines, while real-time
systems prioritize time-bound responses and determinism.

Operating Systems as Resource Managers:

Operating System 11
Operating Systems (OS) act as resource managers that oversee and control the
allocation and utilization of a computer system's hardware and software resources.
Here's how an OS functions as a resource manager:

1. Process Management: The OS manages processes, which are running


instances of programs. It allocates CPU time to processes, schedules them for
execution, and ensures that they run without interfering with each other.
Process management includes creating, terminating, and suspending
processes.

2. Memory Management: The OS handles memory allocation, ensuring that each


process gets the necessary memory space. It also manages memory protection
to prevent one process from accessing another process's memory.

3. File System Management: It manages the file system, allowing users to


create, read, write, and delete files. The OS enforces file access permissions
and maintains the file hierarchy.

4. Device Management: The OS controls the interaction between software and


hardware devices. This involves device drivers that enable communication
between the operating system and various hardware components like printers,
disks, and network interfaces.

5. User and Authentication Management: The OS provides user authentication


and access control, ensuring that only authorized users can access the system
and specific resources. It also maintains user profiles and security policies.

6. Scheduling and Resource Allocation: The OS employs scheduling algorithms


to determine the order in which processes are executed, ensuring fair allocation
of CPU time and system responsiveness. It also allocates resources like I/O
devices, network bandwidth, and memory.

7. Error Handling: The OS provides mechanisms for handling errors and


exceptions, preventing system crashes due to software bugs or hardware
failures. This includes error reporting, logging, and graceful degradation.

8. Networking: In modern operating systems, networking capabilities are


integrated to allow communication over networks, which is vital for internet
connectivity and networked applications.

Operating System 12
9. Security: OSs implement security measures like encryption, firewalls, and
access controls to protect the system from unauthorized access and data
breaches.

10. Load Balancing: In distributed systems, the OS manages load balancing,


ensuring that tasks are distributed evenly across the network to prevent
overloading of certain nodes.

11. Virtualization: Many modern OSs offer virtualization capabilities, allowing


multiple virtual machines (VMs) to run on a single physical server. This is
essential for cloud computing and server consolidation.

In summary, operating systems play a pivotal role in managing the computer's


resources, ensuring their efficient and secure use. They serve as intermediaries
between users, applications, and hardware, abstracting the complexities of
hardware management and providing a user-friendly interface for interaction with
the system. This resource management function is essential for the proper
functioning of computer systems and the execution of various tasks and
applications.

Processes
Introduction to Processes:
In the context of operating systems, a process is a fundamental concept that
represents the execution of a program. It's a unit of work in a computer system that
can be managed and scheduled by the operating system. Here's an overview of
processes:

A process consists of the program's code, its data, and the execution context,
including the program counter, registers, and the stack.

Each process operates in its own isolated memory space, which ensures that
one process cannot directly interfere with or access the memory of another
process.

Processes provide a way to achieve multitasking and concurrency by allowing


multiple programs to run simultaneously on a single or multi-processor system.

Operating System 13
Processes can communicate and share data through inter-process
communication mechanisms provided by the operating system.

Process States:

Processes go through different states during their lifecycle. These states represent
the different stages a process can be in. The typical process states are:

1. New: In this state, a process is being created but has not yet started execution.

2. Ready: A process in the ready state is prepared to run and is waiting for its turn
to be executed. It's typically waiting in a queue.

3. Running: A process in the running state is actively executing its code on the
CPU.

Operating System 14
4. Blocked (or Waiting): When a process is unable to continue its execution due
to the need for some external event (e.g., I/O operation, user input), it enters
the blocked state and is put on hold until the event occurs.

5. Terminated: When a process completes its execution, it enters the terminated


state. Resources associated with the process are released, and the process is
removed from the system.

Process Management:
Process management is a critical aspect of an operating system's responsibilities. It
involves various tasks related to process creation, scheduling, and termination.
Here's an overview of process management:

1. Process Creation: When a user or system request initiates a new process, the
OS is responsible for creating the process. This includes allocating memory,
initializing data structures, and setting up the execution environment.

2. Process Scheduling: The OS uses scheduling algorithms to determine which


process to run next on the CPU. It ensures fair allocation of CPU time to
multiple processes and aims to maximize system throughput.

3. Process Termination: When a process completes its execution or is


terminated due to an error or user action, the OS must clean up its resources,
release memory, and remove it from the system.

4. Process Communication: The OS provides mechanisms for processes to


communicate and share data. This can include inter-process communication
(IPC) methods like message passing or shared memory.

5. Process Synchronization: When multiple processes are accessing shared


resources, the OS manages synchronization to prevent data corruption and
race conditions.

6. Process Priority and Control: The OS allows users to set process priorities,
which influence their order of execution. It also provides mechanisms to control
and monitor processes.

Operating System 15
7. Process State Transitions: The OS manages the transitions between different
process states, ensuring that processes move between states as required.

Effective process management is essential for the efficient and stable operation of a
computer system, enabling multiple programs to run simultaneously, share
resources, and respond to user and system needs.

Interrupts
In the context of operating systems and computer architecture, an interrupt is a
signal or event that halts the normal execution of a program to transfer control to a
specific routine, often called an interrupt service routine (ISR) or interrupt handler.
Interrupts play a crucial role in modern computing systems by allowing the operating
system to respond to events and requests in a timely and efficient manner. Here are
the key aspects of interrupts:

1. Types of Interrupts:

Hardware Interrupts: These are generated by hardware devices or


components to signal the CPU that an event has occurred. For example, a
keyboard interrupt may be triggered when a key is pressed.

Software Interrupts: Also known as software traps or system calls, these


are generated by software programs to request specific services from the
operating system. For example, a program might request I/O operations or
memory allocation through software interrupts.

Exceptions: Exceptions are similar to interrupts but are typically generated


due to errors or exceptional conditions, such as division by zero, invalid
memory access, or illegal instructions. These events cause the CPU to
transfer control to an exception handler.

2. Interrupt Vector: Each type of interrupt is associated with a unique interrupt


vector, which is essentially an address pointing to the location of the
corresponding interrupt service routine (ISR) in memory. When an interrupt
occurs, the CPU uses this vector to find and execute the appropriate ISR.

Operating System 16
3. Interrupt Prioritization: In systems with multiple interrupts, prioritization
mechanisms ensure that the CPU services higher-priority interrupts first. This is
essential for handling critical events promptly.

4. Interrupt Handling: When an interrupt occurs, the CPU temporarily suspends


the currently executing program and transfers control to the ISR associated with
the interrupt. The ISR performs the required actions, which may include saving
the context of the interrupted program, processing the interrupt, and restoring
the program's context to resume execution.

5. Context Switching: Interrupts often involve context switching, where the CPU
switches from one program's context to another. This allows the operating
system to maintain the illusion of concurrent execution, even on single-core
processors.

6. Interrupt Latency: Interrupts are designed to be responsive, but they introduce


some delay (interrupt latency) in handling the event. Reducing interrupt latency
is essential in real-time and critical systems.

7. Masking and Disabling Interrupts: The CPU typically provides mechanisms to


temporarily disable or mask interrupts, which can be useful in certain situations,
such as during critical sections of code or when configuring hardware devices.

8. Interrupt Controllers: In systems with multiple hardware interrupts, interrupt


controllers are often used to manage and prioritize the various interrupt
sources. These controllers help streamline the handling of interrupts.

Interrupts are a fundamental mechanism in modern computing systems, allowing


the operating system to efficiently manage hardware events, respond to user
requests, and execute system services. They are essential for achieving
concurrency, responsiveness, and efficient resource utilization in computer systems.

Interprocess Communication (IPC):


Interprocess Communication (IPC) is a set of mechanisms and techniques used by
processes in an operating system to communicate and share data with each other.
IPC is essential for processes to cooperate, exchange information, and synchronize
their activities. There are several methods and tools for IPC, depending on the

Operating System 17
needs and requirements of the processes involved. Here are some of the key
methods of IPC:

1. Shared Memory:

Shared memory allows processes to share a portion of their address space.


This shared region of memory acts as a communication buffer, allowing
multiple processes to read and write data into it.

Shared memory is a fast and efficient method of IPC since it doesn't involve
the overhead of copying data between processes.

However, it requires careful synchronization and mutual exclusion to


prevent data corruption.

2. Message Passing:

In a message-passing IPC mechanism, processes communicate by sending


and receiving messages through a predefined communication channel.

Message-passing can be either synchronous (blocking) or asynchronous


(non-blocking), depending on whether processes wait for a response or
continue their execution.

It is a more structured and safer method compared to shared memory since


processes don't have direct access to each other's memory.

3. Pipes and FIFOs (Named Pipes):

Pipes are a one-way communication channel that allows data to flow in one
direction between processes.

Named pipes (FIFOs) are similar but have a well-defined name in the file
system, allowing unrelated processes to communicate using a common
pipe.

Pipes and FIFOs are often used in command-line environments to create


data pipelines.

4. Sockets:

Operating System 18
Sockets are a network-based IPC mechanism used for communication
between processes on different machines over a network.

They are widely used for client-server applications and network


communication.

Sockets support both stream (TCP) and datagram (UDP) communication.

5. Signals:

Signals are a form of asynchronous notification sent to a process to notify it


of a specific event or to request that the process take a particular action.

Signals are often used for simple forms of IPC and for handling events like
process termination.

6. Semaphores and Mutexes:

Semaphores and mutexes are synchronization mechanisms that are used


to control access to shared resources, preventing race conditions and
ensuring mutual exclusion.

They are particularly useful for coordinating concurrent access to critical


sections of code.

7. Message Queues:

Message queues are a form of message-passing IPC where messages are


placed in a queue and processes can read or write from the queue in a
controlled and ordered manner.

8. Remote Procedure Call (RPC):

RPC allows a process to execute a procedure (function) in another address


space as if it were a local call. It is often used for distributed computing and
client-server systems.

IPC is fundamental for modern operating systems and plays a crucial role in
enabling processes to work together, share data, and synchronize their actions. The
choice of IPC method depends on factors such as the nature of the communication,
performance requirements, and security considerations.

Operating System 19
Threads: Introduction and Thread States
Introduction to Threads:

In the context of operating systems and concurrent programming, a thread is the


smallest unit of execution within a process. Threads are often referred to as
"lightweight processes" because they share the same memory space as the
process and can execute independently. Multiple threads within a single process
can work together to perform tasks concurrently. Here's an introduction to threads:

1. Thread vs. Process: A process is a separate program execution with its own
memory space, file handles, and system resources. Threads, on the other hand,
share the same memory space as the process and have their own execution
context, such as program counter and registers.

2. Benefits of Threads:

Improved concurrency: Threads allow multiple tasks to be executed


concurrently within the same process, potentially improving system
performance.

Resource efficiency: Threads share resources like memory, reducing the


overhead associated with creating and managing separate processes.

Faster communication: Threads within the same process can communicate


more efficiently than separate processes since they share memory.

3. Types of Threads:

User-Level Threads: These threads are managed entirely by user-level


libraries and do not require kernel support. They are lightweight but may not
take full advantage of multiprocessor systems.

Kernel-Level Threads: These threads are managed by the operating


system kernel, which provides better support for multithreading and can
fully utilize multiprocessor systems.

Thread States:

Threads go through different states during their lifecycle, just like processes. The
typical thread states are:

Operating System 20
1. New: In this state, a thread is created but has not yet started execution.

2. Runnable: A thread in the runnable state is ready to execute and waiting for the
CPU. It is typically waiting in a queue and is eligible for execution.

3. Running: A thread in the running state is actively executing its code on the
CPU.

4. Blocked (or Waiting): When a thread cannot continue its execution due to the
need for some external event (e.g., I/O operation), it enters the blocked state
and is put on hold until the event occurs.

5. Terminated: When a thread completes its execution or is explicitly terminated,


it enters the terminated state. Resources associated with the thread are
released.

Thread Transitions:

Threads transition between these states based on various factors, including their
priority, the availability of CPU time, and external events. Thread scheduling
algorithms determine which thread runs next and aim to provide fair execution and
efficient resource utilization.

Thread Management:

Operating systems provide APIs and libraries to create, manage, and synchronize
threads. Popular programming languages like C, C++, Java, and Python have built-
in support for threading. Threads can communicate and synchronize their activities
using synchronization primitives like semaphores, mutexes, and condition variables.
Effective thread management is crucial for achieving concurrent execution in
applications, improving performance, and making efficient use of modern multicore
processors. However, it also introduces challenges related to synchronization, data
sharing, and avoiding race conditions.

Operating System 21
Thread Operation:
Thread operations are fundamental for creating, managing, and controlling threads
within a program or process. Here are the key thread operations:

1. Thread Creation:

To create a new thread, a program typically calls a thread creation function


or constructor provided by the programming language or threading library.
The new thread starts executing a specified function or method concurrently
with the calling thread.

2. Thread Termination:

Threads can terminate for various reasons, such as completing their tasks,
receiving a termination signal, or encountering an error. Proper thread
termination is essential to release resources and avoid memory leaks.

3. Thread Synchronization:

Thread synchronization is crucial to coordinate the execution of multiple


threads. Synchronization mechanisms like mutexes, semaphores, and

Operating System 22
condition variables are used to prevent race conditions and ensure orderly
access to shared resources.

4. Thread Joining:

A thread can wait for another thread to complete its execution by using a
thread join operation. This is often used to wait for the results of a thread's
work before continuing with the main thread.

5. Thread Detachment:

Threads can be detached from the calling thread, which allows them to
continue running independently. Detached threads automatically release
their resources when they terminate, without requiring the main thread to
join them.

6. Thread Prioritization:

Some threading models or libraries allow you to set thread priorities, which
influence the order in which threads are scheduled to run by the operating
system.

7. Thread Communication:

Threads communicate with each other by passing data or signals. Inter-


thread communication mechanisms include shared memory, message
queues, pipes, and other IPC methods.

Threading Models:
Threading models define how threads are created, scheduled, and managed within
a program or an operating system. Different threading models offer various
advantages and trade-offs, depending on the application's requirements. Here are
common threading models:

1. Many-to-One Model:

In this model, many user-level threads are mapped to a single kernel-level


thread. It is simple to implement and suitable for applications with infrequent
thread blocking.

Operating System 23
However, it doesn't fully utilize multiprocessor systems since a single thread
can run at a time.

2. One-to-One Model:

In the one-to-one model, each user-level thread corresponds to a separate


kernel-level thread. This model provides full support for multithreading and
can take advantage of multiprocessor systems.

It offers fine-grained control but may have higher overhead due to the
increased number of kernel threads.

3. Many-to-Many Model:

The many-to-many model combines characteristics of both the many-to-one


and one-to-one models. It allows multiple user-level threads to be
multiplexed onto a smaller number of kernel threads.

This model seeks to balance control and efficiency by allowing both user-
level and kernel-level threads.

4. Hybrid Model:

A hybrid threading model combines different threading approaches to take


advantage of both user-level and kernel-level threads. For example, it might
use one-to-one for CPU-bound threads and many-to-one for I/O-bound
threads.

Hybrid models aim to strike a balance between performance and resource


utilization.

The choice of a threading model depends on factors like the application's


requirements, the platform's support, and the trade-offs between control, resource
usage, and performance. It's essential to select the appropriate model to achieve
the desired concurrency and efficiency in a multithreaded application.

Processor Scheduling:
Processor scheduling is a core component of operating systems that manages the
execution of processes and threads on a CPU. It aims to allocate CPU time

Operating System 24
efficiently and fairly to multiple competing processes. Below, we'll explore various
aspects of processor scheduling in detail.

Scheduling Levels:
Scheduling levels, also known as scheduling domains, represent the different
stages at which scheduling decisions are made within an operating system. These
levels help determine which process or thread gets access to the CPU at any given
time. There are typically three primary scheduling levels:

1. Long-Term Scheduling (Job Scheduling):

Objective: The primary goal of long-term scheduling is to manage the


admission of new processes into the system, deciding which processes should
be loaded into memory for execution.

Role: Long-term scheduling selects processes from the job pool, which is a
queue of new processes waiting to enter the system.

Characteristics:

It determines when and how many processes should be admitted to the


system. This decision is influenced by factors like available memory, CPU
load, and process mix.

The long-term scheduler seeks to maintain a balance between CPU-bound


and I/O-bound processes to ensure efficient resource utilization.

It operates at a relatively slow pace, often measured in minutes or hours.

Illustration: Imagine an operating system receiving multiple requests to start


new applications. The long-term scheduler decides which of these applications
will be allowed to run in the system, considering the available resources and the
system's load.

2. Medium-Term Scheduling:

Objective: The medium-term scheduler focuses on managing the memory


resources of the system by deciding when to swap processes in and out of
memory.

Operating System 25
Role: This level of scheduling determines which processes that are already in
memory should be suspended (swapped out) to secondary storage or moved
back into memory (swapped in).

Characteristics:

It plays a vital role in managing memory effectively, ensuring that the


system remains responsive and doesn't become sluggish due to excessive
memory consumption.

Processes may be swapped out to create space for other processes,


particularly when they are blocked due to I/O operations or when memory is
under pressure.

Medium-term scheduling operates at a faster pace than long-term


scheduling, often measured in seconds or minutes.

Illustration: Consider a computer system running multiple applications. The


medium-term scheduler decides to temporarily swap out a background
application to free up memory for a foreground application that the user is
currently interacting with.

3. Short-Term Scheduling (CPU Scheduling):

Objective: Short-term scheduling, also known as CPU scheduling, determines


which process from the ready queue (a queue of processes ready to execute)
will be given access to the CPU.

Role: Its primary goal is to optimize CPU utilization, response time, and overall
system throughput.

Characteristics:

It operates at a very fast pace, with scheduling decisions made in


milliseconds or microseconds.

The short-term scheduler needs to make quick decisions based on factors


like process priorities, burst times, and fairness.

It aims to achieve a balance between processes to ensure that CPU time is


allocated efficiently, responsiveness is maintained, and all processes have

Operating System 26
an opportunity to run.

Illustration: In a time-sharing operating system, the short-term scheduler


continuously selects and allocates CPU time to processes based on factors like
priority, quantum (time slice), and the round-robin algorithm. This ensures that
all running processes get a fair share of CPU time.

In summary, scheduling levels are crucial for managing processes in an operating


system. Long-term scheduling handles the admission of new processes, medium-
term scheduling manages memory resources, and short-term scheduling optimizes
CPU utilization and responsiveness. Each level of scheduling has its unique
objectives and time scales, contributing to the overall efficiency and performance of
the system.

Preemptive Scheduling:
Preemptive scheduling is a scheduling policy where the operating system has the
authority to interrupt a running process and allocate the CPU to another process if a
higher-priority process becomes available or if a process exceeds its allocated time
slice (quantum). Preemptive scheduling ensures fairness, responsiveness, and
prioritization of tasks.

Characteristics of Preemptive Scheduling:

1. Fairness: Preemptive scheduling promotes fairness by ensuring that all


processes, especially high-priority ones, have the opportunity to run. Lower-
priority processes may be temporarily paused to give way to higher-priority
tasks.

2. Responsiveness: Preemptive scheduling allows high-priority processes to


quickly access the CPU, making it suitable for interactive tasks like user input
handling, real-time systems, and multitasking environments.

3. Scheduling Flexibility: Preemptive scheduling is flexible in terms of adapting


to the dynamic nature of process priorities and workloads. The scheduler can
easily accommodate changes in process states and priorities.

4. Overhead: Preemptive scheduling introduces additional overhead because the


operating system must manage the context switching process, which includes

Operating System 27
saving the state of the currently executing process and restoring the state of the
newly scheduled process.

5. Use Cases: Preemptive scheduling is well-suited for general-purpose operating


systems, interactive systems, and environments where timely response to
events is critical.

Examples of Preemptive Scheduling Policies:

Round Robin: A process is allocated a fixed time slice (quantum) of CPU time.
When the quantum expires, the process is preempted, and another process is
given the CPU.

Priority Scheduling: Processes are executed based on their priority levels,


with higher-priority processes preempting lower-priority ones.

Real-Time Scheduling: Hard real-time systems rely on preemptive scheduling


to ensure that critical tasks meet strict deadlines.

Non-Preemptive Scheduling:
Non-preemptive scheduling, also known as cooperative scheduling, allows a
process to continue running until it voluntarily releases the CPU by either blocking
(e.g., for I/O) or completing its execution. The operating system does not forcibly
interrupt a running process to allocate the CPU to another process. Instead, it relies
on the cooperation of processes.
Characteristics of Non-Preemptive Scheduling:

1. Simplicity: Non-preemptive scheduling is simpler to implement because it does


not involve frequent context switches or forced process interruptions.

2. Lower Overhead: Since there are no forced preemptions, non-preemptive


scheduling has lower overhead compared to preemptive scheduling. This can
be advantageous in resource-constrained environments.

3. Process Control: Non-preemptive scheduling allows a process to maintain


control of the CPU until it decides to yield, which can be useful for specific types
of applications or cooperative multitasking.

Operating System 28
4. Potential Responsiveness Issues: In non-preemptive scheduling, if a process
does not voluntarily yield the CPU, it can monopolize it, potentially causing
unresponsiveness in the system for other processes or tasks.

5. Use Cases: Non-preemptive scheduling is typically used in embedded


systems, cooperative multitasking environments, and certain real-time systems
where the timing and behavior of processes are highly predictable.

Examples of Non-Preemptive Scheduling Policies:

First-Come, First-Served (FCFS): Processes are executed in the order they


arrive, and they continue to run without preemption until they complete their
execution or block.

Shortest Job Next (SJN) or Shortest Job First (SJF): The process with the
shortest burst time is executed without preemption, allowing it to complete
before other processes are given the CPU.

In summary, the choice between preemptive and non-preemptive scheduling


depends on the specific requirements of the system and its workloads. Preemptive
scheduling offers fairness and responsiveness but introduces higher overhead,
while non-preemptive scheduling is simpler and may be suitable for specific
environments where processes cooperate effectively. The decision should align with
the system's goals and priorities.

Priorities in Scheduling:
In the context of processor scheduling, priorities play a crucial role in determining
the order in which processes or threads are granted access to the CPU.
Prioritization is used to manage the execution of processes based on their relative
importance or urgency. Let's delve into the concept of priorities in scheduling:

1. Importance of Priorities:

Priorities are assigned to processes or threads to reflect their significance within the
system. High-priority processes are given preference in CPU allocation, ensuring
that critical tasks are executed promptly. Here's how priorities are used and their
significance:

Operating System 29
Responsiveness: High-priority processes are scheduled more frequently,
ensuring that tasks with immediate user interaction or real-time requirements
receive timely CPU attention. This enhances system responsiveness and user
experience.

Resource Allocation: Priorities help allocate CPU resources efficiently.


Processes that require more CPU time or have higher system importance can
be assigned higher priorities.

Fairness: Prioritization ensures that processes with different levels of


importance coexist harmoniously. Lower-priority processes still get a chance to
execute, even though high-priority tasks receive preferential treatment.

2. Priority Levels:

Priority levels can vary from system to system, with different operating systems
using distinct scales to represent priorities. Common approaches include:

Absolute Priorities: In some systems, priorities are assigned as absolute


values, with higher numbers indicating higher priority. For example, a process
with priority 10 is more important than a process with priority 5.

Relative Priorities: In other systems, priorities are assigned relative to each


other, with lower numbers indicating higher priority. A process with priority 1 is
more important than a process with priority 5. This approach is sometimes used
to avoid confusion.

Priority Ranges: Some systems categorize processes into priority ranges,


such as "high," "medium," and "low." Each range represents a group of
priorities, simplifying the priority assignment process.

3. Static vs. Dynamic Priorities:

Priorities can be classified as static or dynamic:

Static Priorities: In static priority scheduling, priorities are assigned to


processes at the time of their creation and remain fixed throughout the
process's lifetime. Changes to priorities require manual intervention or
administrative actions.

Operating System 30
Dynamic Priorities: Dynamic priority scheduling allows priorities to change
during the execution of a process based on factors like aging, process behavior,
and resource usage. This approach adapts to the system's current workload
and requirements.

4. Priority Inversion:

Priority inversion is a situation in which a lower-priority process holds a resource


required by a higher-priority process. This can cause a priority inversion anomaly,
where the higher-priority process is effectively blocked by the lower-priority process.
To address this issue, priority inheritance or priority ceiling protocols are used to
temporarily boost the priority of the lower-priority process.

5. Use of Priorities in Scheduling Algorithms:

Various scheduling algorithms utilize priorities as a key criterion for making


scheduling decisions. Common priority-based scheduling policies include:

Priority Scheduling: This policy allocates CPU time to processes based on


their assigned priorities. Higher-priority processes are executed before lower-
priority ones.

Multilevel Queue Scheduling: Processes are categorized into multiple priority


queues, each with its own priority level. The scheduler selects the queue to
serve, and within the queue, processes are scheduled based on their priorities.

6. Priority Inheritance and Priority Ceiling Protocols:

To avoid priority inversion issues in real-time systems, priority inheritance and


priority ceiling protocols are employed. Priority inheritance temporarily boosts the
priority of a lower-priority process that holds a resource required by a higher-priority
process. Priority ceiling ensures that a resource is held by the highest-priority task
accessing it.

In summary, priorities are vital for managing process execution in scheduling. They
determine the order in which processes or threads are granted access to the CPU
and play a significant role in ensuring system responsiveness, resource allocation,
and fairness. Assigning and managing priorities effectively is crucial in optimizing
system performance and meeting specific application requirements.

Operating System 31
Scheduling Objectives and Scheduling Criteria:

Scheduling objectives and criteria are fundamental aspects of processor scheduling


in operating systems. They define the goals and parameters for making scheduling
decisions. Let's explore these concepts in detail:

Scheduling Objectives:
Scheduling objectives specify the high-level goals that a scheduling algorithm aims
to achieve. These objectives guide the scheduler in making decisions about which
process or thread to execute next. Common scheduling objectives include:

1. Fairness: Fairness in scheduling ensures that each process or thread gets a


fair share of the CPU's time. The objective is to distribute CPU time equitably
among competing processes, preventing any single process from monopolizing
resources.

2. Efficiency: Efficiency aims to maximize CPU utilization and system throughput.


An efficient scheduling algorithm should keep the CPU busy as much as
possible, minimizing idle time.

3. Responsiveness: Responsiveness focuses on minimizing response time for


interactive tasks. Interactive processes, such as user input handling, should
receive quick access to the CPU to provide a smooth user experience.

4. Predictability: Predictability is critical for real-time systems. The objective is to


ensure that tasks meet specific deadlines consistently. Predictable scheduling is
crucial in environments where timing requirements must be met without fail.

5. Throughput: Throughput measures the number of processes or threads


completed within a given time frame. High throughput is desired in scenarios
where the goal is to execute as many tasks as possible.

6. Resource Utilization: Resource utilization considers the efficient use of


resources beyond the CPU, such as memory and I/O devices. An optimal
scheduling algorithm should maximize the utilization of all system resources.

Operating System 32
7. Response Time: Response time is the time taken for a process to start
executing after it enters the ready queue. Minimizing response time is essential
for ensuring rapid task initiation.

Scheduling Criteria:
Scheduling criteria are specific parameters and attributes used to make scheduling
decisions. These criteria help the scheduler compare and prioritize processes
based on measurable factors. Common scheduling criteria include:

1. Burst Time: Burst time is the time a process spends running on the CPU
before it either blocks (for I/O or other reasons) or terminates. Shorter burst
times often indicate processes that can complete quickly.

2. Priority: Priority is an assigned value or level that reflects a process's


importance or urgency. Processes with higher priority are typically scheduled
before those with lower priority.

3. Waiting Time: Waiting time is the total time a process has spent waiting in the
ready queue before gaining access to the CPU. Reducing waiting time is often
a scheduling goal to improve system responsiveness.

4. Response Time: Response time measures how quickly an interactive process


receives the CPU after entering the ready queue. Minimizing response time is
essential for providing a responsive user experience.

5. Deadline: In real-time systems, processes may have specific deadlines by


which they must complete their execution. Adherence to deadlines is a critical
scheduling criterion in such environments.

6. Quantum (Time Slice): The quantum is the maximum amount of CPU time
allocated to a process in round-robin or time-sharing scheduling. Setting an
appropriate quantum helps balance fairness and system responsiveness.

7. I/O and CPU Burst Times: Different processes may have varying I/O burst
times and CPU burst times. Schedulers may prioritize I/O-bound processes to
improve overall system efficiency.

Operating System 33
8. Process Age and Aging: Aging is a dynamic criterion that increases the priority
of processes in the ready queue if they have been waiting for a long time. Aging
helps prevent processes from being indefinitely starved.

9. Execution State: Processes can be in different states, such as running,


blocked, or ready. Schedulers take these states into account when making
scheduling decisions. For example, blocked processes may be deprioritized,
and ready processes may be prioritized.

10. Resource Availability: The availability of resources, such as memory or


specific I/O devices, can impact scheduling decisions. Schedulers may prioritize
processes that are ready to use available resources.

In practice, different scheduling algorithms use a combination of these criteria to


make decisions that align with the system's objectives. The choice of scheduling
criteria depends on the specific requirements of the system, the workload, and the
desired system behavior.

Scheduling algorithms

https://www.youtube.com/watch?v=zFnrUVqtiOY&list=PLxCzCOWd7aiGz9d
onHRrE9I3Mwn6XdP8p&index=14&pp=iAQB

watch the playlist for this

1. First-Come, First-Served (FCFS) Scheduling:

Description: FCFS is one of the simplest scheduling algorithms. Processes are


executed in the order they arrive in the ready queue. Once a process starts
execution, it runs to completion.

Characteristics:

Easy to implement.

Operating System 34
May lead to poor CPU utilization if long processes arrive first (the "convoy
effect").

Example: Imagine three processes arriving in the order P1, P2, P3. They
execute sequentially, with P1 running to completion before P2 and P3 start.

2. Shortest Job Next (SJN) or Shortest Job First (SJF) Scheduling:

Description: SJN or SJF scheduling selects the process with the shortest burst
time for execution. This minimizes average waiting time.

Characteristics:

Efficient in terms of average waiting time.

Requires knowledge of the burst times, which is often not available in


practice.

Example: If processes P1, P2, and P3 have burst times of 5, 3, and 7,


respectively, SJN would execute P2, then P1, and finally P3.

3. Round Robin Scheduling:

Description: Round Robin (RR) is a preemptive scheduling algorithm that


allocates a fixed time slice (quantum) of CPU time to each process in a circular
order. If a process doesn't complete within its time quantum, it's moved to the
back of the queue.

Characteristics:

Ensures fairness by providing each process an equal opportunity to run.

Suitable for time-sharing systems.

Overhead increases with a smaller quantum.

Example: If processes P1, P2, and P3 each get a time quantum of 2, they take
turns executing in a cyclic manner, like P1 -> P2 -> P3 -> P1 -> P2 -> ...

4. Priority Scheduling:

Description: Priority scheduling allocates the CPU to processes based on their


priority levels. Higher-priority processes are executed before lower-priority

Operating System 35
ones.

Characteristics:

Ensures that important tasks receive preference.

Can lead to starvation if lower-priority tasks are indefinitely delayed.

Example: If P1 has higher priority than P2, the scheduler executes P1 before
P2.

5. Multilevel Queue Scheduling:

Description: In this approach, processes are divided into multiple priority


queues, each with its own scheduling algorithm. Each queue may have different
priority levels and scheduling policies.

Characteristics:

Supports a mix of scheduling policies within the system.

Commonly used in time-sharing systems.

Example: Processes are assigned to different queues based on their


characteristics. High-priority processes are scheduled using RR, while low-
priority ones are scheduled using FCFS.

6. Multilevel Feedback Queue Scheduling:

Description: This is an extension of multilevel queue scheduling with feedback.


Processes can move between queues based on their behavior and resource
requirements. The goal is to dynamically adapt to the needs of processes.

Characteristics:

Combines aspects of multilevel queue and round-robin scheduling.

Adapts well to varying workloads.

Example: Processes may start in a low-priority queue and move to higher-


priority queues if they use a lot of CPU time, indicating that they are compute-
bound.

7. Lottery Scheduling:

Operating System 36
Description: In lottery scheduling, each process is assigned a number of
lottery tickets. The scheduler selects a ticket at random, and the process
holding that ticket is granted access to the CPU.

Characteristics:

Provides a probabilistic approach to scheduling.

Allows for allocation of CPU time proportional to the number of tickets.

Example: If a process holds 10 out of 100 total tickets, it has a 10% chance of
being selected.

8. Real-Time Scheduling:

Description: Real-time scheduling is used in real-time systems where tasks


have strict timing requirements. It focuses on meeting task deadlines, and
processes are scheduled based on their importance and timing constraints.

Characteristics:

Critical for applications where missing deadlines can lead to failure.

Differentiated by hard real-time (strict deadlines) and soft real-time


(tolerates occasional deadline misses) systems.

Example: In a hard real-time system for controlling a medical device, the


scheduler ensures that critical control tasks meet their timing requirements.

These are some of the most common scheduling algorithms. The choice of
scheduling algorithm depends on the specific requirements of the system, the
nature of the workloads, and the desired system behavior. The selection of an
appropriate scheduling algorithm is crucial for optimizing system performance and
meeting the objectives and criteria set for scheduling.

Demand Scheduling:
Demand scheduling, also known as event-driven scheduling or on-demand
scheduling, is a scheduling mechanism where a process requests CPU time when it
needs it, rather than being allocated a fixed time slice or being scheduled by a pre-

Operating System 37
defined policy. This approach is often used in interactive and event-driven systems.
Here's how demand scheduling works:

Event-Driven: In demand scheduling, processes indicate when they are ready


to execute by generating events or requests. These events can be triggered by
user input, device signals, or other events that require immediate attention.

Resource Allocation: When an event is generated, the scheduler grants the


requesting process access to the CPU. This allocation is based on the principle
of serving processes on a first-come, first-served basis.

Response Time: Demand scheduling is well-suited for situations where low


response time is essential, such as in interactive systems. Processes that
generate events receive immediate attention and are executed promptly.

Examples: User interactions with graphical user interfaces (GUIs) often trigger
demand scheduling. When a user clicks a button or enters text, the associated
event handler is executed immediately.

Overheads: Demand scheduling can introduce some overhead, as the


scheduler must manage and respond to a potentially large number of events.
Ensuring fairness among competing processes can be challenging.

Real-Time Scheduling:
Real-time scheduling is used in systems with time-critical tasks where meeting
specific deadlines is crucial. These systems include applications like avionics,
industrial control systems, medical devices, and telecommunications. Real-time
scheduling is classified into two categories: hard real-time and soft real-time.

Hard Real-Time Scheduling:

In hard real-time systems, missing a task's deadline is unacceptable and


can lead to system failure. Schedulers are designed to ensure that critical
tasks meet their strict timing requirements.

The scheduler prioritizes tasks based on their importance and ensures that
high-priority tasks are executed before lower-priority ones. This may involve
preemptive scheduling.

Operating System 38
Examples include flight control systems, medical equipment, and
automotive safety systems.

Soft Real-Time Scheduling:

In soft real-time systems, occasional deadline misses are tolerable, and the
system can recover. While meeting deadlines is still a priority, there is some
flexibility.

The scheduler aims to maximize the number of deadlines met and minimize
the number of missed deadlines. Tasks are often assigned priorities based
on their timing constraints.

Examples include multimedia applications, online gaming, and streaming


services.

Deterministic Scheduling: Real-time scheduling algorithms aim for


determinism, ensuring that tasks are executed predictably and consistently. This
is essential for maintaining system reliability.

Examples: In a soft real-time system, video streaming services prioritize


delivering frames within a specific time window to provide a smooth user
experience. In a hard real-time system for a pacemaker, the scheduler ensures
that critical heart rhythm monitoring tasks are executed on time.

Real-time scheduling is challenging due to the need for precise timing and meeting
stringent deadlines. Schedulers in real-time systems often employ priority-based
algorithms, rate-monotonic scheduling, earliest deadline first (EDF), and other
techniques to ensure that critical tasks are executed on time and that system
performance is predictable and reliable.

In summary, demand scheduling is event-driven, suitable for interactive systems,


and grants CPU time when processes request it. Real-time scheduling is critical for
time-critical applications, with hard real-time systems having strict timing constraints
and soft real-time systems allowing some flexibility in meeting deadlines. Real-time
scheduling aims for determinism and reliability in task execution.

Operating System 39
Unit 2
Process Synchronization: Mutual Exclusion
critical section :- It is a part of a program where shared
Mutual Exclusion:
resources can be accessed by multiple processes.
Definition: Mutual exclusion is a fundamental concept in process
synchronization that ensures that only one process or thread can access a
critical section of code or a shared resource at a time, preventing concurrent
access and potential data corruption or race conditions.

Objective: To provide a mechanism that allows processes to coordinate and


take turns safely accessing shared resources, thereby avoiding conflicts and
ensuring data consistency.

Implementation: Mutual exclusion can be achieved through various


synchronization mechanisms, including locks, semaphores, and atomic
operations.

Key Considerations:

Processes or threads that require access to a critical section must request


permission (lock) before entering.

If another process holds the lock, the requesting process must wait until the
lock is released.

Once a process exits the critical section, it releases the lock, allowing
another process to enter.

Common Techniques for Achieving Mutual Exclusion:

1. Locks:

Mutex (Mutual Exclusion) locks are a popular mechanism for achieving


mutual exclusion.

A process or thread acquires the lock before entering a critical section and
releases it upon exiting.

Locks ensure that only one thread can hold the lock at a time.

Operating System 40
2. Semaphores:

Semaphores are more versatile synchronization objects, but they can also
be used to implement mutual exclusion.

A binary semaphore, often referred to as a mutex semaphore, can be used


as a simple lock.

When a process wants to enter a critical section, it attempts to acquire the


semaphore, and if it's already held, the process is blocked.

3. Atomic Operations:

In some cases, atomic operations provided by the hardware or


programming language can be used to implement mutual exclusion.

For example, compare-and-swap (CAS) operations can be used to


atomically update shared data while ensuring mutual exclusion.

Benefits of Mutual Exclusion:

Prevents race conditions: Ensures that only one process can access critical
sections, preventing conflicts and data corruption.

Promotes data consistency: Mutual exclusion helps maintain the integrity of


shared data by preventing concurrent writes or reads that could lead to
inconsistency.

Coordination: Facilitates cooperation among processes, ensuring orderly


access to shared resources.

Challenges and Considerations:

Deadlock: Care must be taken to avoid situations where processes wait


indefinitely for a lock that will not be released.

Starvation: Ensuring fairness in granting access to the critical section to prevent


certain processes from being blocked indefinitely.

Performance: Overusing mutual exclusion can lead to reduced parallelism and


system performance.

Operating System 41
In summary, mutual exclusion is a fundamental concept in process synchronization
that ensures that only one process can access a critical section or shared resource
at any given time. Achieving mutual exclusion is crucial to prevent race conditions,
maintain data consistency, and promote orderly access to shared resources.
Various synchronization mechanisms, including locks, semaphores, and atomic
operations, are used to implement mutual exclusion in concurrent programs.

Software Solutions to the Mutual Exclusion Problem:


To achieve mutual exclusion in concurrent programs, several software-based
solutions can be employed. These solutions help ensure that only one process or
thread can access a critical section of code or shared resource at a time. Here are
some common software-based methods for achieving mutual exclusion:

1. Locks (Mutexes):

Description: Locks, also known as mutexes (short for mutual exclusion), are
one of the most widely used software solutions for achieving mutual exclusion.

How It Works: A lock is associated with a critical section of code or a shared


resource. Processes or threads must acquire the lock before entering the
critical section. If the lock is already held by another process, the requesting
process will be blocked until the lock is released.

Implementation: Locks can be implemented using various programming


languages and libraries. Common examples include pthread_mutex in C/C++ or
Lock objects in Java.

2. Semaphores:

Description: Semaphores are synchronization objects used for various


purposes, including achieving mutual exclusion.

How It Works: A binary semaphore, often referred to as a mutex semaphore,


can be used to implement mutual exclusion. The semaphore's value is
initialized to 1. Processes or threads attempt to acquire the semaphore. If the
value is 1, they proceed; otherwise, they are blocked until the semaphore value
is decremented to 0 when acquired.

Operating System 42
Implementation: Semaphores can be found in many programming languages
and libraries, such as sem_init in C/C++ or Semaphore objects in Java.

3. Test-and-Set (TAS) and Compare-and-Swap (CAS) Operations:

Description: Hardware-supported atomic operations like Test-and-Set (TAS)


and Compare-and-Swap (CAS) can be used to implement mutual exclusion.

How It Works: These operations enable a process to atomically update a


shared variable while also acquiring the lock to enter a critical section. TAS sets
a flag to indicate that the lock is acquired, and CAS updates the flag if it hasn't
changed since the last read.

Implementation: TAS and CAS operations are often available as hardware


instructions and can be used to build custom mutual exclusion mechanisms in
low-level languages like C and assembly.

4. Peterson's Algorithm:

Description: Peterson's algorithm is a classic software-based solution for


achieving mutual exclusion between two processes.

How It Works: Two processes coordinate by taking turns accessing a shared


resource. They use two shared boolean variables and a turn variable to signal
when they want to enter the critical section. Each process enters the critical
section in turn while respecting the other's request.

Implementation: Peterson's algorithm can be implemented in languages that


support shared memory and interprocess communication.

5. Lamport's Bakery Algorithm:

Description: Lamport's Bakery algorithm is a software solution for achieving


mutual exclusion among multiple processes.

How It Works: Processes take numbers as "tickets" upon entering a queue.


The process with the smallest ticket number gets access to the critical section.
If multiple processes request access simultaneously, the one with the lowest
ticket number enters first.

Operating System 43
Implementation: Lamport's Bakery algorithm is typically implemented in
shared-memory systems with proper atomic operations.

These software solutions to the mutual exclusion problem provide mechanisms for
controlling access to critical sections of code and shared resources in concurrent
programs. The choice of which solution to use depends on the programming
language, the platform, and the specific requirements of the application.

Hardware Solutions to the Mutual Exclusion


Problem:
Hardware solutions to the mutual exclusion problem involve using specialized
hardware instructions or mechanisms provided by the hardware architecture to
ensure exclusive access to shared resources. These hardware solutions can be
more efficient and reliable compared to purely software-based approaches. Here
are some hardware mechanisms commonly used to achieve mutual exclusion:

1. Test-and-Set (TAS) Instruction:

Description: The Test-and-Set (TAS) instruction is a hardware operation that


atomically reads the current value of a memory location and sets it to a
predetermined value (usually 1). It returns the previous value.

How It Works: To achieve mutual exclusion, processes or threads can use TAS
to acquire a lock. If the previous value is 0, it means the lock was successfully
acquired, and the process can enter the critical section. If the previous value is
1, the process is blocked until the lock is released.

Benefits: TAS is a low-level and efficient hardware instruction for implementing


mutual exclusion without the need for busy-waiting or spinlocks.

Drawbacks: TAS instructions might not be available on all hardware


architectures.

2. Compare-and-Swap (CAS) Operation:

Description: The Compare-and-Swap (CAS) operation is another hardware


instruction that allows atomic modification of a memory location based on a
comparison.

Operating System 44
How It Works: To achieve mutual exclusion, CAS is used to attempt to update
a lock variable. If the current value matches the expected value, CAS sets the
new value and returns whether the operation was successful. Processes can
use CAS to compete for and acquire locks.

Benefits: CAS provides a flexible mechanism for implementing mutual


exclusion and other synchronization primitives, making it a powerful tool for
concurrent programming.

Drawbacks: The availability and behavior of CAS may vary across different
hardware architectures.

3. Atomic Fetch-and-Increment (Fetch-and-Add) Operation:

Description: The atomic fetch-and-increment (or fetch-and-add) operation is a


hardware instruction that atomically increments the value of a memory location
and returns the original value.

How It Works: Processes or threads can use fetch-and-increment to acquire a


unique ticket or token that represents their place in a queue. This token can be
used to grant access to a shared resource, ensuring mutual exclusion.

Benefits: It's useful for implementing mechanisms like Lamport's Bakery


algorithm for mutual exclusion.

Drawbacks: It may not be as widely supported as CAS or TAS on all hardware


platforms.

4. Locking Instructions:

Description: Some modern CPUs provide specific instructions for locking


memory regions, ensuring atomic access.

How It Works: Locking instructions allow a process to acquire exclusive access


to a shared resource or critical section without the need for additional software-
level synchronization. They guarantee that no other process can access the
locked memory region simultaneously.

Benefits: Locking instructions are highly efficient and eliminate the need for
busy-waiting or spinlocks.

Operating System 45
Drawbacks: The availability of locking instructions may be limited to specific
CPU architectures and may not be portable.

These hardware solutions leverage low-level instructions and operations provided


by the hardware to guarantee mutual exclusion in concurrent programs. They can
significantly improve the efficiency and reliability of mutual exclusion mechanisms.
However, the specific hardware support available and the ease of implementation
may vary across different hardware platforms.

Semaphores
Semaphores are a synchronization mechanism used in concurrent programming to
control access to shared resources or coordinate the execution of multiple
processes or threads. They were introduced by Edsger Dijkstra in 1965 and have
become an essential tool in managing mutual exclusion and synchronization.
Semaphores are particularly valuable in situations where mutual exclusion and
coordination are required. Here's an overview of semaphores:

1. Binary Semaphores:

Binary Semaphore: A binary semaphore is a synchronization variable with two


possible values, typically 0 and 1. It is often used as a lock or mutex to control
access to a shared resource. The operations on a binary semaphore are as
follows:

Wait (P) Operation: Decrements the semaphore value. If the value is


already 0, the calling process or thread is blocked until another process
increments the semaphore (release operation).

Signal (V) Operation: Increments the semaphore value. If any processes


or threads are waiting (blocked) due to a "wait" operation, one of them is
unblocked.

2. Counting Semaphores:

Counting Semaphore: A counting semaphore is a synchronization variable


with an integer value that can range over an unrestricted domain. It is used to
manage resources or processes with multiple instances. The operations on a
counting semaphore are the same as for binary semaphores.

Operating System 46
Wait (P) Operation: Decrements the semaphore value. If the value is
already 0, the calling process or thread is blocked until another process
increments the semaphore (release operation).

Signal (V) Operation: Increments the semaphore value. If any processes


or threads are waiting (blocked) due to a "wait" operation, one of them is
unblocked.

3. Semaphore Operations:

Wait (P) Operation: The "P" operation (short for "proberen," which means "to
test" in Dutch) is used to request access to a semaphore. If the semaphore's
value is greater than zero, it is decremented, and the process continues
execution. If the value is zero, the process is blocked.

Signal (V) Operation: The "V" operation (short for "verhogen," which means "to
increment" in Dutch) is used to release a semaphore. It increments the
semaphore's value. If there are any blocked processes or threads waiting for
the semaphore, one of them is unblocked.

4. Use Cases for Semaphores:

Mutual Exclusion: Binary semaphores can be used to ensure that only one
process or thread accesses a critical section at a time.

Producer-Consumer Problem: Counting semaphores can be used to manage


the production and consumption of items in a buffer shared by multiple
producers and consumers.

Reader-Writer Problem: Semaphores can be used to manage concurrent read


and write access to a shared data structure, allowing multiple readers or a
single writer access at a time.

Resource Pool Management: Counting semaphores can control the allocation


and release of resources like network connections, database connections, or
threads in a thread pool.

Process Synchronization: Semaphores can coordinate the execution of


multiple processes or threads to ensure they perform tasks in a synchronized
manner.

Operating System 47
5. Benefits of Semaphores:

Semaphores provide a high level of abstraction for synchronization and make it


easier to implement complex synchronization patterns.

They can be used to handle various synchronization scenarios, from simple


mutual exclusion to more intricate coordination problems.

Semaphores are available in many programming languages and operating


systems, making them a widely supported synchronization tool.

6. Drawbacks of Semaphores:

Incorrect usage of semaphores can lead to deadlocks or other synchronization


issues. Care must be taken to avoid common pitfalls.

Semaphores may be less intuitive to use than higher-level synchronization


primitives, like locks or condition variables.

In summary, semaphores are a versatile and widely used synchronization


mechanism in concurrent programming. They provide a flexible way to control
access to shared resources and coordinate the execution of processes or threads,
making them an essential tool for managing synchronization and ensuring the
orderly execution of concurrent programs.

Critical Section Problems


Critical section problems refer to a class of synchronization issues in concurrent
programming where multiple processes or threads attempt to access shared

Operating System 48
resources, data, or code, and conflicts can occur. The goal is to ensure that only
one process at a time can execute a critical section of code or access a shared
resource to prevent data corruption, race conditions, and other synchronization
problems. Here are some key aspects of critical section problems:

1. Critical Section:

A critical section is a segment of code or a portion of a program where shared


resources, data, or variables are accessed, modified, or updated. The execution
of this section must be mutually exclusive, meaning only one process or thread
can access it at a time.

2. Requirements for Correctness:

In order to achieve correctness and prevent synchronization issues, critical


section problems must satisfy three key requirements:

Mutual Exclusion: Only one process can execute the critical section at any
given time.

Progress: If no process is executing in its critical section and there are


processes that wish to enter the critical section, then only those processes
not in the remainder section can participate in the decision on who should
enter their critical section next, and this selection cannot be postponed
indefinitely.

Bounded Waiting: There exists a bound on the number of times that other
processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is
granted.

3. Common Approaches to Solving Critical Section Problems:

Various synchronization mechanisms and algorithms are used to address


critical section problems. Some of the common approaches include:

Locks: Mutexes (mutual exclusion locks) are used to ensure that only one
process or thread can hold the lock at a time, providing mutual exclusion.

Operating System 49
Semaphores: Binary semaphores or counting semaphores can be used to
coordinate access to critical sections by allowing or blocking processes
based on the semaphore's value.

Condition Variables: Condition variables are used to signal and wait for
specific conditions to be met before accessing critical sections, often in
combination with locks.

Monitors: Monitors are high-level synchronization constructs that


encapsulate both data and procedures, simplifying the management of
critical sections.

Atomic Operations: Low-level atomic operations like Test-and-Set (TAS)


and Compare-and-Swap (CAS) can be used to ensure mutual exclusion.

4. Classic Synchronization Problems:

Some classic synchronization problems illustrate the challenges and solutions


related to critical sections, including the Producer-Consumer problem, the
Reader-Writer problem, and the Dining Philosophers problem.

5. Deadlock and Starvation:

Improper management of critical sections can lead to deadlock, a situation


where processes or threads are blocked and unable to proceed because they
are waiting for resources that are held by other processes in a circular manner.

Starvation can also occur when processes or threads are unfairly treated in
resource allocation, resulting in some processes being delayed indefinitely in
entering their critical sections.

In summary, critical section problems are central to ensuring the orderly execution
of concurrent programs. By applying synchronization mechanisms and algorithms
that satisfy the requirements of mutual exclusion, progress, and bounded waiting,
developers can address these issues and prevent synchronization problems such
as data corruption, race conditions, and deadlocks.

Case Study: The Dining Philosophers Problem

Operating System 50
Problem Description:
The Dining Philosophers problem is a classic synchronization problem that
illustrates the challenges of resource allocation and concurrency in a multi-process
or multi-threaded environment. The problem is often framed as follows:

There are five philosophers sitting around a circular dining table.

Each philosopher thinks and eats. To eat, a philosopher must pick up both the
left and right forks.

Philosophers can only pick up one fork at a time, and they can only eat when
they have both forks.

The goal is to design a solution that allows the philosophers to eat without
leading to deadlocks or other synchronization issues.

Solution:

Several solutions can address the Dining Philosophers problem, ensuring that all
philosophers can eat while avoiding deadlocks. One common solution involves
using semaphores and mutex locks:

Each fork is represented as a semaphore. At the beginning, all semaphores


(forks) are initialized with a count of 1.

Each philosopher is a process or thread, and they follow these steps:

1. Think: The philosopher thinks for a random amount of time.

2. Pick up forks: To eat, a philosopher must pick up both the left and right
forks. To do this, they acquire the semaphores (forks). If both forks are
available, the philosopher picks them up.

3. Eat: The philosopher eats for a random amount of time.

4. Put down forks: After eating, the philosopher releases the forks (releases
the semaphores) for others to use.

Challenges and Considerations:

One key challenge is to avoid deadlocks. This can be achieved by introducing


an order in which the philosophers pick up the forks. For example, if all

Operating System 51
philosophers pick up the left fork first and then the right fork, one philosopher
can pick up the right fork before the left, breaking the circular dependency.

Care must be taken to ensure that the critical section (the process of picking up
and putting down forks) is properly synchronized with mutex locks to avoid race
conditions.

Balancing the use of forks is important to ensure that all philosophers get a
chance to eat.

Case Study: The Barber Shop Problem


Problem Description:
The Barber Shop problem is another classic synchronization problem that simulates
the behavior of a barber and customers in a barber shop. The problem is framed as
follows:

There is one barber and a waiting room with limited capacity for customers.

Customers enter the barber shop and either find an available seat in the waiting
room or leave if it's full.

The barber serves one customer at a time. If there are no customers, the barber
sleeps. When a customer arrives, they wake the barber.

The goal is to design a solution that simulates this behavior while ensuring that
customers are served in an orderly manner.

Solution:
Solving the Barber Shop problem requires coordination and synchronization to
manage customers and the barber's activities. One common solution involves using
semaphores and mutex locks:

A semaphore is used to represent the number of available seats in the waiting


room. Initially, it's set to the maximum number of seats.

A mutex lock is used to control access to shared resources, such as the waiting
room and the barber's chair.

Customers follow these steps:

Operating System 52
1. If a seat is available in the waiting room, they take a seat. If all seats are
occupied, they leave.

2. If the barber is sleeping, they wake the barber.

3. They sit in the barber's chair if it's available (mutex lock).

4. They get a haircut.

5. They leave the shop (releasing the mutex lock).

The barber follows these steps:

1. If there are no customers, the barber sleeps.

2. When a customer arrives, the barber is woken up.

3. The barber serves the customer.

4. If there are more customers, the barber continues serving.

Challenges and Considerations:

Managing the waiting room capacity is crucial to avoid overcrowding or


underutilization of the barber's services.

Proper synchronization is needed to prevent race conditions, ensuring that


customers don't enter the barber's chair simultaneously and that the barber
serves one customer at a time.

The coordination between customers and the barber, as well as the


management of the barber's sleeping state, is essential for a successful
solution.

These two case studies highlight the complexity of synchronization problems in


concurrent programming and the need for careful design and coordination to ensure
orderly execution while avoiding issues such as deadlocks, race conditions, and
overcrowding.

Memory Organization
Memory organization is a fundamental concept in computer systems and computer
architecture. It refers to how a computer's memory is structured, managed, and

Operating System 53
used to store and retrieve data and instructions. Memory organization plays a
critical role in the performance and functionality of a computer system. Here's an
overview of memory organization:

1. Memory Hierarchy:

Modern computer systems often feature a memory hierarchy consisting of


different levels of memory, each with its own characteristics in terms of capacity,
access time, and cost. The memory hierarchy typically includes the following
levels:

Registers: These are the smallest, fastest, and most closely located to the
CPU. Registers store data that the CPU is actively processing.

Cache Memory: Cache memory is a small, high-speed memory located


between the CPU and main memory (RAM). It serves as a buffer for
frequently accessed data and instructions, improving the system's speed
and performance.

Main Memory (RAM): RAM is the primary volatile memory used to store
data and instructions that the CPU needs to access quickly during program
execution.

Secondary Storage (Hard Drives, SSDs): Secondary storage provides a


non-volatile, high-capacity memory for long-term data storage. It is used to
store operating systems, applications, files, and other data.

2. Address Space:

The memory of a computer system is organized into addressable locations,


often referred to as memory cells or memory addresses. The range of possible
memory addresses is known as the address space.

The size of the address space is determined by the number of addressable


memory locations and the number of bits used to represent memory addresses.
For example, a 32-bit system can address 2^32 (4,294,967,296) memory
locations.

The address space is divided into various regions for different purposes,
including program memory, data storage, and system memory.

Operating System 54
3. Memory Units:

Memory is typically organized into smaller units, such as bytes. Each memory
unit is identified by a unique address. In modern systems, the basic unit of data
storage is the byte, which is composed of 8 bits.

4. Memory Types:

Memory can be categorized into different types based on its characteristics and
usage. Common memory types include:

RAM (Random Access Memory): RAM is used for temporary data storage
during program execution. It provides fast read and write access.

ROM (Read-Only Memory): ROM stores non-volatile data, such as


firmware and system software. It is typically read-only and cannot be
modified by the user.

Cache Memory: Cache memory is a small but extremely fast memory used
to store frequently accessed data.

Virtual Memory: Virtual memory is a memory management technique that


uses a portion of secondary storage as an extension of RAM to provide
more addressable memory space.

5. Memory Management:

Memory management is the process of allocating and deallocating memory as


needed by programs and the operating system. It includes tasks such as
managing memory segments, ensuring data security, and optimizing memory
usage.

6. Memory Access:

Memory access involves reading data from or writing data to memory. The
speed and efficiency of memory access are critical for the overall performance
of a computer system.

7. Address Mapping:

Address mapping is the process of mapping logical memory addresses to


physical memory locations. This mapping is crucial for enabling programs to

Operating System 55
access the correct memory locations.

8. Memory Protection:

Memory protection mechanisms prevent unauthorized access to specific


memory regions. They are essential for maintaining system security and
preventing one program from interfering with the memory of another.

Memory organization is a complex and essential aspect of computer systems,


influencing both their performance and functionality. Effective memory organization
ensures efficient data storage and retrieval, supports multitasking, and plays a vital
role in the overall user experience. Understanding memory organization is crucial
for computer architects, software developers, and system administrators.

Memory Hierarchy
The memory hierarchy is a key concept in computer architecture and design,
outlining the various levels of memory in a computer system, each with distinct
characteristics and purposes. The memory hierarchy is structured in a way that
optimizes data access speed and storage capacity while managing costs. Here's an
overview of the memory hierarchy:

1. Registers:

Description: Registers are the smallest and fastest storage units in the
memory hierarchy. They are located within the CPU itself.

Purpose: Registers store data and instructions that the CPU is actively
processing. They are used for rapid data access and temporary storage during
computation.

Characteristics: Registers have very fast access times, but their capacity is
extremely limited. They typically store small amounts of data, such as CPU
registers like the program counter and general-purpose registers.

2. Cache Memory:

Description: Cache memory is a high-speed, small-capacity memory located


between the CPU and main memory (RAM).

Operating System 56
Purpose: Cache memory serves as a buffer for frequently accessed data and
instructions. It helps improve the CPU's speed and performance by reducing the
time it takes to access data.

Characteristics: Cache memory is faster than main memory but has limited
capacity. It operates on the principle of temporal and spatial locality, storing
frequently accessed data to reduce the need for slower access to main memory.

Levels: Cache memory is typically organized into multiple levels, including L1


(closest to the CPU), L2, and sometimes L3 caches. Each level is larger but
slower than the previous one.

3. Main Memory (RAM):

Description: Main memory, often referred to as RAM (Random Access


Memory), is the primary volatile memory in a computer system.

Purpose: RAM stores data and instructions that the CPU needs to access
quickly during program execution. It serves as a bridge between the high-speed
cache and the long-term storage of secondary storage devices.

Characteristics: RAM provides a larger storage capacity compared to cache


memory but is slower. Data stored in RAM is volatile, meaning it is lost when
the computer is powered off.

4. Secondary Storage (Hard Drives, SSDs):

Description: Secondary storage devices include hard disk drives (HDDs) and
solid-state drives (SSDs).

Purpose: These devices provide non-volatile, high-capacity storage for long-


term data storage. They hold the operating system, applications, files, and other
data.

Characteristics: Secondary storage devices have a much larger capacity than


RAM but are significantly slower. Data stored on secondary storage remains
intact even when the computer is turned off, making it suitable for long-term
storage.

5. Tertiary Storage:

Operating System 57
Description: Tertiary storage typically includes slower and higher-capacity
storage solutions like optical discs (e.g., CDs, DVDs) and tape drives.

Purpose: Tertiary storage is used for archival purposes and long-term data
backup. It is slower to access than secondary storage.

Characteristics: These storage media have lower access speeds compared to


hard drives and SSDs but offer large storage capacities.

6. Remote Storage:

Description: Remote storage refers to data storage located on remote servers


or in the cloud.

Purpose: Remote storage is used for data backup, sharing, and access from
multiple devices. It provides redundancy and data availability from various
locations.

Characteristics: Access to remote storage depends on network speed and


latency. It offers scalable and distributed storage solutions.

The memory hierarchy is designed to balance the trade-offs between speed,


capacity, and cost. The hierarchy allows the CPU to access data as quickly as
possible by prioritizing the use of the smallest, fastest, and most expensive storage
at the top of the hierarchy (registers and cache), while also providing larger, slower,
and more cost-effective storage options lower down in the hierarchy (main memory,
secondary storage, tertiary storage, and remote storage). This structure is essential
for optimizing the performance of computer systems while efficiently managing data
storage.

Operating System 58
Memory Management Strategies
Memory management is a critical aspect of computer systems, responsible for
organizing and allocating memory efficiently. It ensures that programs and data are
loaded into the computer's memory and that they are accessible to the CPU when
needed. Here are some key memory management strategies:

1. Single Contiguous Allocation:

Description: In single contiguous allocation, an entire program is loaded into a


single, contiguous block of memory. This method is suitable for small systems
with limited memory.

Advantages: Simple and easy to implement.

Disadvantages: It limits the use of memory, as programs must fit within a


single contiguous block. Fragmentation issues can arise, leading to wasted
memory space.

2. Partitioned Allocation:

Description: In partitioned allocation, memory is divided into fixed-size or


variable-size partitions. Each partition can hold one program. Multiple programs
can run concurrently, each in its allocated partition.

Operating System 59
Advantages: Efficient use of memory, and multiple programs can be run
simultaneously.

Disadvantages: Fixed-size partitions may lead to internal fragmentation


(wasted memory within a partition). Variable-size partitions can be complex to
manage.

3. Paging:

Description: Paging divides physical memory and virtual memory into fixed-
size blocks called pages. Similarly, programs are divided into fixed-size blocks
called frames. The OS manages a page table to map virtual pages to physical
frames.

Advantages: Eliminates fragmentation issues, simplifies memory allocation,


and allows efficient use of memory.

Disadvantages: May lead to external fragmentation (unused memory between


non-contiguous blocks). Page table management can be resource-intensive.

4. Segmentation:

Description: Segmentation divides memory into logical segments based on the


program's structure. Each segment is assigned specific permissions and can
grow or shrink as needed.

Advantages: Supports logical organization of memory and dynamic memory


allocation. Enhances security.

Disadvantages: May still suffer from fragmentation, both internal and external.
Segment management can be complex.

5. Virtual Memory:

Description: Virtual memory extends the available physical memory by using


disk storage as a "backing store." Only a portion of a program is loaded into
physical memory when it's needed, while the rest resides on disk.

Advantages: Efficient use of physical memory, enables running large


programs, and simplifies memory management.

Operating System 60
Disadvantages: Disk access is slower than physical memory, which can lead to
performance issues when swapping data between memory and disk.

6. Demand Paging:

Description: Demand paging is a variant of virtual memory that brings pages


into physical memory only when they are needed (on-demand).

Advantages: Minimizes initial memory usage and enhances overall system


efficiency.

Disadvantages: May incur page faults (performance hits) when a required


page is not in physical memory.

7. Thrashing:

Description: Thrashing occurs when the system spends more time swapping
pages between memory and disk than executing processes. It is a performance
bottleneck caused by excessive page faults.

Prevention: To prevent thrashing, the OS may use various strategies, such as


increasing physical memory, optimizing paging algorithms, and controlling the
number of concurrent processes.

8. Swapping:

Description: Swapping involves moving entire processes (or parts of them)


between physical memory and secondary storage (disk) to free up memory for
other processes.

Advantages: Helps manage memory efficiently by moving less frequently used


processes to disk.

Disadvantages: Swapping can be slow due to disk I/O, leading to performance


issues.

9. Compaction:

Description: Compaction is a technique used to reduce external fragmentation.


It involves shifting memory contents to eliminate small gaps between allocated
memory blocks.

Operating System 61
Advantages: Reduces external fragmentation, making more memory available
for new processes.

Disadvantages: Compaction can be time-consuming, especially when a


significant amount of memory must be moved.

Effective memory management is crucial for optimizing system performance and


ensuring efficient utilization of resources. The choice of memory management
strategy depends on the system's architecture, available hardware, and specific
requirements of the operating system and applications.

Contiguous Memory Allocation vs. Non-Contiguous


Memory Allocation
Memory allocation is a fundamental concept in computer systems that determines
how memory is assigned to processes or programs. Contiguous and non-
contiguous memory allocation are two different approaches to this task, each with
its own advantages and disadvantages.

Contiguous Memory Allocation:

In contiguous memory allocation, each process is allocated a single, continuous


block of memory. Here are the key characteristics:

1. Contiguous Blocks: Each process is loaded into a single, continuous block of


memory. This block is typically allocated from the lower end of memory to the
upper end.

2. Simple Management: Contiguous allocation is straightforward to manage, as


each process occupies a single memory block.

3. Fragmentation: External fragmentation can occur as processes are loaded and


removed, leaving gaps in memory that might not be large enough to
accommodate new processes.

4. Memory Protection: It's relatively easy to implement memory protection, as


each process is isolated in its own block of memory.

5. Limited Address Space: Contiguous allocation may limit the address space
available for a single process, as the size of a process cannot exceed the size

Operating System 62
of the largest available contiguous block.

6. Used in Real-time Systems: Contiguous allocation is commonly used in real-


time systems where memory fragmentation must be minimized, and memory
allocation times need to be predictable.

Non-Contiguous Memory Allocation:

In non-contiguous memory allocation, processes are allocated memory in a


fragmented or scattered manner, with each segment of a process stored in different
locations within the physical memory. Here are the key characteristics:

1. Non-Contiguous Segments: A process is divided into segments, and these


segments can be placed at different locations in memory.

2. Efficient Use of Memory: Non-contiguous allocation can lead to efficient use of


memory, as it can help reduce external fragmentation.

3. Memory Protection: Memory protection can be more complex to implement,


as different segments of a process may need different protection levels.

4. Dynamic Growth: Processes can grow or shrink dynamically by allocating


additional segments of memory as needed.

5. Address Space Flexibility: Non-contiguous allocation allows for more flexible


use of the address space, as it can accommodate larger processes than the
available contiguous blocks.

6. Used in Modern Operating Systems: Non-contiguous allocation is commonly


used in modern operating systems that support multitasking and virtual memory.

Comparison:

Fragmentation: Contiguous allocation is more prone to external fragmentation,


while non-contiguous allocation can reduce external fragmentation by allowing
flexible placement of memory segments.

Memory Efficiency: Non-contiguous allocation is generally more memory-


efficient as it allows for dynamic allocation and better utilization of memory.

Address Space: Non-contiguous allocation offers more flexibility for processes


to grow and use larger address spaces.

Operating System 63
Complexity: Non-contiguous allocation can be more complex to manage,
particularly in terms of memory protection and the management of separate
segments.

Use Cases: Contiguous allocation is common in real-time and embedded


systems where fragmentation must be minimized. Non-contiguous allocation is
prevalent in modern desktop and server operating systems that support
multitasking and virtual memory.

The choice between contiguous and non-contiguous memory allocation depends on


the specific requirements of the system, including the need for memory efficiency,
address space flexibility, and the ability to manage external fragmentation.

Partition Management Techniques


Partition management techniques are strategies for dividing a computer's memory
into partitions to efficiently allocate memory resources to processes or programs.
These techniques are crucial for optimizing memory usage in computer systems.
Here are some common partition management techniques:

1. Fixed Partitioning:

Description: Fixed partitioning involves dividing physical memory into fixed-


size partitions or regions.

How It Works: Each partition is of a predetermined size, and processes are


loaded into these partitions. If a process's size is smaller than the partition, it is
allocated to the partition. If it is larger, it must wait for a partition of sufficient
size.

Advantages: Simple to implement and provides for clear separation of


processes.

Disadvantages: Can lead to internal fragmentation (wasted memory within a


partition) and may not be flexible for varying process sizes.

2. Dynamic Partitioning:

Description: Dynamic partitioning, also known as variable partitioning, divides


physical memory into variable-sized partitions.

Operating System 64
How It Works: When a process is loaded, it is allocated the exact amount of
memory it needs, and the partition size can vary. As processes enter and exit
the system, partitions are created and destroyed as needed.

Advantages: Efficient use of memory as no space is wasted due to


fragmentation. Suitable for systems with varying process sizes.

Disadvantages: May be complex to manage partition creation and destruction,


especially for memory management algorithms.

3. Buddy System:

Description: The buddy system is a variation of dynamic partitioning where


memory is divided into blocks that are powers of 2.

How It Works: Memory is split into blocks of sizes like 1 KB, 2 KB, 4 KB, and
so on. When a process requests memory, it is allocated a block that matches or
exceeds its size, and any excess space is divided recursively until an exact
match is found.

Advantages: Minimizes fragmentation and allows for efficient allocation of


memory.

Disadvantages: Memory allocation and deallocation can be complex due to the


recursive nature of the algorithm.

4. Paging:

Description: Paging divides both physical and virtual memory into fixed-size
blocks called pages.

How It Works: Processes are divided into fixed-size blocks called frames. The
operating system maintains a page table to map virtual pages to physical
frames.

Advantages: Eliminates fragmentation issues and allows for efficient memory


allocation.

Disadvantages: May lead to external fragmentation (unused memory between


non-contiguous blocks). Page table management can be resource-intensive.

5. Segmentation:

Operating System 65
Description: Segmentation divides memory into logical segments based on a
program's structure. Each segment is assigned specific permissions and can
grow or shrink as needed.

How It Works: Memory is divided into segments like code, data, and stack.
Processes can request additional memory segments as needed.

Advantages: Supports logical organization of memory and dynamic memory


allocation. Enhances security.

Disadvantages: May still suffer from fragmentation, both internal and external.
Segment management can be complex.

6. Swapping:

Description: Swapping involves moving entire processes (or parts of them)


between physical memory and secondary storage (disk) to free up memory for
other processes.

How It Works: Processes are temporarily moved to secondary storage when


they are not actively running. They are brought back into memory when
needed.

Advantages: Helps manage memory efficiently by moving less frequently used


processes to disk.

Disadvantages: Swapping can be slow due to disk I/O, leading to performance


issues.

The choice of partition management technique depends on the system's


requirements, including the need for memory efficiency, address space flexibility,
and the ability to manage fragmentation. Different techniques are suitable for
different scenarios, and the specific system's architecture and resources will
influence the choice.

Logical Address Space vs. Physical Address Space


In computer systems, both logical and physical address spaces are essential
concepts, and they play different roles in memory management and addressing.

Operating System 66
Here's a breakdown of the differences between logical and physical address
spaces:

Logical Address Space:

1. Definition: The logical address space, also known as virtual address space, is
the set of addresses generated by a program during its execution. These
addresses are typically generated by the CPU as the program runs and are
used by the program to reference memory locations.

2. Visibility: Logical addresses are visible to the application or program running


on the computer. The program uses logical addresses when accessing data
and instructions.

3. Size: The logical address space is often larger than the physical address
space. It can be as large as the range of values that can be held by the data
type used to represent addresses (e.g., 32-bit or 64-bit).

4. Protection: Logical addresses can be subject to memory protection


mechanisms. The operating system uses page tables and other mechanisms to
control access to different parts of the logical address space to prevent
unauthorized memory access.

5. Dynamic: Logical addresses can be dynamic and change as the program


executes. For instance, in virtual memory systems, the program may generate
logical addresses that do not correspond directly to physical memory locations.

6. Used in Multitasking: Logical addresses are commonly used in multitasking


and virtual memory systems where multiple processes run concurrently, and the
operating system must manage the allocation of physical memory to various
processes.

Physical Address Space:

1. Definition: The physical address space is the set of addresses that directly
correspond to locations in the computer's physical memory hardware (RAM).
These addresses are used by the memory management unit (MMU) to fetch
data from or store data to physical memory chips.

Operating System 67
2. Visibility: Physical addresses are not visible to the application or program. The
program running on the computer deals with logical addresses, and the
translation to physical addresses is handled by the operating system and
hardware.

3. Size: The physical address space is limited by the amount of physical RAM
installed in the computer. It typically ranges from a few gigabytes to terabytes,
depending on the system's hardware.

4. Protection: Protection mechanisms apply to the physical address space as


well, but they are managed by the operating system and hardware, not directly
accessible to the program.

5. Static: Physical addresses are static and do not change during program
execution. They map directly to physical memory locations.

6. Used by Memory Hardware: Physical addresses are used by the memory


management unit (MMU) to access data stored in physical memory chips
(RAM).

Address Translation:
The relationship between logical and physical addresses is managed through
address translation. The MMU, which is part of the CPU, translates logical
addresses generated by the program into physical addresses that are used to
access memory. This translation is a crucial part of virtual memory systems,
allowing programs to operate with a larger logical address space than the available
physical memory.

In summary, logical and physical address spaces serve different purposes in


memory management. Logical addresses are generated by programs, provide
flexibility, and are subject to protection mechanisms. Physical addresses
correspond directly to physical memory locations and are managed by the hardware
and operating system. Address translation bridges the gap between these two
address spaces in virtual memory systems.

Swapping

Operating System 68
Swapping is a memory management technique used in computer systems to
efficiently utilize physical memory (RAM) and provide the illusion of having more
memory than is physically available. It involves moving data between the RAM and
secondary storage (usually a hard disk or SSD) to ensure that the most actively
used processes and data are in RAM, while less active or unused portions are
temporarily stored on the disk. Here's how swapping works and its key aspects:
How Swapping Works:

1. Page Replacement: Swapping is closely related to paging, a memory


management technique that divides both physical and virtual memory into fixed-
size blocks called pages. When a process generates a page fault (an attempt to
access a page that is not in physical memory), the operating system must fetch
the required page from secondary storage.

2. Page Replacement Algorithm: The operating system uses a page


replacement algorithm to determine which pages in physical memory to swap
out to make space for the incoming page. Common page replacement
algorithms include LRU (Least Recently Used), FIFO (First-In-First-Out), and
others.

3. Data Transfer: When a page is selected for swapping, it is moved to secondary


storage, freeing up space in physical memory. If a page needs to be brought
into physical memory, the operating system retrieves it from secondary storage
and loads it into an available memory location.

4. Disk Swap Space: The portion of secondary storage used to temporarily store
swapped-out pages is called the "swap space." This space must be large
enough to accommodate the pages that are not in physical memory.

Key Aspects of Swapping:

1. Performance Impact: Swapping can impact system performance, as


reading/writing to secondary storage is much slower than accessing data in
RAM. Excessive swapping can lead to performance degradation.

2. Page Faults: The frequency of page faults is a key factor in determining when
and how often swapping occurs. A system with a high rate of page faults may
experience more swapping.

Operating System 69
3. Optimizing Swapping: Properly configuring the system's virtual memory
settings, such as the size of physical memory, the size of swap space, and the
page replacement algorithm, is crucial for optimizing swapping and avoiding
performance bottlenecks.

4. Memory Overcommitment: Swapping allows for memory overcommitment,


where the sum of the memory allocated to running processes exceeds the
physical RAM capacity. However, excessive overcommitment can lead to
excessive swapping and reduced performance.

5. Use Cases: Swapping is commonly used in modern operating systems,


especially when implementing virtual memory systems. It allows systems to run
multiple processes concurrently and efficiently manage memory, even when
physical memory is limited.

6. Operating System Control: The operating system is responsible for managing


swapping operations. It controls which pages are swapped in and out, manages
the swap space, and makes decisions based on the system's memory
management policies.

In summary, swapping is a memory management technique that plays a crucial role


in ensuring that a computer's available physical memory is used efficiently. It allows
systems to run multiple processes concurrently, even when the physical memory is
insufficient to hold all of them. However, excessive swapping can negatively impact
performance, so careful system configuration and management are essential for
optimal operation.

Paging
Paging is a memory management technique used in computer systems, particularly
in virtual memory systems, to efficiently manage and access memory. It involves
dividing both physical and virtual memory into fixed-size blocks called pages.
Paging allows for several benefits, including efficient memory allocation, memory
protection, and the illusion of a larger address space. Here's an overview of how
paging works and its key aspects:

How Paging Works:

Operating System 70
1. Page Size: Paging divides memory into fixed-size blocks known as pages. The
page size is typically a power of 2, like 4 KB or 4 MB. Both the physical memory
(RAM) and virtual memory (address space) are divided into pages.

2. Address Translation: When a program generates a memory reference (read


or write), the generated memory address includes a page number and an offset
within the page. The page number is used to index a page table, which maps
virtual pages to physical frames.

3. Page Table: The page table is a data structure maintained by the operating
system. It contains entries that map virtual page numbers to physical frame
numbers. Each process has its own page table, which is used for address
translation. The operating system is responsible for creating and managing
these page tables.

4. Page Faults: If a program tries to access a page that is not in physical memory
(a page fault occurs), the operating system must fetch the required page from
secondary storage (usually a hard disk) into an available physical frame. This
process is known as page swapping.

5. Protection: Paging supports memory protection by ensuring that a process


cannot access memory outside its allocated address space. Unauthorized
memory access attempts result in a segmentation fault or page fault.

Key Aspects of Paging:

1. Memory Management: Paging efficiently manages memory allocation by


dividing it into fixed-size pages. This eliminates external fragmentation, as
pages can be allocated and deallocated independently of one another.

2. Address Space: Paging provides the illusion of a large address space for each
process. The actual physical memory can be smaller than the virtual address
space, allowing programs to access more memory than is physically available.

3. Page Replacement: When physical memory is full, the operating system uses
a page replacement algorithm (e.g., LRU, FIFO) to determine which page to
swap out to secondary storage to make room for a new page. This is crucial for
maintaining the illusion of a larger address space.

Operating System 71
4. Security and Isolation: Paging allows for memory protection and isolation.
Each process has its own address space, and the page table ensures that
processes cannot access each other's memory.

5. Operating System Control: The operating system is responsible for managing


paging operations. This includes maintaining the page tables, handling page
faults, and deciding which pages to swap in and out of physical memory.

6. Performance Impact: Paging can impact system performance, as fetching


pages from secondary storage is slower than accessing data in RAM. Careful
management and optimization are required to minimize performance
bottlenecks.

Paging is a widely used memory management technique in modern computer


systems, as it offers a balance between efficient memory allocation and memory
protection. It enables systems to run multiple processes concurrently and provides
the flexibility to access a larger virtual address space, even when physical memory
is limited.

Operating System 72
Segmentation
Segmentation is a memory management technique used in computer systems to
divide the memory into logical segments, each of which can be assigned specific
attributes and permissions. Segmentation provides a more flexible and structured
approach to memory organization compared to other techniques like contiguous
memory allocation. Here's an overview of how segmentation works and its key
aspects:

How Segmentation Works:

1. Segment Definitions: Memory is divided into different logical segments, each


identified by a name or a label. Common segment types include code segment,
data segment, stack segment, and more.

Operating System 73
2. Segment Attributes: Each segment can be assigned specific attributes, such
as read-only, read-write, or execute permissions. These attributes dictate how
the segment can be accessed and modified.

3. Segment Limits: Segments have defined limits, indicating the size or extent of
each segment. The limits specify the range of addresses that belong to the
segment.

4. Segment Addresses: The operating system maintains a segment table, which


maps segment names to their base addresses and limits. When a program
references a memory location, it specifies the segment and an offset within that
segment.

5. Address Translation: Address translation involves mapping a logical address


(segment and offset) to a physical memory address. The operating system uses
the segment table to perform this translation.

6. Protection: Segmentation supports memory protection by enforcing the


permissions associated with each segment. Unauthorized memory access
attempts result in segmentation faults.

Key Aspects of Segmentation:

1. Logical Organization: Segmentation provides logical organization of memory,


making it easier to manage different types of data and code. For example,
code, data, and stack segments are clearly defined.

2. Memory Protection: Segmentation enforces memory protection by limiting


access to specific segments. This ensures that a program cannot inadvertently
or maliciously access or modify memory outside its designated segments.

3. Flexibility: Segmentation allows processes to dynamically allocate and


deallocate segments as needed, making it suitable for more dynamic memory
allocation scenarios.

4. Security: Segmentation enhances security by isolating different parts of a


program or different programs in separate segments. Unauthorized access to
one segment does not affect other segments.

Operating System 74
5. Complexity: Managing segments and their attributes can be more complex
than traditional memory allocation methods. The operating system must
maintain segment tables and perform address translations.

6. Use Cases: Segmentation is commonly used in operating systems that support


structured and secure memory management, such as those used in modern
desktop and server environments.

7. Operating System Control: The operating system manages segment tables,


enforces protection, and handles address translations. It is responsible for
setting up and managing segments.

Segmentation is a powerful memory management technique, offering flexibility,


security, and structured organization for memory. It is often used in modern
operating systems to provide memory isolation and protection among different
processes and to ensure that each process's data and code are organized in
separate, logically defined segments.

Operating System 75
Segmentation with Paging Virtual Memory: Demand
Paging
Segmentation and paging are two distinct memory management techniques.
However, in some modern computer systems, they are combined to create a more
flexible and efficient memory management scheme. When segmentation and
paging are used together with demand paging, it provides benefits in terms of
memory allocation, protection, and the illusion of a larger address space. Here's an
explanation of how these techniques work together with a focus on demand paging:

Segmentation and Paging Overview:

1. Segmentation: As mentioned earlier, segmentation divides memory into logical


segments, each with a specific name, attributes, and permissions. Each

Operating System 76
segment may be used for different types of data or code, making memory
management more structured and secure.

2. Paging: Paging divides both physical and virtual memory into fixed-size blocks
called pages. Paging eliminates external fragmentation and allows for the
efficient allocation and deallocation of memory.

Demand Paging:

Demand paging is a technique that allows pages of a program or process to be


loaded into physical memory only when they are needed. It's a part of virtual
memory management that aims to minimize the initial memory requirements and to
make more efficient use of physical memory. Here's how demand paging works in
the context of segmentation and paging:

1. Segmentation with Paging: In a system that combines segmentation and


paging, each segment is further divided into pages. This means that the logical
segments are not allocated in a contiguous manner but are paged to better
utilize memory.

2. Address Translation: When a program references a specific memory location,


it generates a logical address that includes the segment name, page number,
and offset within the page.

3. Page Table: The operating system maintains page tables for each segment.
These page tables map virtual page numbers to physical frame numbers. Each
segment has its page table.

4. Demand Paging: Pages are not loaded into physical memory at the start of
program execution. Instead, they are loaded into memory on-demand when
they are accessed by the program. When a page fault occurs (i.e., a requested
page is not in physical memory), the page is brought from secondary storage
(e.g., a hard disk) to physical memory.

5. Performance Optimization: To optimize demand paging, the system may use


various page replacement algorithms (e.g., LRU, FIFO) to determine which
pages should be swapped out to secondary storage to make room for the
incoming page.

Operating System 77
6. Security and Protection: Segmentation and paging together allow for strong
memory protection and isolation. Each segment/page can have specific
attributes and permissions, and unauthorized memory access results in
segmentation and paging faults.

7. Use Cases: This combined approach is commonly used in modern operating


systems to provide memory protection, isolation, efficient use of physical
memory, and the ability to run multiple processes concurrently.

8. Operating System Control: The operating system manages the page tables
for each segment, performs address translations, and handles page faults. It is
responsible for setting up and managing segments and pages.

The combination of segmentation and paging with demand paging allows for a more
flexible, efficient, and secure memory management scheme. It's suitable for
multitasking environments where multiple processes run concurrently, and memory
protection is crucial. This approach ensures that processes can have a structured
and isolated memory layout while benefiting from the efficient use of physical
memory through demand paging.

Page Replacement and Page-Replacement


Algorithms
In virtual memory systems, page replacement is a fundamental concept that comes
into play when physical memory (RAM) becomes fully occupied, and the operating
system needs to swap pages in and out of memory to make room for new pages.
Page-replacement algorithms are used to determine which pages should be
removed from physical memory and replaced with new ones. These algorithms help
optimize memory utilization and system performance. Here's an explanation of page
replacement and various page-replacement algorithms:

Page Replacement:

1. Page Table: In a virtual memory system, a page table is used to map logical
pages in a program's address space to physical frames in RAM.

2. Page Fault: When a program accesses a page that is not currently in physical
memory, a page fault occurs. This means the required page must be brought in

Operating System 78
from secondary storage (e.g., a hard disk) into an available physical frame.

3. Full Memory: When physical memory is fully occupied, and a new page needs
to be brought in, an existing page must be evicted (swapped out) to make room
for the new page.

4. Page Replacement Algorithm: The choice of which page to replace is


determined by a page replacement algorithm, which aims to minimize the
impact on system performance and maintain the illusion of a larger address
space.

Page-Replacement Algorithms:

Several page-replacement algorithms have been developed to determine which


page should be replaced. These algorithms have different characteristics,
advantages, and disadvantages. Some common page-replacement algorithms
include:

1. FIFO (First-In-First-Out): This algorithm replaces the oldest page in memory,


based on the order of pages' arrival in physical memory. It's simple to
implement but may not always provide the best performance, as it may not
consider the actual usage of pages.

2. LRU (Least Recently Used): LRU replaces the page that has not been used
for the longest time. It requires maintaining a linked list or a counter for each
page to track their usage history. While it provides better performance, it can be
complex to implement efficiently.

3. Optimal: The optimal algorithm selects the page that will not be used for the
longest time in the future. This algorithm provides the best possible
performance but is not practical in real systems because it requires knowledge
of future page accesses, which is impossible to predict.

4. LFU (Least Frequently Used): LFU replaces the page that has been used the
least number of times. It tracks the usage count of each page. LFU can be
efficient in some scenarios but may not always provide the best results.

5. Second-Chance: This algorithm is a variation of the FIFO algorithm with a


modified twist. It gives pages a second chance to stay in memory before they

Operating System 79
are replaced. If a page is accessed again before replacement, it is kept in
memory.

6. Clock (or Second-Chance): The clock algorithm is a simplified version of the


second-chance algorithm. It uses a circular list of pages and provides a second
chance to pages before replacement.

7. NRU (Not Recently Used): NRU divides pages into four categories (based on
the recent usage and modification bits), and it replaces a page randomly from
the lowest non-empty category.

8. LFU (Least Frequently Used): This algorithm replaces the page with the
lowest usage count. It aims to keep the pages that are accessed most
frequently in memory.

9. MFU (Most Frequently Used): MFU replaces the page that has the highest
usage count. It attempts to keep the pages that are most heavily used in
memory.

The choice of which page-replacement algorithm to use depends on the specific


system and its performance requirements. Each algorithm has its own trade-offs in
terms of complexity and efficiency. The selection of an appropriate algorithm can
significantly impact system performance, especially in virtual memory systems
where page replacement is a common occurrence.

Performance of Demand Paging


Demand paging is a virtual memory management technique used in modern
computer systems to efficiently utilize physical memory and provide the illusion of a
larger address space. While demand paging offers several advantages, its
performance can vary based on system configuration, page-replacement
algorithms, and workload. Here's an overview of the performance aspects of
demand paging:

Advantages:

1. Efficient Memory Utilization: Demand paging allows processes to start


execution with only a small portion of their code and data loaded into physical

Operating System 80
memory. This initial loading minimizes the memory footprint and allows the
system to run more processes simultaneously.

2. Support for Large Address Spaces: Demand paging makes it feasible to


support programs with larger address spaces than the available physical
memory. This can be crucial for running memory-intensive applications.

3. Quick Program Start: Processes can start running quickly because they don't
need to load their entire code and data into memory before execution. Only the
initially needed pages are loaded on-demand.

4. Memory Isolation: Each process's memory is isolated from other processes.


Unauthorized memory access or modification is prevented through memory
protection mechanisms.

Challenges:

1. Page Faults: A major factor affecting the performance of demand paging is the
frequency of page faults. Frequent page faults, especially when combined with
slow disk I/O operations, can degrade system performance.

2. Page Replacement Overhead: The choice of page-replacement algorithm can


impact the performance of demand paging. Some algorithms, while providing
better overall memory utilization, may require additional computational
overhead.

3. Disk I/O Latency: The performance of demand paging is highly sensitive to the
speed of secondary storage (e.g., hard disks or SSDs). Slow disk I/O can lead
to significant delays in page retrieval and swapping, negatively impacting
performance.

4. Fragmentation: Demand paging can introduce internal fragmentation, as not


all pages are fully utilized. When pages are loaded into memory, there may be
unused portions in a page. Over time, this can lead to memory fragmentation.

Optimizing Demand Paging Performance:

To improve the performance of demand paging, several strategies can be


employed:

Operating System 81
1. Page Replacement Algorithms: Selecting an efficient page replacement
algorithm can significantly impact page fault rates. Algorithms like LRU (Least
Recently Used) and LFU (Least Frequently Used) aim to reduce the number of
page faults.

2. Page Size: The choice of page size can affect the granularity of page swaps.
Smaller pages reduce internal fragmentation but may lead to more frequent
page faults.

3. Secondary Storage Performance: Faster secondary storage devices, such as


SSDs, can reduce disk I/O latency and improve demand paging performance.

4. Memory Size: Increasing physical memory can reduce the frequency of page
faults because more pages can be kept in RAM. Adequate RAM is essential for
efficient demand paging.

5. Tuning: System administrators can fine-tune the operating system's virtual


memory settings to strike a balance between performance and efficient memory
use.

In summary, the performance of demand paging is influenced by various factors,


including page fault rates, page-replacement algorithms, secondary storage
performance, and available physical memory. Careful system configuration and
optimization are essential to strike the right balance between efficient memory
utilization and system performance. A well-configured system can provide the
benefits of demand paging while minimizing its potential drawbacks.

Thrashing
Thrashing is a term used in the context of computer systems and memory
management to describe a situation where the system is excessively swapping data
between physical memory (RAM) and secondary storage (e.g., hard disk or SSD). It
occurs when the demand for memory by processes exceeds the available physical
memory, leading to a constant cycle of page swapping. Thrashing significantly
degrades system performance and can lead to a "death spiral" where the system
becomes virtually unresponsive. Here's a more detailed explanation of thrashing:

Causes of Thrashing:

Operating System 82
1. Insufficient Physical Memory: The primary cause of thrashing is an
inadequate amount of physical memory (RAM) to meet the memory
requirements of the running processes. When RAM is fully occupied, the
operating system starts swapping pages to secondary storage to make room for
new pages. This page swapping can become a vicious cycle if physical memory
remains overwhelmed.

2. Overcommitment: Some systems allow memory overcommitment, which


means allocating more memory to processes than is physically available. While
overcommitment can be useful to a certain extent, excessive overcommitment
can lead to thrashing.

3. High Page-Fault Rate: A high rate of page faults, caused by processes


frequently accessing pages that are not in physical memory, can trigger
thrashing. Frequent page faults result in continuous disk I/O operations, further
reducing system performance.

Characteristics of Thrashing:

1. Excessive Disk Activity: The most noticeable symptom of thrashing is a


continuous, high level of disk activity. The system is constantly swapping pages
in and out of physical memory, which can cause significant delays and slow
down all operations.

2. Severe Performance Degradation: Thrashing leads to a severe degradation in


system performance. The CPU spends most of its time swapping pages,
leaving little processing power for actual tasks.

3. Low CPU Utilization: Paradoxically, during thrashing, CPU utilization is often


low because the CPU is not actively processing tasks. Instead, it is mostly
waiting for pages to be swapped in or out of memory.

4. Unresponsive System: In extreme cases, a system experiencing thrashing


can become unresponsive. Even basic user interactions like moving the mouse
or typing on the keyboard may be delayed or not register at all.

Mitigating and Preventing Thrashing:

Operating System 83
1. Add More Physical Memory: The most straightforward solution to thrashing is
to increase the amount of physical memory in the system, ensuring that there is
enough RAM to accommodate the demands of running processes.

2. Optimize Memory Usage: Careful memory management practices, including


configuring page sizes and minimizing overcommitment, can help prevent
thrashing.

3. Use Efficient Page-Replacement Algorithms: Implementing efficient page-


replacement algorithms like LRU (Least Recently Used) or LFU (Least
Frequently Used) can reduce the impact of thrashing by making more intelligent
decisions about which pages to swap.

4. Monitor System Performance: Regularly monitor system performance,


memory utilization, and page-fault rates to detect signs of thrashing. If detected
early, proactive measures can be taken to prevent it.

5. Reduce Concurrent Processes: Reducing the number of concurrently running


processes or processes with high memory demands can alleviate memory
pressure.

Thrashing is a detrimental condition that significantly impairs system performance.


Addressing thrashing requires a combination of adequate hardware resources,
efficient memory management, and effective monitoring to prevent its occurrence.

Operating System 84
Demand Segmentation and Overlay Concepts
Demand segmentation and overlay are memory management techniques that were
primarily used in early computer systems to optimize memory utilization in situations
where physical memory (RAM) was limited. These techniques allowed programs to
execute efficiently despite the constraints of limited memory resources. Here's an
explanation of demand segmentation and overlay concepts:

Demand Segmentation:

1. Segmentation: As explained earlier, segmentation involves dividing memory


into logical segments, each with specific attributes and permissions. Demand
segmentation extends this concept to handle situations where a program's
logical segments are larger than the available physical memory.

2. Paging within Segments: In demand segmentation, each segment is further


divided into pages. Paging allows for the efficient allocation and deallocation of
memory within each segment. When a program references a specific address
within a segment, the operating system determines which page of the segment
is being accessed.

Operating System 85
3. On-Demand Loading: Demand segmentation takes the concept of "on-
demand loading" to the segment level. Initially, only a small portion of each
segment is loaded into physical memory. As the program runs, additional pages
are loaded into memory only when needed. This approach minimizes the initial
memory requirements for a program and allows it to execute with limited RAM.

4. Protection and Isolation: Segmentation with demand paging maintains


memory protection and isolation, ensuring that different segments are protected
from unauthorized access.

Overlay Concepts:

1. Overlay: Overlay programming is a technique used to work around memory


constraints in early computer systems. It allows a program to use more memory
than could fit in physical memory by dividing the program into multiple sections
called overlays.

2. Overlay Manager: An overlay manager is a program or system component


responsible for managing overlays. It loads and unloads overlays into physical
memory as needed during program execution.

3. Overlay Switching: As the program runs, the overlay manager switches


between different overlays, making only one overlay's code and data available
in physical memory at any given time. The system maintains an overlay table
that keeps track of which overlay is currently in use.

4. Efficient Memory Use: Overlay programming was a way to maximize memory


usage by only loading the portions of a program that were actively being
executed. Overlays enabled programs to be much larger and more complex
than the amount of physical memory would have allowed.

Comparison:

Demand Segmentation and Overlay are techniques used for managing


memory in systems with limited physical memory.

Demand segmentation focuses on dividing a program into segments and then


further dividing those segments into pages, with the ability to load pages on-
demand.

Operating System 86
Overlay programming, on the other hand, splits a program into separate
overlays, with an overlay manager responsible for swapping them in and out of
physical memory.

Demand segmentation provides a more structured approach to memory


management, with segments and pages. Overlay programming is a more
historical technique used in early systems.

Demand segmentation is more commonly associated with modern virtual


memory systems, while overlay programming was more prevalent in older
systems.

In modern computer systems, demand segmentation is less commonly used due to


the availability of larger amounts of physical memory and more sophisticated
memory management techniques. Overlay programming is largely obsolete in
modern systems, as contemporary hardware and software can efficiently handle
memory constraints without the need for overlays.

Unit 3
Deadlocks
Deadlock in operating systems occurs when two or more processes are unable to
proceed because each is waiting for the other to release resources. This situation
leads to a standstill where none of the processes can continue their execution.

Conditions for Deadlock:

1. Mutual Exclusion: At least one resource must be held in a non-sharable mode.


Only one process can use the resource at a time.

2. Hold and Wait: Processes already holding resources may request new
resources while waiting for others to be released.

3. No Preemption: Resources cannot be forcibly taken away from a process; they


must be released voluntarily.

Operating System 87
4. Circular Wait: There must exist a circular chain of two or more processes, each
waiting for a resource held by the next one in the chain.

Examples of Deadlock:

1. Resource Allocation Deadlock: Suppose there are two processes, A and B,


and two resources, R1 and R2.

Process A acquires R1 and waits for R2.

Process B acquires R2 and waits for R1.

Neither process can proceed as they're each holding a resource the other
needs.

2. Dining Philosophers Problem: Involves multiple philosophers sitting around a


table with chopsticks between them.

Each philosopher needs two chopsticks to eat.

If every philosopher picks up the chopstick on the right simultaneously, they


will all be waiting indefinitely for the chopstick on their left, resulting in a
deadlock.

3. Resource Allocation Graph (RAG): In a system with multiple resources and


processes, a cycle in the RAG signifies deadlock.

Nodes represent processes or resources, and edges represent requests or


assignments.

If a cycle exists in the graph where each process is waiting for a resource
held by another process in the cycle, it indicates a deadlock.

4. Operating System Resources: Deadlocks can occur with system resources


such as printers, files, memory, etc.

For instance, if process A has exclusive access to Printer 1 and needs


Printer 2, while process B has exclusive access to Printer 2 and needs
Printer 1, a deadlock can occur if neither releases their current resource.

Preventing or avoiding deadlocks involves various techniques such as resource


allocation strategies, deadlock detection, avoidance algorithms, and recovery

Operating System 88
mechanisms. These approaches aim to either eliminate one of the deadlock
conditions or manage resources in a way that avoids the possibility of a deadlock
occurring.

Deadlock Solution
There are several approaches to solve or prevent deadlocks in operating systems.
Each method aims to eliminate one or more of the necessary conditions for a
deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular
wait.

Deadlock Prevention:
1. Mutual Exclusion Avoidance:

Sometimes, it's possible to eliminate the mutual exclusion condition by


allowing resources to be shareable rather than exclusive. However, this
may not be feasible for all types of resources.

2. Hold and Wait Avoidance:

One way to prevent hold and wait is to require processes to request all their
required resources simultaneously, rather than one at a time.

Alternatively, a process can release all its currently held resources before
requesting new ones, reducing the likelihood of hold and wait scenarios.

3. No Preemption:

Preemption involves forcefully taking resources from a process when


needed by another. If possible, implement a mechanism where resources
can be preempted to prevent deadlock. However, this might not be
applicable in all scenarios.

4. Circular Wait Avoidance:

Enforce a strict ordering of resource requests. This involves numbering


resources and requiring processes to request resources in ascending order.

Deadlock Avoidance with Banker's Algorithm:

Operating System 89
The Banker's Algorithm is used to avoid deadlock by ensuring that the system
doesn’t enter an unsafe state where it can't satisfy the resource requests of all
processes. It works by analyzing the maximum, allocated, and available resources.

Banker's Algorithm Steps:


1. Initialization:

At the beginning or during system initialization, the system needs to know


the total available resources in the system and the maximum resources
each process might need.

It maintains data structures to keep track of resources allocated to


processes and available resources.

2. Resource Requests:

When a process requests resources, the system checks if granting those


resources will lead to an unsafe state or a deadlock.

If the request doesn't push the system into an unsafe state, the resources
are allocated. Otherwise, the process is made to wait until the state
becomes safe again.

3. Safety Check:

The system performs a safety check before granting resources to any


process.

It simulates the allocation to check if the system will remain in a safe state
after granting the resources.

If the system can satisfy all pending requests, it's considered safe to
allocate resources; otherwise, the process must wait.

Deadlock Detection:
Resource Allocation Graph (RAG):
1. Constructing the RAG:

Operating System 90
Nodes represent processes and resources.

Directed edges represent resource requests and resource allocations.

2. Cycle Detection:

Detect cycles in the graph.

If a cycle exists, it indicates a deadlock situation.

3. Algorithm Execution:

Periodically execute the detection algorithm to identify any potential


deadlocks.

Deadlock Recovery:
Recovery Methods:
1. Process Termination:

Identify processes involved in the deadlock.

Terminate one or more processes to break the deadlock cycle.

2. Resource Preemption:

Revoke resources from one or more processes involved in the deadlock.

Allocate these resources to other waiting processes to resolve the


deadlock.

3. Rollback:

Roll back the system to a previously consistent state.

Revert the state of the system to a point where the deadlock didn't exist.

Conclusion:

These techniques—Banker's Algorithm for avoidance, Resource Allocation Graph


for detection, and termination, preemption, or rollback for recovery—play crucial
roles in managing and preventing deadlocks in operating systems. The choice of
method depends on system requirements, efficiency considerations, and the

Operating System 91
criticality of the processes involved. Employing a combination of these techniques
helps ensure system stability and prevent the detrimental effects of deadlocks on
system performance.

Resource concepts
In operating systems, resources refer to any entity that is used by a process and
can be requested, allocated, or manipulated during program execution. These
resources can be categorized into two main types: hardware resources and
software resources.

Hardware Resources:
1. CPU (Central Processing Unit):

The CPU is the primary hardware resource responsible for executing


instructions of a process.

Processors can be single-core or multi-core, determining the number of


tasks that can be executed concurrently.

2. Memory (RAM - Random Access Memory):

Memory is used to store program instructions and data currently being


processed by the CPU.

It's divided into various segments like stack, heap, and code segment.

3. I/O Devices (Input/Output):

Includes devices such as keyboards, monitors, printers, disks, network


interfaces, etc.

These devices allow interaction between the computer and the external
world.

4. Storage Devices:

Hard disks, solid-state drives (SSDs), and other storage mediums store
data persistently even when the computer is turned off.

Used for long-term storage of programs, files, and system data.

Operating System 92
5. Network Resources:

Network devices and interfaces, including routers, switches, cables, and


wireless adapters, facilitate communication between computers.

Software Resources:
1. Files and Databases:

Files are collections of data stored on storage devices.

Databases organize and store data for efficient retrieval and manipulation.

2. Programs and Executables:

Executable files and programs are software resources that execute specific
tasks on a computer system.

3. Memory Space:

Address spaces or memory segments allocated to each process for


program execution.

4. Synchronization Objects:

Semaphores, mutexes, locks, and monitors are synchronization primitives


used to control access to shared resources and prevent race conditions.

Resource Allocation and Management:


1. Resource Allocation:

The process of assigning resources to processes based on their requests


and needs.

Allocation strategies include contiguous or non-contiguous allocation for


memory, scheduling algorithms for CPU, etc.

2. Resource Management:

Involves techniques to efficiently utilize and control resources to prevent


issues like deadlocks, resource contention, and starvation.

Operating System 93
Techniques include deadlock avoidance, detection, and recovery
mechanisms.

3. Resource Sharing and Protection:

Operating systems manage shared resources among multiple processes


while ensuring protection against unauthorized access.

Access control mechanisms like permissions and privileges are used to


regulate resource sharing.

Understanding resource concepts is crucial for effective design and management of


operating systems to ensure efficient utilization and fair allocation of resources
among competing processes or users.

Device Management
Certainly! Device Management is a fundamental aspect of operating systems that
involves efficiently controlling and coordinating input/output (I/O) devices like disks,
printers, keyboards, etc. It encompasses various strategies to optimize device
performance, ensure data integrity, and manage the interaction between the
operating system and the devices.

Device Management Components:


1. Device Drivers:

Device drivers are software components that facilitate communication


between the operating system and hardware devices.

They provide a standardized interface for the OS to interact with various


devices.

2. I/O Scheduling:

Disk Scheduling Strategies: This specifically focuses on managing disk I/O


operations efficiently by determining the order in which read/write requests
are serviced from the disk.

3. Error Handling and Recovery:

Operating System 94
Mechanisms to handle device errors, recover from failures, and ensure data
integrity.

4. Device Allocation and Deallocation:

Managing the allocation and deallocation of devices among multiple


processes.

5. Interrupt Handling:

Dealing with hardware interrupts generated by devices to signal events that


require attention from the operating system.

6. Caching and Buffering:

Strategies to optimize I/O performance by using cache memory and buffers


to store frequently accessed data and reduce the number of direct
accesses to slower storage devices.

Disk Scheduling Strategies:


Disk scheduling algorithms are used to optimize the order in which read/write
requests from different processes are serviced by the disk.

Common Disk Scheduling Algorithms:


1. FCFS (First-Come, First-Served):

Processes are served in the order they arrive, irrespective of their location
on the disk. Can lead to high seek times and poor performance.

2. SSTF (Shortest Seek Time First):

Services the request with the shortest seek time, minimizing disk arm
movement.

However, it may lead to starvation of requests further from the disk's current
position.

3. SCAN (Elevator Algorithm):

The disk arm moves in one direction, serving requests along the way until it
reaches the end, then reverses direction.

Operating System 95
Reduces the average seek time but may cause requests at the extremes to
wait for a long time.

4. C-SCAN (Circular SCAN):

Similar to SCAN but the arm only moves in one direction, servicing requests
in a circular manner.

Minimizes waiting time for requests as the arm moves continuously in one
direction.

5. LOOK and C-LOOK:

Variants of SCAN and C-SCAN that avoid servicing empty areas of the disk,
improving performance for some workloads.

Rotational Optimization:
Rotational optimization techniques aim to minimize the rotational latency associated
with accessing data on a disk. This latency occurs due to the time it takes for the
disk's read/write head to reach the desired sector as the disk platter rotates.

Techniques for Rotational Optimization:


1. Track Skewing:

By arranging sectors in a non-radial order, track skewing reduces the time


needed for the head to move across different tracks.

The sectors are arranged diagonally or in a slightly skewed manner,


optimizing the head's movement to reduce access time.

2. Zone Bit Recording (ZBR):

ZBR divides the disk into zones, with each zone having a different number
of sectors per track.

Outer tracks have more sectors than inner tracks because they cover a
larger circumference. This helps in storing more data in outer tracks where
the linear velocity of the disk is higher, thus improving transfer rates for
these sectors.

Operating System 96
3. Multiple Disks and Redundancy:

Redundant Array of Independent Disks (RAID) configurations, like RAID 0


or RAID 5, leverage multiple disks to distribute data and enhance
performance.

Techniques like striping data across multiple disks or using parity for
redundancy and performance can reduce access times by distributing I/O
operations.

System Consideration:
System considerations in device management involve various factors that impact
the effective management and optimization of devices within an operating system
environment.

Key System Considerations:


1. Performance:

Maximizing throughput and minimizing response times for I/O operations.

Optimizing device management strategies to improve overall system


performance.

2. Reliability:

Ensuring devices operate correctly and reliably, handling errors effectively.

Implementing error handling mechanisms to maintain data integrity and


system stability.

3. Scalability:

Managing devices efficiently as the system scales with additional hardware.

Ensuring that device management strategies remain effective and scalable


as the system grows.

4. Compatibility:

Operating System 97
Ensuring compatibility with different types of devices and adherence to
industry standards.

Providing support for a wide range of devices to ensure seamless


integration with the operating system.

Caching and Buffering:


Caching and buffering are techniques used to optimize I/O performance by
temporarily storing frequently accessed data in fast-access memory, reducing the
need for direct accesses to slower storage devices.

Significance and Benefits:


1. Improved Speed:

Accessing data from cache or buffer memory is faster than retrieving it


directly from slower devices like disks or networks.

Frequently accessed data stored in cache or buffer memory allows for


quicker access.

2. Reduced Latency:

Minimizes the waiting time for data retrieval by preloading frequently


accessed data into memory.

Buffering techniques allow for the prefetching of data, reducing latency


during I/O operations.

3. Enhanced Performance:

Optimizes I/O operations by reducing the number of direct accesses to


slower storage devices.

Helps in balancing I/O operations and system responsiveness, improving


overall performance.

Conclusion:

Rotational optimization techniques focus on minimizing disk access time by


arranging data in an optimal manner on the disk platter. System considerations

Operating System 98
encompass various aspects like performance, reliability, scalability, and
compatibility to ensure effective device management within an operating system.
Caching and buffering techniques enhance I/O performance by storing frequently
accessed data in fast-access memory, reducing latency and improving overall
system efficiency. These components collectively contribute to efficient device
management and optimal utilization of I/O resources in operating systems.

Unit 4
File System Introduction:
A file system is a method used by an operating system to manage and organize
files and directories on storage devices like hard drives, SSDs, optical drives, etc. It
provides a structured way to store, retrieve, and manage data efficiently.

Components of a File System:


1. Files:

Represent individual pieces of data, such as documents, programs, images,


etc.

Have names, types, sizes, and attributes (e.g., permissions, creation date,
etc.).

2. Directories:

Containers used to organize and group related files.

Allow hierarchical organization for easy navigation and management.

3. File Operations:

Include basic operations like create, read, write, delete, rename, etc.

The file system provides interfaces for applications and users to perform
these operations.

File Organization:

Operating System 99
File organization refers to the way files are physically stored and structured on a
storage medium. Various file organization methods exist, each offering different
advantages and trade-offs in terms of access speed, storage efficiency, and ease of
file manipulation.

Common File Organization Techniques:


1. Sequential File Organization:

Files are stored one after another in a continuous manner.

Data is accessed sequentially from the start to the end of the file.

Suitable for applications that access data in a linear manner, like tape
storage.

2. Indexed Sequential Access Method (ISAM):

Combines sequential and indexed access methods.

Uses an index to allow direct access to specific parts of a file while


maintaining sequential access.

3. Direct (Random) Access File Organization:

Files are divided into fixed-size blocks or records that can be accessed
directly using an index or address.

Allows immediate access to any part of the file without reading the
preceding data, common in disk-based storage.

4. Hashed File Organization:

Uses a hash function to compute an address or location for each record


based on its key.

Provides fast access to records based on their unique identifier.

5. Clustered or Contiguous File Organization:

Stores files in contiguous blocks on the storage medium.

Reduces fragmentation and improves access speed, but may lead to


wasted space due to fragmentation over time.

Operating System 100


Conclusion:

File systems manage the storage and retrieval of data by organizing files and
directories on storage devices. Understanding different file organization techniques
is crucial as it impacts the speed of access, storage efficiency, and overall
performance of file systems. The selection of a file organization method often
depends on the specific requirements of the applications and the characteristics of
the storage medium being used. Efficient file organization contributes significantly to
the effective management and utilization of storage resources within an operating
system.

Logical File System:


The Logical File System is an abstract interface that the operating system provides
to users and applications to interact with files and directories. It defines how users
perceive and access files, specifying the structure, naming conventions,
permissions, and attributes associated with files and directories.

Characteristics and Functions:


1. File Naming and Structure:

Provides a hierarchical structure for organizing files into directories or


folders.

Allows users to assign names to files and directories to facilitate


organization and retrieval.

2. File Operations:

Offers a set of operations (create, read, write, delete, rename, etc.) to


manipulate files and directories.

Presents a uniform interface for applications to perform file-related tasks


without dealing with hardware-specific details.

3. File Attributes and Metadata:

Manages file attributes such as permissions, timestamps (creation,


modification), file types, and ownership details.

Operating System 101


Maintains metadata associated with files, providing information about the
file without accessing its content.

4. Access Control:

Enforces security measures by regulating user access to files and


directories through permissions and access control lists (ACLs).

5. File System Abstraction:

Provides an abstraction layer separating the logical representation of files


from the physical storage details.

Physical File System:


The Physical File System deals with the actual implementation and management of
files and data on storage devices at the hardware level. It represents how files are
physically stored, managed, and accessed on disk or other storage media.

Key Aspects and Functions:


1. Data Storage and Organization:

Organizes files and data on physical storage media, such as hard drives,
SSDs, or tapes, using specific data structures and algorithms.

Manages space allocation, disk partitioning, and data structures for efficient
storage and retrieval.

2. Low-Level Data Access:

Handles the details of reading from and writing to storage devices, including
disk I/O operations and device drivers.

Manages block-level access and retrieval of data from storage media.

3. File System Structures:

Includes structures like File Allocation Tables (FAT), Master File Table
(MFT), inode tables, or other data structures to map logical file names to
physical disk addresses.

Operating System 102


4. Optimization and Performance:

Implements disk scheduling algorithms, caching strategies, and


optimization techniques to enhance I/O performance and storage utilization.

5. Error Handling and Recovery:

Manages mechanisms for error detection, data recovery, and maintaining


data integrity in case of hardware failures or system crashes.

Relationship between Logical and Physical File Systems:


The Logical File System abstracts the way users perceive and interact with
files, hiding the physical complexities of storage devices.

It uses the services provided by the Physical File System to implement and
manage files on the actual storage medium according to the logical structure.

Both the Logical and Physical File Systems work in tandem to manage files and
directories in an organized and efficient manner, providing users and applications
with a standardized and user-friendly interface while efficiently managing storage
resources at the hardware level.

File allocation strategies


File allocation strategies refer to the techniques used by the file system to allocate
space on storage devices, such as hard disks, for storing files. These strategies
determine how files are stored, managed, and retrieved efficiently. There are
several file allocation methods, each with its advantages, limitations, and suitability
for different scenarios:

Common File Allocation Strategies:


1. Contiguous Allocation:

Allocates consecutive blocks of disk space to a file.

Simple and efficient for sequential access.

However, leads to fragmentation over time (external fragmentation) as files


are created, modified, and deleted, causing scattered free spaces.

Operating System 103


2. Linked Allocation:

Allocates space dynamically by linking together blocks of data through


pointers or links.

Each block contains a pointer to the next block in the file.

Doesn't suffer from fragmentation but can be inefficient for random access
due to traversal of linked blocks.

3. Indexed Allocation:

Uses an index block that contains pointers to all the blocks of a file
scattered across the disk.

Allows direct access to any block in the file without traversing pointers.

Reduces fragmentation and improves random access but requires extra


space for the index block.

4. File Allocation Table (FAT):

A variation of indexed allocation with a table that maps file blocks to their
respective locations on the disk.

Provides efficient access and reduces fragmentation but can be less space-
efficient due to overhead from maintaining the table.

5. Multi-level Indexing:

Uses multiple levels of indexing to address large disk sizes efficiently.

Breaks down the index structure into several layers to manage large files
and disks effectively.

6. Extent-Based Allocation:

Allocates a contiguous block of multiple disk sectors, known as an extent, to


a file instead of single blocks.

Reduces overhead by reducing the number of pointers needed and


minimizes fragmentation.

Factors influencing File Allocation Strategies:

Operating System 104


1. File Size: Strategies may vary depending on the size of the files to be stored.
For instance, contiguous allocation works well for smaller files, while larger files
may benefit from indexed or extent-based allocation.

2. Disk Fragmentation: Strategies aim to minimize fragmentation to ensure


efficient space utilization and access speed.

3. Access Patterns: Different strategies suit different access patterns (sequential


or random access) of files.

4. Overhead: Some methods incur additional overhead in terms of storage or


computational complexity for managing pointers, indices, etc.

Conclusion:

File allocation strategies play a crucial role in file system design, impacting space
utilization, access speed, and fragmentation levels. The choice of allocation method
depends on the characteristics of the file system, the nature of the data being
stored, and the trade-offs between efficient space utilization and access speed. A
well-designed file allocation strategy ensures optimal utilization of storage resources
while providing efficient access to files.

Free Space Management


Free space management in a file system refers to the techniques and mechanisms
used to keep track of available and unused space on storage devices. It involves
maintaining information about free space blocks or clusters within the storage
medium to efficiently allocate space for new files and manage storage utilization.

Common Free Space Management Techniques:


1. Bit Vector (Bitmap)

Represents each block or cluster on the disk with a bit in a bitmap.

If a block is free, its corresponding bit is set to 1; otherwise, it's set to 0.

Simple and efficient for determining free blocks but can be space-inefficient
for large disks.

Operating System 105


2. Linked List or Linked Blocks

Maintains a linked list of free blocks.

Each free block contains a pointer to the next free block.

Efficient for small file systems but can be slow for large storage systems
due to traversal.

3. Grouping or Segmentation

Divides the disk into segments or groups and maintains free lists for each
segment.

Helps in reducing search time for free blocks by narrowing down the search
space.

4. Counting Methods

Tracks the total number of free blocks instead of individual block status.

Allows quick determination of total free space but requires scanning to find
specific free blocks.

Allocation of Free Space:

1. Initial Allocation:

Occurs during disk formatting or file system creation, where the entire disk
space is marked as free.

2. Space Allocation to Files:

When a new file is created, the file system allocates space by selecting free
blocks/clusters.

Different allocation strategies, such as contiguous, linked, or indexed,


determine how space is allocated to files.

Challenges and Considerations:

1. Fragmentation:

Internal Fragmentation: Unused space within allocated blocks.

Operating System 106


External Fragmentation: Scattered free space that is unusable for file
allocation due to small, non-contiguous free blocks.

2. Efficiency and Performance:

Free space management techniques impact performance in terms of


allocation speed and storage utilization.

Efficient methods aim to minimize fragmentation and reduce search times


for free blocks.

3. Dynamic Allocation:

Techniques that efficiently manage free space in a dynamic and adaptive


manner as files are created, modified, and deleted.

4. Concurrency and Locking:

Ensuring the integrity of free space data structures in multi-user or multi-


threaded environments.

Implementing proper locking mechanisms to prevent data corruption during


simultaneous access.

Conclusion:

Free space management is a critical aspect of file system design, ensuring efficient
utilization of storage and facilitating optimal allocation of space for new files. The
choice of a particular free space management technique depends on factors like
disk size, access patterns, performance considerations, and trade-offs between
storage efficiency and access speed. A well-designed free space management
mechanism contributes to maintaining an organized file system, reducing
fragmentation, and optimizing storage utilization within the operating system.

File Access Control


File access control refers to the mechanisms and techniques used by operating
systems to regulate and manage access to files and their associated resources. It
involves defining permissions, privileges, and restrictions on who can access,
modify, or delete files and directories within a file system.

Operating System 107


File Access Control Mechanisms:

1. Access Control Lists (ACLs):

ACLs provide detailed permissions for files and directories, specifying


access rights for individual users or groups.

Permissions include read, write, execute, and special permissions like


ownership and access control settings.

2. Ownership and Group Permissions:

Each file is associated with an owner and a specific group.

Permissions are assigned to the owner, the group, and other users, defining
what actions each category of users can perform on the file.

3. Role-Based Access Control (RBAC):

RBAC assigns permissions based on job roles or responsibilities rather


than individual users.

Users are assigned to specific roles, and each role has predefined access
permissions.

4. Mandatory Access Control (MAC):

MAC defines access permissions based on security labels and policies set
by the system administrator or security policy.

Access is determined by strict rules defined by the system rather than by


users or owners.

Data Access Techniques:


Data access techniques in file systems involve methods used to retrieve and
manipulate data stored within files. These techniques vary in terms of efficiency,
speed, and complexity of data retrieval.

1. Sequential Access:

Reads data sequentially from the beginning to the end of a file.

Operating System 108


Suitable for scenarios where data is accessed in a linear manner, such as
scanning through a file sequentially.

2. Random Access:

Allows direct access to any part of a file without having to read through the
preceding data.

Efficient for applications requiring immediate access to specific parts of a


file, often used with indexed file structures.

3. Buffering and Caching:

Techniques involving the use of buffer memory to temporarily store data


fetched from the disk.

Buffering reduces the number of disk accesses by storing recently


accessed data in memory.

4. File Mapping:

Maps a file directly into memory, enabling direct manipulation of the file's
contents in memory without explicit read/write operations.

Efficient for certain data processing operations where data needs to be


accessed frequently.

Considerations in File Access Control and Data Access:


1. Security and Privacy:

File access control mechanisms ensure that sensitive data is protected from
unauthorized access or modification.

2. Performance:

Different data access techniques have implications for performance.


Random access is faster but may involve more overhead compared to
sequential access.

3. Concurrency and Locking:

Operating System 109


Managing simultaneous access to files by multiple users or processes,
ensuring data integrity using locking mechanisms.

4. Resource Utilization:

Efficient data access techniques help optimize resource utilization and


enhance system performance.

Conclusion:

File access control mechanisms define and enforce permissions and restrictions on
file access and modifications. Data access techniques determine how efficiently and
effectively data is retrieved, processed, and manipulated within the file system.
Balancing security requirements with performance considerations is crucial in
designing file access control and data access techniques to ensure data integrity,
confidentiality, and efficient file management within an operating system.

Data Integrity Protection


Data integrity protection refers to the measures and mechanisms put in place to
ensure that data remains accurate, consistent, and unaltered throughout its lifecycle
within a system. It involves safeguarding data against unauthorized modifications,
corruption, or accidental changes, ensuring its reliability and trustworthiness.

Techniques and Measures for Data Integrity Protection:


1. Checksums and Hash Functions:

Checksums and cryptographic hash functions generate fixed-size values


(checksums or hashes) based on the data content.

These values are compared before and after data transmission or storage
to verify data integrity. Any changes in data would result in different
checksums or hashes.

2. Cyclic Redundancy Check (CRC):

CRC is a type of checksum used for error detection in data transmission.

Operating System 110


It generates a checksum based on the contents of the data and appends it
to the transmitted data. The receiver recalculates the checksum to verify
data integrity.

3. Digital Signatures:

Used in cryptographic systems, digital signatures verify the authenticity and


integrity of data.

They involve the use of public and private key pairs to sign and verify the
data, ensuring that it has not been altered or tampered with.

4. Data Backups and Redundancy:

Regularly backing up data and maintaining redundant copies can protect


against data loss due to corruption or accidental modifications.

Redundancy ensures that if one copy becomes corrupted, there are


additional copies available for recovery.

5. Access Control and Permissions:

Proper access controls and permissions prevent unauthorized users from


modifying or accessing critical data, reducing the risk of intentional or
accidental alterations.

6. Error Detection and Correction Codes:

In addition to CRC, error detection and correction codes are used in storage
systems to detect and fix data errors or corruption.

7. Logging and Auditing:

Logging mechanisms track changes made to data, providing a record of


modifications for auditing purposes.

Auditing ensures accountability and helps trace any unauthorized changes


to data.

Importance and Benefits:


1. Data Trustworthiness:

Operating System 111


Ensures that data remains reliable and unchanged, fostering trust among
users and systems.

2. Prevention of Data Corruption:

Protects against accidental or deliberate alterations to data, maintaining its


accuracy and consistency.

3. Compliance and Legal Requirements:

Many industries and regulations mandate data integrity protection to comply


with security and privacy standards.

4. Business Continuity:

Ensures that in case of data corruption or loss, there are measures in place
for data recovery, preserving business continuity.

Challenges:
1. Overhead and Performance Impact:

Some data integrity protection measures may impose additional


computational overhead, impacting system performance.

2. Complexity and Implementation:

Implementing robust data integrity measures requires careful planning and


can be complex, especially in large-scale systems.

3. Maintenance and Monitoring:

Regular monitoring and maintenance are necessary to ensure the


continued effectiveness of data integrity protection mechanisms.

Conclusion:

Data integrity protection is crucial to maintaining the reliability and trustworthiness of


data within an operating system or any computing environment. Implementing a
combination of techniques such as checksums, digital signatures, access controls,
backups, and auditing helps safeguard data against corruption, unauthorized
modifications, and ensures its accuracy and reliability over time. Balancing the level

Operating System 112


of protection with system performance and usability is essential for effective data
integrity protection.

File systems
File systems like FAT32, NTFS, and Ext2/Ext3 are used by various operating
systems to organize and manage data on storage devices like hard drives, SSDs,
USB drives, and memory cards. Each file system has its characteristics, features,
and limitations, catering to different needs and scenarios.

FAT32 (File Allocation Table 32):


1. Characteristics:

Suitable for compatibility across different operating systems, including


Windows, macOS, and Linux.

Supports maximum file size of 4 GB and a maximum volume size of 2 TB.

2. Limitations:

Limited security features and file permissions.

Prone to fragmentation, leading to reduced performance with larger


volumes.

3. Usage:

Commonly used for USB drives, memory cards, and external storage due to
its cross-platform compatibility.

NTFS (New Technology File System):


1. Characteristics:

Provides advanced features like file compression, encryption, and access


control.

Supports larger file sizes and volumes compared to FAT32.

Better resilience to disk errors and supports journaling for improved


recovery after system crashes.

Operating System 113


2. Limitations:

Limited compatibility with non-Windows systems, although some Linux


distributions offer read-only support.

3. Usage:

Primary file system for Windows-based operating systems, especially for


internal hard drives.

Ext2/Ext3 (Second Extended File System/Third Extended File


System):
1. Ext2 Characteristics:

Ext2 was one of the earliest Linux file systems.

Supports file permissions and ownership.

Lacks journaling, which can lead to data inconsistency in case of abrupt


system shutdowns.

2. Ext3 Characteristics:

An extension of Ext2 with the addition of journaling support, providing


improved reliability and data consistency.

Retains compatibility with Ext2, allowing backward compatibility.

3. Usage:

Widely used by Linux distributions for internal storage due to its


compatibility and performance.

APFS (Apple File System):


1. Characteristics:

Developed by Apple for macOS and iOS devices.

Supports features like snapshots, encryption, and space sharing.

Optimized for SSDs and flash storage, offering improved performance and
efficiency.

Operating System 114


2. Usage:

Primary file system for macOS and iOS devices, replacing HFS+.

ReFS (Resilient File System):


1. Characteristics:

Developed by Microsoft for Windows Server operating systems.

Focuses on data resilience, offering advanced error correction and data


protection features.

Supports large volumes and files, along with integrity checking.

2. Usage:

Mainly used for high-capacity storage systems and servers requiring robust
data protection.

Conclusion:

Different file systems offer various features, performance levels, and compatibility
with different operating systems. The choice of a file system depends on the
specific requirements, platform compatibility, security needs, and intended use
(such as internal storage, external drives, or specialized environments). Each file
system has its advantages and limitations, and selecting the right file system is
crucial for optimal data management and storage within an operating system or
computing environment.

made by yashs using chatgpt so content maybe wrong :)

updated version here: https://yashnote.notion.site/Operating-System-


6d1a23bfc9c74eaba2fbab05b10c86ce?pvs=4

Operating System 115

You might also like